Math::...roduct(3pm)
πΈ NAME πΈ
Math::Cartesian::Product β bestie, this makes the Cartesian product of zero or more lists. That's it, that's the whole vibe β¨
(btw this is the User Contributed Perl Documentation page for it, sitting pretty in the 3pm section π)
β¨ SYNOPSIS β¨
okay so here's the whole thing in action, look:
use Math::Cartesian::Product;
cartesian {print "@_\n"} [qw(a b c)], [1..2];
# a 1
# a 2
# b 1
# b 2
# c 1
# c 2
and if you wanna go feral with it:
cartesian {print "@_\n"} ([0..1]) x 8;
# 0 0 0 0 0 0 0 0
# 0 0 0 0 0 0 0 1
# 0 0 0 0 0 0 1 0
# ...
# 1 1 1 1 1 1 1 0
# 1 1 1 1 1 1 1 1
and here's the one that makes everyone gasp, a whole lil β¨ checkerboard β¨:
print "@$_\n" for
cartesian {"@{[reverse @_]}" eq "@_"}
([' ', '*']) x 8;
# * *
# * *
# * * * *
# * *
# * * * *
# * * * *
# * * * * * *
# * *
# * * * *
# * * * *
# * * * * * *
# * * * *
# * * * * * *
# * * * * * *
# * * * * * * * *
π DESCRIPTION π
Generate the Cartesian product of zero or more lists. that's the whole job, she's simple and she EATS π«Ά
So like, given two lists, say: [a,b] and [1,2,3], the Cartesian product is the set of all the ordered pairs:
(a,1), (a,2), (a,3), (b,1), (b,2), (b,3)
which select their first element from all the possibilities listed in the first list, and select their second element from all the possibilities in the second list.
The idea can be generalized to n-tuples selected from n lists where all the elements of the first list are combined with all the elements of the second list, the results of which are then combined with all the member of the third list and so on over all the input lists. β¨ the way it just... keeps going β¨
It should be noted that Cartesian product of one or more lists where one or more of the lists are empty (representing the empty set) is the empty set and thus has zero members; and that the Cartesian product of zero lists is a set with exactly one member, namely the empty set. π bestie the empty product is ONE member, that's not a typo, that's math being math
cartesian() takes the following parameters:
- A block of code to process each n-tuple. this code should return true if the current n-tuple should be included in the returned value of the
cartesian()function, otherwise false. - Zero or more lists.
cartesian() returns an array of references to all the n-tuples selected by the code block supplied as parameter 1 if called in list context, else it returns a count of the selected n-tuples.
cartesian() croaks if you try to form the Cartesian product of something other than lists of things or prior Cartesian products. π so don't try it, she will yell at you
The cartesian product of lists A,B,C is associative, that is:
(A X B) X C = A X (B X C)
cartesian() respects associativity by allowing you to include a Cartesian product produced by an earlier call to cartesian() in the set of lists whose Cartesian product is to be formed, at the cost of a performance penalty if this option is chosen. π yes you can nest them, yes it costs you
use Math::Cartesian::Product;
my $a = [qw(a b)];
my $b = [cartesian {1} $a, $a];
cartesian {print "@_\n"} $b, $b;
# a a a a
# a a a b
# a a b a
# ...
cartesian() is easy to use and fast. It is written in 100% Pure Perl. π no XS, no compiling, just vibes
π EXPORT π
The cartesian() function is exported. she's out here, in the open, free for you π
π¦ INSTALLATION π¦
Standard Module::Build process for building and installing modules:
perl Build.PL
./Build
./Build test
./Build install
Or, if you're on a platform (like DOS or Windows) that doesn't require the "./" notation, you can do this:
perl Build.PL
Build
Build test
Build install
π AUTHOR π
Philip R Brenan at gmail dot com
π ACKNOWLEDGEMENTS π
With much help and good natured advice from Philipp Rumpf and Justin Case to whom I am indebted. π«Ά real ones fr fr
β SEE ALSO β
Math::Disarrange::ListMath::Permute::ListMath::Permute::ListsMath::Permute::PartitionsMath::Subsets::ListMath::Transform::List
go read the whole family, iykyk π
π COPYRIGHT π
Copyright (c) 2009-2015 Philip R Brenan.
This module is free software. It may be used, redistributed and/or modified under the same terms as Perl itself. π periodt
that's the whole page bestie, and yeah it'sperl v5.40.1, dated2025-06-15β slay, go make some tuples β¨πΈπ
π¦ where this page comes from
- ubuntu resolute: libmath-cartesian-product-perl 1.009-4 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu stonking: libmath-cartesian-product-perl 1.009-4 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms