SlideShare a Scribd company logo
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...
Functional perl
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
Functional perl
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 (20)

PPT
Python легко и просто. Красиво решаем повседневные задачи
Maxim Kulsha
 
PDF
Closure, Higher-order function in Swift
SeongGyu Jo
 
PPTX
Share test
Anton Stuk
 
PDF
Functional programming in Python
Colin Su
 
PDF
Closures
SV.CO
 
PDF
Ramda, a functional JavaScript library
Derek Willian Stavis
 
PDF
Beginning Haskell, Dive In, Its Not That Scary!
priort
 
PDF
Linguagem sql
Tic Eslc
 
PDF
Perl6 one-liners
Andrew Shitov
 
PDF
Ramda lets write declarative js
Pivorak MeetUp
 
ODP
The secrets of inverse brogramming
Richie Cotton
 
PDF
Linux class 15 26 oct 2021
Khawar Nehal [email protected]
 
PDF
Functional Pattern Matching on Python
Daker Fernandes
 
PPTX
Sharper tools with F#
Kevin Avignon
 
TXT
Menu func-sh(1)
Ben Pope
 
PDF
Rakudo
awwaiid
 
PDF
Workshop on command line tools - day 1
Leandro Lima
 
PDF
Side by Side - Scala and Java Adaptations of Martin Fowler’s Javascript Refac...
Philip Schwarz
 
PDF
Workshop on command line tools - day 2
Leandro Lima
 
PDF
Road to Power Query NINJA – 1st STEP
Takeshi Kagata
 
Python легко и просто. Красиво решаем повседневные задачи
Maxim Kulsha
 
Closure, Higher-order function in Swift
SeongGyu Jo
 
Share test
Anton Stuk
 
Functional programming in Python
Colin Su
 
Closures
SV.CO
 
Ramda, a functional JavaScript library
Derek Willian Stavis
 
Beginning Haskell, Dive In, Its Not That Scary!
priort
 
Linguagem sql
Tic Eslc
 
Perl6 one-liners
Andrew Shitov
 
Ramda lets write declarative js
Pivorak MeetUp
 
The secrets of inverse brogramming
Richie Cotton
 
Linux class 15 26 oct 2021
Khawar Nehal [email protected]
 
Functional Pattern Matching on Python
Daker Fernandes
 
Sharper tools with F#
Kevin Avignon
 
Menu func-sh(1)
Ben Pope
 
Rakudo
awwaiid
 
Workshop on command line tools - day 1
Leandro Lima
 
Side by Side - Scala and Java Adaptations of Martin Fowler’s Javascript Refac...
Philip Schwarz
 
Workshop on command line tools - day 2
Leandro Lima
 
Road to Power Query NINJA – 1st STEP
Takeshi Kagata
 

Viewers also liked (7)

PDF
Program and Project Management at the New World Trade Center
novacsi
 
PPT
How to Build a Tower
alaska150
 
PDF
Functional Planning of a Building
Mr. Ramesh Nayaka
 
PPTX
اسس تصميم المباني الادارية Office building design & The Edge Office a Greenes...
Ahmed SHoukry ELhfnawy
 
PDF
مباني ادارية
freemadoo
 
PDF
State of Education in the Philippines 2012
Arangkada Philippines
 
PPT
Burj khalifa
Safa Aboelssaad
 
Program and Project Management at the New World Trade Center
novacsi
 
How to Build a Tower
alaska150
 
Functional Planning of a Building
Mr. Ramesh Nayaka
 
اسس تصميم المباني الادارية Office building design & The Edge Office a Greenes...
Ahmed SHoukry ELhfnawy
 
مباني ادارية
freemadoo
 
State of Education in the Philippines 2012
Arangkada Philippines
 
Burj khalifa
Safa Aboelssaad
 
Ad

Similar to Functional perl (20)

PDF
Wheels we didn't re-invent: Perl's Utility Modules
Workhorse Computing
 
PDF
Our Friends the Utils: A highway traveled by wheels we didn't re-invent.
Workhorse Computing
 
ODP
Perls Functional functions
daoswald
 
PDF
Scripting3
Nao Dara
 
PDF
Taking Perl to Eleven with Higher-Order Functions
David Golden
 
PDF
perl course-in-mumbai
vibrantuser
 
PDF
perl course-in-mumbai
vibrantuser
 
PDF
Learning Perl 6
brian d foy
 
PDF
What's New in Perl? v5.10 - v5.16
Ricardo Signes
 
KEY
Good Evils In Perl (Yapc Asia)
Kang-min Liu
 
PDF
Learning Perl 6 (NPW 2007)
brian d foy
 
PPTX
SL-2.pptx
Satyanandaram Nandigam
 
PDF
newperl5
tutorialsruby
 
PDF
newperl5
tutorialsruby
 
PDF
Cs3430 lecture 16
Tanwir Zaman
 
ODP
Intermediate Perl
Dave Cross
 
ODP
Introduction to Perl - Day 1
Dave Cross
 
