Clojure Data Structures - Part One

Clojure Data Structures - Part One

An introductory presentation to collection types in Clojure and the underlying design principles.

891a8ee280b3722a080a7f7e47b53417?s=128

Balint Erdi

June 18, 2013
Tweet

Transcript

  1. Clojure Data Structures Part One Wednesday, June 19, 13

  2. Selling points • Dynamic programming language • Functional programming (Lisp

    dialect) • Pragmatic: runs on the JVM • Pragmatic: easy interop. with Java libs • Strong concurrency support Wednesday, June 19, 13
  3. Selling points • Dynamic programming language • Functional programming (Lisp

    dialect) • Pragmatic: runs on the JVM • Pragmatic: easy interop. with Java libs • Strong concurrency support Wednesday, June 19, 13
  4. In Clojure • All data structures are immutable • Functions

    return copies of the original • Copies share structure and are very performant • “persistent” data structures Wednesday, June 19, 13
  5. Collection types Wednesday, June 19, 13

  6. List • ‘(:alice :bob :carol) • The classic from Lisp

    • Fast access to • first item • rest of the list Wednesday, June 19, 13
  7. Vector • [:alice :bob :carol] • Fast access to •

    any item in the list by its index • associative Wednesday, June 19, 13
  8. Map • {:alice 24 :bob 34} • Fast access to

    • any value by its key • associative Wednesday, June 19, 13
  9. Set • #{:alice :bob} • Fast membership check Wednesday, June

    19, 13
  10. The sequence abstraction 1. • “It is better to have

    1 data structure and 100 functions than 10 data types and 10 functions” • Clojure adheres to the Lisp philosophy: • sequence abstraction • all collection types are “sequable” Wednesday, June 19, 13
  11. The sequence abstraction 2. • all sequence functions can be

    used on all basic data structures • first, rest, map, filter, some, reduce • +30 more • some collection-specific functions • e.g subvec for vectors Wednesday, June 19, 13
  12. REPL demo • “Adding” an item • “Deleting” an item

    • Getting an arbitrary item (association) • Getting an item more succinctly Wednesday, June 19, 13
  13. Do you know Javascript? Wednesday, June 19, 13

  14. Do you really know Javascript? Wednesday, June 19, 13

  15. Do you really know Javascript? • x = [1,2,3] Wednesday,

    June 19, 13
  16. Do you really know Javascript? • x = [1,2,3] •

    y = x.push(4) Wednesday, June 19, 13
  17. Do you really know Javascript? • x = [1,2,3] •

    y = x.push(4) • x ? Wednesday, June 19, 13
  18. Do you really know Javascript? • x = [1,2,3] •

    y = x.push(4) • x ? • [1,2,3,4] Wednesday, June 19, 13
  19. Do you really know Javascript? • x = [1,2,3] •

    y = x.push(4) • x ? • [1,2,3,4] • y ? Wednesday, June 19, 13
  20. Do you really know Javascript? • x = [1,2,3] •

    y = x.push(4) • x ? • [1,2,3,4] • y ? • 4 Wednesday, June 19, 13
  21. Do you really know Javascript? Wednesday, June 19, 13

  22. Do you really know Javascript? • x = [1,2,3] Wednesday,

    June 19, 13
  23. Do you really know Javascript? • x = [1,2,3] •

    y = x.concat([4]) Wednesday, June 19, 13
  24. Do you really know Javascript? • x = [1,2,3] •

    y = x.concat([4]) • x ? Wednesday, June 19, 13
  25. Do you really know Javascript? • x = [1,2,3] •

    y = x.concat([4]) • x ? • [1,2,3] - did not change Wednesday, June 19, 13
  26. Do you really know Javascript? • x = [1,2,3] •

    y = x.concat([4]) • x ? • [1,2,3] - did not change • y ? Wednesday, June 19, 13
  27. Do you really know Javascript? • x = [1,2,3] •

    y = x.concat([4]) • x ? • [1,2,3] - did not change • y ? • [1,2,3,4] Wednesday, June 19, 13
  28. Do you really know Javascript? Wednesday, June 19, 13

  29. Do you really know Javascript? • x = [1,2] Wednesday,

    June 19, 13
  30. Do you really know Javascript? • x = [1,2] •

    y = [3,4] Wednesday, June 19, 13
  31. Do you really know Javascript? • x = [1,2] •

    y = [3,4] • z = x + y Wednesday, June 19, 13
  32. Do you really know Javascript? • x = [1,2] •

    y = [3,4] • z = x + y • z ? Wednesday, June 19, 13
  33. Do you really know Javascript? • x = [1,2] •

    y = [3,4] • z = x + y • z ? • “1,23,4” Wednesday, June 19, 13
  34. Do you really know Javascript? • x = [1,2] •

    y = [3,4] • z = x + y • z ? • “1,23,4” • WAT? Wednesday, June 19, 13
  35. Takeaways • All data structures in Clojure are immutable •

    A huge win for concurrency • Functions return copies of the original • The copies are very efficient ones • Collection types implement the sequable interface => “Few data types, lots of functions operating on them” Wednesday, June 19, 13
  36. Further possible topics • Higher-level collection functions • Lazy seqs

    • Persistent data structures • Homoiconicity Wednesday, June 19, 13
  37. Thank you. @baaz balinterdi.com Wednesday, June 19, 13