Perl provides some very useful functional style functions, such as grep, map, and sort. This is a beginner's introduction to some of the most common ones.
Video of this presentation is available at http://youtu.be/Rjy2Q4qRdz4
This presentation was given at Salt Lake Perl Mongers, June 11, 2013. The Salt Lake Perl Mongers website is http://saltlake.pm.org
2. The common (core) ones...
● Built-in:
– grep – Filters a list.
– map – Creates or transforms a list.
– sort – Sorts a list.
● List::Util (core)
– first – Like grep, but returns first one found.
– reduce – Summarizes a list.
3. The common CPAN ones:
● List::MoreUtils
– any – Return true if any element in list matches.
– all – Return true if all elements match.
– pairwise – Transform two lists, pairwise, into one.
– Many others!
4. Filtering a list
my @odds;
foreach ( qw( a b c d e ) ) {
push @filtered, $_
if ord( $_ ) % 2;
}
say for @odds;
5. Filtering a list with grep
my @odds;
foreach (
qw( a b c d e )
) {
push @odds, $_
if ord( $_ ) % 2;
}
say for @odds;
my @odds = grep {
ord( $_ ) % 2
} qw( a b c d e );
say for @odds;
6. my @odds = grep { ord($_) % 2 } qw( a b c d e );
7. Which indices contain odd ords?
my @chars = qw( a c f b q r n b d );
my @odds_by_idx = grep {
ord( $chars[$_] ) % 2
} 0 .. $#chars;
8. grep
● list_b = grep { code block } ( list_a )
● Inside code block, $_ is it.
● If code block's return value is true, $_ is appended to
list_b.
● As with foreach, $_ is an alias.
● list_b and list_a need not be arrays.
● Simple expressions may be used in place of code
block.
9. Simple expressions in place of code
block
@explosions = grep { /bkaboomb/i } @phrases;
@explosions = grep /bkaboomb/i, @phrases;
@ones = grep { $_ == 1 } @booleans;
@ones = grep $_ == 1, @booleans;
● If it can't be expressed as a simple expression,
use a code block.
● Expression form requires a comma.
10. Transforming a list
sub chars_to_ords {
my @ords;
foreach ( @_ ) {
push @ords, ord $_;
}
return @ords;
}
my @ord_vals = chars_to_ords( qw( a b c d e ) );
11. Transforming a list with map
sub chars_to_ords {
my @ords;
foreach ( @_ ) {
push @ords, ord $_;
}
return @ords;
}
my @ordinals
= chars_to_ords(
qw( a b c d e )
);
my @ordinals
= map { ord $_ }
qw( a b c d e );
13. Lists don't have to be arrays
say for map { ord } qw( a b c d e );
14. map
● list_b = map { code block } ( list_a )
● Inside code block, $_ is it.
● code block's return value is appended to list_b.
● As with foreach, $_ is an alias.
● list_b and list_a need not be arrays.
● code block is a subroutine; no last, no next.
● Skip current iteration by returning an empty list.
15. Simple expressions don't require
code blocks.
say for map ord, qw( a b c d e );
# The expression form requires a comma.
16. Chaining is legal (even encouraged)
say for
map { $_->[0] }
sort { $a->[1] <=> $b->[1] }
map { [ $_, ord fc $_ ] }
qw( a b c d e );
# The Schwartzian Transform.
20. List::MoreUtils “natatime”
my @alphabet = ( 'a' .. 'z' );
my $iterator = natatime 3, @alphabet;
while( my @chars = $iterator->() ) {
print “@charsn”;
}
__END__
a b c
d e f
…
y z