Functional perl

1,564 views

Published on

Unpolished talk on functional concepts in perl.

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
1,564
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
8
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Functional perl

  1. 1. Functional Programming Perl Christopher Mckay error@errorific.com
  2. 2. What Am I
  3. 3. Whats this talk all about?
  4. 4. History
  5. 5. How
  6. 6. Why not?
  7. 7. But I dont use/appreciate/like perl?
  8. 8. Functional Programming is Easy
  9. 9. So easy sysadmins do it
  10. 10. In the beginning...
  11. 11. Wrote Pearl Perl
  12. 12. People wanted a sort function Larry was generous
  13. 13. sortF(a, a), [a] --------> [a]
  14. 14. @sorted_list = sort {$_[0] <=> $_[1]} @list
  15. 15. @sorted_list = sort {$a <=> $b } @list
  16. 16. @sorted_list = sort {$a <=> $b} @list
  17. 17. sort String sortingsort {$a cmp $b} Also string sortingsort {$a <=> $b} Sort numericallysort &comparator Sort by function named comparator
  18. 18. sort { @a = split ///, $a; @b = split ///, $b; ($a[2] <=> $b[2]) || ($a[1] <=> $b[1]) || ($a[0] <=> $b[0]) || 0;} @list_of_dmy_dates
  19. 19. 1988 - Perl 2.01989 - Perl 3.01991 - Perl 4.0
  20. 20. grepF(a, a), [a] --------> [a]
  21. 21. mapF(a), [a] --------> [a]
  22. 22. 1994 – Perl 5.0
  23. 23. memoizeF(a) → b --------------> F(a) → b
  24. 24. List::Util reduceF(a, b) → b, [a] --------------> b
  25. 25. List::MoreUtilsany, all, none, notall, true, false, firstidx, first_index, lastidx, part, mesh,last_index, insert_after, zip, uniq, apply, indexes, after, after_incl, before, before_incl, firstval, first_value, lastval, last_value, pairwise, distinct, minmax
  26. 26. So Perl evolved a functional theme
  27. 27. So how do I make functional stuff in perl instead of just using it?
  28. 28. Lexical scoping
  29. 29. First class functions
  30. 30. Thats about it.Your language does that too right?
  31. 31. So lets write a function that rolls a die
  32. 32. my $dx = sub { my ($sides) = @_; return (rand() * $sides) + 1;}my $roll = $dx->(6);
  33. 33. I dont like rolling 1s though so...
  34. 34. my $cheat = sub { my ($roll) = @_; if ($roll < 2) { return 2; } else { return $roll; }}
  35. 35. I dont want to get caught turning the die over though so ...
  36. 36. my $inconspicuous_dx = sub { my ($sides) = @_; return $cheat->($dx->($sides));}
  37. 37. my $compose = sub { my ($fa, $fb) = @_; return sub { return $fa->($fb->(@_)); }}
  38. 38. my $inconspicuous_dx = $compose->($cheat, $dx);my $better_roll = $inconspicuous_dx->(6);
  39. 39. Monads in PerlMasahiro Honma (hiratara) YAPC::Asia 2011 Slides are in english Data::Monad
  40. 40. All I ever need is d6!
  41. 41. my $d6 = sub { return $inconspicuous_dx->(6);};my $roll_a_6 = $d6->();
  42. 42. Too lazy to roll 20d6
  43. 43. my $dice_tower_maker = sub { my ($die) = @_; return sub { my ($rolls) = @_; return map { $die->() } (1 .. $rolls); }}
  44. 44. my $dice_tower = $dice_tower_generator->($d6);say join(, , $dice_tower->(20));
  45. 45. use List::Util;say sum $dice_tower->(20);
  46. 46. use List::Util;say reduce {$a + $b} $dice_tower->(20);
  47. 47. So why not use this stuff for everything?
  48. 48. The compilers not out to help you
  49. 49. Declaring functions gets obtuse after awhile
  50. 50. Purity isnt an option, everything changes
  51. 51. The community loves objects
  52. 52. If it was simple in a functionallanguage it probably takes some hacking to make it work in perl
  53. 53. And the language isnt extensible, yet
  54. 54. Good things are to come
  55. 55. Perl 6
  56. 56. Static typing Better syntax Blocks and closures List comprehensionAutomatic parallelisation Lots more
  57. 57. ThanksQuestions?

×