Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Perls Functional functions

929 views

Published on

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

Published in: Technology, Art & Photos
  • Video of this presentation is available on youtube, at http://youtu.be/Rjy2Q4qRdz4
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here

Perls Functional functions

  1. 1. Perls Functional Functions
  2. 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. 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. 4. Filtering a listmy @odds;foreach ( qw( a b c d e ) ) {push @filtered, $_if ord( $_ ) % 2;}say for @odds;
  5. 5. Filtering a list with grepmy @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. 6. my @odds = grep { ord($_) % 2 } qw( a b c d e );
  7. 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. 8. grep● list_b = grep { code block } ( list_a )● Inside code block, $_ is it.● If code blocks return value is true, $_ is appended tolist_b.● As with foreach, $_ is an alias.● list_b and list_a need not be arrays.● Simple expressions may be used in place of codeblock.
  9. 9. Simple expressions in place of codeblock@explosions = grep { /bkaboomb/i } @phrases;@explosions = grep /bkaboomb/i, @phrases;@ones = grep { $_ == 1 } @booleans;@ones = grep $_ == 1, @booleans;● If it cant be expressed as a simple expression,use a code block.● Expression form requires a comma.
  10. 10. Transforming a listsub chars_to_ords {my @ords;foreach ( @_ ) {push @ords, ord $_;}return @ords;}my @ord_vals = chars_to_ords( qw( a b c d e ) );
  11. 11. Transforming a list with mapsub 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 );
  12. 12. my @ordinals = map { ord $_ } qw( a b c d e );
  13. 13. Lists dont have to be arrayssay for map { ord } qw( a b c d e );
  14. 14. map● list_b = map { code block } ( list_a )● Inside code block, $_ is it.● code blocks 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. 15. Simple expressions dont requirecode blocks.say for map ord, qw( a b c d e );# The expression form requires a comma.
  16. 16. Chaining is legal (even encouraged)say formap { $_->[0] }sort { $a->[1] <=> $b->[1] }map { [ $_, ord fc $_ ] }qw( a b c d e );# The Schwartzian Transform.
  17. 17. Sort?@sorted = sort @unsorted;@sorted = sort { $a <=> $b } @unsorted; # Ascend, numeric@sorted = sort { $b <=> $a } @unsorted; # Descend, numeric@sorted = sort { $a cmp $b } @unsorted; # Ascend, stringy@sorted = sort {$a <=> $b || $a cmp $b} @unsorted; # Ascending numeric, then stringy.@sorted= sort { $a->{name} cmp $b->{name} } @unsorted;
  18. 18. List::Util$first_one = first { /^kaboomb/i } @haystack;$sum = reduce { $a + $b } @numbers;$max = reduce { $a > $b ? $a : $b } @numbers;
  19. 19. List::MoreUtils$has_quiche = any { /bquicheb/ } @foods; # T/F$all_unicorns= all { $_ eq unicorn } @animals; # T/F@joint_incomes = pairwise { $a + $b } @his, @hers;@evens_odds = part { $_ % 2 } 1 .. 9; # LOLs
  20. 20. List::MoreUtils “natatime”my @alphabet = ( a .. z );my $iterator = natatime 3, @alphabet;while( my @chars = $iterator->() ) {print “@charsn”;}__END__a b cd e f…y z
  21. 21. List::MoreUtils● Too many to mention them all.– See https://metacpan.org/module/List::MoreUtils
  22. 22. Resources● perldoc -f grep● perldoc -f map● perldoc -f sort● perldoc List::Util● https://metacpan.org/module/List::MoreUtils
  23. 23. Salt Lake Perl Mongershttp://saltlake.pm.orgSlides on slideshare.nethttp://www.slideshare.net/daoswald/perls-functional-functions
  24. 24. Dave Oswalddavido@cpan.org

×