Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use yield #232

Open
wants to merge 2 commits into
base: core
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 12 additions & 7 deletions lib/hamster/enumerable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,27 +23,27 @@ def compact

# Search the collection for elements which are `#===` to `item`. Yield them to
# the optional code block if provided, and return them as a new collection.
def grep(pattern, &block)
def grep(pattern)
result = select { |item| pattern === item }
result = result.map(&block) if block_given?
result = result.map { |i| yield(i) } if block_given?
result
end

# Search the collection for elements which are not `#===` to `item`. Yield
# them to the optional code block if provided, and return them as a new
# collection.
def grep_v(pattern, &block)
def grep_v(pattern)
result = select { |item| !(pattern === item) }
result = result.map(&block) if block_given?
result = result.map { |i| yield(i) } if block_given?
result
end

# Yield all integers from 0 up to, but not including, the number of items in
# this collection. For collections which provide indexed access, these are all
# the valid, non-negative indices into the collection.
def each_index(&block)
def each_index
return enum_for(:each_index) unless block_given?
0.upto(size-1, &block)
0.upto(size-1) { |i| yield(i) }
self
end

Expand Down Expand Up @@ -153,7 +153,12 @@ def pretty_print(pp)
# @private
def sort_by(&block)
result = to_a
result.frozen? ? result.sort_by(&block) : result.sort_by!(&block)

if result.frozen?
result.sort_by { |i| yield(i) }
else
result.sort_by! { |i| yield(i) }
end
end
end
end
Expand Down
12 changes: 6 additions & 6 deletions lib/hamster/hash.rb
Original file line number Diff line number Diff line change
Expand Up @@ -332,9 +332,9 @@ def delete(key)
#
# @yield [key, value] Once for each key/value pair.
# @return [self]
def each(&block)
def each
return to_enum if not block_given?
@trie.each(&block)
@trie.each { |entry| yield(entry) }
self
end
alias :each_pair :each
Expand All @@ -352,9 +352,9 @@ def each(&block)
#
# @yield [key, value] Once for each key/value pair.
# @return [self]
def reverse_each(&block)
def reverse_each
return enum_for(:reverse_each) if not block_given?
@trie.reverse_each(&block)
@trie.reverse_each { |entry| yield(entry) }
self
end

Expand Down Expand Up @@ -424,9 +424,9 @@ def map
# @yield [key, value] Once for each key/value pair.
# @yieldreturn Truthy if this pair should be present in the new `Hash`.
# @return [Hash]
def select(&block)
def select
return enum_for(:select) unless block_given?
derive_new_hash(@trie.select(&block))
derive_new_hash(@trie.select { |entry| yield(entry) })
end
alias :find_all :select
alias :keep_if :select
Expand Down
4 changes: 2 additions & 2 deletions lib/hamster/mutable_hash.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ def self.[](pairs = {})
MutableHash.new(Hash[pairs])
end

def put(key, value = Undefined, &block)
transform { |hash| hash.put(key, value, &block) }
def put(key, value = Undefined)
transform { |hash| hash.put(key, value) { |i| yield(i) } }
end

def store(key, value)
Expand Down
70 changes: 35 additions & 35 deletions lib/hamster/sorted_set.rb
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ def initialize(items=[], &block)
items = items.to_a
if block
if block.arity == 1 || block.arity == -1
comparator = lambda { |a,b| block.call(a) <=> block.call(b) }
comparator = lambda { |a,b| yield(a) <=> yield(b) }
items = items.sort_by(&block)
else
comparator = block
Expand Down Expand Up @@ -354,9 +354,9 @@ def values_at(*indices)
#
# @yield [item]
# @return [self, Enumerator]
def each(&block)
def each
return @node.to_enum if not block_given?
@node.each(&block)
@node.each { |entry| yield(entry) }
self
end

Expand All @@ -373,9 +373,9 @@ def each(&block)
# # => Hamster::SortedSet["A", "B", "C"]
#
# @return [self]
def reverse_each(&block)
def reverse_each
return @node.enum_for(:reverse_each) if not block_given?
@node.reverse_each(&block)
@node.reverse_each { |entry| yield(entry) }
self
end

Expand Down Expand Up @@ -477,7 +477,7 @@ def include?(item)
#
# @return [SortedSet]
def sort(&block)
if block
if block_given?
self.class.new(self.to_a, &block)
else
self.class.new(self.to_a.sort)
Expand Down Expand Up @@ -757,9 +757,9 @@ def intersect?(other)
# # => nil
#
# @param item [Object]
def above(item, &block)
def above(item)
if block_given?
@node.each_greater(item, false, &block)
@node.each_greater(item, false) { |n| yield(n) }
else
self.class.alloc(@node.suffix(item, false))
end
Expand Down Expand Up @@ -788,9 +788,9 @@ def above(item, &block)
# # => nil
#
# @param item [Object]
def below(item, &block)
def below(item)
if block_given?
@node.each_less(item, false, &block)
@node.each_less(item, false) { |n| yield(n) }
else
self.class.alloc(@node.prefix(item, false))
end
Expand Down Expand Up @@ -820,9 +820,9 @@ def below(item, &block)
# # => nil
#
# @param item [Object]
def from(item, &block)
def from(item)
if block_given?
@node.each_greater(item, true, &block)
@node.each_greater(item, true) { |n| yield(n) }
else
self.class.alloc(@node.suffix(item, true))
end
Expand Down Expand Up @@ -854,9 +854,9 @@ def from(item, &block)
# # => nil
#
# @param item [Object]
def up_to(item, &block)
def up_to(item)
if block_given?
@node.each_less(item, true, &block)
@node.each_less(item, true) { |n| yield(n) }
else
self.class.alloc(@node.prefix(item, true))
end
Expand Down Expand Up @@ -888,9 +888,9 @@ def up_to(item, &block)
#
# @param from [Object]
# @param to [Object]
def between(from, to, &block)
def between(from, to)
if block_given?
@node.each_between(from, to, &block)
@node.each_between(from, to) { |n| yield(n) }
else
self.class.alloc(@node.between(from, to))
end
Expand Down Expand Up @@ -1175,50 +1175,50 @@ def between(from, to)
end
end

