splitting: adding split1-when.

db4
John Benediktsson 2012-04-18 15:55:15 -07:00
parent 0322b4d028
commit 94b73d9371
3 changed files with 12 additions and 0 deletions

View 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 } "." } ;

View 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

View 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 ;