SlideShare a Scribd company logo
Functional Pe(a)rls



          osfameron @ IPW2011, Turin
      the “purely functional data structures”
                     edition

http://www.fickr.com/photos/jef_saf/3493852795/
previously on Functional Pe(a)rls...



      (IPW, LPW, YAPC::EU, nwe.pm)
                 currying
         operator references: op(+)
              Acme::Monads
              Devel::Declare
C       I A O
            vs.


C       I         A   O
0       1    2   3


    C       I A O
0        1    2   3


     C        I A O


0th element
0        1   2   3


     M I A O


0th element
0         1         2    3


      M I A O


 0th element
no kittens were harmed
  during the making of
       this presentation
0    1    2       3


    M I A O


         2nd element
0    1   2   3


    M I A O


                 4th element
0    1   2   3


    M I A O
max: 3
0    1   2   3


    M I A O
max: 3
0    1   2   3   4

    M I A O W
max: 4

                 4th element
max: 3
0 1 2 3       100,000
M I A O
          …
0    1   2   3


    M I A O
max: 3
0    1   2   3   4

    M I A O W
max: 4


0    1   2   3   4   5


    M I A O W !
max: 5
0    1   2   3   4

    M I A O W
max: 4


0    1   2   3   4   5


    M I A O W !
max: 5
Arrays

    C       I A O
            vs.


C       I         A       O
Perl @arrays

    C       I A O
                      “dynamic array”




            vs.


C       I         A       O
C   I   A   O
C   I   A   O
C     I   A   O
Head
C    I     A   O
    Tail
C   I   A   O
C   I   A   O
I   A   O
A   O
O
C     I   A   O

0th
C     I       A   O

    nth - 1
C          I        A   O

nth[2]?   nth[1]?
C         I   A         O

nth[2]?       nth[0]!
C   I   A   O
                ?
●
    C             I
    tail “ciao” → “iao”
                          A   O
                                  ?
●
    C             I
    tail “ciao” → “iao”
                          A   O
                                  ?
●   tail “iao” → “ao”
●   tail “ao” → “o”
●   tail “o” → ?
●
    C             I
    tail “ciao” → “iao”
                               A       O
                                           ?
●   tail “iao” → “ao”
●   tail “ao” → “o”
●   tail “o” → “” (the empty string)
C   I   A   O
List =

Head           Tail
               (another List)


       or...
Here comes the science^wPerl!
Moose(X::Declare)
use MooseX::Declare;

BEGIN { role_type 'List' }

role List {
     requires 'head';
     requires 'tail';
}
List::Link
class List::Link with List {
  has head => (
       is => 'ro',
       isa => 'Any'
   );
  has tail => (
       is => 'ro',
       isa => 'List'
    ),
}
List::Link
class List::Link with List {
  has head => (
       is => 'ro',
       isa => 'Any'
   );
  has tail => (
       is => 'ro',
       isa => 'List'
    ),
}
List::Link
class List::Link with List {
  has head => (
       is => 'ro',
       isa => 'Any'
   );
  has tail => (
       is => 'ro',
       isa => 'List'
    ),
}
List::Empty
class List::Empty with List {
   method head {
     die "Can't take head of empty list!"
   }
   method tail {
     die "Can't take tail of empty list!"
   }
}
So we can write:

