SlideShare ist ein Scribd-Unternehmen logo
1 von 184
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? ,[object Object]
Cry!
Steal from Haskell!
What now? ,[object Object]
(+)  ← ref to add
Wrapping operators ,[object Object]
Wrapping operators ,[object Object]
$add->(1, 2); # 3
YAY!
YAY? ,[object Object]
op('+')
Devel::Declare ,[object Object]
(But better than source filters)
Devel::Declare ,[object Object]
(But better than source filters) ,[object Object]
MooseX::Declare
Sub::Auto
Sub::Section ,[object Object]
Gives nice syntax, using  Devel::Declare
op (+)
Sub::Section ,[object Object]
Gives nice syntax, using  Devel::Declare
op   (Bwahaha!)  (+) Devel::Declare  custom parser hook can inject code etc.
Sub::Section ,[object Object]
Gives nice syntax, using  Devel::Declare
op   (Bwahaha!)  ('+')
Sub::Section ,[object Object]
Gives nice syntax, using  Devel::Declare
Op ('+')  # Perl is none the wiser Tee hee!
Devel::Declare ,[object Object]
(With a bit of scary XS magic) ,[object Object]
changing the source as you compile
horrible perl tricks to get methods installed, and braces closed
mst++, rafl++
Sections
Sections ,[object Object]
Sections ,[object Object]
(+1) is
Sections ,[object Object]
(+1) 8  #  9
Currying ,[object Object]
Currying ,[object Object]
1 arg at a time
Currying ,[object Object]
Currying ,[object Object]
(That's 2 arguments)
Curried functions ,[object Object]
(That's 2 arguments)
add(5)  ...
Curried functions ,[object Object]
(That's 2 arguments)
add(5)  # $left is bound to 5
Curried functions ,[object Object]
(That's 2 arguments)
add(5)  # $left is bound to 5  ->(6);  # 11
Implement in Perl ,[object Object]
sub add { my $left = shift; return sub { my $right = shift; return $left + $right; } }
Implement in Perl ,[object Object]
sub add { my $left = shift; return sub { my $right = shift; return $left + $right; } }
Not pretty or convenient though
Sub::Curried ,[object Object]
Gives nice syntax, using  Devel::Declare
sub  add ($left, $right) { return $left + $right; }
Sub::Curried ,[object Object]
Gives nice syntax, using  Devel::Declare
curry  add ($left, $right) { return $left + $right; }
Sub::Curried ,[object Object]
Gives nice syntax, using  Devel::Declare
curry   (bwahaha!)   add ($left, $right) { return $left + $right; }
Sub::Curried ,[object Object]
curry add { return ROUTINE unless @_; check_args(2, @_);  my $f = sub { my $f = sub { ... } $f = $f->(shift) for @_; return $f; }
Sub::Curried ,[object Object]
curry add { return  ROUTINE unless @_ ; check_args(2, @_);  my $f = sub { my $f = sub { ... } $f = $f->(shift) for @_; return $f; }
Sub::Curried ,[object Object]
Sub::Curried ,[object Object]
add(5);  # function that adds 5
Sub::Curried ,[object Object]
add(5);  # function that adds 5
add();  # function that adds...
Sub::Curried ,[object Object]
add(5);  # function that adds 5
add();  # function that adds...
i.e =  amp;add
Sub::Curried ,[object Object]
curry add { return ROUTINE unless @_; check_args(2 , @_);  my $f = sub { my $f = sub { ... } $f = $f->(shift) for @_; return $f; }
Sub::Curried ,[object Object]
Sub::Curried ,[object Object]
curry add { return ROUTINE unless @_; check_args(2, @_);  my $f = sub { my $f = sub { ... } $f = $f->(shift) for @_; return $f; }
Sub::Curried ,[object Object]
add(1)->(2)
Why? ,[object Object]
Why? ,[object Object]
Why? ,[object Object]
... turns out to be useful/elegant in Haskell
Why? ,[object Object]
... turns out to be useful/elegant in Haskell
(actually, similar techniques  are  used in Perl)
Currying the Invocant ,[object Object]
Currying the Invocant ,[object Object]
Currying the Invocant ,[object Object]
sub add_accessor { my ( $self , $accessor) = @_; ....
Currying the Invocant ,[object Object]
Currying the Invocant ,[object Object]
*{$CALLER::has} = mk_has();
Currying the Invocant ,[object Object]
*{$CALLER::has} = mk_has();
*{$CALLER::has} = has($CALLER);
Currying the Invocant ,[object Object]
Currying the Invocant ,[object Object]
Where were we? ,[object Object]
Where were we? ,[object Object]
my $add = op(+);  # YAY
Where were we? ,[object Object]
my $add = op(+);  # YAY
my $add2 = op(+2);   # take THAT, Haskell!
But... ,[object Object]
But... ,[object Object]
(1-)
But... ,[object Object]
(1-)  # minus(1)
But... ,[object Object]
(1-)  # minus(1)
But... ,[object Object]
(1-)  # minus(1)
Flip ,[object Object]
Flip ,[object Object]
Minus  (3, 1)  # 2
flip(minus)->(3, 1)  # -2;
Flip ,[object Object]
my $prev = flip(minus)->(1);
$prev->( 5 ); ,[object Object]
$right = 5
minus($right=5, $left=1); # 4
Sub::Section ,[object Object]
Sub::Section ,[object Object]
Sub::Section ,[object Object]
$greet->(“World”); # “Hello World”
Sub::Section ,[object Object]
> (2,4,6,8)
Sub::Section ,[object Object]
> (2,4,6,8)
Sub::Section ,[object Object]
Sub::Section ,[object Object]
map op(*2) ->($_) , @numbers; ,[object Object]
> (2,4,6,8)
(rant) ,[object Object]
(rant) ,[object Object]
(rant) ,[object Object]
Perl's $_ is a horrible hack around not doing currying properly
(rant) ,[object Object]
Perl's $_ is a horrible hack around not doing currying properly
Why am I still programming this silly language? ;-)
(yay!) ,[object Object]
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 ,[object Object]
fmap sub { … },  @list;
fmap op(+2),  @list;
TODO:  Functional::Map ,[object Object]
fmap sub { … },  @list;
fmap op(+2),  @list;
(those will all work anyway)
TODO:  Functional::Map ,[object Object]
fmap sub { … },  @list;
fmap op(+2),  @list;
(those will all work anyway)
fmap { uc } @list;
TODO:  Functional::Map ,[object Object]
fmap sub { … },  @list;
fmap op(+2),  @list;

Weitere ähnliche Inhalte

Was ist angesagt?

優しいWAFの作り方
優しいWAFの作り方優しいWAFの作り方
優しいWAFの作り方
techmemo
 
Perl 101 - The Basics of Perl Programming
Perl  101 - The Basics of Perl ProgrammingPerl  101 - The Basics of Perl Programming
Perl 101 - The Basics of Perl Programming
Utkarsh Sengar
 
Perl programming language
Perl programming languagePerl programming language
Perl programming language
Elie Obeid
 

Was ist angesagt? (20)

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

Andere mochten auch (6)

Ruby Object Design
Ruby Object DesignRuby Object Design
Ruby Object Design
 
XForms
XFormsXForms
XForms
 
XForms with Linux
XForms with LinuxXForms with Linux
XForms with Linux
 
Ruby Security
Ruby SecurityRuby Security
Ruby Security
 
Ruby on Rails Security Guide
Ruby on Rails Security GuideRuby on Rails Security Guide
Ruby on Rails Security Guide
 
How i won a golf set from reg.ru
How i won a golf set from reg.ruHow i won a golf set from reg.ru
How i won a golf set from reg.ru
 

Ähnlich wie Functional Pearls 4 (YAPC::EU::2009 remix)

Good Evils In Perl
Good Evils In PerlGood Evils In Perl
Good Evils In Perl
Kang-min Liu
 
Good Evils In Perl (Yapc Asia)
Good Evils In Perl (Yapc Asia)Good Evils In Perl (Yapc Asia)
Good Evils In Perl (Yapc Asia)
Kang-min Liu
 
Writing Maintainable Perl
Writing Maintainable PerlWriting Maintainable Perl
Writing Maintainable Perl
tinypigdotcom
 
Perl.Hacks.On.Vim
Perl.Hacks.On.VimPerl.Hacks.On.Vim
Perl.Hacks.On.Vim
Lin Yo-An
 
Jsphp 110312161301-phpapp02
Jsphp 110312161301-phpapp02Jsphp 110312161301-phpapp02
Jsphp 110312161301-phpapp02
Seri Moth
 
JavaScript for PHP developers
JavaScript for PHP developersJavaScript for PHP developers
JavaScript for PHP developers
Stoyan Stefanov
 

Ähnlich wie Functional Pearls 4 (YAPC::EU::2009 remix) (20)

Functional perl
Functional perlFunctional perl
Functional perl
 
Perl Bag of Tricks - Baltimore Perl mongers
Perl Bag of Tricks  -  Baltimore Perl mongersPerl Bag of Tricks  -  Baltimore Perl mongers
Perl Bag of Tricks - Baltimore Perl mongers
 
Good Evils In Perl
Good Evils In PerlGood Evils In Perl
Good Evils In Perl
 
Good Evils In Perl (Yapc Asia)
Good Evils In Perl (Yapc Asia)Good Evils In Perl (Yapc Asia)
Good Evils In Perl (Yapc Asia)
 
Writing Maintainable Perl
Writing Maintainable PerlWriting Maintainable Perl
Writing Maintainable Perl
 
Modern Perl
Modern PerlModern Perl
Modern Perl
 
Perl.Hacks.On.Vim
Perl.Hacks.On.VimPerl.Hacks.On.Vim
Perl.Hacks.On.Vim
 
Functional Programming in PHP
Functional Programming in PHPFunctional Programming in PHP
Functional Programming in PHP
 
Why async and functional programming in PHP7 suck and how to get overr it?
Why async and functional programming in PHP7 suck and how to get overr it?Why async and functional programming in PHP7 suck and how to get overr it?
Why async and functional programming in PHP7 suck and how to get overr it?
 
Ethiopian multiplication in Perl6
Ethiopian multiplication in Perl6Ethiopian multiplication in Perl6
Ethiopian multiplication in Perl6
 
Functional pe(a)rls: Huey's zipper
Functional pe(a)rls: Huey's zipperFunctional pe(a)rls: Huey's zipper
Functional pe(a)rls: Huey's zipper
 
Php2
Php2Php2
Php2
 
Jsphp 110312161301-phpapp02
Jsphp 110312161301-phpapp02Jsphp 110312161301-phpapp02
Jsphp 110312161301-phpapp02
 
Is Haskell an acceptable Perl?
Is Haskell an acceptable Perl?Is Haskell an acceptable Perl?
Is Haskell an acceptable Perl?
 
mro-every.pdf
mro-every.pdfmro-every.pdf
mro-every.pdf
 
Cleancode
CleancodeCleancode
Cleancode
 
JavaScript for PHP developers
JavaScript for PHP developersJavaScript for PHP developers
JavaScript for PHP developers
 
Maybe you do not know that ...
Maybe you do not know that ...Maybe you do not know that ...
Maybe you do not know that ...
 
BSDM with BASH: Command Interpolation
BSDM with BASH: Command InterpolationBSDM with BASH: Command Interpolation
BSDM with BASH: Command Interpolation
 
Functional Pe(a)rls version 2
Functional Pe(a)rls version 2Functional Pe(a)rls version 2
Functional Pe(a)rls version 2
 

Mehr von osfameron

Mehr von osfameron (12)

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

Kürzlich hochgeladen

Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
panagenda
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
Joaquim Jorge
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 

Kürzlich hochgeladen (20)

Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
Manulife - Insurer Innovation Award 2024
Manulife - Insurer Innovation Award 2024Manulife - Insurer Innovation Award 2024
Manulife - Insurer Innovation Award 2024
 
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live StreamsTop 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
 

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