SlideShare a Scribd company logo
1 of 59
Functional Programming
                Perl
      Christopher Mckay
      error@errorific.com
What Am I
What's this talk all about?
History
How
Why not?
But I don't use/appreciate/like perl?
Functional Programming is Easy
So easy sysadmins do it
In the beginning...
Wrote Pearl Perl
People wanted a sort function
    Larry was generous
sort
F(a, a), [a] --------> [a]
@sorted_list
  = sort {$_[0] <=> $_[1]} @list
@sorted_list
  = sort {$a   <=> $b   } @list
@sorted_list
     = sort {$a <=> $b} @list
sort               String sorting

sort {$a cmp $b} Also string sorting

sort {$a <=> $b}   Sort numerically

sort &comparator Sort by function
                 named comparator
sort {
  @a = split ///, $a;
  @b = split ///, $b;
  ($a[2] <=> $b[2])
      || ($a[1] <=> $b[1])
      || ($a[0] <=> $b[0])
      || 0;
} @list_of_dmy_dates
1988 - Perl 2.0
1989 - Perl 3.0
1991 - Perl 4.0
grep
F(a, a), [a] --------> [a]
map
F(a), [a] --------> [a]
1994 – Perl 5.0
memoize
F(a) → b --------------> F(a) → b
List::Util


                    reduce
F(a, b) → b, [a] --------------> b
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
So Perl evolved a functional theme
So how do I make functional stuff in
    perl instead of just using it?
Lexical scoping
First class functions
That's about it.
Your language does that too right?
So lets write a function that rolls a
                 die
my $dx = sub {
  my ($sides) = @_;
  return (rand() * $sides) + 1;
}

my $roll = $dx->(6);
I don't like rolling 1's though so...
my $cheat = sub {
  my ($roll) = @_;
  if ($roll < 2) {
      return 2;
  } else {
      return $roll;
  }
}
I don't want to get caught turning the
         die over though so ...
my $inconspicuous_dx = sub {
  my ($sides) = @_;

    return $cheat->($dx->($sides));
}
my $compose = sub {
  my ($fa, $fb) = @_;
  return sub {
     return $fa->($fb->(@_));
  }
}
my $inconspicuous_dx =
  $compose->($cheat, $dx);

my $better_roll =
  $inconspicuous_dx->(6);
Monads in Perl
Masahiro Honma (hiratara)
   YAPC::Asia 2011
  Slides are in english

     Data::Monad
All I ever need is d6!
my $d6 = sub {
   return $inconspicuous_dx->(6);
};

my $roll_a_6 = $d6->();
Too lazy to roll 20d6
my $dice_tower_maker = sub {
  my ($die) = @_;
  return sub {
     my ($rolls) = @_;
     return map {
        $die->()
     } (1 .. $rolls);
  }
}
my $dice_tower =
  $dice_tower_generator->($d6);

say join(', ', $dice_tower->(20));
use List::Util;

say sum $dice_tower->(20);
use List::Util;

say reduce {$a + $b}
  $dice_tower->(20);
So why not use this stuff for
       everything?
The compilers not out to help you
Declaring functions gets obtuse
          after awhile
Purity isn't an option, everything
              changes
The community loves objects
If it was simple in a functional
language it probably takes some
 hacking to make it work in perl
And the language isn't extensible,
              yet
Good things are to come
Perl 6
Static typing
      Better syntax
  Blocks and closures
  List comprehension
Automatic parallelisation
        Lots more
Thanks

Questions?

More Related Content

What's hot

Functional programming in Python
Functional programming in PythonFunctional programming in Python
Functional programming in Python
Colin Su
 
Linguagem sql
Linguagem sqlLinguagem sql
Linguagem sql
Tic Eslc
 
Functional Pattern Matching on Python
Functional Pattern Matching on PythonFunctional Pattern Matching on Python
Functional Pattern Matching on Python
Daker Fernandes
 
Menu func-sh(1)
Menu func-sh(1)Menu func-sh(1)
Menu func-sh(1)
Ben Pope
 

