sequences: adding find-index-from.
parent
6f9db0d163
commit
e38e9f2850
|
@ -450,6 +450,14 @@ HELP: find-index
|
||||||
{ "elt" "the first matching element, 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." } ;
|
{ $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
|
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 } } }
|
{ $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." } ;
|
{ $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." } ;
|
||||||
|
|
|
@ -50,6 +50,10 @@ IN: sequences.tests
|
||||||
[ 3 CHAR: d ] [ "abcdefg" [ 3 = nip ] 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
|
[ 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 [ ] member? ] unit-test
|
||||||
[ f ] [ 3 [ 1 2 ] member? ] unit-test
|
[ f ] [ 3 [ 1 2 ] member? ] unit-test
|
||||||
[ t ] [ 1 [ 1 2 ] member? ] unit-test
|
[ t ] [ 1 [ 1 2 ] member? ] unit-test
|
||||||
|
|
|
@ -420,6 +420,12 @@ PRIVATE>
|
||||||
: (find-index) ( seq quot quot' -- i elt )
|
: (find-index) ( seq quot quot' -- i elt )
|
||||||
pick [ [ (each-index) ] dip call ] dip finish-find ; inline
|
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 )
|
: (accumulate) ( seq identity quot -- identity seq quot )
|
||||||
swapd [ curry keep ] curry ; inline
|
swapd [ curry keep ] curry ; inline
|
||||||
|
|
||||||
|
@ -494,6 +500,9 @@ PRIVATE>
|
||||||
: find-last ( ... seq quot: ( ... elt -- ... ? ) -- ... i elt )
|
: find-last ( ... seq quot: ( ... elt -- ... ? ) -- ... i elt )
|
||||||
[ [ 1 - ] dip find-last-integer ] (find) ; inline
|
[ [ 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-index ( ... seq quot: ( ... elt i -- ... ? ) -- ... i elt )
|
||||||
[ find-integer ] (find-index) ; inline
|
[ find-integer ] (find-index) ; inline
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue