diff --git a/core/splitting/splitting-docs.factor b/core/splitting/splitting-docs.factor index 0e8e392879..6b06674561 100644 --- a/core/splitting/splitting-docs.factor +++ b/core/splitting/splitting-docs.factor @@ -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 } "." } ; diff --git a/core/splitting/splitting-tests.factor b/core/splitting/splitting-tests.factor index e672624d96..a0d12f069e 100644 --- a/core/splitting/splitting-tests.factor +++ b/core/splitting/splitting-tests.factor @@ -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 diff --git a/core/splitting/splitting.factor b/core/splitting/splitting.factor index 5ac6297ddc..b9b4f87b24 100644 --- a/core/splitting/splitting.factor +++ b/core/splitting/splitting.factor @@ -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 ) [ ] bi@ split1 [ reverse ] bi@ dup [ swap ] when ;