Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: jruby/jruby
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: f77b626ece44
Choose a base ref
...
head repository: jruby/jruby
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: ab2d094f3eba
Choose a head ref
  • 2 commits
  • 2 files changed
  • 1 contributor

Commits on Nov 23, 2016

  1. Copy the full SHA
    971f1ce View commit details
  2. Add Enumerator::Lazy#chunk_while.

    This also cleans up the "lazy super" methods a bit.
    
    ruby/ruby#1186
    headius committed Nov 23, 2016
    Copy the full SHA
    ab2d094 View commit details
Showing with 39 additions and 12 deletions.
  1. +8 −3 core/src/main/ruby/jruby/kernel/enumerable.rb
  2. +31 −9 core/src/main/ruby/jruby/kernel/enumerator.rb
11 changes: 8 additions & 3 deletions core/src/main/ruby/jruby/kernel/enumerable.rb
Original file line number Diff line number Diff line change
@@ -127,17 +127,22 @@ def lazy
end

def uniq
values = []
hash = {}
if defined? yield
if block_given?
each do |obj|
ret = yield *obj
hash[ret] = obj unless hash.key? ret
next if hash.key? ret
hash[ret] = obj
values << obj
end
else
each do |obj|
next if hash.key? obj
hash[obj] = obj unless hash.key? obj
values << obj
end
end
hash.values
values
end
end
40 changes: 31 additions & 9 deletions core/src/main/ruby/jruby/kernel/enumerator.rb
Original file line number Diff line number Diff line change
@@ -45,15 +45,19 @@ def inspect
"#<#{self.class}: #{@receiver.inspect}#{suff}>"
end

{
:slice_before => //,
:with_index => [],
:cycle => [],
:each_with_object => 42,
:each_slice => 42,
:each_entry => [],
:each_cons => 42,
}.each do |method, args|
[
:slice_before,
:slice_after,
:slice_when,
:chunk_while,
:chunk,
:with_index,
:cycle,
:each_with_object,
:each_slice,
:each_entry,
:each_cons,
].each do |method|
module_eval <<-EOT, __FILE__, __LINE__ + 1
def #{method}(*args) # def cycle(*args)
return to_enum(:#{method}, *args) unless block_given? # return to_enum(:cycle, *args) unless block_given?
@@ -204,6 +208,24 @@ def zip(*args)
end.__set_inspect :zip, args
end

def uniq
hash = {}
if block_given?
Lazy.new(self) do |yielder, obj|
ret = yield *obj
next if hash.key? ret
hash[ret] = obj
yielder << obj
end
else
Lazy.new(self) do |yielder, obj|
next if hash.key? obj
hash[obj] = obj unless hash.key? obj
yielder << obj
end
end
end

protected
def __set_inspect(method, args = nil, receiver = nil)
@method = method