0
Upcoming SlideShare
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Standard text messaging rates apply

# Functional perl

1,112

Published on

Unpolished talk on functional concepts in perl.

Unpolished talk on functional concepts in perl.

Published in: Technology
0 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

• Be the first to like this

Views
Total Views
1,112
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
6
0
Likes
0
Embeds 0
No embeds

No notes for slide

### Transcript

• 1. Functional Programming Perl Christopher Mckay error@errorific.com
• 2. What Am I
• 3. Whats this talk all about?
• 4. History
• 5. How
• 6. Why not?
• 7. But I dont use/appreciate/like perl?
• 8. Functional Programming is Easy
• 9. So easy sysadmins do it
• 10. In the beginning...
• 11. Wrote Pearl Perl
• 12. People wanted a sort function Larry was generous
• 13. sortF(a, a), [a] --------&gt; [a]
• 14. @sorted_list = sort {\$_[0] &lt;=&gt; \$_[1]} @list
• 15. @sorted_list = sort {\$a &lt;=&gt; \$b } @list
• 16. @sorted_list = sort {\$a &lt;=&gt; \$b} @list
• 17. sort String sortingsort {\$a cmp \$b} Also string sortingsort {\$a &lt;=&gt; \$b} Sort numericallysort &amp;comparator Sort by function named comparator
• 18. sort { @a = split ///, \$a; @b = split ///, \$b; (\$a[2] &lt;=&gt; \$b[2]) || (\$a[1] &lt;=&gt; \$b[1]) || (\$a[0] &lt;=&gt; \$b[0]) || 0;} @list_of_dmy_dates
• 19. 1988 - Perl 2.01989 - Perl 3.01991 - Perl 4.0
• 20. grepF(a, a), [a] --------&gt; [a]
• 21. mapF(a), [a] --------&gt; [a]
• 22. 1994 &#x2013; Perl 5.0
• 23. memoizeF(a) &#x2192; b --------------&gt; F(a) &#x2192; b
• 24. List::Util reduceF(a, b) &#x2192; b, [a] --------------&gt; b
• 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. So Perl evolved a functional theme
• 27. So how do I make functional stuff in perl instead of just using it?
• 28. Lexical scoping
• 29. First class functions
• 31. So lets write a function that rolls a die
• 32. my \$dx = sub { my (\$sides) = @_; return (rand() * \$sides) + 1;}my \$roll = \$dx-&gt;(6);
• 33. I dont like rolling 1s though so...
• 34. my \$cheat = sub { my (\$roll) = @_; if (\$roll &lt; 2) { return 2; } else { return \$roll; }}
• 35. I dont want to get caught turning the die over though so ...
• 36. my \$inconspicuous_dx = sub { my (\$sides) = @_; return \$cheat-&gt;(\$dx-&gt;(\$sides));}
• 37. my \$compose = sub { my (\$fa, \$fb) = @_; return sub { return \$fa-&gt;(\$fb-&gt;(@_)); }}
• 38. my \$inconspicuous_dx = \$compose-&gt;(\$cheat, \$dx);my \$better_roll = \$inconspicuous_dx-&gt;(6);
• 39. Monads in PerlMasahiro Honma (hiratara) YAPC::Asia 2011 Slides are in english Data::Monad
• 40. All I ever need is d6!
• 41. my \$d6 = sub { return \$inconspicuous_dx-&gt;(6);};my \$roll_a_6 = \$d6-&gt;();
• 42. Too lazy to roll 20d6
• 43. my \$dice_tower_maker = sub { my (\$die) = @_; return sub { my (\$rolls) = @_; return map { \$die-&gt;() } (1 .. \$rolls); }}
• 44. my \$dice_tower = \$dice_tower_generator-&gt;(\$d6);say join(, , \$dice_tower-&gt;(20));
• 45. use List::Util;say sum \$dice_tower-&gt;(20);
• 46. use List::Util;say reduce {\$a + \$b} \$dice_tower-&gt;(20);
• 47. So why not use this stuff for everything?