Skip to content

Commit

Permalink
sequences.extras: adding subseq-indices word
Browse files Browse the repository at this point in the history
  • Loading branch information
mrjbq7 committed Nov 12, 2024
1 parent 9193f64 commit ae50ca8
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 0 deletions.
4 changes: 4 additions & 0 deletions extra/sequences/extras/extras-tests.factor
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions extra/sequences/extras/extras.factor
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit ae50ca8

Please sign in to comment.