Skip to content

Commit

Permalink
Removed Hash#reduce redefinition for next version
Browse files Browse the repository at this point in the history
Ary Borenszweig committed Jun 10, 2016

Verified

This commit was signed with the committer’s verified signature. The key has expired.
andir Andreas Rammhold
1 parent 979e63c commit 46317ac
Showing 2 changed files with 60 additions and 39 deletions.
45 changes: 32 additions & 13 deletions spec/std/hash_spec.cr
Original file line number Diff line number Diff line change
@@ -678,22 +678,41 @@ describe "Hash" do
end

describe "reduce" do
it "passes memo, key and value into block" do
hash = {:a => 'b'}
hash.reduce(:memo) do |memo, k, v|
memo.should eq(:memo)
k.should eq(:a)
v.should eq('b')
{% if Crystal::VERSION == "0.18.0" %}
it "passes memo, key and value into block" do
hash = {:a => 'b'}
hash.reduce(:memo) do |memo, (k, v)|
memo.should eq(:memo)
k.should eq(:a)
v.should eq('b')
end
end
end

it "reduces the hash to the accumulated value of memo" do
hash = {:a => 'b', :c => 'd', :e => 'f'}
result = hash.reduce("") do |memo, k, v|
memo + v
it "reduces the hash to the accumulated value of memo" do
hash = {:a => 'b', :c => 'd', :e => 'f'}
result = hash.reduce("") do |memo, (k, v)|
memo + v
end
result.should eq("bdf")
end
result.should eq("bdf")
end
{% else %}
it "passes memo, key and value into block" do
hash = {:a => 'b'}
hash.reduce(:memo) do |memo, k, v|
memo.should eq(:memo)
k.should eq(:a)
v.should eq('b')
end
end

it "reduces the hash to the accumulated value of memo" do
hash = {:a => 'b', :c => 'd', :e => 'f'}
result = hash.reduce("") do |memo, k, v|
memo + v
end
result.should eq("bdf")
end
{% end %}
end

describe "reject" do
54 changes: 28 additions & 26 deletions src/hash.cr
Original file line number Diff line number Diff line change
@@ -902,33 +902,35 @@ class Hash(K, V)
!empty?
end

# Yields all key-value pairs to the given block with a initial value *memo*,
# which is replaced with each returned value in iteration.
# Returns the last value of *memo*.
#
# ```
# prices = {
# "apple": 5,
# "lemon": 3,
# "papaya": 6,
# "orange": 4,
# }
#
# prices.reduce("apple") do |highest, item, price|
# if price > prices[highest]
# item
# else
# highest
# end
# end
# # => "papaya"
# ```
def reduce(memo)
each do |k, v|
memo = yield(memo, k, v)
{% if Crystal::VERSION != "0.18.0" %}
# Yields all key-value pairs to the given block with a initial value *memo*,
# which is replaced with each returned value in iteration.
# Returns the last value of *memo*.
#
# ```
# prices = {
# "apple": 5,
# "lemon": 3,
# "papaya": 6,
# "orange": 4,
# }
#
# prices.reduce("apple") do |highest, item, price|
# if price > prices[highest]
# item
# else
# highest
# end
# end
# # => "papaya"
# ```
def reduce(memo)
each do |k, v|
memo = yield(memo, k, v)
end
memo
end
memo
end
{% end %}

protected def find_entry(key)
index = bucket_index key

0 comments on commit 46317ac

Please sign in to comment.