]> gitweb.factorcode.org Git - factor.git/commitdiff
sequences: adding find-index.
authorJohn Benediktsson <mrjbq7@gmail.com>
Thu, 13 Oct 2011 02:41:54 +0000 (19:41 -0700)
committerJohn Benediktsson <mrjbq7@gmail.com>
Thu, 13 Oct 2011 02:41:54 +0000 (19:41 -0700)
basis/unicode/breaks/breaks.factor
core/sequences/sequences-docs.factor
core/sequences/sequences-tests.factor
core/sequences/sequences.factor

index ea88912d02f35f37a3290a421cf68b0fab0d9cf1..f4e9b739e344050853883ae6cb773b0886ae2ff8 100644 (file)
@@ -4,9 +4,9 @@ USING: accessors alien.syntax arrays assocs combinators
 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
 
@@ -249,12 +249,6 @@ words init-table table
         [ 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 )
index f256c965d52fc0eb714bfa568d940a1d29f66d31..f9c390ee4964a89710b02cb111dc7379db60bb30 100644 (file)
@@ -418,6 +418,13 @@ HELP: find-last-from
 { $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." } ;
index 586e312edbcf1cc8fc7db9e1c844bb16af73485c..ac5798d946ed141e49ed1664863738c6923538a3 100644 (file)
@@ -45,6 +45,11 @@ IN: sequences.tests
 [ 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
index 153ef91809238af1f4da8683a759d3644518bf47..17d2f00607aadd344376a9679832cb5f99b921e1 100644 (file)
@@ -403,6 +403,9 @@ PRIVATE>
     [ 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
 
@@ -477,6 +480,9 @@ PRIVATE>
 : 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