]> gitweb.factorcode.org Git - factor.git/commitdiff
sequences.extras: adding map-concat.
authorJohn Benediktsson <mrjbq7@gmail.com>
Wed, 25 Apr 2012 20:28:56 +0000 (13:28 -0700)
committerJohn Benediktsson <mrjbq7@gmail.com>
Wed, 25 Apr 2012 20:28:56 +0000 (13:28 -0700)
extra/sequences/extras/extras-tests.factor
extra/sequences/extras/extras.factor

index ec7d9ec17fea597f167c9f6f298181a5a74071d0..90c6676ad317e8f0d0a8d5dd0cafdc7b625bed2e 100644 (file)
@@ -54,3 +54,8 @@ IN: sequences.extras.tests
 { "llohe" } [ "hello" 2 rotate ] unit-test
 { "hello" } [ "hello" dup 0 rotate! ] unit-test
 { "lohel" } [ "hello" dup 3 rotate! ] unit-test
+
+{ { } } [ { } [ ] map-concat ] unit-test
+{ V{ 0 0 1 0 1 2 } } [ 4 iota [ iota ] map-concat ] unit-test
+{ "abc" } [ "abc" [ 1string ] map-concat ] unit-test
+{ { 97 98 99 } } [ "abc" [ 1string ] { } map-concat-as ] unit-test
index 323a4c076ffe5f51c91757a051af20faf496fd45..542cf04c448216ab39d2abbfd25e65837da367d9 100644 (file)
@@ -114,3 +114,16 @@ IN: sequences.extras
         dup end = [ drop over ] when
         2over = [ -rot nip over ] when
     ] until 3drop ;
+
+: appender-for ( quot exemplar -- quot' vec )
+    [ length ] keep new-resizable
+    [ [ push-all ] curry compose ] keep ; inline
+
+: appender ( quot -- quot' vec )
+    V{ } appender-for ; inline
+
+: map-concat-as ( ... seq quot: ( ... elt -- ... newelt ) exemplar -- ... newseq )
+    dup [ appender-for [ each ] dip ] curry dip like ; inline
+
+: map-concat ( ... seq quot: ( ... elt -- ... newelt ) -- ... newseq )
+    over map-concat-as ; inline