Skip to content
Permalink
Browse files

Merge pull request #607 from kachick/implement-enumerable-zip

Enumerable#zip, Array#zip
  • Loading branch information
alex committed Apr 12, 2013
2 parents 98dd89b + a83604d commit 32267d2b52e985316766286ba02b8833ee136bdb
Showing with 23 additions and 15 deletions.
  1. +0 −8 lib-topaz/array.rb
  2. +23 −0 lib-topaz/enumerable.rb
  3. +0 −2 spec/tags/core/array/zip_tags.txt
  4. +0 −5 spec/tags/core/enumerable/zip_tags.txt
@@ -79,14 +79,6 @@ def each(&block)
return self
end

def zip(ary)
result = []
self.each_with_index do |obj, idx|
result << [obj, ary[idx]]
end
result
end

def product(ary)
result = []
self.each do |obj|
@@ -367,4 +367,27 @@ def collect_concat(&block)
out
end
alias flat_map collect_concat

def zip(*lists, &block)
lists = lists.map do |l|
l.respond_to?(:to_ary) ? l.to_ary : l.to_enum(:each)
end

index = -1
tail = proc do
index += 1
lists.map do |l|
l.kind_of?(Array) ? l[index] : l.next
end
end

if block
self.each do |elm|
yield [elm, *tail.call]
end
nil
else
self.map { |elm| [elm, *tail.call] }
end
end
end
@@ -1,3 +1 @@
fails:Array#zip calls #to_ary to convert the argument to an Array
fails:Array#zip uses #each to extract arguments' elements when #to_ary fails
fails:Array#zip calls block if supplied
@@ -1,6 +1 @@
fails:Enumerable#zip combines each element of the receiver with the element of the same index in arrays given as arguments
fails:Enumerable#zip passes each element of the result array to a block and return nil if a block is given
fails:Enumerable#zip fills resulting array with nils if an argument array is too short
fails:Enumerable#zip converts arguments to arrays using #to_ary
fails:Enumerable#zip converts arguments to enums using #to_enum
fails:Enumerable#zip gathers whole arrays as elements when each yields multiple

0 comments on commit 32267d2

Please sign in to comment.