The Hamster Gem - Ryan Mulligan

The Hamster Gem - Ryan Mulligan

B044a0f039af800f4df09bf3b2465f18?s=128

Las Vegas Ruby Group

July 16, 2014
Tweet

Transcript

  1. “Efficient, immutable, and thread-safe collection classes for Ruby.” the hamster

    gem
  2. “Efficient, immutable, and thread-safe collection classes for Ruby.”

  3. “Efficient, immutable, and thread-safe collection classes for Ruby.”

  4. containers for objects

  5. tuple (1, 2, 3)

  6. set {1, 2, 3}

  7. hash {:name => "James", :gender => :male}

  8. vector (like array) [1, 2, 3, 4, 5]

  9. stack User:Boivie http://en.wikipedia.org/wiki/Stack_(abstract_data_type)#mediaviewer/File:Data_stack.svg

  10. queue User:Vegpuff on wikipedia http://en.wikipedia.org/wiki/Queue_(abstract_data_type)#mediaviewer/File:Data_Queue.svg

  11. (linked) list

  12. “Efficient, immutable, and thread-safe collection classes for Ruby.”

  13. immutability

  14. require "hamster" person = Hamster.hash(:name => "Simon", :gender => :male)

    person[:name] # => "Simon" friend = person.put(:name, "James") # => {:name => "James", :gender => :male} male = person.delete(:name) # => {:gender => :male} person # => {:name => "Simon", :gender => :male} male.key?(:name) # => false
  15. the sales pitch

  16. immutability is a good thing™

  17. Mutable stateful objects are the new spaghetti code: - hard

    to understand, test, reason about - Concurrency disaster Rich Hickey - Clojure “
  18. Immutable objects are simple. Classes should be immutable unless there’s

    a very good reason to make them mutable. If a class cannot be made immutable, limit its mutability as much as possible. Josh Bloch, in Effective Java “
  19. you are using immutable data

  20. git, bitcoin blockchain, :ruby

  21. benefits readability concurrency (thread-safety)

  22. “Efficient, immutable, and thread-safe collection classes for Ruby.”

  23. person = Hamster.hash( :name => "Simon", :gender => :male, #...

    1000 other key-value pairs ) person[:name] # => "Simon" friend = person.put(:name, "James") # => {:name => "James", :gender => :male, #...} Does friend have to copy everything from person?
  24. NO!

  25. “Efficient, immutable, and thread-safe collection classes for Ruby.” NO!

  26. “Persistent, immutable, and thread-safe collection classes for Ruby.” NO!

  27. zs = xs + ys

  28. None
  29. adding e

  30. “Efficient, immutable, and thread-safe collection classes for Ruby.”

  31. immutability => thread safety

  32. warning: references immutable but contents might be mutable

  33. require 'hamster' v = "A" h = Hamster.hash(:k=>v) #=>{:k =>

    "A"} v[0] = "B" h #=> {:k => "B"}
  34. “Efficient, immutable, and thread-safe collection classes for Ruby.”

  35. references https://deveo.com/blog/2013/03/22/immutability-in-ruby-part-1/ https://deveo.com/blog/2013/03/28/immutability-in-ruby-part-2/ http://clojure.org/state http://en.wikipedia.org/wiki/Persistent_data_structure https://github.com/hamstergem/hamster slides https://github.com/ryantm/lvrug_hamster