From 0075b6bc28c0423de1fff0527d2cc30f36b62984 Mon Sep 17 00:00:00 2001 From: John Benediktsson Date: Wed, 12 Oct 2011 19:41:54 -0700 Subject: [PATCH] sequences: adding find-index. --- basis/unicode/breaks/breaks.factor | 12 +++--------- core/sequences/sequences-docs.factor | 7 +++++++ core/sequences/sequences-tests.factor | 5 +++++ core/sequences/sequences.factor | 6 ++++++ 4 files changed, 21 insertions(+), 9 deletions(-) diff --git a/basis/unicode/breaks/breaks.factor b/basis/unicode/breaks/breaks.factor index ea88912d02..f4e9b739e3 100644 --- a/basis/unicode/breaks/breaks.factor +++ b/basis/unicode/breaks/breaks.factor @@ -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 ) diff --git a/core/sequences/sequences-docs.factor b/core/sequences/sequences-docs.factor index f256c965d5..f9c390ee49 100644 --- a/core/sequences/sequences-docs.factor +++ b/core/sequences/sequences-docs.factor @@ -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." } ; diff --git a/core/sequences/sequences-tests.factor b/core/sequences/sequences-tests.factor index 586e312edb..ac5798d946 100644 --- a/core/sequences/sequences-tests.factor +++ b/core/sequences/sequences-tests.factor @@ -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 diff --git a/core/sequences/sequences.factor b/core/sequences/sequences.factor index 153ef91809..17d2f00607 100644 --- a/core/sequences/sequences.factor +++ b/core/sequences/sequences.factor @@ -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 -- 2.34.1