]> gitweb.factorcode.org Git - factor.git/commitdiff
splitting: adding split1-when.
authorJohn Benediktsson <mrjbq7@gmail.com>
Wed, 18 Apr 2012 22:55:15 +0000 (15:55 -0700)
committerJohn Benediktsson <mrjbq7@gmail.com>
Wed, 18 Apr 2012 22:55:15 +0000 (15:55 -0700)
core/splitting/splitting-docs.factor
core/splitting/splitting-tests.factor
core/splitting/splitting.factor

index 0e8e3928798b8ae054dd03a2392daf42afafa3a7..6b0667456182d9cfe0ee75930908925e1682372a 100644 (file)
@@ -10,6 +10,7 @@ ARTICLE: "sequences-split" "Splitting sequences"
     ?tail-slice
     split1
     split1-slice
+    split1-when
     split1-last
     split1-last-slice
     split
@@ -28,6 +29,10 @@ HELP: split1-slice
 { $values { "seq" "a sequence" } { "subseq" "a sequence" } { "before-slice" slice } { "after-slice" slice } }
 { $description "Splits " { $snippet "seq" } " at the first occurrence of " { $snippet "subseq" } ", and outputs the pieces before and after the split as slices. If " { $snippet "subseq" } " does not occur in " { $snippet "seq" } ", then " { $snippet "before" } " is just " { $snippet "seq" } " and " { $snippet "after" } " is " { $link f } "." } ;
 
+HELP: split1-when
+{ $values { "seq" "a sequence" } { "quot" { $quotation "( ... elt -- ... ? )" } } { "pieces" "a new array" } }
+{ $description "Splits " { $snippet "seq" } " at the first occurrence of an element for which " { $snippet "quot" } " gives a true output and outputs the pieces before and after the split." } ;
+
 HELP: split1-last
 { $values { "seq" "a sequence" } { "subseq" "a sequence" } { "before" "a new sequence" } { "after" "a new sequence" } }
 { $description "Splits " { $snippet "seq" } " at the last occurrence of " { $snippet "subseq" } ", and outputs the pieces before and after the split. If " { $snippet "subseq" } " does not occur in " { $snippet "seq" } ", then " { $snippet "before" } " is just " { $snippet "seq" } " and " { $snippet "after" } " is " { $link f } "." } ;
index e672624d9677363893a7704648b205b431f761c6..a0d12f069ee8a531a7a29501820a99ba38e67a32 100644 (file)
@@ -60,3 +60,7 @@ unit-test
 
 [ { "hey" "world" "what's" "happening" } ]
 [ "heyAworldBwhat'sChappening" [ LETTER? ] split-when ] unit-test
+
+[ "" f ] [ "" [ blank? ] split1-when ] unit-test
+[ "" "ABC" ] [ " ABC" [ blank? ] split1-when ] unit-test
+[ "a" " bc" ] [ "a  bc" [ blank? ] split1-when ] unit-test
index 5ac6297ddc550a26829510bb08cc00df04806800..b9b4f87b24cc837a34fc4ee88651a5870818b044 100644 (file)
@@ -44,6 +44,9 @@ PRIVATE>
 : split1-slice ( seq subseq -- before-slice after-slice )
     [ snip-slice ] (split1) ;
 
+: split1-when ( ... seq quot: ( ... elt -- ... ? ) -- ... before after )
+    dupd find drop [ swap [ dup 1 + ] dip snip ] [ f ] if* ; inline
+
 : split1-last ( seq subseq -- before after )
     [ <reversed> ] bi@ split1 [ reverse ] bi@
     dup [ swap ] when ;