Functional Pe(a)rls          osfameron @ IPW2011, Turin      the “purely functional data structures”                     e...
previously on Functional Pe(a)rls...      (IPW, LPW, YAPC::EU, nwe.pm)                 currying         operator reference...
C       I A O            vs.C       I         A   O
0       1    2   3    C       I A O
0        1    2   3     C        I A O0th element
0        1   2   3     M I A O0th element
0         1         2    3      M I A O 0th elementno 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 Omax: 3
0    1   2   3    M I A Omax: 3
0    1   2   3   4    M I A O Wmax: 4                 4th element
max: 30 1 2 3       100,000M I A O          …
0    1   2   3    M I A Omax: 3
0    1   2   3   4    M I A O Wmax: 40    1   2   3   4   5    M I A O W !max: 5
0    1   2   3   4    M I A O Wmax: 40    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   OHead
C    I     A   O    Tail
C   I   A   O
C   I   A   O
I   A   O
A   O
O
C     I   A   O0th
C     I       A   O    nth - 1
C          I        A   Onth[2]?   nth[1]?
C         I   A         Onth[2]?       nth[0]!
C   I   A   O                ?
●    C             I    tail “ciao” → “iao”                          A   O                                  ?
●    C             I    tail “ciao” → “iao”                          A   O                                  ?●   tail “iao...
●    C             I    tail “ciao” → “iao”                               A       O                                       ...
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::Linkclass List::Link with List {  has head => (       is => ro,       isa => Any   );  has tail => (       is => ro,...
List::Linkclass List::Link with List {  has head => (       is => ro,       isa => Any   );  has tail => (       is => ro,...
List::Linkclass List::Link with List {  has head => (       is => ro,       isa => Any   );  has tail => (       is => ro,...
List::Emptyclass List::Empty with List {   method head {     die "Cant take head of empty list!"   }   method tail {     d...
So we can write:my $list = List::Link->new(    head => c,    tail => List::Link->new(       head => i,       tail => List:...
Sugar!my $list = List->fromArray( qw/ c i a o /);
Multimethodsuse MooseX::MultiMethods;multi method fromArray ($class:) {  return List::Empty->new;}
Multimethodsuse MooseX::MultiMethods;multi method fromArray ($class:) {  return List::Empty->new;}
Multimethodsmulti method fromArray  ($class: $head, @tail) {    return List::Link->new(       head => $head,       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,       tai...
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)       ...
Eeek! Recursion!fromArray  List::Link->new  (..., 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)       ...
Eeek! Recursion!fromArray  List::Link->new  (..., fromArray)            List::Link->new            (..., fromArray)   = $l...
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 EliminationSub::Call::TailSub::Call::Recurby nothingmuch
Tail call eliminationfromArray
Tail call eliminationList::Link->new(..., fromArray)
Tail call eliminationList::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 Eliminationuse Sub::Import Sub::Call::Tail      tail => { -as => tail_call };multi method fromArray ($self: $hea...
Indexing into Listmulti method nth  (List::Empty $self: Int $pos){  die "Cant index into an Empty list";}
Indexing into List multi method nth  (Int $pos where { $_ == 0 }){  return $self->head;}
Indexing into Listmulti method nth  (Int $pos where { $_ > 0 }){  tail_call $self->tail->nth( $pos - 1 );}
C   I   A   OM
C   I   A   OM               W
C    I              A        OM                                W    Mutation leads to bugs    (and misspellings!)
C   I   A   OM   I   A   O   W
C   I   A   OC   I   B
C           I               A              OC           I              B    Copy everything upstream of a change    Downst...
C            I              A                 OC           I               B                 O    Doubly linked lists have...
pure-fp-book●   https://github.com/osfameron/pure-fp-book●   Purely Functional Data Structures for the...    ●   Impure   ...
Upcoming SlideShare
Loading in …5
×

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

864
-1

Published on

All new material, this time about one of the fundamental functional datastructures, the Linked List, and the overview of an implementation in Moosey Perl.

This covers some of the same material as https://github.com/osfameron/pure-fp-book but perhaps with more explanation (and covering much less material - it was only a 20 minute talk)

Published in: Technology, Design
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
864
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
9
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

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

  1. 1. Functional Pe(a)rls osfameron @ IPW2011, Turin the “purely functional data structures” editionhttp://www.fickr.com/photos/jef_saf/3493852795/
  2. 2. previously on Functional Pe(a)rls... (IPW, LPW, YAPC::EU, nwe.pm) currying operator references: op(+) Acme::Monads Devel::Declare
  3. 3. C I A O vs.C I A O
  4. 4. 0 1 2 3 C I A O
  5. 5. 0 1 2 3 C I A O0th element
  6. 6. 0 1 2 3 M I A O0th element
  7. 7. 0 1 2 3 M I A O 0th elementno kittens were harmed during the making of this presentation
  8. 8. 0 1 2 3 M I A O 2nd element
  9. 9. 0 1 2 3 M I A O 4th element
  10. 10. 0 1 2 3 M I A Omax: 3
  11. 11. 0 1 2 3 M I A Omax: 3
  12. 12. 0 1 2 3 4 M I A O Wmax: 4 4th element
  13. 13. max: 30 1 2 3 100,000M I A O …
  14. 14. 0 1 2 3 M I A Omax: 3
  15. 15. 0 1 2 3 4 M I A O Wmax: 40 1 2 3 4 5 M I A O W !max: 5
  16. 16. 0 1 2 3 4 M I A O Wmax: 40 1 2 3 4 5 M I A O W !max: 5
  17. 17. Arrays C I A O vs.C I A O
  18. 18. Perl @arrays C I A O “dynamic array” vs.C I A O
  19. 19. C I A O
  20. 20. C I A O
  21. 21. C I A OHead
  22. 22. C I A O Tail
  23. 23. C I A O
  24. 24. C I A O
  25. 25. I A O
  26. 26. A O
  27. 27. O
  28. 28. C I A O0th
  29. 29. C I A O nth - 1
  30. 30. C I A Onth[2]? nth[1]?
  31. 31. C I A Onth[2]? nth[0]!
  32. 32. C I A O ?
  33. 33. ● C I tail “ciao” → “iao” A O ?
  34. 34. ● C I tail “ciao” → “iao” A O ?● tail “iao” → “ao”● tail “ao” → “o”● tail “o” → ?
  35. 35. ● C I tail “ciao” → “iao” A O ?● tail “iao” → “ao”● tail “ao” → “o”● tail “o” → “” (the empty string)
  36. 36. C I A O
  37. 37. List =Head Tail (another List) or...
  38. 38. Here comes the science^wPerl!
  39. 39. Moose(X::Declare)use MooseX::Declare;BEGIN { role_type List }role List { requires head; requires tail;}
  40. 40. List::Linkclass List::Link with List { has head => ( is => ro, isa => Any ); has tail => ( is => ro, isa => List ),}
  41. 41. List::Linkclass List::Link with List { has head => ( is => ro, isa => Any ); has tail => ( is => ro, isa => List ),}
  42. 42. List::Linkclass List::Link with List { has head => ( is => ro, isa => Any ); has tail => ( is => ro, isa => List ),}
  43. 43. List::Emptyclass List::Empty with List { method head { die "Cant take head of empty list!" } method tail { die "Cant take tail of empty list!" }}
  44. 44. So we can write:my $list = List::Link->new( head => c, tail => List::Link->new( head => i, tail => List::Link->new(...
  45. 45. Sugar!my $list = List->fromArray( qw/ c i a o /);
  46. 46. Multimethodsuse MooseX::MultiMethods;multi method fromArray ($class:) { return List::Empty->new;}
  47. 47. Multimethodsuse MooseX::MultiMethods;multi method fromArray ($class:) { return List::Empty->new;}
  48. 48. Multimethodsmulti method fromArray ($class: $head, @tail) { return List::Link->new( head => $head, tail => $class->fromArray(@tail), );}
  49. 49. Eeek! Recursion!my $list = List::fromArray(1..1000000);
  50. 50. Eeek! Recursion!my $list = List::fromArray(1..1000000);Deep recursion on subroutine"List::fromArray" at foo.pl line 20
  51. 51. Eeek! Recursion!multi method fromArray ($class: $head, @tail) { return List::Link->new( head => $head, tail => $class->fromArray(@tail), );}
  52. 52. Eeek! Recursion!fromArray
  53. 53. Eeek! Recursion!fromArray List::Link->new (..., fromArray)
  54. 54. Eeek! Recursion!fromArray List::Link->new (..., fromArray) List::Link->new (..., fromArray)
  55. 55. Eeek! Recursion!fromArray List::Link->new (..., fromArray) List::Link->new (..., fromArray) List::Link->new (..., fromArray)
  56. 56. Eeek! Recursion!fromArray List::Link->new (..., fromArray) List::Link->new (..., fromArray) List::Link->new (..., fromArray) List::Link->new (..., fromArray)
  57. 57. Eeek! Recursion!fromArray List::Link->new (..., fromArray) List::Link->new (..., fromArray) List::Link->new (..., fromArray) List::Link->new (..., fromArray) = $list
  58. 58. Eeek! Recursion!fromArray List::Link->new (..., fromArray) List::Link->new (..., fromArray) List::Link->new (..., fromArray) = $list
  59. 59. Eeek! Recursion!fromArray List::Link->new (..., fromArray) List::Link->new (..., fromArray) = $list
  60. 60. Eeek! Recursion!fromArray List::Link->new (..., fromArray) = $list
  61. 61. Eeek! Recursion!fromArray = $list
  62. 62. Eeek! Recursion!no warnings recursion;$DB::deep = 100_000_000;
  63. 63. Eeek! Recursion!no warnings recursion;$DB::deep = 100_000_000;Papering over the cracks
  64. 64. Tail Call EliminationSub::Call::TailSub::Call::Recurby nothingmuch
  65. 65. Tail call eliminationfromArray
  66. 66. Tail call eliminationList::Link->new(..., fromArray)
  67. 67. Tail call eliminationList::Link->new(..., fromArray)
  68. 68. Tail call elimination List::Link->new (..., fromArray)
  69. 69. Tail call elimination List::Link->new (..., fromArray)
  70. 70. Tail call elimination List::Link->new (..., fromArray) = $list
  71. 71. Tail Call Eliminationuse 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), );}
  72. 72. Indexing into Listmulti method nth (List::Empty $self: Int $pos){ die "Cant index into an Empty list";}
  73. 73. Indexing into List multi method nth (Int $pos where { $_ == 0 }){ return $self->head;}
  74. 74. Indexing into Listmulti method nth (Int $pos where { $_ > 0 }){ tail_call $self->tail->nth( $pos - 1 );}
  75. 75. C I A OM
  76. 76. C I A OM W
  77. 77. C I A OM W Mutation leads to bugs (and misspellings!)
  78. 78. C I A OM I A O W
  79. 79. C I A OC I B
  80. 80. C I A OC I B Copy everything upstream of a change Downstream of changes can be shared
  81. 81. C I A OC I B O Doubly linked lists have no downstream!
  82. 82. 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

×