Module: Immutable

Defined in:
lib/immutable/set.rb,
lib/immutable/hash.rb,
lib/immutable/trie.rb,
lib/immutable/deque.rb,
lib/immutable/nested.rb,
lib/immutable/vector.rb,
lib/immutable/version.rb,
lib/immutable/undefined.rb,
lib/immutable/enumerable.rb,
lib/immutable/sorted_set.rb

Defined Under Namespace

Modules: Enumerable, Undefined Classes: Deque, Hash, Set, SortedSet, Trie, Vector

Constant Summary collapse

EmptySet =

The canonical empty Set. Returned by Set[] when invoked with no arguments; also returned by Set.empty. Prefer using this one rather than creating many empty sets using Set.new.

Immutable::Set.empty
EmptyHash =

The canonical empty Hash. Returned by Hash[] when invoked with no arguments; also returned by Hash.empty. Prefer using this one rather than creating many empty hashes using Hash.new.

Immutable::Hash.empty
EmptyTrie =
Trie.new(0)
EmptyDeque =

The canonical empty Deque. Returned by Deque[] when invoked with no arguments; also returned by Deque.empty. Prefer using this one rather than creating many empty deques using Deque.new.

Immutable::Deque.empty
EmptyVector =

The canonical empty Vector. Returned by Vector[] when invoked with no arguments; also returned by Vector.empty. Prefer using this one rather than creating many empty vectors using Vector.new.

Immutable::Vector.empty
VERSION =

Current released gem version. Note that master will often have the same value as a release gem but with different code.

"0.0.1"
EmptySortedSet =

The canonical empty SortedSet. Returned by SortedSet[] when invoked with no arguments; also returned by SortedSet.empty. Prefer using this one rather than creating many empty sorted sets using SortedSet.new.

Immutable::SortedSet.empty

Class Method Summary collapse

Class Method Details

.from(obj) ⇒ Hash, ...

Create a nested Immutable data structure from a nested Ruby object obj. This method recursively “walks” the Ruby object, converting Ruby Hash to Hash, Ruby Array to Vector, Ruby Set to Set, and Ruby SortedSet to SortedSet. Other objects are left as-is.

Examples:

h = Immutable.from({ "a" => [1, 2], "b" => "c" })
# => Immutable::Hash["a" => Immutable::Vector[1, 2], "b" => "c"]

Returns:



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/immutable/nested.rb', line 23

def from(obj)
  case obj
  when ::Hash
    res = obj.map { |key, value| [from(key), from(value)] }
    Immutable::Hash.new(res)
  when Immutable::Hash
    obj.map { |key, value| [from(key), from(value)] }
  when ::Array
    res = obj.map { |element| from(element) }
    Immutable::Vector.new(res)
  when ::SortedSet
    # This clause must go before ::Set clause, since ::SortedSet is a ::Set.
    res = obj.map { |element| from(element) }
    Immutable::SortedSet.new(res)
  when ::Set
    res = obj.map { |element| from(element) }
    Immutable::Set.new(res)
  when Immutable::Vector, Immutable::Set, Immutable::SortedSet
    obj.map { |element| from(element) }
  else
    obj
  end
end

.to_ruby(obj) ⇒ ::Hash, ...

Create a Ruby object from Immutable data. This method recursively “walks” the Immutable object, converting Hash to Ruby Hash, Vector and Deque to Ruby Array, Set to Ruby Set, and SortedSet to Ruby SortedSet. Other objects are left as-is.

Examples:

h = Immutable.to_ruby(Immutable.from({ "a" => [1, 2], "b" => "c" }))
# => { "a" => [1, 2], "b" => "c" }

Returns:

  • (::Hash, ::Array, ::Set, ::SortedSet, Object)


58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/immutable/nested.rb', line 58

def to_ruby(obj)
  case obj
  when Immutable::Hash, ::Hash
    obj.each_with_object({}) { |keyval, hash| hash[to_ruby(keyval[0])] = to_ruby(keyval[1]) }
  when Immutable::Vector, ::Array
    obj.each_with_object([]) { |element, arr| arr << to_ruby(element) }
  when Immutable::Set, ::Set
    obj.each_with_object(::Set.new) { |element, set| set << to_ruby(element) }
  when Immutable::SortedSet, ::SortedSet
    obj.each_with_object(::SortedSet.new) { |element, set| set << to_ruby(element) }
  when Immutable::Deque
    obj.to_a.tap { |arr| arr.map! { |element| to_ruby(element) }}
  else
    obj
  end
end