def each_less(item, inclusive, &block)
def each_less(item, inclusive)
dir = direction(item)
if dir > 0 || (inclusive && dir == 0)
@left.each(&block)
@left.each { |entry| yield(entry) }
yield @item
@right.each_less(item, inclusive, &block)
@right.each_less(item, inclusive) { |entry| yield(entry) }
else
@left.each_less(item, inclusive, &block)
@left.each_less(item, inclusive) { |entry| yield(entry) }
end
end

def each_greater(item, inclusive, &block)
def each_greater(item, inclusive)
dir = direction(item)
if dir < 0 || (inclusive && dir == 0)
@left.each_greater(item, inclusive, &block)
@left.each_greater(item, inclusive) { |entry| yield(entry) }
yield @item
@right.each(&block)
@right.each { |entry| yield(entry) }
else
@right.each_greater(item, inclusive, &block)
@right.each_greater(item, inclusive) { |entry| yield(entry) }
end
end

def each_between(from, to, &block)
def each_between(from, to)
if direction(from) > 0 # all on the right
@right.each_between(from, to, &block)
@right.each_between(from, to) { |entry| yield(entry) }
elsif direction(to) < 0 # all on the left
@left.each_between(from, to, &block)
@left.each_between(from, to) { |entry| yield(entry) }
else
@left.each_greater(from, true, &block)
@left.each_greater(from, true) { |entry| yield(entry) }
yield @item
@right.each_less(to, true, &block)
@right.each_less(to, true) { |entry| yield(entry) }
end
end

def each(&block)
@left.each(&block)
def each
@left.each { |entry| yield(entry) }
yield @item
@right.each(&block)
@right.each { |entry| yield(entry) }
end

def reverse_each(&block)
@right.reverse_each(&block)
def reverse_each
@right.reverse_each { |entry| yield(entry) }
yield @item
@left.reverse_each(&block)
@left.reverse_each { |entry| yield(entry) }
end

def drop(n)
Expand Down
18 changes: 5 additions & 13 deletions lib/hamster/trie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,25 +28,17 @@ def key?(key)
end

# Calls <tt>block</tt> once for each entry in the trie, passing the key-value pair as parameters.
def each(&block)
# TODO: Using block.call here is slower than using yield by 5-10%, but
# the latter segfaults on ruby 2.2 and above. Once that is fixed and
# broken versions are sufficiently old, we should revert back to yield
# with a warning that the broken versions are unsupported.
#
# For more context:
# * https://bugs.ruby-lang.org/issues/11451
# * https://github.com/hamstergem/hamster/issues/189
@entries.each { |entry| block.call(entry) if entry }
def each
@entries.each { |entry| yield(entry) if entry }
@children.each do |child|
child.each(&block) if child
child.each { |child| yield(child) } if child
end
nil
end

def reverse_each(&block)
def reverse_each
@children.reverse_each do |child|
child.reverse_each(&block) if child
child.reverse_each { |child| yield(child) } if child
end
@entries.reverse_each { |entry| yield(entry) if entry }
nil
Expand Down
23 changes: 13 additions & 10 deletions lib/hamster/vector.rb
Original file line number Diff line number Diff line change
Expand Up @@ -426,9 +426,9 @@ def shift
# # => Hamster::Vector["A", "B", "C"]
#
# @return [self, Enumerator]
def each(&block)
def each
return to_enum unless block_given?
traverse_depth_first(@root, @levels, &block)
traverse_depth_first(@root, @levels) { |entry| yield(entry) }
self
end

Expand All @@ -443,9 +443,9 @@ def each(&block)
# Element: A
#
# @return [self]
def reverse_each(&block)
def reverse_each
return enum_for(:reverse_each) unless block_given?
reverse_traverse_depth_first(@root, @levels, &block)
reverse_traverse_depth_first(@root, @levels) { |entry| yield(entry) }
self
end

Expand Down Expand Up @@ -1300,14 +1300,17 @@ def marshal_load(array)

private

def traverse_depth_first(node, level, &block)
return node.each(&block) if level == 0
node.each { |child| traverse_depth_first(child, level - 1, &block) }
def traverse_depth_first(node, level)
return node.each { |n| yield(n) } if level == 0
node.each { |child| traverse_depth_first(child, level - 1) { |n| yield(n) } }
end

def reverse_traverse_depth_first(node, level, &block)
return node.reverse_each(&block) if level == 0
node.reverse_each { |child| reverse_traverse_depth_first(child, level - 1, &block) }
def reverse_traverse_depth_first(node, level)
return node.reverse_each { |n| yield(n) } if level == 0

node.reverse_each do |child|
reverse_traverse_depth_first(child, level - 1) { |n| yield(n) }
end
end

def leaf_node_for(node, bitshift, index)
Expand Down