Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Purely functional data structures demystified

2,661 views

Published on

I spoke about 'Purely Functional Data structures' at Functional Conference 2014 in Bangalore. These are my slides with an extra section on further study.

This talk is based on Chris Okasaki's book title Purely Functional Data Structures. The gist is that immutable and persistent data structures can be designed without sacrificing performance.

Published in: Technology

Purely functional data structures demystified

  1. 1. Purely Functional Data Structures Demystified Mohit Thatte Functional Conf 2014, Bangalore
  2. 2. About Me Work The great outdoors @mohitthatte github.com/pastafari
  3. 3. Why? What? Learnings
  4. 4. Image: User:Matthew,Wikimedia Commons
  5. 5. Programs that use Maps Towers of Abstraction Map as ADT Map implementation Primitives…
  6. 6. Image: https://www.flickr.com/photos/cverdier/4321365261
  7. 7. Any sufficiently advanced data structure is indistinguishable from magic With apologies to Arthur C. Clarke!
  8. 8. Why? What? Learnings
  9. 9. Data Structures
  10. 10. AbstractionsData
  11. 11. Abstract Data Type Invariants Stack push, pop LIFO Name Operations ADT
  12. 12. Image: Apollo 8,NASA
  13. 13. Change perspective Think like a data structure designer
  14. 14. The ADT Challenge Given an ADT, give a correct implementation of all operations.
  15. 15. Source: https://www.flickr.com/photos/manicstreetpreacher/5310984196/
  16. 16. “You can program in any language you want… as long as it is imperative”
  17. 17. make it easier to reason about code Why Functional? Immutability Persistence
  18. 18. x = 5 y = x + 1 pr!nt x x = [1,2] y = x << 3 pr!nt x 5 is immutable [1,2] is NOT!
  19. 19. x = [1,2] foo(x) x ("et [x [1 2]] (foo x) x) What is x now? What is x now?
  20. 20. The ADT Challenge Given an ADT, give a correct and functional implementation of all operations.
  21. 21. Performance matters too. A lot.
  22. 22. The ADT Challenge Correct Performant Functional X
  23. 23. The ADT Challenge Given an ADT, give a correct, functional and performant implementation of all operations.
  24. 24. Challenge Accepted!
  25. 25. Why? What? Learnings
  26. 26. 3 -2 3 -2 The Pair cons (def x (cons 3 -2)) (head x) (ta!" x) 3 -2 head ta!"
  27. 27. Cons, the Magnificent (defn ta!" [z] (z (fn [p q] q))) (defn head [z] (z (fn [p q] p))) (defn cons [x y] (fn [m] (m x y)))
  28. 28. Voila, a list! 3 2 1 nil (cons 3 (cons 2 (cons 1 n!")))
  29. 29. And so it goes… Pair List Sets …
  30. 30. A functional ‘update’
  31. 31. 3 2 1 nil 3 4 Structural Sharing
  32. 32. Queue ADT enqueue peek dequeue append an e"ement f!rst e"ement rema!n!ng e"ements Invar!ant FIFO Operat!ons
  33. 33. Queue ADT
  34. 34. Ideas? Structural Decomposition Use a List
  35. 35. Pattern Matching
  36. 36. Correct Functional Performant
  37. 37. Use 2 lists… Be clever!
  38. 38. Correct Functional Performant
  39. 39. Amortized Analysis Care about a sequence of operations rather than worst case Earn credits each time you don’t do an expensive operation, spend credits when you do
  40. 40. Why? What? Learnings
  41. 41. Map ADT put get assoc!ate a key w!th va"ue "ookup key, return va"ue de"ete remove key & va"ue Invar!ant Operat!ons what you put !s what you get
  42. 42. Map ADT
  43. 43. Ideas?
  44. 44. Use a list? Nope. Too slow.
  45. 45. Use a Tree Yep!
  46. 46. Source: Wikipedia
  47. 47. All is well… 13 15 17 21 11 9 12
  48. 48. 13 … until this happens 15 17 21
  49. 49. स"तuलन का होना ब,त ज़.री 1 !
  50. 50. Balance is essential
  51. 51. Red Black Tree No red node can have a red child Every path from root to an empty node contains the same number of black nodes Guibas, Sedgewick 1978 Each node is colored Red or Black
  52. 52. Red Black Tree Source: Wikipedia
  53. 53. Red Black Tree Source: Wikipedia 7
  54. 54. Red Black Tree Source: Wikipedia 7
  55. 55. Red Black Tree Source: Wikipedia 7
  56. 56. Red Black Tree Source: Wikipedia 7
  57. 57. Red Black Tree Source: Wikipedia 7
  58. 58. Red Black Tree Source: Wikipedia 7
  59. 59. z a b c d a b y x y x z c d y x z x y z a b c d a b c d a b c d y z x
  60. 60. Pattern Matching is awesome!
  61. 61. Persistent Bit-Partitioned Vector Trie Phil Bagwell, Rich Hickey
  62. 62. Source: Wikipedia
  63. 63. Image: Jean Niklas L’orange http://hypirion.com/musings/understanding-persistent-vector-pt-1
  64. 64. Why? What? Learnings
  65. 65. Abstraction is vital
  66. 66. Study implementations to understand tradeoffs.
  67. 67. Structural Decomposition Structural Sharing
  68. 68. Amortized Analysis ! Laziness
  69. 69. Extreme Cleverness DJ Spiewak ©
  70. 70. Further study
  71. 71. Persistent vectors in Clojure http://hypirion.com/musings/understanding-persistent-vector-pt-1
  72. 72. Red Black Trees http://www.eternallyconfuzzled.com/tuts/datastructures/jsw_tut_rbtree.aspx Julienne Walker
  73. 73. Persistent Data Structures http://debasishg.blogspot.in/2010/06/functional-data-structures-in.html
  74. 74. Coursera https://www.coursera.org/course/algo Algorithms, Tim Roughgarden https://www.coursera.org/course/algs4partI Algorithms, Wayne &Sedgewick

×