Successfully reported this slideshow.

Functional perl

0

Share

Upcoming SlideShare
PHP 101
PHP 101
Loading in …3
×
1 of 59
1 of 59

Functional perl

0

Share

Download to read offline

Description

Unpolished talk on functional concepts in perl.

Transcript

  1. 1. Functional Programming Perl Christopher Mckay error@errorific.com
  2. 2. What Am I
  3. 3. What's this talk all about?
  4. 4. History
  5. 5. How
  6. 6. Why not?
  7. 7. But I don't 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. sort F(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 sorting sort {$a cmp $b} Also string sorting sort {$a <=> $b} Sort numerically sort &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.0 1989 - Perl 3.0 1991 - Perl 4.0
  20. 20. grep F(a, a), [a] --------> [a]
  21. 21. map F(a), [a] --------> [a]
  22. 22. 1994 – Perl 5.0
  23. 23. memoize F(a) → b --------------> F(a) → b
  24. 24. List::Util reduce F(a, b) → b, [a] --------------> b
  25. 25. List::MoreUtils any, 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. That's 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 don't like rolling 1's though so...
  34. 34. my $cheat = sub { my ($roll) = @_; if ($roll < 2) { return 2; } else { return $roll; } }
  35. 35. I don't 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 Perl Masahiro 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 isn't an option, everything changes
  51. 51. The community loves objects
  52. 52. If it was simple in a functional language it probably takes some hacking to make it work in perl
  53. 53. And the language isn't extensible, yet
  54. 54. Good things are to come
  55. 55. Perl 6
  56. 56. Static typing Better syntax Blocks and closures List comprehension Automatic parallelisation Lots more
  57. 57. Thanks Questions?

Description

Unpolished talk on functional concepts in perl.

Transcript

  1. 1. Functional Programming Perl Christopher Mckay error@errorific.com
  2. 2. What Am I
  3. 3. What's this talk all about?
  4. 4. History
  5. 5. How
  6. 6. Why not?
  7. 7. But I don't 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. sort F(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 sorting sort {$a cmp $b} Also string sorting sort {$a <=> $b} Sort numerically sort &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.0 1989 - Perl 3.0 1991 - Perl 4.0
  20. 20. grep F(a, a), [a] --------> [a]
  21. 21. map F(a), [a] --------> [a]
  22. 22. 1994 – Perl 5.0
  23. 23. memoize F(a) → b --------------> F(a) → b
  24. 24. List::Util reduce F(a, b) → b, [a] --------------> b
  25. 25. List::MoreUtils any, 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. That's 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 don't like rolling 1's though so...
  34. 34. my $cheat = sub { my ($roll) = @_; if ($roll < 2) { return 2; } else { return $roll; } }
  35. 35. I don't 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 Perl Masahiro 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 isn't an option, everything changes
  51. 51. The community loves objects
  52. 52. If it was simple in a functional language it probably takes some hacking to make it work in perl
  53. 53. And the language isn't extensible, yet
  54. 54. Good things are to come
  55. 55. Perl 6
  56. 56. Static typing Better syntax Blocks and closures List comprehension Automatic parallelisation Lots more
  57. 57. Thanks Questions?

More Related Content

Related Books

Free with a 30 day trial from Scribd

See all

×