What's hot (20)

Python легко и просто. Красиво решаем повседневные задачи
Python легко и просто. Красиво решаем повседневные задачиPython легко и просто. Красиво решаем повседневные задачи
Python легко и просто. Красиво решаем повседневные задачи
 
Closure, Higher-order function in Swift
Closure, Higher-order function in SwiftClosure, Higher-order function in Swift
Closure, Higher-order function in Swift
 
Share test
Share testShare test
Share test
 
Functional programming in Python
Functional programming in PythonFunctional programming in Python
Functional programming in Python
 
Closures
ClosuresClosures
Closures
 
Ramda, a functional JavaScript library
Ramda, a functional JavaScript libraryRamda, a functional JavaScript library
Ramda, a functional JavaScript library
 
Beginning Haskell, Dive In, Its Not That Scary!
Beginning Haskell, Dive In, Its Not That Scary!Beginning Haskell, Dive In, Its Not That Scary!
Beginning Haskell, Dive In, Its Not That Scary!
 
Linguagem sql
Linguagem sqlLinguagem sql
Linguagem sql
 
Perl6 one-liners
Perl6 one-linersPerl6 one-liners
Perl6 one-liners
 
Ramda lets write declarative js
Ramda   lets write declarative jsRamda   lets write declarative js
Ramda lets write declarative js
 
The secrets of inverse brogramming
The secrets of inverse brogrammingThe secrets of inverse brogramming
The secrets of inverse brogramming
 
Linux class 15 26 oct 2021
Linux class 15   26 oct 2021Linux class 15   26 oct 2021
Linux class 15 26 oct 2021
 
Functional Pattern Matching on Python
Functional Pattern Matching on PythonFunctional Pattern Matching on Python
Functional Pattern Matching on Python
 
Sharper tools with F#
Sharper tools with F#Sharper tools with F#
Sharper tools with F#
 
Menu func-sh(1)
Menu func-sh(1)Menu func-sh(1)
Menu func-sh(1)
 
Rakudo
RakudoRakudo
Rakudo
 
Workshop on command line tools - day 1
Workshop on command line tools - day 1Workshop on command line tools - day 1
Workshop on command line tools - day 1
 
Side by Side - Scala and Java Adaptations of Martin Fowler’s Javascript Refac...
Side by Side - Scala and Java Adaptations of Martin Fowler’s Javascript Refac...Side by Side - Scala and Java Adaptations of Martin Fowler’s Javascript Refac...
Side by Side - Scala and Java Adaptations of Martin Fowler’s Javascript Refac...
 
Workshop on command line tools - day 2
Workshop on command line tools - day 2Workshop on command line tools - day 2
Workshop on command line tools - day 2
 
Road to Power Query NINJA – 1st STEP
Road to Power Query NINJA – 1st STEP Road to Power Query NINJA – 1st STEP
Road to Power Query NINJA – 1st STEP
 

Viewers also liked

مباني ادارية
مباني اداريةمباني ادارية
مباني ادارية
freemadoo
 

Viewers also liked (8)

Crv book n ys
Crv book n ysCrv book n ys
Crv book n ys
 
Program and Project Management at the New World Trade Center
Program and Project Management at the New World Trade CenterProgram and Project Management at the New World Trade Center
Program and Project Management at the New World Trade Center
 
How to Build a Tower
How to Build a TowerHow to Build a Tower
How to Build a Tower
 
Functional Planning of a Building
 Functional Planning of a Building Functional Planning of a Building
Functional Planning of a Building
 
اسس تصميم المباني الادارية Office building design & The Edge Office a Greenes...
اسس تصميم المباني الادارية Office building design & The Edge Office a Greenes...اسس تصميم المباني الادارية Office building design & The Edge Office a Greenes...
اسس تصميم المباني الادارية Office building design & The Edge Office a Greenes...
 
مباني ادارية
مباني اداريةمباني ادارية
مباني ادارية
 
