]> gitweb.factorcode.org Git - factor.git/commitdiff
shuffle( -- ) arbitrary stack shuffling word
authorJoe Groff <arcata@gmail.com>
Mon, 9 Feb 2009 17:40:05 +0000 (11:40 -0600)
committerJoe Groff <arcata@gmail.com>
Mon, 9 Feb 2009 17:40:05 +0000 (11:40 -0600)
basis/shuffle/shuffle-tests.factor
basis/shuffle/shuffle.factor

index f190544e198aef7a4998bb8b7ecc0aaca28e9754..8202146b3d8d93d9d1ed4244a7caa04bf6a29262 100644 (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
index b195e4abf903bd261d199d61741af8a4577cc9b7..632c09e33840273757d688ca2c2ed620777b284f 100644 (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