]> gitweb.factorcode.org Git - factor.git/commitdiff
sequences.generalizations: add "nfind" and "nany?".
authorJohn Benediktsson <mrjbq7@gmail.com>
Thu, 4 Oct 2012 21:48:46 +0000 (14:48 -0700)
committerJohn Benediktsson <mrjbq7@gmail.com>
Thu, 4 Oct 2012 21:48:46 +0000 (14:48 -0700)
basis/sequences/generalizations/generalizations-docs.factor
basis/sequences/generalizations/generalizations-tests.factor
basis/sequences/generalizations/generalizations.factor

index 6ec9d11176015c9e40a9ee7617a2d503030b02d0..26ddc080273a5727715c77ff87281428dd27b5af 100644 (file)
@@ -118,6 +118,14 @@ HELP: nall?
 { $values { "seqs..." { $snippet "n" } " sequences on the datastack" } { "quot" { $quotation "( element... - ? )" } } { "n" integer } { "?" boolean } }
 { $description "A generalization of " { $link all? } " that can be applied to any number of sequences." } ;
 
+HELP: nfind
+{ $values { "seqs..." { $snippet "n" } " sequences on the datastack" } { "quot" { $quotation "( element... - ? )" } } { "n" integer } { "i" integer } { "elts..." { $snippet "n" } " elements on the datastack" } }
+{ $description "A generalization of " { $link find } " that can be applied to any number of sequences." } ;
+
+HELP: nany?
+{ $values { "seqs..." { $snippet "n" } " sequences on the datastack" } { "quot" { $quotation "( element... - ? )" } } { "n" integer } { "?" boolean } }
+{ $description "A generalization of " { $link any? } " that can be applied to any number of sequences." } ;
+
 ARTICLE: "sequences.generalizations" "Generalized sequence words"
 "The " { $vocab-link "sequences.generalizations" } " vocabulary defines generalized versions of various sequence operations."
 { $subsections
index bdce788615e717885426701b337dfb705110bfeb..0558d5d050c320e918fd02c8f8417a990e164203 100644 (file)
@@ -140,3 +140,13 @@ D4d$
 { t } [
     { 1 3 5 } { 2 4 6 } { 4 8 12 } [ + + odd? ] 3 nall?
 ] unit-test
+
+{ t } [
+    { 2 4 5 } { 4 6 7 } { 6 8 9 }
+    [ [ odd? ] tri@ and and ] 3 nany?
+] unit-test
+
+{ f } [
+    { 1 2 3 } { 4 5 6 } { 7 8 9 }
+    [ [ odd? ] tri@ and and ] 3 nany?
+] unit-test
index a722760fcfb1d9311c21156db861ea8d7bacc3e4..81f18f41d64d9feb5bbdbc540e7bd3de2ee8db1c 100644 (file)
@@ -124,3 +124,23 @@ MACRO: nmap-reduce ( map-quot reduce-quot n -- quot )
 
 : nall? ( seqs... quot n -- ? )
     (neach) all-integers? ; inline
+
+MACRO: finish-nfind ( n -- quot )
+    [ 1 + ] keep dup dup dup '[
+        _ npick
+        [ [ dup ] _ ndip _ nnth-unsafe ]
+        [ _ ndrop _ [ f ] times ]
+        if
+    ] ;
+
+: (nfind) ( seqs... quot n quot' -- i elts... )
+    over
+    [ '[ _ _ (neach) @ ] ] dip
+    [ '[ _ finish-nfind ] ] keep
+    nbi ; inline
+
+: nfind ( seqs... quot n -- i elts... )
+    [ find-integer ] (nfind) ; inline
+
+: nany? ( seqs... quot n -- ? )
+    [ nfind ] [ ndrop ] bi >boolean ; inline