combinators.short-circuit compiler.units fry interval-maps io
io.encodings.ascii io.files kernel literals locals make math
math.parser math.ranges memoize namespaces sequences
-sequences.private sets simple-flat-file splitting
-unicode.categories unicode.categories.syntax unicode.data
-unicode.normalize unicode.normalize.private values words ;
+sets simple-flat-file splitting unicode.categories
+unicode.categories.syntax unicode.data unicode.normalize
+unicode.normalize.private values words ;
FROM: sequences => change-nth ;
IN: unicode.breaks
[ str i ] dip word-break?
] if ;
-: (find-index) ( seq quot quot' -- i elt )
- pick [ [ (each-index) ] dip call ] dip finish-find ; inline
-
-: find-index ( ... seq quot: ( ... elt i -- ... ? ) -- ... i elt )
- [ find-integer ] (find-index) ; inline
-
PRIVATE>
: first-word ( str -- i )
{ $values { "n" "a starting index" } { "seq" sequence } { "quot" { $quotation "( ... elt -- ... ? )" } } { "i" "the index of the first match, or f" } { "elt" "the first matching element, or " { $link f } } }
{ $description "Applies the quotation to each element of the sequence in reverse order, until it outputs a true value or the start of the sequence is reached. If the quotation yields a true value for some sequence element, the word outputs the element index and the element itself. Otherwise, the word outputs an index of f and " { $link f } " as the element." } ;
+HELP: find-index
+{ $values { "seq" sequence }
+ { "quot" { $quotation "( ... elt i -- ... ? )" } }
+ { "i" "the index of the first match, or " { $link f } }
+ { "elt" "the first matching element, or " { $link f } } }
+{ $description "A varient of " { $link find } " where the quotation takes both an element and its index." } ;
+
HELP: map-find
{ $values { "seq" sequence } { "quot" { $quotation "( ... elt -- ... result/f )" } } { "result" "the first non-false result of the quotation" } { "elt" "the first matching element, or " { $link f } } }
{ $description "Applies the quotation to each element of the sequence, until the quotation outputs a true value. If the quotation ever yields a result which is not " { $link f } ", then the value is output, along with the element of the sequence which yielded this." } ;
[ 4 CHAR: o ]
[ 3 "hello world" "aeiou" [ member? ] curry find-from ] unit-test
+[ f f ] [ "abcd" [ 10 > nip ] find-index ] unit-test
+[ f f ] [ "abcd" [ drop CHAR: e = ] find-index ] unit-test
+[ 3 CHAR: d ] [ "abcdefg" [ 3 = nip ] find-index ] unit-test
+[ 3 CHAR: d ] [ "abcdefg" [ drop CHAR: d = ] find-index ] unit-test
+
[ f ] [ 3 [ ] member? ] unit-test
[ f ] [ 3 [ 1 2 ] member? ] unit-test
[ t ] [ 1 [ 1 2 ] member? ] unit-test
[ 2drop f f ]
if ; inline
+: (find-index) ( seq quot quot' -- i elt )
+ pick [ [ (each-index) ] dip call ] dip finish-find ; inline
+
: (accumulate) ( seq identity quot -- identity seq quot )
[ swap ] dip [ curry keep ] curry ; inline
: find-last ( ... seq quot: ( ... elt -- ... ? ) -- ... i elt )
[ [ 1 - ] dip find-last-integer ] (find) ; inline
+: find-index ( ... seq quot: ( ... elt i -- ... ? ) -- ... i elt )
+ [ find-integer ] (find-index) ; inline
+
: all? ( ... seq quot: ( ... elt -- ... ? ) -- ... ? )
(each) all-integers? ; inline