Cartesian product operations
Factor handbook » The language » Collections » Sequence operations

Prev:Trimming sequences
Next:Deep sequence combinators


The cartesian product of two sequences is a sequence of all pairs where the first element of each pair is from the first sequence, and the second element of each pair is from the second sequence. The number of elements in the cartesian product is the product of the lengths of the two sequences.

Combinators which pair every element of the first sequence with every element of the second:
cartesian-each ( ... seq1 seq2 quot: ( ... elt1 elt2 -- ... ) -- ... )

cartesian-map ( ... seq1 seq2 quot: ( ... elt1 elt2 -- ... newelt ) -- ... newseq )

cartesian-find ( ... seq1 seq2 quot: ( ... elt1 elt2 -- ... ? ) -- ... elt1 elt2 )


Computing the cartesian product of two sequences:
cartesian-product ( seq1 seq2 -- newseq )

cartesian-product-as ( seq1 seq2 exemplar -- newseq )