{ "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: find-index-from
+{ $values { "n" "a starting index" }
+ { "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-from } " 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." } ;
[ 3 CHAR: d ] [ "abcdefg" [ 3 = nip ] find-index ] unit-test
[ 3 CHAR: d ] [ "abcdefg" [ drop CHAR: d = ] find-index ] unit-test
+[ 0 CHAR: a ] [ 0 "abcdef" [ drop CHAR: a >= ] find-index-from ] unit-test
+[ 1 CHAR: b ] [ 0 "abcdef" [ drop CHAR: a > ] find-index-from ] unit-test
+[ 2 CHAR: c ] [ 1 "abcdef" [ drop CHAR: b > ] find-index-from ] unit-test
+
[ f ] [ 3 [ ] member? ] unit-test
[ f ] [ 3 [ 1 2 ] member? ] unit-test
[ t ] [ 1 [ 1 2 ] member? ] unit-test
: (find-index) ( seq quot quot' -- i elt )
pick [ [ (each-index) ] dip call ] dip finish-find ; inline
+: (find-index-from) ( n seq quot quot' -- i elt )
+ [ 2dup bounds-check? ] 2dip
+ [ (find-index) ] 2curry
+ [ 2drop f f ]
+ if ; inline
+
: (accumulate) ( seq identity quot -- identity seq quot )
swapd [ curry keep ] curry ; inline
: find-last ( ... seq quot: ( ... elt -- ... ? ) -- ... i elt )
[ [ 1 - ] dip find-last-integer ] (find) ; inline
+: find-index-from ( ... n seq quot: ( ... elt i -- ... ? ) -- ... i elt )
+ [ (find-integer) ] (find-index-from) ; inline
+
: find-index ( ... seq quot: ( ... elt i -- ... ? ) -- ... i elt )
[ find-integer ] (find-index) ; inline