diff --git a/extra/sequences/extras/extras-tests.factor b/extra/sequences/extras/extras-tests.factor index 1c124b7d201..79a8c6b6a24 100644 --- a/extra/sequences/extras/extras-tests.factor +++ b/extra/sequences/extras/extras-tests.factor @@ -261,6 +261,10 @@ strings tools.test ; { "cdef" } [ 2 f "abcdef" subseq* ] unit-test { "cd" } [ -4 -2 "abcdef" subseq* ] unit-test +{ { } } [ "abcabc" "abcd" subseq-indices ] unit-test +{ { 0 3 } } [ "abcabc" "abc" subseq-indices ] unit-test +{ { 0 3 } } [ "abcabcabc" "abcabc" subseq-indices ] unit-test + { "foo" "" } [ "foo" [ ascii:blank? ] cut-when ] unit-test { "foo" " " } [ "foo " [ ascii:blank? ] cut-when ] unit-test { "" " foo" } [ " foo" [ ascii:blank? ] cut-when ] unit-test diff --git a/extra/sequences/extras/extras.factor b/extra/sequences/extras/extras.factor index 4a2494ba14d..541bdf39d3a 100644 --- a/extra/sequences/extras/extras.factor +++ b/extra/sequences/extras/extras.factor @@ -24,6 +24,11 @@ IN: sequences.extras dup _ (a..b] [ rot [ subseq _ call ] keep ] with each ] each drop ; inline +: subseq-indices ( seq subseq -- indices ) + [ 0 ] 2dip '[ + _ _ subseq-index-from [ [ 1 + ] keep ] [ f f ] if* dup + ] [ ] produce 2nip ; + : map-like ( seq exemplar -- seq' ) '[ _ like ] map ; inline