]> gitweb.factorcode.org Git - factor.git/commitdiff
sequences: adding cartesian-find.
authorJohn Benediktsson <mrjbq7@gmail.com>
Sat, 14 Dec 2019 03:20:27 +0000 (19:20 -0800)
committerJohn Benediktsson <mrjbq7@gmail.com>
Sat, 14 Dec 2019 03:20:27 +0000 (19:20 -0800)
core/sequences/sequences-docs.factor
core/sequences/sequences-tests.factor
core/sequences/sequences.factor

index 2404d2e17e760fb4b9372e9fa8d54e7d2829b9d4..1ce18af8b25212d62cf478245feb42090626d3f8 100644 (file)
@@ -1621,6 +1621,10 @@ HELP: assert-sequence=
   }
 } ;
 
+HELP: cartesian-find
+{ $values { "seq1" sequence } { "seq2" sequence } { "quot" { $quotation ( ... elt1 elt2 -- ... ? ) } } { "elt1" object } { "elt2" object } }
+{ $description "Applies the quotation to every possible pairing of elements from the two sequences, returning the first two elements where the quotation returns a true value." } ;
+
 HELP: cartesian-each
 { $values { "seq1" sequence } { "seq2" sequence } { "quot" { $quotation ( ... elt1 elt2 -- ... ) } } }
 { $description "Applies the quotation to every possible pairing of elements from the two sequences." } ;
@@ -1981,6 +1985,7 @@ $nl
 { $subsections
     cartesian-each
     cartesian-map
+    cartesian-find
 }
 "Computing the cartesian product of two sequences:"
 { $subsections
index 72a326aaab5089394102d87c25865deb1b8bdc1d..74320d76bab40514103734053092a0ec7ab24e3d 100644 (file)
@@ -363,6 +363,9 @@ M: bogus-hashcode hashcode* 2drop 0 >bignum ;
 { { { { 1 "a" } { 1 "b" } } { { 2 "a" } { 2 "b" } } } }
 [ { 1 2 } { "a" "b" } cartesian-product ] unit-test
 
+{ 2 4 } [ { 1 2 3 } { 4 5 6 } [ [ even? ] both? ] cartesian-find ] unit-test
+{ f f } [ { 1 2 3 } { 4 5 6 } [ [ 10 > ] both? ] cartesian-find ] unit-test
+
 [ { } [ string>digits sum ] [ + ] map-reduce ] must-infer
 [ { } [ ] [ + ] map-reduce ] must-fail
 { 4 } [ { 1 1 } [ 1 + ] [ + ] map-reduce ] unit-test
index a711f645af9745dd9603e57974ccc8507ccf8164..d0b2a7bc426cf2875cb7847b2a4a9c7f1e3bfd3e 100644 (file)
@@ -1082,6 +1082,9 @@ M: repetition sum [ elt>> ] [ length>> ] bi * ; inline
 : cartesian-product ( seq1 seq2 -- newseq )
     [ { } 2sequence ] cartesian-map ;
 
+: cartesian-find ( ... seq1 seq2 quot: ( ... elt1 elt2 -- ... ? ) -- ... elt1 elt2 )
+    [ f ] 3dip [ with find swap ] 2curry [ nip ] prepose find nip swap ; inline
+
 <PRIVATE
 
 : select-by ( ... seq quot: ( ... elt -- ... x ) compare: ( obj1 obj2 -- ? ) -- ... elt )