Your SlideShare is downloading. ×
0
Functional Pearls 4 (YAPC::EU::2009 remix)
Functional Pearls 4 (YAPC::EU::2009 remix)
Functional Pearls 4 (YAPC::EU::2009 remix)
Functional Pearls 4 (YAPC::EU::2009 remix)
Functional Pearls 4 (YAPC::EU::2009 remix)
Functional Pearls 4 (YAPC::EU::2009 remix)
Functional Pearls 4 (YAPC::EU::2009 remix)
Functional Pearls 4 (YAPC::EU::2009 remix)
Functional Pearls 4 (YAPC::EU::2009 remix)
Functional Pearls 4 (YAPC::EU::2009 remix)
Functional Pearls 4 (YAPC::EU::2009 remix)
Functional Pearls 4 (YAPC::EU::2009 remix)
Functional Pearls 4 (YAPC::EU::2009 remix)
Functional Pearls 4 (YAPC::EU::2009 remix)
Functional Pearls 4 (YAPC::EU::2009 remix)
Functional Pearls 4 (YAPC::EU::2009 remix)
Functional Pearls 4 (YAPC::EU::2009 remix)
Functional Pearls 4 (YAPC::EU::2009 remix)
Functional Pearls 4 (YAPC::EU::2009 remix)
Functional Pearls 4 (YAPC::EU::2009 remix)
Functional Pearls 4 (YAPC::EU::2009 remix)
Functional Pearls 4 (YAPC::EU::2009 remix)
Functional Pearls 4 (YAPC::EU::2009 remix)
Functional Pearls 4 (YAPC::EU::2009 remix)
Functional Pearls 4 (YAPC::EU::2009 remix)
Functional Pearls 4 (YAPC::EU::2009 remix)
Functional Pearls 4 (YAPC::EU::2009 remix)
Functional Pearls 4 (YAPC::EU::2009 remix)
Functional Pearls 4 (YAPC::EU::2009 remix)
Functional Pearls 4 (YAPC::EU::2009 remix)
Functional Pearls 4 (YAPC::EU::2009 remix)
Functional Pearls 4 (YAPC::EU::2009 remix)
Functional Pearls 4 (YAPC::EU::2009 remix)
Functional Pearls 4 (YAPC::EU::2009 remix)
Functional Pearls 4 (YAPC::EU::2009 remix)
Functional Pearls 4 (YAPC::EU::2009 remix)
Functional Pearls 4 (YAPC::EU::2009 remix)
Functional Pearls 4 (YAPC::EU::2009 remix)
Functional Pearls 4 (YAPC::EU::2009 remix)
Functional Pearls 4 (YAPC::EU::2009 remix)
Functional Pearls 4 (YAPC::EU::2009 remix)
Functional Pearls 4 (YAPC::EU::2009 remix)
Functional Pearls 4 (YAPC::EU::2009 remix)
Functional Pearls 4 (YAPC::EU::2009 remix)
Functional Pearls 4 (YAPC::EU::2009 remix)
Functional Pearls 4 (YAPC::EU::2009 remix)
Functional Pearls 4 (YAPC::EU::2009 remix)
Functional Pearls 4 (YAPC::EU::2009 remix)
Functional Pearls 4 (YAPC::EU::2009 remix)
Functional Pearls 4 (YAPC::EU::2009 remix)
Functional Pearls 4 (YAPC::EU::2009 remix)
Functional Pearls 4 (YAPC::EU::2009 remix)
Functional Pearls 4 (YAPC::EU::2009 remix)
Functional Pearls 4 (YAPC::EU::2009 remix)
Functional Pearls 4 (YAPC::EU::2009 remix)
Functional Pearls 4 (YAPC::EU::2009 remix)
Functional Pearls 4 (YAPC::EU::2009 remix)
Functional Pearls 4 (YAPC::EU::2009 remix)
Functional Pearls 4 (YAPC::EU::2009 remix)
Functional Pearls 4 (YAPC::EU::2009 remix)
Functional Pearls 4 (YAPC::EU::2009 remix)
Functional Pearls 4 (YAPC::EU::2009 remix)
Functional Pearls 4 (YAPC::EU::2009 remix)
Functional Pearls 4 (YAPC::EU::2009 remix)
Functional Pearls 4 (YAPC::EU::2009 remix)
Functional Pearls 4 (YAPC::EU::2009 remix)
Functional Pearls 4 (YAPC::EU::2009 remix)
Functional Pearls 4 (YAPC::EU::2009 remix)
Functional Pearls 4 (YAPC::EU::2009 remix)
Functional Pearls 4 (YAPC::EU::2009 remix)
Functional Pearls 4 (YAPC::EU::2009 remix)
Functional Pearls 4 (YAPC::EU::2009 remix)
Functional Pearls 4 (YAPC::EU::2009 remix)
Functional Pearls 4 (YAPC::EU::2009 remix)
Functional Pearls 4 (YAPC::EU::2009 remix)
Functional Pearls 4 (YAPC::EU::2009 remix)
Functional Pearls 4 (YAPC::EU::2009 remix)
Functional Pearls 4 (YAPC::EU::2009 remix)
Functional Pearls 4 (YAPC::EU::2009 remix)
Functional Pearls 4 (YAPC::EU::2009 remix)
Functional Pearls 4 (YAPC::EU::2009 remix)
Functional Pearls 4 (YAPC::EU::2009 remix)
Functional Pearls 4 (YAPC::EU::2009 remix)
Functional Pearls 4 (YAPC::EU::2009 remix)
Functional Pearls 4 (YAPC::EU::2009 remix)
Functional Pearls 4 (YAPC::EU::2009 remix)
Functional Pearls 4 (YAPC::EU::2009 remix)
Functional Pearls 4 (YAPC::EU::2009 remix)
Functional Pearls 4 (YAPC::EU::2009 remix)
Functional Pearls 4 (YAPC::EU::2009 remix)
Functional Pearls 4 (YAPC::EU::2009 remix)
Functional Pearls 4 (YAPC::EU::2009 remix)
Functional Pearls 4 (YAPC::EU::2009 remix)
Functional Pearls 4 (YAPC::EU::2009 remix)
Functional Pearls 4 (YAPC::EU::2009 remix)
Functional Pearls 4 (YAPC::EU::2009 remix)
Functional Pearls 4 (YAPC::EU::2009 remix)
Functional Pearls 4 (YAPC::EU::2009 remix)
Functional Pearls 4 (YAPC::EU::2009 remix)
Functional Pearls 4 (YAPC::EU::2009 remix)
Functional Pearls 4 (YAPC::EU::2009 remix)
Functional Pearls 4 (YAPC::EU::2009 remix)
Functional Pearls 4 (YAPC::EU::2009 remix)
Functional Pearls 4 (YAPC::EU::2009 remix)
Functional Pearls 4 (YAPC::EU::2009 remix)
Functional Pearls 4 (YAPC::EU::2009 remix)
Functional Pearls 4 (YAPC::EU::2009 remix)
Functional Pearls 4 (YAPC::EU::2009 remix)
Functional Pearls 4 (YAPC::EU::2009 remix)
Functional Pearls 4 (YAPC::EU::2009 remix)
Functional Pearls 4 (YAPC::EU::2009 remix)
Functional Pearls 4 (YAPC::EU::2009 remix)
Functional Pearls 4 (YAPC::EU::2009 remix)
Functional Pearls 4 (YAPC::EU::2009 remix)
Functional Pearls 4 (YAPC::EU::2009 remix)
Functional Pearls 4 (YAPC::EU::2009 remix)
Functional Pearls 4 (YAPC::EU::2009 remix)
Functional Pearls 4 (YAPC::EU::2009 remix)
Functional Pearls 4 (YAPC::EU::2009 remix)
Functional Pearls 4 (YAPC::EU::2009 remix)
Functional Pearls 4 (YAPC::EU::2009 remix)
Functional Pearls 4 (YAPC::EU::2009 remix)
Functional Pearls 4 (YAPC::EU::2009 remix)
Functional Pearls 4 (YAPC::EU::2009 remix)
Functional Pearls 4 (YAPC::EU::2009 remix)
Functional Pearls 4 (YAPC::EU::2009 remix)
Functional Pearls 4 (YAPC::EU::2009 remix)
Functional Pearls 4 (YAPC::EU::2009 remix)
Functional Pearls 4 (YAPC::EU::2009 remix)
Functional Pearls 4 (YAPC::EU::2009 remix)
Functional Pearls 4 (YAPC::EU::2009 remix)
Functional Pearls 4 (YAPC::EU::2009 remix)
Functional Pearls 4 (YAPC::EU::2009 remix)
Functional Pearls 4 (YAPC::EU::2009 remix)
Functional Pearls 4 (YAPC::EU::2009 remix)
Functional Pearls 4 (YAPC::EU::2009 remix)
Functional Pearls 4 (YAPC::EU::2009 remix)
Functional Pearls 4 (YAPC::EU::2009 remix)
Functional Pearls 4 (YAPC::EU::2009 remix)
Functional Pearls 4 (YAPC::EU::2009 remix)
Functional Pearls 4 (YAPC::EU::2009 remix)
Functional Pearls 4 (YAPC::EU::2009 remix)
Functional Pearls 4 (YAPC::EU::2009 remix)
Functional Pearls 4 (YAPC::EU::2009 remix)
Functional Pearls 4 (YAPC::EU::2009 remix)
Functional Pearls 4 (YAPC::EU::2009 remix)
Functional Pearls 4 (YAPC::EU::2009 remix)
Functional Pearls 4 (YAPC::EU::2009 remix)
Functional Pearls 4 (YAPC::EU::2009 remix)
Functional Pearls 4 (YAPC::EU::2009 remix)
Functional Pearls 4 (YAPC::EU::2009 remix)
Functional Pearls 4 (YAPC::EU::2009 remix)
Functional Pearls 4 (YAPC::EU::2009 remix)
Functional Pearls 4 (YAPC::EU::2009 remix)
Functional Pearls 4 (YAPC::EU::2009 remix)
Functional Pearls 4 (YAPC::EU::2009 remix)
Functional Pearls 4 (YAPC::EU::2009 remix)
Functional Pearls 4 (YAPC::EU::2009 remix)
Functional Pearls 4 (YAPC::EU::2009 remix)
Functional Pearls 4 (YAPC::EU::2009 remix)
Functional Pearls 4 (YAPC::EU::2009 remix)
Functional Pearls 4 (YAPC::EU::2009 remix)
Functional Pearls 4 (YAPC::EU::2009 remix)
Functional Pearls 4 (YAPC::EU::2009 remix)
Functional Pearls 4 (YAPC::EU::2009 remix)
Functional Pearls 4 (YAPC::EU::2009 remix)
Functional Pearls 4 (YAPC::EU::2009 remix)
Functional Pearls 4 (YAPC::EU::2009 remix)
Functional Pearls 4 (YAPC::EU::2009 remix)
Functional Pearls 4 (YAPC::EU::2009 remix)
Functional Pearls 4 (YAPC::EU::2009 remix)
Functional Pearls 4 (YAPC::EU::2009 remix)
Functional Pearls 4 (YAPC::EU::2009 remix)
Functional Pearls 4 (YAPC::EU::2009 remix)
Functional Pearls 4 (YAPC::EU::2009 remix)
Functional Pearls 4 (YAPC::EU::2009 remix)
Functional Pearls 4 (YAPC::EU::2009 remix)
Functional Pearls 4 (YAPC::EU::2009 remix)
Functional Pearls 4 (YAPC::EU::2009 remix)
Functional Pearls 4 (YAPC::EU::2009 remix)
Functional Pearls 4 (YAPC::EU::2009 remix)
Functional Pearls 4 (YAPC::EU::2009 remix)
Functional Pearls 4 (YAPC::EU::2009 remix)
Functional Pearls 4 (YAPC::EU::2009 remix)
Upcoming SlideShare
Loading in...5
×

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.
Text the download link to your phone
Standard text messaging rates apply

