]> gitweb.factorcode.org Git - factor.git/commitdiff
sequences.extras: adding round-robin.
authorJohn Benediktsson <mrjbq7@gmail.com>
Thu, 31 May 2012 00:50:00 +0000 (17:50 -0700)
committerJohn Benediktsson <mrjbq7@gmail.com>
Thu, 31 May 2012 00:50:00 +0000 (17:50 -0700)
extra/sequences/extras/extras-tests.factor
extra/sequences/extras/extras.factor

index e33819e580018d800f57b4f968cedfdaf6c04c0a..2ef0bfe15f920b1074e4b783e646066a1ff9640e 100644 (file)
@@ -71,3 +71,5 @@ IN: sequences.extras.tests
 
 { 8 } [ 3 iota dup [ 1 + * ] 2map-sum ] unit-test
 { 4 } [ "hello" "jello" [ = ] 2count ] unit-test
+
+{ "ADEBFC" } [ { "ABC" "D" "EF" } round-robin ] unit-test
index b565894fc18f4fc0cc6efd1ad738c351294e7b16..0759d8172f81fd084633b1244a473f8a752583a8 100644 (file)
@@ -183,3 +183,16 @@ PRIVATE>
 
 : 2count ( ... seq1 seq2 quot: ( ... elt1 elt2 -- ... ? ) -- ... n )
     [ 1 0 ? ] compose 2map-sum ; inline
+
+:: round-robin-as ( seqs exemplar -- newseq )
+    seqs length :> len
+    0 0 seqs sum-lengths [
+        f [
+            drop dup len >= [ drop 1 + 0 ] when
+            2dup seqs nth-unsafe ?nth
+            [ 1 + ] [ dup not ] bi*
+        ] loop
+    ] exemplar replicate-as 2nip ;
+
+: round-robin ( seqs -- newseq )
+    [ { } ] [ dup first round-robin-as ] if-empty ;