Maptastic

 view release on metacpan or  search on metacpan

lib/Maptastic.pm  view on Meta::CPAN

with for, you stop at the end of the list).

Note that the exact behvaviour of `mapcar' apparently varied from LISP
to LISP, so the version given here is the one that was widely
publicised on PerlMonks.

=cut

# This function has been updated to include support for certain types
# of iterators
sub mapcar(&@)
{
    my $sub= shift;
    if(  ! @_  ) {
        croak( "mapcar: Nothing to map" );
    }

    my @which;

    for my $av (  @_  ) {
	if (ref $av eq "ARRAY") {

lib/Maptastic.pm  view on Meta::CPAN

=item map_shift { code } \@list, \@list, \@list...

"mapcaru" is a version that works similarly to `mapcar', but puts
I<undef> (hence the u) into locations in the input array where the
input list has no elements.  This function is also available as
`map_shift' (because with `shift', you get undef out if there was
nothing in the list).

=cut

sub mapcaru(&@)
{
    my $sub= shift;
    if(  ! @_  ) {
        croak( "mapcaru: nothing to map" );
    }
    my $max= 0;
    for my $av (  @_  ) {
        if(  ! UNIVERSAL::isa( $av, "ARRAY" )  ) {
            croak( "mapcaru: `$av' is not an array reference" );
        }
        $max = @$av if $max < @$av;
    }
    my @ret;
    for(  my $i= 0;  $i < $max;  $i++  ) {
        push @ret, &$sub( map { $_->[$i] } @_ );
    }
    return wantarray ? @ret : \@ret;
}
sub map_shift(&@) { goto \&mapcaru }

=item map_each { code } \%hash, \%hash, ...

"map_each" is a version of `map' that works on hashes.  B<It does not
work like mapcar or mapcaru, it is a simple map for hashes>.
Supplying multiple hashes iterates over all of the hashes in sequence.

=cut

sub map_each(&@)
{
    my $sub = shift;
    if(  ! @_  ) {
        croak( "mapeach: Nothing to map" );
    }
    map { UNIVERSAL::isa($_, "HASH") or do {
        croak( "mapeach: `$_' is not a hash reference" );
    }; } @_;

    my @results;

lib/Maptastic.pm  view on Meta::CPAN

=item imap_for ...

=item imap_foreach ...

Returns an iterator version of mapcar (a CODE reference)

=back

=cut

sub imapcar(&@) {
    die "imapcar not yet implemented";
}

sub imap_for (&@) { goto \&imapcar };
sub imap_foreach (&@) { goto \&imapcar };

=head2 map's cousins

While not as mapxy as our star, this group of functions will be found
alongside map and imap in many a code fragment.

lib/Maptastic.pm  view on Meta::CPAN

   @a = ( filter { s{.*/(.*)}{} }
          split /\0/,
          `find . -type f -print0` );

   for (@a) {
       # do something with each filename
   }

=cut

sub filter(&@) {
    my $sub = shift;
    my @rv;
    my @input = slurp @_;
    while (@input) {
	local($_) = shift @input;
	$sub->();
	push @rv, $_;
    }
    @rv;
}

lib/Maptastic.pm  view on Meta::CPAN

   FIND->input_record_seperator("\0");

   $iter = ifilter { s{.*/(.*)}{} } \*FIND;

   while ( my $filename = $iter->() ) {
       # do something with each filename
   }

=cut

sub ifilter(&@) {
    my $sub = shift;
    my $iter = iter(@_);

    return bless sub {
	my $val = $iter->();
	if (defined($val)) {
	    local($_) = $val;
	    $sub->();
	    return $_;
	} else {

lib/Maptastic.pm  view on Meta::CPAN

	}
    }, __PACKAGE__;
}

=item igrep { BLOCK }, [...]

Iterative `grep'

=cut

sub igrep(&@) {
    my $sub = shift;
    my $iter = iter @_;

    return bless sub {
	my $ok = 0;
	while (1) {
	    local($_) = $iter->();
	    return unless defined $_;
	    if ($sub->()) {
		return $_;



( run in 1.246 second using v1.01-cache-2.11-cpan-49f99fa48dc )