Functional Pearls 4 (YAPC::EU::2009 remix)

2,067

Published on

The YAPC::EU::2009 (Lisbon) remix of my Functional Pe(a)rls talk.

The YAPC::EU::2009 (Lisbon) remix of my Functional Pe(a)rls talk.

Published in: Technology, Business
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
2,067
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
25
Comments
0
Likes
1
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. osfameron Functional Pe(a)rls
  • 2. Functional pe(a)rls World tour! IPW 2008 LPW 2008 NWE.pm May 2009 YAPC::EU Lisbon 2009 version 4 Osfameron
  • 3. Functional Programming What is it?
  • 4. Functional Programming About functions?
  • 5. Functional Programming About functions? (like Object-oriented is about objects?)
  • 6. Functional Programming everything's a function!
  • 7. Functional Programming 1 + 2
  • 8. Functional Programming 1 + 2 Spot the function?
  • 9. Functional Programming 1 + 2 Spot the function?
  • 10. Functional Programming 1 + 2 first class?
  • 11. Functional Programming 1 + 2 +
  • 12. Functional Programming 1 + 2 op<+>
  • 13. OPERATORS UNITE! STOP this discrimation now!
  • 14. What now?
  • 17. What now?
    • Steal from Haskell!
    • 18. (+) ← ref to add
  • 19. Wrapping operators
    • sub op ($op) { return sub ($left, $right) { eval “$left $op $right” ; }; }
  • 20. Wrapping operators
    • my $add = op('+');
    • 21. $add->(1, 2); # 3
  • 22. YAY!
  • 23. YAY?
    • Still uglier than Haskell
  • 25. Devel::Declare
    • New syntax!
    • 26. (But better than source filters)
  • 27. Devel::Declare
    • New syntax!
    • 28. (But better than source filters)
      • Method declaration
      • 29. MooseX::Declare
      • 30. Sub::Auto
  • 31. Sub::Section
    • on github
    • 32. Gives nice syntax, using Devel::Declare
    • 33. op (+)
  • 34. Sub::Section
    • on github
    • 35. Gives nice syntax, using Devel::Declare
    • 36. op (Bwahaha!) (+)
    Devel::Declare custom parser hook can inject code etc.
  • 37. Sub::Section
    • on github
    • 38. Gives nice syntax, using Devel::Declare
    • 39. op (Bwahaha!) ('+')
  • 40. Sub::Section
    • on github
    • 41. Gives nice syntax, using Devel::Declare
    • 42. Op ('+') # Perl is none the wiser
    Tee hee!
  • 43. Devel::Declare
    • In Perl!
    • 44. (With a bit of scary XS magic)
      • hooks into the compiler
      • 45. changing the source as you compile
      • 46. horrible perl tricks to get methods installed, and braces closed
      • 47. mst++, rafl++
  • 48. Sections
  • 49. Sections
    • (+) isn't really a section
  • 50. Sections
    • (+) isn't really a section
    • 51. (+1) is
  • 52. Sections
    • (+) 10, 5 # 15
    • 53. (+1) 8 # 9
  • 54. Currying
    • Partial application
  • 55. Currying
    • Partial application
    • 56. 1 arg at a time
  • 57. Currying
    • sub add ($left, $right) { return $left + $right; }
  • 58. Currying
    • sub add ($left, $right) { return $left + $right; }
    • 59. (That's 2 arguments)
  • 60. Curried functions
    • sub add ($left, $right) { return $left + $right; }
    • 61. (That's 2 arguments)
    • 62. add(5) ...
  • 63. Curried functions
    • sub add ($left, $right) { return $left + $right; }
    • 64. (That's 2 arguments)
    • 65. add(5) # $left is bound to 5
  • 66. Curried functions
    • sub add ($left, $right) { return $left + $right; }
    • 67. (That's 2 arguments)
    • 68. add(5) # $left is bound to 5 ->(6); # 11
  • 69. Implement in Perl
    • Quite simple to do with closures:
    • 70. sub add { my $left = shift; return sub { my $right = shift; return $left + $right; } }
  • 71. Implement in Perl
    • Quite simple to do with closures:
    • 72. sub add { my $left = shift; return sub { my $right = shift; return $left + $right; } }
    • 73. Not pretty or convenient though
  • 74. Sub::Curried
    • on CPAN
    • 75. Gives nice syntax, using Devel::Declare
    • 76. sub add ($left, $right) { return $left + $right; }
  • 77. Sub::Curried
    • on CPAN
    • 78. Gives nice syntax, using Devel::Declare
    • 79. curry add ($left, $right) { return $left + $right; }
  • 80. Sub::Curried
    • on CPAN
    • 81. Gives nice syntax, using Devel::Declare
    • 82. curry (bwahaha!) add ($left, $right) { return $left + $right; }
  • 83. Sub::Curried
    • Turn into something like
    • 84. curry add { return ROUTINE unless @_; check_args(2, @_); my $f = sub { my $f = sub { ... } $f = $f->(shift) for @_; return $f; }
  • 85. Sub::Curried
    • Turn into something like
    • 86. curry add { return ROUTINE unless @_ ; check_args(2, @_); my $f = sub { my $f = sub { ... } $f = $f->(shift) for @_; return $f; }
  • 87. Sub::Curried
    • add(5,6); # 11
  • 88. Sub::Curried
    • add(5,6); # 11
    • 89. add(5); # function that adds 5
  • 90. Sub::Curried
    • add(5,6); # 11
    • 91. add(5); # function that adds 5
    • 92. add(); # function that adds...
  • 93. Sub::Curried
    • add(5,6); # 11
    • 94. add(5); # function that adds 5
    • 95. add(); # function that adds...
    • 96. i.e = &add
  • 97. Sub::Curried
    • Turn into something like
    • 98. curry add { return ROUTINE unless @_; check_args(2 , @_); my $f = sub { my $f = sub { ... } $f = $f->(shift) for @_; return $f; }
  • 99. Sub::Curried
    • Give diagnostics if called with too many arguments
  • 100. Sub::Curried
    • Turn into something like
    • 101. curry add { return ROUTINE unless @_; check_args(2, @_); my $f = sub { my $f = sub { ... } $f = $f->(shift) for @_; return $f; }
  • 102. Sub::Curried
    • Handle
  • 104. Why?
    • Partial application
  • 105. Why?
    • Automatic partial application everywhere
  • 106. Why?
    • Automatic partial application everywhere
    • 107. ... turns out to be useful/elegant in Haskell
  • 108. Why?
    • Automatic partial application everywhere
    • 109. ... turns out to be useful/elegant in Haskell
    • 110. (actually, similar techniques are used in Perl)
  • 111. Currying the Invocant
    • package My::Class use base 'Class::Accessor'; __PACKAGE__->add_accessor('foo'); __PACKAGE__->add_accessor('bar'); __PACKAGE__->add_accessor('baz');
  • 112. Currying the Invocant
    • package My::Class use base 'Class::Accessor'; My::Class ->add_accessor('foo'); My::Class ->add_accessor('bar'); My::Class ->add_accessor('baz');
  • 113. Currying the Invocant
    • package My::Class use base 'Class::Accessor'; My::Class ->add_accessor('foo'); My::Class ->add_accessor('bar'); My::Class ->add_accessor('baz');
    • 114. sub add_accessor { my ( $self , $accessor) = @_; ....
  • 115. Currying the Invocant
    • Perl importing
      • *{$CALLER::has} = &has;
  • 116. Currying the Invocant
    • Perl importing
      • *{$CALLER::has} = &has;
      • 117. *{$CALLER::has} = mk_has();
  • 118. Currying the Invocant
    • Perl importing
      • *{$CALLER::has} = &has;
      • 119. *{$CALLER::has} = mk_has();
      • 120. *{$CALLER::has} = has($CALLER);
  • 121. Currying the Invocant
    • Moose!
      • package My::Class; use Moose; # imports has=has('My::Class') has 'foo'; has 'bar'; has 'baz';
  • 122. Currying the Invocant
    • Moose!
      • package My::Class; use Moose; # imports has=has('My::Class') has 'foo'; has 'bar'; has 'baz';
    • (handwave)
  • 123. Where were we?
    • my $add = +; # FAIL
  • 124. Where were we?
    • my $add = +; # FAIL
    • 125. my $add = op(+); # YAY
  • 126. Where were we?
    • my $add = +; # FAIL
    • 127. my $add = op(+); # YAY
    • 128. my $add2 = op(+2); # take THAT, Haskell!
  • 129. But...
    • (-1)
  • 130. But...
  • 132. But...
  • 134. But...
    • (-1) # minus(???)->(1)
    • 135. (1-) # minus(1)
  • 136. But...
    • (-1) # (flip(minus))->(1)
    • 137. (1-) # minus(1)
  • 138. Flip
    • curry flip ($fn, $left, $right) { $fn->($right, $left); }
  • 139. Flip
    • curry flip ($fn, $left, $right ) { $fn->($right, $left); }
    • 140. Minus (3, 1) # 2
    • 141. flip(minus)->(3, 1) # -2;
  • 142. Flip
    • … and with currying
    • 143. my $prev = flip(minus)->(1);
    • 144. $prev->( 5 );
      • $left = 1
      • 145. $right = 5
      • 146. minus($right=5, $left=1); # 4
  • 147. Sub::Section
    • More cool stuff
  • 148. Sub::Section
    • my $greet = op(“Hello ” .);
  • 149. Sub::Section
    • my $greet = op(“Hello ” .);
    • 150. $greet->(“World”); # “Hello World”
  • 151. Sub::Section
    • map op(*2), @numbers;
  • 153. Sub::Section
    • map op(*2), @numbers;
  • 155. Sub::Section
    • Perl map doesn't take functions...
  • 156. Sub::Section
    • Perl map doesn't take functions...
    • 157. map op(*2) ->($_) , @numbers;
  • 159. (rant)
    • Perl's map is not functional
  • 160. (rant)
    • Perl's map is not functional
      • We took a feature from FP... and made it not take functions.
  • 161. (rant)
    • Perl's map is not functional
    • 162. Perl's $_ is a horrible hack around not doing currying properly
  • 163. (rant)
    • Perl's map is not functional
    • 164. Perl's $_ is a horrible hack around not doing currying properly
    • 165. Why am I still programming this silly language? ;-)
  • 166. (yay!)
    • Perl's map is not functional
    • 167. Perl's $_ is a horrible hack around not doing currying properly
    • 168. Why am I still programming this silly language? ;-)
    • 169. Oh yes! - because I can change it.
  • 170. TODO: Functional::Map
    • fmap &some_func, @list;
    • 171. fmap sub { … }, @list;
    • 172. fmap op(+2), @list;
  • 173. TODO: Functional::Map
    • fmap &some_func, @list;
    • 174. fmap sub { … }, @list;
    • 175. fmap op(+2), @list;
    • 176. (those will all work anyway)
  • 177. TODO: Functional::Map
    • fmap &some_func, @list;
    • 178. fmap sub { … }, @list;
    • 179. fmap op(+2), @list;
    • 180. (those will all work anyway)
    • 181. fmap { uc } @list;
  • 182. TODO: Functional::Map
    • fmap &some_func, @list;
    • 183. fmap sub { … }, @list;
    • 184. fmap op(+2), @list;
    • 185. (those will all work anyway)
    • 186. fmap (bwahahaha!) { uc } @list;
  • 187. TODO: Functional::Map
    • fmap &some_func, @list;
    • 188. fmap sub { … }, @list;
    • 189. fmap op(+2), @list;
    • 190. (those will all work anyway)
    • 191. fmap sub { uc } , @list;
  • 192. TODO: Functional::Map
    • fmap &some_func, @list;
    • 193. fmap sub { … }, @list;
    • 194. fmap op(+2), @list;
    • 195. (those will all work anyway)
    • 196. fmap underscorify( sub { uc } ) , @list;
  • 197. TODO: Functional::Map
    • Dear lazyYAPC...
  • 198. Sub::Section
    • my $contains_foo = op(=~/foo/);
  • 199. More complex examples
    • centigrade = (fahrenheit-32)*5/9
  • 200. More complex examples
    • centigrade = (fahrenheit-32)*5/9
    • 201. my $f2c = sub { op(*5/9)->( op(-32)->(shift)) };
  • 202. More complex examples
    • centigrade = (fahrenheit-32)*5/9
    • 203. my $f2c = sub { op(*5/9)->( op(-32)->(shift)) };
    • 204. MY EYES!
  • 205. More complex examples
    • centigrade = (fahrenheit-32)*5/9
    • 206. my $f2c = sub { op(*5/9) ->( op(-32) ->(shift)) };
  • 207. More complex examples
    • centigrade = (fahrenheit-32)*5/9
    • 208. my $f2c = sub { op(*5/9) ->( op(-32) ->(shift)) };
    • 209. Never mind the boilerplate...
  • 210. More complex examples
    • centigrade = (fahrenheit-32)*5/9
    • 211. my $f2c = op(*5/9) << op(-32)
    • 212. Composition
  • 213. More complex examples
    • centigrade = (fahrenheit-32)*5/9
    • 214. my $f2c = op(*5/9) << op(-32)
    • 215. Sub::Compose
  • 216. More complex examples
    • centigrade = (fahrenheit-32)*5/9
    • 217. my $f2c = op(*5/9) << op(-32)
    • 218. Sub::Compose
      • base class for Sub::Curried, Sub::Section
  • 219. More complex examples
    • centigrade = (fahrenheit-32)*5/9
    • 220. my $f2c = op(*5/9) << op(-32)
    • 221. Sub::Compose
      • base class for Sub::Curried, Sub::Section
      • 222. ... autobox?
  • 223. Point-free!
    • Or point less programming
    • 224. (obfu for maths types)
  • 225. Point-free!
    • my $square = op(*) << dup;
    • 226. $square->(4); # 16
  • 227. Point-free!
    • curry dup ($arg) { ($arg)x 2 }
    • 228. my $square = op(*) << dup;
    • 229. $square->(4); # 16
  • 230. Point-free!
    • curry dup ($arg) { ($arg)x 2 }
    • 231. my $dup = op(x 2); # ?
  • 232. Point-free!
    • curry dup ($arg) { ($arg)x 2 }
    • 233. my $dup = op(x 2); “$arg$arg”
  • 234. Point-free!
    • curry dup ($arg) { ($arg)x 2 }
    • 235. my $dup = op(x 2); “$arg$arg”
      • TODO: alternate syntax for list context:
      • 236. op[x 2] # thanks mst
  • 237. Some light relief
  • 238. Some light relief Monads
  • 239. Monads
    • Come from maths
    • 240. “Category theory”
  • 241. Monads
    • Come from maths
    • 242. “Category theory”
    • 243. Very clever people rave about them being useful
  • 244. Monads
    • Come from maths
    • 245. “Category theory”
    • 246. Very clever people rave about them being useful
    • 247. Have a reputation for being hard to understand
  • 248. Monad tutorials
  • 249. Monad tutorials
  • 250. Monad tutorials
  • 251. Monad tutorials
  • 252. Monad tutorials Step 1: Write Monad Tutorial
  • 253. Monad tutorials Step 2: ???
  • 254. Monad tutorials Step 3: Profit!
  • 255. Monads
    • You already use monads
  • 256. Monads
    • You already use monads
    • 257. YAY!
  • 258. Monads
    • You already use monads
    • 259. Sequences of commands?
  • 260. Sequencing my $x = 1; my $y = 2; my $z = $x * $y; say “$x * $y = $z”;
  • 261. Sequencing my $x = 1; my $y = 2; my $z = $x * $y; say “$x * $y = $z”;
  • 262. Sequencing my $x = 1; my $y = 2; my $z = $x * $y; say “$x * $y = $z”;
  • 263. Sequencing my @seq = sub { my $x = 1 }, sub { my $y = 2 }, sub { my $z = $x * $y }, sub { say &quot;$x * $y = $z&quot; };
  • 264. Sequencing my @seq = sub { my $x = 1 }, sub { my $y = 2 }, sub { my $z = $x * $y }, sub { say &quot;$x * $y = $z&quot; }; # Global symbol &quot;$x&quot; requires explicit package name at ...
  • 265. Nesting my $x = 1; my $y = 2; my $z = $x * $y; say “$x * $y = $z”;
  • 266. Nesting sub { my $x = 1; sub { my $y = 2; sub { my $z = $x * $y; sub { say &quot;$x * $y = $z&quot;; }->() }->() }->() }->();
  • 267. Monadic programming is impractical in Perl... only because of syntactic issues – Mark Jason Dominus http://perl.plover.com/classes/fp/samples/slide027.html
  • 268. Monads made pretty
    • Source filters!
      • http://sleepingsquirrel.org/monads/monads.html
  • 269. Monads made pretty
    • Source filters!
      • http://sleepingsquirrel.org/monads/monads.html
    • Source tree manipulation (B::OP magic)
    • 270. Deparse and source text munging
  • 271. Monads made pretty
    • We want a syntax like
    • 272. mdo { my $x = mbind(1); my $y = mbind(2); my $z = mbind($x + $y); say “$x * $y = $z”; }
  • 273. Monads made pretty
    • We want a syntax like
    • 274. mdo { my $x = mbind(1); my $y = mbind(2); my $z = mbind($x + $y); say “$x * $y = $z”; }
    • 275. mdo introduces the block
    • 276. mbind gives us a hook to rotate around
  • 277. Optree munging
    • 19: my $x << Just 2; ... n <;> nextstate(main 2078 b.pl:19) v:*,&,$ ->o t <2> left_shift[t3] vK ->u o <0> padsv[$x:2078,2080] sM/LVINTRO ->p s <1> entersub[t2] sKS/TARG,3 ->t - <1> ex-list sK ->s p <0> pushmark s ->q q <$> const(IV 2) sM ->r - <1> ex-rv2cv sK/130 ->- r <$> gv(*Just) s ->s u <;> nextstate(main 2079 b.pl:20) v:*,&,$ ->v
    • # : mbind (Just 2), sub { my $x = shift; ... }; <;> nextstate(main b.pl:) v:*,&,{,$ -> <@> list K -> <0> pushmark s -> <1> entersub[t2] KS/TARG,3 -> - <1> ex-list K -> <0> pushmark s -> <1> entersub[t1] lKMS/NO(),TARG,INARGS,3 -> - <1> ex-list lK -> <0> pushmark s -> <$> const(IV 2) sM -> - <1> ex-rv2cv sK/130 ->- <$> gv(*Just) s -> - <1> ex-rv2cv sK/2 ->- # mbind instead of >> <$> gv(*mbind) s -> <1> refgen K/1 -> - <1> ex-list lKRM -> <0> pushmark sRM -> <$> anoncode[CV ] lRM -> # ??? set up anon sub # my $x = shift <0> padsv[$x:2078,2080] sM/LVINTRO ->p # the next ; is moved into this new lambda! <;> nextstate(main 2079 b.pl:20) v:*,&,$ ->v
  • 278. Optree munging
    • 19: my $x << Just 2; ... n <;> nextstate(main 2078 b.pl:19) v:*,&,$ ->o t <2> left_shift[t3] vK ->u o <0> padsv[$x:2078,2080] sM/LVINTRO ->p s <1> entersub[t2] sKS/TARG,3 ->t - <1> ex-list sK ->s p <0> pushmark s ->q q <$> const(IV 2) sM ->r - <1> ex-rv2cv sK/130 ->- r <$> gv(*Just) s ->s u <;> nextstate(main 2079 b.pl:20) v:*,&,$ ->v
    • # : mbind (Just 2), sub { my $x = shift; ... }; <;> nextstate(main b.pl:) v:*,&,{,$ -> <@> list K -> <0> pushmark s -> <1> entersub[t2] KS/TARG,3 -> - <1> ex-list K -> <0> pushmark s -> <1> entersub[t1] lKMS/NO(),TARG,INARGS,3 -> - <1> ex-list lK -> <0> pushmark s -> <$> const(IV 2) sM -> - <1> ex-rv2cv sK/130 ->- <$> gv(*Just) s -> - <1> ex-rv2cv sK/2 ->- # mbind instead of >> <$> gv(*mbind) s -> <1> refgen K/1 -> - <1> ex-list lKRM -> <0> pushmark sRM -> <$> anoncode[CV ] lRM -> # ??? set up anon sub # my $x = shift <0> padsv[$x:2078,2080] sM/LVINTRO ->p # the next ; is moved into this new lambda! <;> nextstate(main 2079 b.pl:20) v:*,&,$ ->v
    KABOOM
  • 279. Source deparsing
    • Works surprisingly well
  • 280. Source deparsing
    • Works surprisingly well
    • 281. for trivial cases
    • 282. (a bit fragile)
  • 283. Source deparsing
    • Works surprisingly well
    • 284. for trivial cases
    • 285. (a bit fragile)
    • 286. though localised to mdo { ... }
  • 287. Devel::Declare
    • Even nicer syntax
    • 288. mdo { mbind $x = 1; mbind $y = 2; mbind $z = $x + $y; say “$x * $y = $z”; }
  • 289. Devel::Declare
    • And cuter implementation:
    • 290. mdo { mbind $x = 1; mbind $y = 2; mbind $z = $x + $y; say “$x * $y = $z”; }
  • 291. Devel::Declare
    • The problem:
    • 292. mdo { mbind 1, sub { my $x = shift; mbind 2, sub { my $y = shift; mbind $x + $y, sub { my $z = shift; say “$x * $y = $z”; } ...
  • 293. Devel::Declare
    • No need to count nesting:
    • 294. scope_inject
    • 295. B::Hooks::EndOfScope
    • 296. Use to inject a ; at the end of method declarations:
    • 297. method foo ($x) { print $x; } # look Ma, no semicolon!
  • 298. Devel::Declare
    • mbind's scope_inject adds a “}”
    • 299. mdo { mbind 1, sub { my $x = shift; mbind 2, sub { my $y = shift; mbind $x + $y, sub { my $z = shift; say “$x * $y = $z”; } # adds a closing brace }
  • 300. Devel::Declare
    • mbind's scope_inject adds a “}”
    • 301. mdo { mbind 1, sub { my $x = shift; mbind 2, sub { my $y = shift; mbind $x + $y, sub { my $z = shift; say “$x * $y = $z”; } } # adds a closing brace }
  • 302. Devel::Declare
    • mbind's scope_inject adds a “}”
    • 303. mdo { mbind 1, sub { my $x = shift; mbind 2, sub { my $y = shift; mbind $x + $y, sub { my $z = shift; say “$x * $y = $z”; } } } # adds a closing brace }
  • 304. Devel::Declare
    • mbind's scope_inject adds a “}”
    • 305. mdo { mbind 1, sub { my $x = shift; mbind 2, sub { my $y = shift; mbind $x + $y, sub { my $z = shift; say “$x * $y = $z”; } } } } # closes block
  • 306. So...
    • We can now sequence commands!
    • 307. mdo { mbind $x = 1; mbind $y = 2; mbind $z = $x + $y; say “$x * $y = $z”; }
  • 308. So...
    • We can now sequence commands!
    • 309. in pure Perl!
  • 310. So...
    • OK, so this was big news in Haskell in 1990s
  • 311. So...
    • OK, so this was big news in Haskell in 1990s
    • 312. Imperative languages have always done this
  • 313. What else can monads do ?
    • Sequencing
    • 314. mdo { mbind $x = 1; mbind $y = 2; mbind $z = $x + $y; say “$x * $y = $z”; }
  • 315. What else can monads do ?
    • Sequencing
    • 316. mdo { mbind $x = 1; mbind $y = 2; mbind $z = $x + $y; say “$x * $y = $z”; }
  • 317. What else can monads do ?
    • Sequencing
    • 318. mdo { mbind $x = 1 ; mbind $y = 2 ; mbind $z = $x + $y ; say “$x * $y = $z”; }
  • 319. What else can monads do ?
    • Sequencing
    • 320. mdo { mbind $x = 1 ; mbind $y = 2 ; mbind $z = $x + $y ; say “$x * $y = $z”; }
    • 321. Programmable semicolon!
  • 322. Maybe
    • Success/Failure
    • 323. mdo (Maybe) { mbind $FH = m_open('<', $file) ; mbind $line = <$FH> ; mbind $val = lookup(h, $line) ; say “Found $val!”; }
  • 324. Maybe
    • Success/Failure
    • 325. mdo (Maybe) { mbind $FH = m_open('<', $file) ; mbind $line = <$FH> ; mbind $val = lookup(h, $line) ; say “Found $val!”; }
    • 326. Will give up if can't open file, read a line from it, or look it up in a hash
  • 327. Maybe
    • Success/Failure
    • 328. mdo (Maybe) { mbind $FH = m_open('<', $file) ; mbind $line = <$FH> ; mbind $val = lookup(h, $line) ; say “Found $val!”; }
    • 329. Compare chain of if (foo) { if (bar) { ...
    • 330. or eval { ... }
  • 331. Maybe $FH = open($file) $line = <$FH> $val = lookup ( $line ) say “Found $val”
  • 332. Maybe $FH = open($file) $line = <$FH> $val = lookup ( $line ) say “Found $val” ?
  • 333. Sequence
  • 334. Maybe ? ? ?
  • 335. Multiple (List)
  • 336. List $x = [1..10] $y = [1..10] guard $x+$y == 10 say “$x+$y=10”
  • 337. List
    • Cartesian product
    • 338. mdo { mbind $x = [1..10] ; mbind $y = [1..10] ; mbind guard $x+$y == 10 ; say “$x+$y=10”; }
    • 339. Run every $x against every $y and filter
  • 340. List
    • Cartesian product
    • 341. mdo { mbind $x = [1..10] ; mbind $y = [1..10] ; mbind guard $x+$y == 10 ; say “$x+$y=10”; }
    • 342. 1+9=10
    • 343. 2+8=10
    • 344. ...
  • 345. List
    • Cartesian product
    • 346. mdo { mbind $x = [1..10] ; mbind $y = [1..10] ; mbind guard $x+$y == 10 ; say “$x+$y=10”; }
    • 347. just like SQL
    • 348. or LINQ
  • 349. List comprehension
    • More compact syntax
    • 350. mcomp ($x <- [1..10]; $y <- [1..10]; $x+y==10) { say “$x+$y=10” }
  • 351. List
    • mdo { mbind $x = [1..10] ; mbind $y = [1..10] ; mbind guard $x+$y == 10 ; say “$x+$y=10”; }
    • 352. We're actually calling mbind on a list
  • 353. List
    • mdo { mbind $x = [1..10] ; mbind $y = [1..10] ; mbind guard $x+$y == 10 ; say “$x+$y=10”; }
    • 354. We're actually calling mbind on a list
    • 355. autobox
  • 356. Continuations call_cc (&somefunc, ... ... ... )
  • 357. Continuations somefunc
  • 358. (clonable!) Continuations Somefunc for 1..3 {...}
  • 359. Next steps?
    • Devel::Declare + PPI
      • (parse till end of statement)
  • 360. Next steps?
    • Devel::Declare + PPI
      • (parse till end of statement)
      • 361. $x ← foo(); # monadic
      • 362. bar(); # also monadic!
      • 363. my $x = 12; # “normal” value
      • 364. (e.g. no more 'mbind' keyword)
  • 365. Perl++
    • Not really a functional language...
    • 366. But you can take it surprisingly far
    • 367. (CPAN++)
  • 368. What else can I do with monads?
  • 371. Wants monads now!
    • Techniques are valuable now
    • 372. Nicely wrapped implementation will be ready soon....
  • 373. Wants monads now!
    • Techniques are valuable now
    • 374. Nicely wrapped implementation will be ready soon....
    • 375. Github: Acme::Monads – patches welcome
  • 376. Thanks!
    • questions?
  • 377. Thanks!
    • osfameron,
      • #london.pm, #perl.it, #northwestengland.pm
      • 378. CPAN
      • 379. github
    • Images
      • Red pepper section, by docman http://www.flickr.com/photos/docman/107252072/ (cc-by-nc)

×