Skip to content

Commit

Permalink
Enumerable#chunk from rubinius
Browse files Browse the repository at this point in the history
  • Loading branch information
kostya committed May 25, 2013
1 parent 9a203d7 commit f86234e
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 18 deletions.
33 changes: 33 additions & 0 deletions lib-topaz/enumerable.rb
Expand Up @@ -437,4 +437,37 @@ def grep(pattern, &block)
end
ret
end

def chunk(initial_state = nil, &original_block)
raise ArgumentError.new("no block given") unless original_block
::Enumerator.new do |yielder|
previous = nil
accumulate = []
block = initial_state.nil? ? original_block : Proc.new{ |val| original_block.yield(val, initial_state.clone)}
each do |val|
key = block.yield(val)
if key.nil? || (key.is_a?(Symbol) && key.to_s[0, 1] == "_")
yielder.yield [previous, accumulate] unless accumulate.empty?
accumulate = []
previous = nil
case key
when nil, :_separator
when :_alone
yielder.yield [key, [val]]
else
raise RuntimeError.new("symbols beginning with an underscore are reserved")
end
else
if previous.nil? || previous == key
accumulate << val
else
yielder.yield [previous, accumulate] unless accumulate.empty?
accumulate = [val]
end
previous = key
end
end
yielder.yield [previous, accumulate] unless accumulate.empty?
end
end
end
18 changes: 0 additions & 18 deletions spec/tags/core/enumerable/chunk_tags.txt

This file was deleted.

0 comments on commit f86234e

Please sign in to comment.