Functional perl

Embed Size (px)

DESCRIPTION

Unpolished talk on functional concepts in perl.

Citation preview

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 functionLarry was generous 13. sortF(a, a), [a] --------> [a] 14. @sorted_list= sort {$_[0] $_[1]} @list 15. @sorted_list= sort {$a $b } @list 16. @sorted_list = sort {$a $b} @list 17. sort String sortingsort {$a cmp $b} Also string sortingsort {$a $b} Sort numericallysort &comparator Sort by function named comparator 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. 1988 - Perl 2.01989 - Perl 3.01991 - Perl 4.0 20. grepF(a, a), [a] --------> [a] 21. mapF(a), [a] --------> [a] 22. 1994 Perl 5.0 23. memoizeF(a) b --------------> F(a) b 24. List::UtilreduceF(a, b) b, [a] --------------> 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 inperl instead of just using it? 28. Lexical scoping 29. First class functions 30. Thats about it.Your language does that too right? 31. So lets write a function that rolls a die 32. my $dx = sub {my ($sides) = @_;return (rand() * $sides) + 1;}my $roll = $dx->(6); 33. I dont like rolling 1s though so... 34. my $cheat = sub {my ($roll) = @_;if ($roll < 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->($dx->($sides));} 37. my $compose = sub {my ($fa, $fb) = @_;return sub { return $fa->($fb->(@_));}} 38. my $inconspicuous_dx =$compose->($cheat, $dx);my $better_roll =$inconspicuous_dx->(6); 39. Monads in PerlMasahiro Honma (hiratara) YAPC::Asia 2011Slides are in english Data::Monad 40. All I ever need is d6! 41. my $d6 = sub { return $inconspicuous_dx->(6);};my $roll_a_6 = $d6->(); 42. Too lazy to roll 20d6 43. my $dice_tower_maker = sub {my ($die) = @_;return sub { my ($rolls) = @_; return map {$die->() } (1 .. $rolls);}} 44. my $dice_tower =$dice_tower_generator->($d6);say join(, , $dice_tower->(20)); 45. use List::Util;say sum $dice_tower->(20); 46. use List::Util;say reduce {$a + $b}$dice_tower->(20); 47. So why not use this stuff for everything? 48. The compilers not out to help you 49. Declaring functions gets obtuseafter awhile 50. Purity isnt an option, everythingchanges 51. The community loves objects 52. If it was simple in a functionallanguage it probably takes some hacking to make it work in perl 53. And the language isnt extensible,yet 54. Good things are to come 55. Perl 6 56. Static typingBetter syntaxBlocks and closuresList comprehensionAutomatic parallelisationLots more 57. ThanksQuestions?