Functional Pe(a)rls - the Purely Functional Datastructures edition
Upcoming SlideShare
Loading in...5
×

Like this? Share it with your network

Share

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

  • 884 views
Uploaded 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....

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)

More in: Technology , Design
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
    Be the first to like this
No Downloads

Views

Total Views
884
On Slideshare
884
From Embeds
0
Number of Embeds
0

Actions

Shares
Downloads
8
Comments
0
Likes
0

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

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