ODP
Introduction to Perl - Day 2
Dave Cross
 
PPT
LPW: Beginners Perl
Dave Cross
 
Wheels we didn't re-invent: Perl's Utility Modules
Workhorse Computing
 
Our Friends the Utils: A highway traveled by wheels we didn't re-invent.
Workhorse Computing
 
Perls Functional functions
daoswald
 
Scripting3
Nao Dara
 
Taking Perl to Eleven with Higher-Order Functions
David Golden
 
perl course-in-mumbai
vibrantuser
 
perl course-in-mumbai
vibrantuser
 
Learning Perl 6
brian d foy
 
What's New in Perl? v5.10 - v5.16
Ricardo Signes
 
Good Evils In Perl (Yapc Asia)
Kang-min Liu
 
Learning Perl 6 (NPW 2007)
brian d foy
 
newperl5
tutorialsruby
 
newperl5
tutorialsruby
 
Cs3430 lecture 16
Tanwir Zaman
 
Intermediate Perl
Dave Cross
 
Introduction to Perl - Day 1
Dave Cross
 
Introduction to Perl - Day 2
Dave Cross
 
LPW: Beginners Perl
Dave Cross
 
Ad

Recently uploaded (20)

PDF
New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
PDF
Exolore The Essential AI Tools in 2025.pdf
Srinivasan M
 
PDF
"Beyond English: Navigating the Challenges of Building a Ukrainian-language R...
Fwdays
 
PPTX
OpenID AuthZEN - Analyst Briefing July 2025
David Brossard
 
PDF
Newgen Beyond Frankenstein_Build vs Buy_Digital_version.pdf
darshakparmar
 
PDF
POV_ Why Enterprises Need to Find Value in ZERO.pdf
darshakparmar
 
PPTX
WooCommerce Workshop: Bring Your Laptop
Laura Hartwig
 
PDF
What Makes Contify’s News API Stand Out: Key Features at a Glance
Contify
 
PDF
IoT-Powered Industrial Transformation – Smart Manufacturing to Connected Heal...
Rejig Digital
 
PDF
LOOPS in C Programming Language - Technology
RishabhDwivedi43
 
PDF
Achieving Consistent and Reliable AI Code Generation - Medusa AI
medusaaico
 
PDF
Mastering Financial Management in Direct Selling
Epixel MLM Software
 
PDF
Jak MŚP w Europie Środkowo-Wschodniej odnajdują się w świecie AI
dominikamizerska1
 
PPTX
Future Tech Innovations 2025 – A TechLists Insight
TechLists
 
PDF
Building Real-Time Digital Twins with IBM Maximo & ArcGIS Indoors
Safe Software
 
PPTX
"Autonomy of LLM Agents: Current State and Future Prospects", Oles` Petriv
Fwdays
 
DOCX
Python coding for beginners !! Start now!#
Rajni Bhardwaj Grover
 
PDF
Go Concurrency Real-World Patterns, Pitfalls, and Playground Battles.pdf
Emily Achieng
 
PDF
The Rise of AI and IoT in Mobile App Tech.pdf
IMG Global Infotech
 
DOCX
Cryptography Quiz: test your knowledge of this important security concept.
Rajni Bhardwaj Grover
 
New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
Exolore The Essential AI Tools in 2025.pdf
Srinivasan M
 
"Beyond English: Navigating the Challenges of Building a Ukrainian-language R...
Fwdays
 
OpenID AuthZEN - Analyst Briefing July 2025
David Brossard
 
Newgen Beyond Frankenstein_Build vs Buy_Digital_version.pdf
darshakparmar
 
POV_ Why Enterprises Need to Find Value in ZERO.pdf
darshakparmar
 
WooCommerce Workshop: Bring Your Laptop
Laura Hartwig
 
What Makes Contify’s News API Stand Out: Key Features at a Glance
Contify
 
IoT-Powered Industrial Transformation – Smart Manufacturing to Connected Heal...
Rejig Digital
 
LOOPS in C Programming Language - Technology
RishabhDwivedi43
 
Achieving Consistent and Reliable AI Code Generation - Medusa AI
medusaaico
 
Mastering Financial Management in Direct Selling
Epixel MLM Software
 
Jak MŚP w Europie Środkowo-Wschodniej odnajdują się w świecie AI
dominikamizerska1
 
Future Tech Innovations 2025 – A TechLists Insight
TechLists
 
Building Real-Time Digital Twins with IBM Maximo & ArcGIS Indoors
Safe Software
 
"Autonomy of LLM Agents: Current State and Future Prospects", Oles` Petriv
Fwdays
 
Python coding for beginners !! Start now!#
Rajni Bhardwaj Grover
 
Go Concurrency Real-World Patterns, Pitfalls, and Playground Battles.pdf
Emily Achieng
 
The Rise of AI and IoT in Mobile App Tech.pdf
IMG Global Infotech
 
Cryptography Quiz: test your knowledge of this important security concept.
Rajni Bhardwaj Grover
 

Functional perl