Algorithm-Odometer-Tiny
view release on metacpan or search on metacpan
lib/Algorithm/Odometer/Tiny.pm view on Meta::CPAN
wheel is advanced to the next position, and if it wraps around, the
next higher wheel is incremented by one, and so on - it is the same
basic algorithm that we use to count from 0 to 100 and onwards,
except with different "digits".
The constructor of this class takes a list of array references, each
of which represents a wheel in the odometer. The constructor returns
an object of this class, which can be called as a code reference
(C<< $odometer->() >>), or the C<< <> >> I/O operator can be used to
read the next item. Calling the code reference or C<< <> >> operator
in scalar context returns the current state of the wheels joined
together as a string, while calling the code reference in list
context returns the current state of the wheels as a list of
individual values. In Perl 5.18 and above, calling the C<< <> >>
operator in list context will return all of the (remaining) values in
the sequence as strings. In scalar context, the iterator will return
C<undef> once, and then start the sequence from the beginning.
This class is named C<::Tiny> because the code for the odometer fits
on a single page, and if you look at the source, you'll see a
C<sub odometer> that you can copy out of the source code if you wish
(if you're not using L<Carp|Carp>, just replace C<croak> with C<die>).
=head2 Example
The following wheels:
["Hello","Hi"], ["World","this is"], ["a test.","cool!"]
produce this sequence:
("Hello", "World", "a test.")
("Hello", "World", "cool!")
("Hello", "this is", "a test.")
("Hello", "this is", "cool!")
("Hi", "World", "a test.")
("Hi", "World", "cool!")
("Hi", "this is", "a test.")
("Hi", "this is", "cool!")
=head1 See Also
=over
=item *
L<Algorithm::Odometer::Gray>
=back
Here are some other implementations of the Cartesian product,
although they may not produce items in the same order as this module.
Note that if you want speed, XS-based implementations such as
L<Math::Prime::Util|Math::Prime::Util> or L<Set::Product::XS|Set::Product::XS>
are probably going to be fastest.
=over
=item *
Perl's L<glob|perlfunc/glob> can produce a Cartesian product, if
non-empty braces are the only wildcard characters used in the pattern.
=item *
L<Algorithm::Loops|Algorithm::Loops>'s C<NestedLoops>
=item *
L<List::Gen|List::Gen>'s C<cartesian>
=item *
L<List::MapMulti|List::MapMulti>
=item *
L<Math::Cartesian::Product|Math::Cartesian::Product>
=item *
L<Math::Prime::Util|Math::Prime::Util>'s C<forsetproduct>
=item *
L<Set::CartesianProduct::Lazy|Set::CartesianProduct::Lazy>
=item *
L<Set::CrossProduct|Set::CrossProduct>
=item *
L<Set::Product|Set::Product> / L<Set::Product::XS|Set::Product::XS>
=item *
L<Set::Scalar|Set::Scalar>'s C<cartesian_product>
=back
=head1 Acknowledgements
The motivation to release this module kindly provided by:
L<Some Kiwi Novice @ PerlMonks|https://www.perlmonks.org/?node_id=11107116>
=head1 References
=over
=item 1
Dominus, M. (2005). Higher-Order Perl: Transforming Programs with Programs. Burlington: Elsevier.
L<http://hop.perl.plover.com/>.
Chapter 4 "Iterators", Section 4.3.1 "Permutations".
=back
=head1 Author, Copyright, and License
Copyright (c) 2019 Hauke Daempfling (haukex@zero-g.net).
( run in 2.545 seconds using v1.01-cache-2.11-cpan-524268b4103 )