my $list = List::Link->new(
    head => 'c',
    tail => List::Link->new(
       head => 'i',
       tail => List::Link->new(...
Sugar!

my $list = List->fromArray(
 qw/ c i a o /);
Multimethods

use MooseX::MultiMethods;

multi method fromArray ($class:) {
  return List::Empty->new;
}
Multimethods

use MooseX::MultiMethods;

multi method fromArray ($class:) {
  return List::Empty->new;
}
Multimethods

multi method fromArray  ($class: $head, @tail) {
    return List::Link->new(
       head => $head,
       tail => $class->fromArray(@tail),
    );
}
Eeek! Recursion!
my $list = List::fromArray(1..1000000);
Eeek! Recursion!
my $list = List::fromArray(1..1000000);

Deep recursion on subroutine
"List::fromArray" at foo.pl line 20
Eeek! Recursion!

multi method fromArray  ($class: $head, @tail) {
    return List::Link->new(
       head => $head,
       tail => $class->fromArray(@tail),
    );
}
Eeek! Recursion!
fromArray
Eeek! Recursion!
fromArray


  List::Link->new
  (..., fromArray)
Eeek! Recursion!
fromArray


  List::Link->new
  (..., fromArray)

            List::Link->new
            (..., fromArray)
Eeek! Recursion!
fromArray


  List::Link->new
  (..., fromArray)

            List::Link->new
            (..., fromArray)

                   List::Link->new
                   (..., fromArray)
Eeek! Recursion!
fromArray


  List::Link->new
  (..., fromArray)

            List::Link->new
            (..., fromArray)

                   List::Link->new
                   (..., fromArray)

                               List::Link->new
                               (..., fromArray)
Eeek! Recursion!
fromArray


  List::Link->new
  (..., fromArray)

            List::Link->new
            (..., fromArray)

                   List::Link->new
                   (..., fromArray)

                               List::Link->new
                               (..., fromArray)   = $list
Eeek! Recursion!
fromArray


  List::Link->new
  (..., fromArray)

            List::Link->new
            (..., fromArray)

                   List::Link->new
                   (..., fromArray)   = $list
Eeek! Recursion!
fromArray


  List::Link->new
  (..., fromArray)

            List::Link->new
            (..., fromArray)   = $list
Eeek! Recursion!
fromArray


  List::Link->new
  (..., fromArray)   = $list
Eeek! Recursion!
fromArray = $list
Eeek! Recursion!

no warnings 'recursion';
$DB::deep = 100_000_000;
Eeek! Recursion!

no warnings 'recursion';
$DB::deep = 100_000_000;

Papering over the cracks
Tail Call Elimination

Sub::Call::Tail
Sub::Call::Recur

by nothingmuch
Tail call elimination
fromArray
Tail call elimination


List::Link->new
(..., fromArray)
Tail call elimination



List::Link->new
(..., fromArray)
Tail call elimination




  List::Link->new
  (..., fromArray)
Tail call elimination




      List::Link->new
      (..., fromArray)
Tail call elimination




      List::Link->new
      (..., fromArray)   = $list
Tail Call Elimination

use Sub::Import 'Sub::Call::Tail'
      tail => { -as => 'tail_call' };

multi method fromArray ($self: $head, @tail)
{
    tail_call List::Link->new(
       head => $head,
       tail => $self->fromArray(@tail),
    );
}
Indexing into List

multi method nth
  (List::Empty $self: Int $pos)
{
  die "Can't index into an Empty list";
}
Indexing into List

 multi method nth
  (Int $pos where { $_ == 0 })
{
  return $self->head;
}
Indexing into List

multi method nth
  (Int $pos where { $_ > 0 })
{
  tail_call $self->tail->nth( $pos - 1 );
}
C   I   A   O

M
C   I   A   O

M               W
C    I              A        O

M                                W
    Mutation leads to bugs
    (and misspellings!)
C   I   A   O

M   I   A   O   W
C   I   A   O

C   I   B
C           I               A              O

C           I              B
    Copy everything upstream of a change
    Downstream of changes can be shared
C            I              A                 O

C           I               B                 O
    Doubly linked lists have no downstream!
pure-fp-book



●   https://github.com/osfameron/pure-fp-book
●   Purely Functional Data Structures for the...
    ●   Impure
    ●   Perl Programmer
    ●   Working Programmer
    ●   Mutable, Rank-Scented Many

More Related Content

What's hot (20)

PDF
RxSwift 시작하기
Suyeol Jeon
 
PDF
Descobrindo a linguagem Perl
garux
 
PPT
An Elephant of a Different Colour: Hack
Vic Metcalfe
 
PPTX
Perl6 a whistle stop tour
Simon Proctor
 
PDF
Perl6 a whistle stop tour
Simon Proctor
 
PDF
The Perl6 Type System
abrummett
 
KEY
Invertible-syntax 入門
Hiromi Ishii
 
PDF
Wx::Perl::Smart
lichtkind
 
KEY
Desarrollando aplicaciones web en minutos
Edgar Suarez
 
PDF
Frege is a Haskell for the JVM
jwausle
 
PDF
Perl 6 in Context
lichtkind
 
PDF
Introdução ao Perl 6
garux
 
PDF
Perl.Hacks.On.Vim
Lin Yo-An
 
PDF
Swift 함수 커링 사용하기
진성 오
 
KEY
Template Haskell とか
Hiromi Ishii
 
PPT
PHP and MySQL
Sanketkumar Biswas
 
KEY
groovy & grails - lecture 3
Alexandre Masselot
 
PDF
Static Optimization of PHP bytecode (PHPSC 2017)
Nikita Popov
 
PPTX
第二讲 预备-Python基礎
anzhong70
 
PPTX
第二讲 Python基礎
juzihua1102
 
RxSwift 시작하기
Suyeol Jeon
 
Descobrindo a linguagem Perl
garux
 
An Elephant of a Different Colour: Hack
Vic Metcalfe
 
Perl6 a whistle stop tour
Simon Proctor
 
Perl6 a whistle stop tour
Simon Proctor
 
The Perl6 Type System
abrummett
 
Invertible-syntax 入門
Hiromi Ishii
 
Wx::Perl::Smart
lichtkind
 
Desarrollando aplicaciones web en minutos
Edgar Suarez
 
Frege is a Haskell for the JVM
jwausle
 
Perl 6 in Context
lichtkind
 
Introdução ao Perl 6
garux
 
Perl.Hacks.On.Vim
Lin Yo-An
 
Swift 함수 커링 사용하기
진성 오
 
Template Haskell とか
Hiromi Ishii
 
PHP and MySQL
Sanketkumar Biswas
 
groovy & grails - lecture 3
Alexandre Masselot
 
Static Optimization of PHP bytecode (PHPSC 2017)
Nikita Popov
 
第二讲 预备-Python基礎
anzhong70
 
第二讲 Python基礎
juzihua1102
 

Similar to Functional Pe(a)rls - the Purely Functional Datastructures edition (20)

PPTX
PHP Functions & Arrays
Henry Osborne
 
PDF
Scripting3
Nao Dara
 
PPT
Basic perl programming
Thang Nguyen
 
PPTX
Switching from java to groovy
Paul Woods
 
PDF
Hypers and Gathers and Takes! Oh my!
Workhorse Computing
 
PPT
Crash Course in Perl – Perl tutorial for C programmers
Gil Megidish
 
PDF
Barcelona.pm Curs1211 sess01
Javier Arturo Rodríguez
 
ODP
Writing Maintainable Perl
tinypigdotcom
 
PDF
Perl 6 by example
Andrew Shitov
 
PDF
Regexp Master
Paolo Marcatili
 
PPT
computer notes - Data Structures - 32
ecomputernotes
 
KEY
1 the ruby way
Luis Doubrava
 
PPT
Computer notes - Binary Search
ecomputernotes
 
PDF
Wheels we didn't re-invent: Perl's Utility Modules
Workhorse Computing
 
ODP
Intro to The PHP SPL
Chris Tankersley
 
PDF
PHP 101
Muhammad Hijazi
 
KEY
Searching ORM: First Why, Then How
sfarmer10
 
PDF
First steps in PERL
Brahma Killampalli
 
PDF
List Unfolding - 'unfold' as the Computational Dual of 'fold', and how 'unfol...
Philip Schwarz
 
PHP Functions & Arrays
Henry Osborne
 
Scripting3
Nao Dara
 
Basic perl programming
Thang Nguyen
 
Switching from java to groovy
Paul Woods
 
Hypers and Gathers and Takes! Oh my!
Workhorse Computing
 
Crash Course in Perl – Perl tutorial for C programmers
Gil Megidish
 
Barcelona.pm Curs1211 sess01
Javier Arturo Rodríguez
 
Writing Maintainable Perl
tinypigdotcom
 
Perl 6 by example
Andrew Shitov
 
Regexp Master
Paolo Marcatili
 
computer notes - Data Structures - 32
ecomputernotes
 
1 the ruby way
Luis Doubrava
 
Computer notes - Binary Search
ecomputernotes
 
Wheels we didn't re-invent: Perl's Utility Modules
Workhorse Computing
 
Intro to The PHP SPL
Chris Tankersley
 
PHP 101
Muhammad Hijazi
 
Searching ORM: First Why, Then How
sfarmer10
 
First steps in PERL
Brahma Killampalli
 
List Unfolding - 'unfold' as the Computational Dual of 'fold', and how 'unfol...
Philip Schwarz
 
Ad

More from osfameron (11)

PDF
Writing a Tile-Matching Game - FP Style
osfameron
 
PPTX
Data Structures for Text Editors
osfameron
 
PDF
Rewriting the Apocalypse
osfameron
 
PDF
Global Civic Hacking 101 (lightning talk)
osfameron
 
PDF
Adventures in civic hacking
osfameron
 
PDF
Oyster: an incubator for perls in the cloud
osfameron
 
PDF
Semantic Pipes (London Perl Workshop 2009)
osfameron
 
ODP
Functional Pearls 4 (YAPC::EU::2009 remix)
osfameron
 
PDF
Functional Pe(a)rls
osfameron
 
PDF
Readable Perl
osfameron
 
PDF
Bigbadwolf
osfameron
 
Writing a Tile-Matching Game - FP Style
osfameron
 
Data Structures for Text Editors
osfameron
 
Rewriting the Apocalypse
osfameron
 
Global Civic Hacking 101 (lightning talk)
osfameron
 
Adventures in civic hacking
osfameron
 
Oyster: an incubator for perls in the cloud
osfameron
 
Semantic Pipes (London Perl Workshop 2009)
osfameron
 
Functional Pearls 4 (YAPC::EU::2009 remix)
osfameron
 
Functional Pe(a)rls
osfameron
 
Readable Perl
osfameron
 
Bigbadwolf
osfameron
 
Ad

Recently uploaded (20)

PPTX
From Sci-Fi to Reality: Exploring AI Evolution
Svetlana Meissner
 
PDF
Achieving Consistent and Reliable AI Code Generation - Medusa AI
medusaaico
 
PDF
Transcript: New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
PDF
Presentation - Vibe Coding The Future of Tech
yanuarsinggih1
 
PDF
The Rise of AI and IoT in Mobile App Tech.pdf
IMG Global Infotech
 
PPTX
Building Search Using OpenSearch: Limitations and Workarounds
Sease
 
PDF
Using FME to Develop Self-Service CAD Applications for a Major UK Police Force
Safe Software
 
PDF
CIFDAQ Market Insights for July 7th 2025
CIFDAQ
 
PDF
Reverse Engineering of Security Products: Developing an Advanced Microsoft De...
nwbxhhcyjv
 
PDF
IoT-Powered Industrial Transformation – Smart Manufacturing to Connected Heal...
Rejig Digital
 
PDF
Chris Elwell Woburn, MA - Passionate About IT Innovation
Chris Elwell Woburn, MA
 
PDF
Bitcoin for Millennials podcast with Bram, Power Laws of Bitcoin
Stephen Perrenod
 
PDF
LLMs.txt: Easily Control How AI Crawls Your Site
Keploy
 
PDF
"AI Transformation: Directions and Challenges", Pavlo Shaternik
Fwdays
 
PDF
Agentic AI lifecycle for Enterprise Hyper-Automation
Debmalya Biswas
 
PDF
Mastering Financial Management in Direct Selling
Epixel MLM Software
 
PDF
"Beyond English: Navigating the Challenges of Building a Ukrainian-language R...
Fwdays
 
PPTX
AUTOMATION AND ROBOTICS IN PHARMA INDUSTRY.pptx
sameeraaabegumm
 
PDF
From Code to Challenge: Crafting Skill-Based Games That Engage and Reward
aiyshauae
 
PDF
POV_ Why Enterprises Need to Find Value in ZERO.pdf
darshakparmar
 
From Sci-Fi to Reality: Exploring AI Evolution
Svetlana Meissner
 
Achieving Consistent and Reliable AI Code Generation - Medusa AI
medusaaico
 
Transcript: New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
Presentation - Vibe Coding The Future of Tech
yanuarsinggih1
 
The Rise of AI and IoT in Mobile App Tech.pdf
IMG Global Infotech
 
Building Search Using OpenSearch: Limitations and Workarounds
Sease
 
Using FME to Develop Self-Service CAD Applications for a Major UK Police Force
Safe Software
 
CIFDAQ Market Insights for July 7th 2025
CIFDAQ
 
Reverse Engineering of Security Products: Developing an Advanced Microsoft De...
nwbxhhcyjv
 
IoT-Powered Industrial Transformation – Smart Manufacturing to Connected Heal...
Rejig Digital
 
Chris Elwell Woburn, MA - Passionate About IT Innovation
Chris Elwell Woburn, MA
 
Bitcoin for Millennials podcast with Bram, Power Laws of Bitcoin
Stephen Perrenod
 
LLMs.txt: Easily Control How AI Crawls Your Site
Keploy
 
"AI Transformation: Directions and Challenges", Pavlo Shaternik
Fwdays
 
Agentic AI lifecycle for Enterprise Hyper-Automation
Debmalya Biswas
 
Mastering Financial Management in Direct Selling
Epixel MLM Software
 
"Beyond English: Navigating the Challenges of Building a Ukrainian-language R...
Fwdays
 
AUTOMATION AND ROBOTICS IN PHARMA INDUSTRY.pptx
sameeraaabegumm
 
From Code to Challenge: Crafting Skill-Based Games That Engage and Reward
aiyshauae
 
POV_ Why Enterprises Need to Find Value in ZERO.pdf
darshakparmar
 

Functional Pe(a)rls - the Purely Functional Datastructures edition