Algorithm-Loops
view release on metacpan or search on metacpan
lib/Algorithm/Loops.pm view on Meta::CPAN
If you find yourself trying to use s/// or tr/// inside of map (or grep),
then you should probably use Filter instead.
For example:
use Algorithm::Loops qw( Filter );
@copy = Filter { s/\\(.)/$1/g } @list;
$text = Filter { s/^\s+// } @lines;
The same process can be accomplished using a careful and more complex
invocation of map, grep, or foreach. However, many incorrect ways to
attempt this seem rather seductively appropriate so this function helps
to discourage such (rather common) mistakes.
=head3 Usage
Filter has a prototype specification of (\&@).
This means that it demands that the first argument that you pass to it be
a CODE reference. After that you can pass a list of as many or as few
lib/Algorithm/Loops.pm view on Meta::CPAN
=item MapCarMin(\&@)
=back
=head3 Usage
The MapCar* functions are all like C<map> except they each loop over more
than one list at the same time.
[ The name "mapcar" comes from LISP. As I understand it, 'car' comes from
the acronym for a register of the processor where LISP was first
developed, one of two registers used to implement lists in LISP. I only
mention this so you won't waste too much time trying to figure out what
"mapcar" is supposed to mean. ]
The MapCar* functions all have prototype specifications of (\&@).
This means that they demand that the first argument that you pass be a
CODE reference. After that you should pass zero or more array references.
Your subroutine is called (in a list context) and is passed the first
element of each of the arrays whose references you passed in (in the
corresponding order). Any value(s) returned by your subroutine are
pushed onto an array that will eventually be returned by MapCar*.
Next your subroutine is called and is passed the B<second> element of
each of the arrays and any value(s) returned are pushed onto the results
array. Then the process is repeated with the B<third> elements.
This continues until your subroutine has been passed all elements [except
for some cases with MapCarMin()]. If the longest array whose reference
you passed to MapCar() or MapCarU() contained $N elements, then your
subroutine would get called $N times.
Finally, the MapCar* function returns the accumulated list of values. If
called in a scalar context, the MapCar* function returns a reference to
an array containing these values.
lib/Algorithm/Loops.pm view on Meta::CPAN
=over 4
=item Iterators - No huge memory requirements
Some permutation generators return the full set of all permutations (as a
huge list of lists). Your input list doesn't have to be very big at all
for the resulting set to be too large to fit in your available memory.
So the NextPermute* routines return each permutation, one at a time, so
you can process them all (eventually) without the need for lots of memory.
A programming object that gives you access to things one-at-a-time is
called an "iterator".
=item No context - Hardly any memory required
The NextPermute* routines require no extra memory in the way of context
or lists to keep track of while constructing the permutations.
Each call to a NextPermute* routine shuffles the items in the list
( run in 0.237 second using v1.01-cache-2.11-cpan-8d75d55dd25 )