osfameron Functional Pe(a)rls
Functional pe(a)rls World tour! IPW 2008 LPW 2008 NWE.pm May 2009 YAPC::EU Lisbon 2009 version 4 Osfameron
Functional Programming What is it?
Functional Programming About functions?
Functional Programming About  functions? (like Object-oriented is about objects?)
Functional Programming everything's a function!
Functional Programming 1 + 2
Functional Programming 1 + 2 Spot the  function?
Functional Programming 1 + 2 Spot the  function?
Functional Programming 1 + 2 first class?
Functional Programming 1 + 2 \+
Functional Programming 1 + 2 op<+>
OPERATORS UNITE! STOP this discrimation now!
What now? Protest!
Cry!
Steal from Haskell!
What now? Steal from Haskell!
(+)  ← ref to add
Wrapping operators sub op ($op) {   return    sub  ($left, $right)  {   eval  “$left $op $right” ;   }; }
Wrapping operators my $add = op('+');
$add->(1, 2); # 3
YAY!
YAY? Still uglier than Haskell (+)
op('+')
Devel::Declare New syntax!
(But better than source filters)
Devel::Declare New syntax!
(But better than source filters) Method declaration
MooseX::Declare
Sub::Auto
Sub::Section on github
Gives nice syntax, using  Devel::Declare
op (+)
Sub::Section on github
Gives nice syntax, using  Devel::Declare
op   (Bwahaha!)  (+) Devel::Declare  custom parser hook can inject code etc.
Sub::Section on github
Gives nice syntax, using  Devel::Declare
op   (Bwahaha!)  ('+')
Sub::Section on github
Gives nice syntax, using  Devel::Declare
Op ('+')  # Perl is none the wiser Tee hee!
Devel::Declare In Perl!
(With a bit of scary XS magic) hooks into the compiler
changing the source as you compile
horrible perl tricks to get methods installed, and braces closed
mst++, rafl++
Sections
Sections (+) isn't really a section
Sections (+) isn't really a section
(+1) is
Sections (+)  10, 5  # 15
(+1) 8  #  9
Currying Partial application
Currying Partial application
1 arg at a time
Currying sub add ($left, $right) { return $left + $right; }
Currying sub add ($left, $right) { return $left + $right; }
(That's 2 arguments)
Curried functions sub add ($left, $right) { return $left + $right; }
(That's 2 arguments)
add(5)  ...
Curried functions sub add ($left, $right) { return $left + $right; }
(That's 2 arguments)
add(5)  # $left is bound to 5
Curried functions sub add ($left, $right) { return $left + $right; }
(That's 2 arguments)
add(5)  # $left is bound to 5  ->(6);  # 11
Implement in Perl Quite simple to do with closures:
sub add { my $left = shift; return sub { my $right = shift; return $left + $right; } }
Implement in Perl Quite simple to do with closures:
sub add { my $left = shift; return sub { my $right = shift; return $left + $right; } }
Not pretty or convenient though
Sub::Curried on CPAN
Gives nice syntax, using  Devel::Declare
sub  add ($left, $right) { return $left + $right; }
Sub::Curried on CPAN
Gives nice syntax, using  Devel::Declare
curry  add ($left, $right) { return $left + $right; }
Sub::Curried on CPAN
Gives nice syntax, using  Devel::Declare
curry   (bwahaha!)   add ($left, $right) { return $left + $right; }
Sub::Curried Turn into something like
curry add { return ROUTINE unless @_; check_args(2, @_);  my $f = sub { my $f = sub { ... } $f = $f->(shift) for @_; return $f; }
Sub::Curried Turn into something like
curry add { return  ROUTINE unless @_ ; check_args(2, @_);  my $f = sub { my $f = sub { ... } $f = $f->(shift) for @_; return $f; }
Sub::Curried add(5,6);  # 11
Sub::Curried add(5,6);  # 11
add(5);  # function that adds 5
Sub::Curried add(5,6);  # 11
add(5);  # function that adds 5
add();  # function that adds...
Sub::Curried add(5,6);  # 11
add(5);  # function that adds 5
add();  # function that adds...
i.e =  \&add
Sub::Curried Turn into something like
curry add { return ROUTINE unless @_; check_args(2 , @_);  my $f = sub { my $f = sub { ... } $f = $f->(shift) for @_; return $f; }
Sub::Curried Give diagnostics if called with too many arguments
Sub::Curried Turn into something like
curry add { return ROUTINE unless @_; check_args(2, @_);  my $f = sub { my $f = sub { ... } $f = $f->(shift) for @_; return $f; }
Sub::Curried Handle add(1,2)
add(1)->(2)
Why? Partial application
Why? Automatic partial application everywhere
Why? Automatic partial application everywhere
... turns out to be useful/elegant in Haskell
Why? Automatic partial application everywhere
... turns out to be useful/elegant in Haskell
(actually, similar techniques  are  used in Perl)
Currying the Invocant package My::Class use base 'Class::Accessor'; __PACKAGE__->add_accessor('foo'); __PACKAGE__->add_accessor('bar'); __PACKAGE__->add_accessor('baz');
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');
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');
sub add_accessor { my ( $self , $accessor) = @_; ....
Currying the Invocant Perl importing *{$CALLER::has} = \&has;
Currying the Invocant Perl importing *{$CALLER::has} = \&has;
*{$CALLER::has} = mk_has();
Currying the Invocant Perl importing *{$CALLER::has} = \&has;
*{$CALLER::has} = mk_has();
*{$CALLER::has} = has($CALLER);
Currying the Invocant Moose! package My::Class; use Moose;  # imports has=has('My::Class') has 'foo'; has 'bar'; has 'baz';
Currying the Invocant Moose! package My::Class; use Moose;  # imports has=has('My::Class') has 'foo'; has 'bar'; has 'baz'; (handwave)
Where were we? my $add = \+;  # FAIL
Where were we? my $add = \+;  # FAIL
my $add = op(+);  # YAY
Where were we? my $add = \+;  # FAIL
my $add = op(+);  # YAY
my $add2 = op(+2);   # take THAT, Haskell!
But... (-1)
But... (-1)
(1-)
But... (-1)
(1-)  # minus(1)
But... (-1)  # minus(???)->(1)
(1-)  # minus(1)
But... (-1)  # (flip(minus))->(1)
(1-)  # minus(1)
Flip curry flip ($fn, $left, $right) { $fn->($right, $left); }
Flip curry flip ($fn,  $left, $right ) { $fn->($right, $left); }
Minus  (3, 1)  # 2
flip(minus)->(3, 1)  # -2;
Flip …  and with currying
my $prev = flip(minus)->(1);
$prev->( 5 ); $left  = 1
$right = 5
minus($right=5, $left=1); # 4
Sub::Section More cool stuff
Sub::Section my $greet = op(“Hello ” .);
Sub::Section my $greet = op(“Hello ” .);
$greet->(“World”); # “Hello World”
Sub::Section map op(*2), @numbers; (1,2,3,4)
> (2,4,6,8)
Sub::Section map op(*2), @numbers; (1,2,3,4)
> (2,4,6,8)
Sub::Section Perl map doesn't take functions...
Sub::Section Perl map doesn't take functions...
map op(*2) ->($_) , @numbers; (1,2,3,4)
> (2,4,6,8)
(rant) Perl's map is not functional
(rant) Perl's map is not functional We took a feature from FP...  and made it not take functions.
(rant) Perl's map is not functional
Perl's $_ is a horrible hack around not doing currying properly
(rant) Perl's map is not functional
Perl's $_ is a horrible hack around not doing currying properly
Why am I still programming this silly language? ;-)
(yay!) Perl's map is not functional
Perl's $_ is a horrible hack around not doing currying properly
Why am I still programming this silly language? ;-)
Oh yes! - because I can change it.
TODO:  Functional::Map fmap \&some_func, @list;
fmap sub { … },  @list;
fmap op(+2),  @list;
TODO:  Functional::Map fmap \&some_func, @list;
fmap sub { … },  @list;
fmap op(+2),  @list;
(those will all work anyway)
TODO:  Functional::Map fmap \&some_func, @list;
fmap sub { … },  @list;
fmap op(+2),  @list;
(those will all work anyway)
fmap { uc } @list;
TODO:  Functional::Map fmap \&some_func, @list;
fmap sub { … },  @list;
fmap op(+2),  @list;

More Related Content

ODP
Advanced Perl Techniques
PDF
Perl 5.10 for People Who Aren't Totally Insane
PDF
Perl6 Regexen: Reduce the line noise in your code.
PDF
Neatly Hashing a Tree: FP tree-fold in Perl5 & Perl6
ODP
Introduction to Perl - Day 1
ODP
Introduction to Perl - Day 2
PPT
ODP
Introduction to Modern Perl
Advanced Perl Techniques
Perl 5.10 for People Who Aren't Totally Insane
Perl6 Regexen: Reduce the line noise in your code.
Neatly Hashing a Tree: FP tree-fold in Perl5 & Perl6
Introduction to Perl - Day 1
Introduction to Perl - Day 2
Introduction to Modern Perl

What's hot (20)

KEY
Elegant APIs
KEY
Introduction to Perl Best Practices
ODP
Programming in perl style
PPT
LPW: Beginners Perl
PDF
優しいWAFの作り方
PDF
Abuse Perl
PDF
Loops and Unicorns - The Future of the Puppet Language - PuppetConf 2013
PDF
How to develop modern web application framework
PDF
Improving Dev Assistant
PPT
Perl 101 - The Basics of Perl Programming
PPTX
P4 2018 io_functions
PPTX
Bioinformatics p1-perl-introduction v2013
PPT
Python - Getting to the Essence - Points.com - Dave Park
PDF
Perl programming language
PDF
Why functional why scala
PDF
Findbin libs
PPTX
Return Oriented Programming (ROP chaining)
PDF
Metadata-driven Testing
PDF
Petitparser at the Deep into Smalltalk School 2011
PDF
GNU Parallel
Elegant APIs
Introduction to Perl Best Practices
Programming in perl style
LPW: Beginners Perl
優しいWAFの作り方
Abuse Perl
Loops and Unicorns - The Future of the Puppet Language - PuppetConf 2013
How to develop modern web application framework
Improving Dev Assistant
Perl 101 - The Basics of Perl Programming
P4 2018 io_functions
Bioinformatics p1-perl-introduction v2013
Python - Getting to the Essence - Points.com - Dave Park
Perl programming language
Why functional why scala
Findbin libs
Return Oriented Programming (ROP chaining)
Metadata-driven Testing
Petitparser at the Deep into Smalltalk School 2011
GNU Parallel
Ad

Viewers also liked (6)

PDF
Ruby Object Design
PPT
XForms
 
PPT
XForms with Linux
 
PPT
Ruby Security
 
PDF
Ruby on Rails Security Guide
PPTX
How i won a golf set from reg.ru
Ruby Object Design
XForms
 
XForms with Linux
 
Ruby Security
 
Ruby on Rails Security Guide
How i won a golf set from reg.ru
Ad

Similar to Functional Pearls 4 (YAPC::EU::2009 remix) (20)

ODP
Functional perl
PDF
Perl Bag of Tricks - Baltimore Perl mongers
PDF
Good Evils In Perl
KEY
Good Evils In Perl (Yapc Asia)
ODP
Writing Maintainable Perl
ODP
Modern Perl
PDF
Perl.Hacks.On.Vim
PDF
Functional Programming in PHP
PDF
Why async and functional programming in PHP7 suck and how to get overr it?
PDF
Ethiopian multiplication in Perl6
PDF
Functional pe(a)rls: Huey's zipper
DOC
Jsphp 110312161301-phpapp02
PDF
Is Haskell an acceptable Perl?
PDF
mro-every.pdf
PPT
Cleancode
PDF
JavaScript for PHP developers
ODP
Maybe you do not know that ...
PDF
BSDM with BASH: Command Interpolation
PPT
Functional Pe(a)rls version 2
Functional perl
Perl Bag of Tricks - Baltimore Perl mongers
Good Evils In Perl
Good Evils In Perl (Yapc Asia)
Writing Maintainable Perl
Modern Perl
Perl.Hacks.On.Vim
Functional Programming in PHP
Why async and functional programming in PHP7 suck and how to get overr it?
Ethiopian multiplication in Perl6
Functional pe(a)rls: Huey's zipper
Jsphp 110312161301-phpapp02
Is Haskell an acceptable Perl?
mro-every.pdf
Cleancode
JavaScript for PHP developers
Maybe you do not know that ...
BSDM with BASH: Command Interpolation
Functional Pe(a)rls version 2

More from osfameron (12)

PDF
Writing a Tile-Matching Game - FP Style
PPTX
Data Structures for Text Editors
PDF
Rewriting the Apocalypse
PDF
Global Civic Hacking 101 (lightning talk)
PDF
Adventures in civic hacking
PDF
Functional Pe(a)rls - the Purely Functional Datastructures edition
PDF
Haskell in the Real World
PDF
Oyster: an incubator for perls in the cloud
PDF
Semantic Pipes (London Perl Workshop 2009)
PDF
Functional Pe(a)rls
PDF
Readable Perl
PDF
Bigbadwolf
Writing a Tile-Matching Game - FP Style
Data Structures for Text Editors
Rewriting the Apocalypse
Global Civic Hacking 101 (lightning talk)
Adventures in civic hacking
Functional Pe(a)rls - the Purely Functional Datastructures edition
Haskell in the Real World
Oyster: an incubator for perls in the cloud
Semantic Pipes (London Perl Workshop 2009)
Functional Pe(a)rls
Readable Perl
Bigbadwolf

Recently uploaded (20)

PDF
A Late Bloomer's Guide to GenAI: Ethics, Bias, and Effective Prompting - Boha...
PDF
Architecture types and enterprise applications.pdf
PDF
1 - Historical Antecedents, Social Consideration.pdf
PDF
Hybrid horned lizard optimization algorithm-aquila optimizer for DC motor
PDF
A comparative study of natural language inference in Swahili using monolingua...
PDF
Getting started with AI Agents and Multi-Agent Systems
PDF
Developing a website for English-speaking practice to English as a foreign la...
PPTX
The various Industrial Revolutions .pptx
PPT
Geologic Time for studying geology for geologist
PDF
A review of recent deep learning applications in wood surface defect identifi...
PDF
ENT215_Completing-a-large-scale-migration-and-modernization-with-AWS.pdf
PDF
NewMind AI Weekly Chronicles – August ’25 Week III
PDF
TrustArc Webinar - Click, Consent, Trust: Winning the Privacy Game
PPTX
Benefits of Physical activity for teenagers.pptx
PDF
A proposed approach for plagiarism detection in Myanmar Unicode text
PDF
sustainability-14-14877-v2.pddhzftheheeeee
PDF
Zenith AI: Advanced Artificial Intelligence
PPT
What is a Computer? Input Devices /output devices
PDF
OpenACC and Open Hackathons Monthly Highlights July 2025
DOCX
search engine optimization ppt fir known well about this
A Late Bloomer's Guide to GenAI: Ethics, Bias, and Effective Prompting - Boha...
Architecture types and enterprise applications.pdf
1 - Historical Antecedents, Social Consideration.pdf
Hybrid horned lizard optimization algorithm-aquila optimizer for DC motor
A comparative study of natural language inference in Swahili using monolingua...
Getting started with AI Agents and Multi-Agent Systems
Developing a website for English-speaking practice to English as a foreign la...
The various Industrial Revolutions .pptx
Geologic Time for studying geology for geologist
A review of recent deep learning applications in wood surface defect identifi...
ENT215_Completing-a-large-scale-migration-and-modernization-with-AWS.pdf
NewMind AI Weekly Chronicles – August ’25 Week III
TrustArc Webinar - Click, Consent, Trust: Winning the Privacy Game
Benefits of Physical activity for teenagers.pptx
A proposed approach for plagiarism detection in Myanmar Unicode text
sustainability-14-14877-v2.pddhzftheheeeee
Zenith AI: Advanced Artificial Intelligence
What is a Computer? Input Devices /output devices
OpenACC and Open Hackathons Monthly Highlights July 2025
search engine optimization ppt fir known well about this

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