Skip to content

Commit

Permalink
Showing 3 changed files with 31 additions and 10 deletions.
7 changes: 0 additions & 7 deletions spec/truffle/tags/core/enumerable/slice_after_tags.txt

This file was deleted.

2 changes: 0 additions & 2 deletions spec/truffle/tags/core/enumerable/slice_before_tags.txt

This file was deleted.

32 changes: 31 additions & 1 deletion truffle/src/main/ruby/core/enumerable.rb
Original file line number Diff line number Diff line change
@@ -162,17 +162,47 @@ def group_by
h
end

def slice_after(arg = undefined, &block)
has_arg = !(undefined.equal? arg)
if block_given?
raise ArgumentError, "both pattern and block are given" if has_arg
else
raise ArgumentError, "wrong number of arguments (0 for 1)" unless has_arg
block = Proc.new{ |elem| arg === elem }
end
Enumerator.new do |yielder|
accumulator = nil
each do |*elem|
elem = elem[0] if elem.size == 1
end_slice = block.yield(elem)
accumulator ||= []
if end_slice
accumulator << elem
yielder.yield accumulator
accumulator = nil
else
accumulator << elem
end
end
yielder.yield accumulator if accumulator
end
end

def slice_before(arg = undefined, &block)
if block_given?
has_init = !(undefined.equal? arg)
if has_init
raise ArgumentError, "both pattern and block are given"
end
else
raise ArgumentError, "wrong number of arguments (0 for 1)" if undefined.equal? arg
block = Proc.new{ |elem| arg === elem }
end
Enumerator.new do |yielder|
init = arg.dup if has_init
accumulator = nil
each do |elem|
each do |*elem|
elem = elem[0] if elem.size == 1
start_new = has_init ? block.yield(elem, init) : block.yield(elem)
if start_new
yielder.yield accumulator if accumulator

0 comments on commit 2136a33

Please sign in to comment.