Skip to content

Commit

Permalink
Merge pull request #4 from lucaong/fixes_for_crystal_0.17
Browse files Browse the repository at this point in the history
Fixes for crystal 0.17
  • Loading branch information
lucaong committed May 18, 2016
2 parents 796fbbe + c359dd4 commit a813f01
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 36 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,15 +65,15 @@ end

```crystal
# Map behaves mostly like a Hash:
map = Immutable::Map[{ foo: 1, bar: 2 }] # => Map {foo: 1, bar: 2}
map = Immutable::Map[{:a => 1, :b => 2 }] # => Map {:a => 1, :b => 2}
map[:foo] # => 1
# Updating a Map always returns a modified copy:
map2 = map.set(:baz, 3) # => Map {foo: 1, bar: 2, baz: 3}
map2 = map2.delete(:bar) # => Map {foo: 1, baz: 3}
map2 = map.set(:c, 3) # => Map {:a => 1, :b => 2, :c => 3}
map2 = map2.delete(:b) # => Map {:a => 1, :c => 3}
# The original map in unchanged:
map # => Map {foo: 1, bar: 2}
map # => Map {:a => 1, :b => 2}
# Bulk updates can be made faster by using `transient`:
map3 = Immutable::Map(String, Int32)[]
Expand All @@ -88,7 +88,7 @@ end
# Nested arrays/hashes can be turned into immutable versions with the `.from`
# method:
nested = Immutable.from({ name: "Ada", colors: [:blue, :green, :red] })
nested = Immutable.from({:name => "Ada", :colors => [:blue, :green, :red] })
nested # => Map {:name => "Ada", :colors => Vector [:blue, :green, :red]}
```

Expand Down
14 changes: 7 additions & 7 deletions spec/immutable/map/transient_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,27 @@ describe Immutable::Map::Transient do

describe "#delete" do
it "deletes a key-value pair" do
t = Immutable::Map::Transient(Symbol, Int32).new({foo: 1, bar: 2})
t = Immutable::Map::Transient(Symbol, Int32).new({:foo => 1, :bar => 2})
t.delete(:foo).fetch(:foo, nil).should eq(nil)
end
end

describe "#merge" do
it "merges key-value pairs" do
t = Immutable::Map::Transient(Symbol, Int32).new({foo: 1, bar: 2})
t.merge({baz: 3})[:baz].should eq(3)
t = Immutable::Map::Transient(Symbol, Int32).new({:foo => 1, :bar => 2})
t.merge({:baz => 3})[:baz].should eq(3)
end
end

describe "#persist!" do
it "returns a persistent immutable vector unaffected by further changes" do
tr = Immutable::Map::Transient(Symbol, Int32).new({foo: 1, bar: 2})
tr = Immutable::Map::Transient(Symbol, Int32).new({:foo => 1, :bar => 2})
m = tr.persist!
m.should be_a(Immutable::Map(Symbol, Int32))
m.should_not be_a(Immutable::Map::Transient(Symbol, Int32))
tr = tr.delete(:foo).set(:baz, 3).merge({qux: 4, quux: 5})
tr.to_h.should eq({bar: 2, baz: 3, qux: 4, quux: 5})
m.to_h.should eq({foo: 1, bar: 2})
tr = tr.delete(:foo).set(:baz, 3).merge({:qux => 4, :quux => 5})
tr.to_h.should eq({:bar => 2, :baz => 3, :qux => 4, :quux => 5})
m.to_h.should eq({:foo => 1, :bar => 2})
end
end
end
18 changes: 9 additions & 9 deletions spec/immutable/map_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ describe Immutable do
end

it "with a hash, it creates and initialize an immutable map" do
m = Immutable::Map.new({foo: 123})
m = Immutable::Map.new({:foo => 123})
m[:foo].should eq(123)
end

it "with a hash and a block, it creates and initialize a map with default" do
m = Immutable::Map.new({foo: 123}) { |k| 42 }
m = Immutable::Map.new({:foo => 123}) { |k| 42 }
m[:foo].should eq(123)
m[:xxx].should eq(42)
end
Expand All @@ -37,7 +37,7 @@ describe Immutable do

describe ".[]" do
it "it creates and initialize an immutable map" do
m = Immutable::Map[{foo: 123, bar: 321}]
m = Immutable::Map[{:foo => 123, :bar => 321}]
m[:foo].should eq(123)
end
end
Expand Down Expand Up @@ -189,30 +189,30 @@ describe Immutable do
end

it "returns true for equal maps" do
m1 = Immutable::Map.new({foo: 123})
m2 = Immutable::Map.new({foo: 123})
m1 = Immutable::Map.new({:foo => 123})
m2 = Immutable::Map.new({:foo => 123})
(m1 == m2).should eq(true)
end
end

describe "#inspect" do
it "returns a string representation of the map" do
m = Immutable::Map.new({foo: 123, bar: 321})
m = Immutable::Map.new({:foo => 123, :bar => 321})
m.inspect.should eq("Map {:foo => 123, :bar => 321}")
end
end

describe "#to_a" do
it "returns an array of entries" do
m = Immutable::Map.new({foo: 123, bar: 321})
m = Immutable::Map.new({:foo => 123, :bar => 321})
m.to_a.should eq([{:foo, 123}, {:bar, 321}])
end
end

describe "#to_h" do
it "returns a hash of the same entries" do
m = Immutable::Map.new({foo: 123, bar: 321})
m.to_h.should eq({foo: 123, bar: 321})
m = Immutable::Map.new({:foo => 123, :bar => 321})
m.to_h.should eq({:foo => 123, :bar => 321})
end
end

