Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Clojure Data Structures - Part One

Clojure Data Structures - Part One

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

Balint Erdi

June 18, 2013
Tweet

More Decks by Balint Erdi

Other Decks in Technology

Transcript

  1. 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
  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. 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
  4. List • ‘(:alice :bob :carol) • The classic from Lisp

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

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

    • any value by its key • associative Wednesday, June 19, 13
  7. 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
  8. 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
  9. REPL demo • “Adding” an item • “Deleting” an item

    • Getting an arbitrary item (association) • Getting an item more succinctly Wednesday, June 19, 13
  10. Do you really know Javascript? • x = [1,2,3] •

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

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

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

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

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

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

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

    y = x.concat([4]) • x ? • [1,2,3] - did not change Wednesday, June 19, 13
  18. 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
  19. 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
  20. Do you really know Javascript? • x = [1,2] •

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

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

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

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

    y = [3,4] • z = x + y • z ? • “1,23,4” • WAT? Wednesday, June 19, 13
  25. 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
  26. Further possible topics • Higher-level collection functions • Lazy seqs

    • Persistent data structures • Homoiconicity Wednesday, June 19, 13