Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
28 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
unless Enumerable.method_defined? :chunk_while | ||
require 'backports/tools/arguments' | ||
require 'backports/1.9.1/enumerator/new' | ||
|
||
module Enumerable | ||
def chunk_while(&block) | ||
raise ArgumentError, 'tried to create Proc object without a block' unless block | ||
enum = self | ||
Enumerator.new do |y| | ||
acc = [] | ||
prev = Backports::Undefined | ||
enum.each do |*elem| | ||
elem = elem.first if elem.length == 1 | ||
unless prev == Backports::Undefined | ||
unless block.call(prev, elem) | ||
y.yield acc | ||
acc = [] | ||
end | ||
end | ||
acc << elem | ||
prev = elem | ||
end | ||
y.yield acc unless acc.empty? | ||
end | ||
end | ||
end | ||
end |