List-oo
view release on metacpan or search on metacpan
lib/List/oo.pm view on Meta::CPAN
Aside: I'm not sure I really like this as an interface point. The need
to use qr// is at least a little annoying.
=head2 Split
my $l = Split(qr/\s+/, $string);
=cut
sub Split {
my ($regex, $string) = @_;
## warn "$regex, $string\n";
UNIVERSAL::isa($regex, 'Regexp') or
croak("First argument to Split must be a regular expression");
return(List::oo->new(split($regex, $string)));
} # end subroutine Split definition
########################################################################
=head1 Convenience Functions
=head2 F
Declare a subroutine.
F{...};
See also L<lambda>, which lets you use C<λ{}> instead.
=over
=item About the C<sub {...}> syntax
Sadly, perl5 does not allow prototypes on methods. Thus, we cannot use
the undecorated block syntax as with
map({...} @list);
Rather, you must use the explicit C<sub {...}> syntax
$l->map(sub {...});
Or, use the C<F{}> or C<λ{}> shortcuts.
use List::oo qw(F);
...
$l->map(F{...});
With L<lambda>
use lambda;
...
$l->map(λ{...});
(If the above doesn't render as the greek character lambda, your pod
viewer is not playing nice.)
=back
=cut
sub F (&) {
my $sub = CORE::shift(@_);
@_ and croak;
UNIVERSAL::isa($sub, 'CODE') and return($sub);
eval($sub->isa('List::oo')) and croak 'not a method';
croak('why bother');
} # end subroutine F definition
########################################################################
=head1 List Methods
These methods are mostly analogous to the perl builtins. Where the
builtin would return a list, we return a List::oo object. Where the
builtin returns a scalar or some data which was not the primary list
(e.g. C<push>, C<pop>, C<splice>, etc.), you'll find some iI<foo>()
methods (the 'i' prefix is for 'inline'.)
=head2 grep
$l = $l->grep(sub {...});
=cut
sub grep {
my $self = CORE::shift;
my $sub = CORE::shift;
return($self->new(CORE::grep({$sub->($_)} @$self)));
} # end subroutine grep definition
########################################################################
=head2 map
$l = $l->map(sub {...});
=cut
sub map {
my $self = CORE::shift;
my $sub = CORE::shift;
return($self->new(CORE::map({$sub->($_)} @$self)));
} # end subroutine map definition
########################################################################
=head2 reverse
$l = $l->reverse;
=cut
sub reverse {
my $self = CORE::shift;
return($self->new(CORE::reverse(@$self)));
} # end subroutine reverse definition
########################################################################
=head2 dice
Does things that can't be done with map.
$l2 = $l->dice(sub {my @a = @_; ... return(@a);});
( run in 1.753 second using v1.01-cache-2.11-cpan-800906f7e73 )