State of Education in the Philippines 2012
State of Education in the Philippines 2012State of Education in the Philippines 2012
State of Education in the Philippines 2012
 
Burj khalifa
Burj khalifaBurj khalifa
Burj khalifa
 

Similar to Functional perl

perl usage at database applications
perl usage at database applicationsperl usage at database applications
perl usage at database applications
Joe Jiang
 
Writing Maintainable Perl
Writing Maintainable PerlWriting Maintainable Perl
Writing Maintainable Perl
tinypigdotcom
 
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
 
Good Evils In Perl
Good Evils In PerlGood Evils In Perl
Good Evils In Perl
Kang-min Liu
 
Refactoring to Macros with Clojure
Refactoring to Macros with ClojureRefactoring to Macros with Clojure
Refactoring to Macros with Clojure
Dmitry Buzdin
 

Similar to Functional perl (20)

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)
 
Simple Ways To Be A Better Programmer (OSCON 2007)
Simple Ways To Be A Better Programmer (OSCON 2007)Simple Ways To Be A Better Programmer (OSCON 2007)
Simple Ways To Be A Better Programmer (OSCON 2007)
 
Perl6 a whistle stop tour
Perl6 a whistle stop tourPerl6 a whistle stop tour
Perl6 a whistle stop tour
 
Perl6 a whistle stop tour
Perl6 a whistle stop tourPerl6 a whistle stop tour
Perl6 a whistle stop tour
 
perl usage at database applications
perl usage at database applicationsperl usage at database applications
perl usage at database applications
 
From Javascript To Haskell
From Javascript To HaskellFrom Javascript To Haskell
From Javascript To Haskell
 
Is Haskell an acceptable Perl?
Is Haskell an acceptable Perl?Is Haskell an acceptable Perl?
Is Haskell an acceptable Perl?
 
Writing Maintainable Perl
Writing Maintainable PerlWriting Maintainable Perl
Writing Maintainable 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)
 
Good Evils In Perl
Good Evils In PerlGood Evils In Perl
Good Evils In Perl
 
How to write code you won't hate tomorrow
How to write code you won't hate tomorrowHow to write code you won't hate tomorrow
How to write code you won't hate tomorrow
 
Perl6 Regexen: Reduce the line noise in your code.
Perl6 Regexen: Reduce the line noise in your code.Perl6 Regexen: Reduce the line noise in your code.
Perl6 Regexen: Reduce the line noise in your code.
 
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?
 
Mips1
Mips1Mips1
Mips1
 
FP in scalaで鍛える関数型脳
FP in scalaで鍛える関数型脳FP in scalaで鍛える関数型脳
FP in scalaで鍛える関数型脳
 
Top 10 php classic traps
Top 10 php classic trapsTop 10 php classic traps
Top 10 php classic traps
 
Internationalizing CakePHP Applications
Internationalizing CakePHP ApplicationsInternationalizing CakePHP Applications
Internationalizing CakePHP Applications
 
PHPCon 2016: PHP7 by Witek Adamus / XSolve
PHPCon 2016: PHP7 by Witek Adamus / XSolvePHPCon 2016: PHP7 by Witek Adamus / XSolve
PHPCon 2016: PHP7 by Witek Adamus / XSolve
 
Refactoring to Macros with Clojure
Refactoring to Macros with ClojureRefactoring to Macros with Clojure
Refactoring to Macros with Clojure
 
Neatly Hashing a Tree: FP tree-fold in Perl5 & Perl6
Neatly Hashing a Tree: FP tree-fold in Perl5 & Perl6Neatly Hashing a Tree: FP tree-fold in Perl5 & Perl6
Neatly Hashing a Tree: FP tree-fold in Perl5 & Perl6
 

Recently uploaded

Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
Joaquim Jorge
 

Recently uploaded (20)

04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
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...
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
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)
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
Evaluating the top large language models.pdf
Evaluating the top large language models.pdfEvaluating the top large language models.pdf
Evaluating the top large language models.pdf
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
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
 
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
 

Functional perl