Skip to content
Permalink
Browse files

Merge pull request #736 from kostya/chunk

Enumerable#chunk from rubinius
  • Loading branch information
alex committed May 26, 2013
2 parents 9a203d7 + f86234e commit a4a85a47a3d0e64cd0aee74c24a9186d0d83df34
Showing with 33 additions and 18 deletions.
  1. +33 −0 lib-topaz/enumerable.rb
  2. +0 −18 spec/tags/core/enumerable/chunk_tags.txt
@@ -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

This file was deleted.

0 comments on commit a4a85a4

Please sign in to comment.
You can’t perform that action at this time.