]> gitweb.factorcode.org Git - factor.git/commitdiff
sequences: adding find-index-from.
authorJohn Benediktsson <mrjbq7@gmail.com>
Fri, 27 Apr 2012 20:41:47 +0000 (13:41 -0700)
committerJohn Benediktsson <mrjbq7@gmail.com>
Fri, 27 Apr 2012 20:41:47 +0000 (13:41 -0700)
core/sequences/sequences-docs.factor
core/sequences/sequences-tests.factor
core/sequences/sequences.factor

index 34164d89fee3f6fab7904e631a79f26c7469b2ac..f3dfebedf722284e5f55d02ffcfb97bc60b91be5 100644 (file)
@@ -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." } ;
index 615a21545e930006939f600a85b75c8889aae3fd..86624c0c19a0d0a1cd78b1eb1b4d12950b5c32af 100644 (file)
@@ -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
index 79847b0abf98604bf89f34eb53d125950ef1c220..0f208e2cabdcf882ae342b8c43b509063041eb32 100644 (file)
@@ -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