shuffle( -- ) arbitrary stack shuffling word

db4
Joe Groff 2009-02-09 11:40:05 -06:00
parent 118f2de466
commit 3bc557467e
2 changed files with 23 additions and 2 deletions

View File

@ -3,3 +3,5 @@ USING: shuffle tools.test ;
[ 8 ] [ 5 6 7 8 3nip ] unit-test
[ 3 1 2 3 ] [ 1 2 3 tuckd ] unit-test
[ 1 2 3 4 ] [ 3 4 1 2 2swap ] unit-test
[ 4 2 3 ] [ 1 2 3 4 shuffle( a b c d -- d b c ) ] unit-test

View File

@ -1,9 +1,28 @@
! Copyright (C) 2007 Chris Double, Doug Coleman.
! See http://factorcode.org/license.txt for BSD license.
USING: kernel generalizations ;
USING: accessors assocs effects.parser generalizations
hashtables kernel locals locals.backend macros make math
parser sequences ;
IN: shuffle
<PRIVATE
: >locals-assoc ( sequence -- assoc )
dup length dup 1- [ - ] curry map zip >hashtable ;
PRIVATE>
MACRO: shuffle-effect ( effect -- )
[ out>> ] [ in>> >locals-assoc ] bi
[
[ nip assoc-size , \ load-locals , ]
[ [ at , \ get-local , ] curry each ]
[ nip assoc-size , \ drop-locals , ] 2tri
] [ ] make ;
: shuffle(
")" parse-effect parsed \ shuffle-effect parsed ; parsing
: 2swap ( x y z t -- z t x y ) 2 2 mnswap ; inline
: nipd ( a b c -- b c ) rot drop ; inline