From e38e9f2850f61c6e041583c9082a45394dbad001 Mon Sep 17 00:00:00 2001 From: John Benediktsson Date: Fri, 27 Apr 2012 13:41:47 -0700 Subject: [PATCH] sequences: adding find-index-from. --- core/sequences/sequences-docs.factor | 8 ++++++++ core/sequences/sequences-tests.factor | 4 ++++ core/sequences/sequences.factor | 9 +++++++++ 3 files changed, 21 insertions(+) diff --git a/core/sequences/sequences-docs.factor b/core/sequences/sequences-docs.factor index 34164d89fe..f3dfebedf7 100644 --- a/core/sequences/sequences-docs.factor +++ b/core/sequences/sequences-docs.factor @@ -450,6 +450,14 @@ HELP: find-index { "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." } ; diff --git a/core/sequences/sequences-tests.factor b/core/sequences/sequences-tests.factor index 615a21545e..86624c0c19 100644 --- a/core/sequences/sequences-tests.factor +++ b/core/sequences/sequences-tests.factor @@ -50,6 +50,10 @@ IN: sequences.tests [ 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 diff --git a/core/sequences/sequences.factor b/core/sequences/sequences.factor index 79847b0abf..0f208e2cab 100644 --- a/core/sequences/sequences.factor +++ b/core/sequences/sequences.factor @@ -420,6 +420,12 @@ PRIVATE> : (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 @@ -494,6 +500,9 @@ PRIVATE> : 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 -- 2.34.1