Expand Down
8 changes: 4 additions & 4 deletions spec/immutable_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,16 @@ describe Immutable do

describe ".map" do
it "creates a map of the given key-values" do
Immutable.map({foo: "bar"}).should eq(Immutable::Map.new({foo: "bar"}))
Immutable.map({:foo => "bar"}).should eq(Immutable::Map.new({:foo => "bar"}))
end
end

describe ".from" do
it "creates immutable objects by deeply traversing the given object" do
x = Immutable.from({foo: [1, 2, 3], bar: {baz: 1}})
x = Immutable.from({:foo => [1, 2, 3], :bar => {:baz => 1}})
y = Immutable.map({
foo: Immutable.vector([1, 2, 3]),
bar: Immutable.map({baz: 1}),
:foo => Immutable.vector([1, 2, 3]),
:bar => Immutable.map({:baz => 1}),
})
x.should eq(y)
end
Expand Down
22 changes: 11 additions & 11 deletions src/immutable/map.cr
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
# A map can be constructed from a hash:
#
# ```
# Immutable::Map(Symbol, Int32).new # => Map {}
# Immutable::Map.new({ foo: 1, bar: 2 }) # => Map {:foo => 1, :bar => 2}
# Immutable::Map[{ foo: 1, bar: 2 }] # => Map {:foo => 1, :bar => 2}
# Immutable::Map(Symbol, Int32).new # => Map {}
# Immutable::Map.new({:foo => 1, :bar => 2}) # => Map {:foo => 1, :bar => 2}
# Immutable::Map[{:foo => 1, :bar => 2 }] # => Map {:foo => 1, :bar => 2}
# ```
#
# `Immutable::Map` works similarly to a regular hash, except it never mutates in
Expand All @@ -31,7 +31,7 @@ module Immutable
# Creates a map with the given key-values
#
# ```
# m = Immutable::Map.new({ foo: 123, bar: 321 }) # Map {foo: 123, bar: 321}
# m = Immutable::Map.new({:a => 1, :b => true}) # Map {:a => 1, :b => true}
# ```
def initialize(hash : Hash(K, V) = {} of K => V)
@trie = hash.reduce(Trie(K, V).empty(object_id)) do |trie, k, v|
Expand All @@ -46,7 +46,7 @@ module Immutable
# return value is returned.
#
# ```
# m = Immutable::Map.new({ foo: 123, bar: 321 }) # Map {foo: 123, bar: 321}
# m = Immutable::Map.new({:a => 123, :b => 321 }) # Map {:a => 123, :b => 321}
# ```
def initialize(hash : Hash(K, V) = {} of K => V, &block : K -> V)
@trie = hash.reduce(Trie(K, V).empty(object_id)) do |trie, k, v|
Expand All @@ -63,7 +63,7 @@ module Immutable
# Creates a map with the given key-values.
#
# ```
# m = Immutable::Map.new([{:foo, 123}, {:bar, 321}]) # Map {foo: 123, bar: 321}
# m = Immutable::Map.new([{:a, 123}, {:b, 321}]) # Map {:a => 123, :b => 321}
# ```
def self.new(e : Enumerable(Enumerable(U)))
t = e.reduce(Transient(typeof(e.first[0]), typeof(e.first[1])).new) do |m, kv|
Expand All @@ -75,7 +75,7 @@ module Immutable
# Creates a map with the given key-values
#
# ```
# m = Immutable::Map[{ foo: 123, bar: 321 }] # Map {foo: 123, bar: 321}
# m = Immutable::Map[{:a => 123, :b => 321}] # Map {:a => 123, :b => 321}
# ```
def self.[](hash : Hash(K, V) = {} of K => V)
new(hash)
Expand Down Expand Up @@ -145,7 +145,7 @@ module Immutable
# Returns a modified copy of the map where key is associated to value
#
# ```
# m = Immutable::Map[{ foo: 123 }]
# m = Immutable::Map[{:foo => 123}]
# m2 = m.set(:bar, 321) # => Map {:foo => 123, :bar => 321}
# m # => Map {:foo => 123}
# ```
Expand All @@ -158,7 +158,7 @@ module Immutable
# key is not existing, it raises `KeyError`
#
# ```
# m = Immutable::Map[{ foo: 123, bar: 321 }]
# m = Immutable::Map[{:foo => 123, :bar => 321 }]
# m2 = m.delete(:bar) # => Map {:foo => 123}
# m # => Map {:foo => 123, bar: 321}
# ```
Expand All @@ -172,7 +172,7 @@ module Immutable
#
# ```
# map = Immutable::Map[{"foo" => "bar"}]
# merged = map.merge({"baz": "qux"})
# merged = map.merge({"baz" => "qux"})
# merged # => Map {"foo" => "bar", "baz" => "qux"}
# map # => Map {"foo" => "bar"}
# ```
Expand All @@ -189,7 +189,7 @@ module Immutable
#
# ```
# map = Immutable::Map[{"foo" => "bar"}]
# merged = map.merge(Immutable::Map[{"baz": "qux"}])
# merged = map.merge(Immutable::Map[{"baz" => "qux"}])
# merged # => Map {"foo" => "bar", "baz" => "qux"}
# map # => Map {"foo" => "bar"}
# ```
Expand Down

0 comments on commit a813f01

Please sign in to comment.