rename last-slice1 to slice1-last. add slice1-slice and slice1-last-slice, docs, and tests

db4
Doug Coleman 2008-11-22 19:59:43 -06:00
parent 86546552d3
commit 26cb48b0ab
3 changed files with 34 additions and 7 deletions

View File

@ -8,6 +8,9 @@ ARTICLE: "sequences-split" "Splitting sequences"
{ $subsection ?tail }
{ $subsection ?tail-slice }
{ $subsection split1 }
{ $subsection split1-slice }
{ $subsection split1-last }
{ $subsection split1-last-slice }
{ $subsection split }
"Splitting a string into lines:"
{ $subsection string-lines } ;
@ -18,11 +21,19 @@ HELP: split1
{ $values { "seq" "a sequence" } { "subseq" "a sequence" } { "before" "a new sequence" } { "after" "a new sequence" } }
{ $description "Splits " { $snippet "seq" } " at the first 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 } "." } ;
HELP: last-split1
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-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 } "." } ;
{ split1 last-split1 } related-words
HELP: split1-last-slice
{ $values { "seq" "a sequence" } { "subseq" "a sequence" } { "before-slice" slice } { "after-slice" slice } }
{ $description "Splits " { $snippet "seq" } " at the last 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 } "." } ;
{ split1 split1-slice split1-last split1-last-slice } related-words
HELP: split
{ $values { "seq" "a sequence" } { "separators" "a sequence" } { "pieces" "a new array" } }

View File

@ -6,10 +6,15 @@ IN: splitting.tests
[ "goodbye" f ] [ "goodbye" " " split1 ] unit-test
[ "" "" ] [ "great" "great" split1 ] unit-test
[ "hello world" "." ] [ "hello world ." " " last-split1 ] unit-test
[ "hello-+world" "." ] [ "hello-+world-+." "-+" last-split1 ] unit-test
[ "goodbye" f ] [ "goodbye" " " last-split1 ] unit-test
[ "" "" ] [ "great" "great" last-split1 ] unit-test
[ "hello world" "." ] [ "hello world ." " " split1-last ] unit-test
[ "hello-+world" "." ] [ "hello-+world-+." "-+" split1-last ] unit-test
[ "goodbye" f ] [ "goodbye" " " split1-last ] unit-test
[ "" "" ] [ "great" "great" split1-last ] unit-test
[ "hello world" "." ] [ "hello world ." " " split1-last-slice [ >string ] bi@ ] unit-test
[ "hello-+world" "." ] [ "hello-+world-+." "-+" split1-last-slice [ >string ] bi@ ] unit-test
[ "goodbye" f ] [ "goodbye" " " split1-last-slice [ dup [ >string ] when ] bi@ ] unit-test
[ "" "" ] [ "great" "great" split1-last-slice [ >string ] bi@ ] unit-test
[ "and end" t ] [ "Beginning and end" "Beginning " ?head ] unit-test
[ "Beginning and end" f ] [ "Beginning and end" "Beginning x" ?head ] unit-test

View File

@ -23,10 +23,21 @@ IN: splitting
2drop f
] if ;
: last-split1 ( seq subseq -- before after )
: split1-slice ( seq subseq -- before-slice after-slice )
dup pick start dup [
[ >r over r> head-slice -rot length ] keep + tail-slice
] [
2drop f
] if ;
: split1-last ( seq subseq -- before after )
[ <reversed> ] bi@ split1 [ reverse ] bi@
dup [ swap ] when ;
: split1-last-slice ( seq subseq -- before-slice after-slice )
[ <reversed> ] bi@ split1-slice [ <reversed> ] bi@
[ f ] [ swap ] if-empty ;
: (split) ( separators n seq -- )
3dup rot [ member? ] curry find-from drop
[ [ swap subseq , ] 2keep 1+ swap (split) ]