summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
-rw-r--r--mrbgems/mruby-enum-ext/mrblib/enum.rb32
-rw-r--r--mrbgems/mruby-enum-ext/test/enum.rb9
-rw-r--r--mrbgems/mruby-enumerator/mrblib/enumerator.rb45
3 files changed, 68 insertions, 18 deletions
diff --git a/mrbgems/mruby-enum-ext/mrblib/enum.rb b/mrbgems/mruby-enum-ext/mrblib/enum.rb
index ead9a794a..2affe555f 100644
--- a/mrbgems/mruby-enum-ext/mrblib/enum.rb
+++ b/mrbgems/mruby-enum-ext/mrblib/enum.rb
@@ -552,6 +552,8 @@ module Enumerable
#
def cycle(n=nil, &block)
+ return to_enum :cycle if !block_given? && n == nil
+
ary = []
if n == nil
self.each do|*val|
@@ -615,4 +617,34 @@ module Enumerable
end
nil
end
+
+ ##
+ # call-seq:
+ # enum.zip(arg, ...) -> an_array_of_array
+ #
+ # Takes one element from <i>enum</i> and merges corresponding
+ # elements from each <i>args</i>. This generates a sequence of
+ # <em>n</em>-element arrays, where <em>n</em> is one more than the
+ # count of arguments. The length of the resulting sequence will be
+ # <code>enum#size</code>. If the size of any argument is less than
+ # <code>enum#size</code>, <code>nil</code> values are supplied.
+ #
+
+ def zip(*arg)
+ ary = []
+ arg = arg.map{|a|a.to_a}
+ i = 0
+ self.each do |*val|
+ a = []
+ a.push(val.__svalue)
+ idx = 0
+ while idx < arg.size
+ a.push(arg[idx][i])
+ idx += 1
+ end
+ ary.push(a)
+ i += 1
+ end
+ ary
+ end
end
diff --git a/mrbgems/mruby-enum-ext/test/enum.rb b/mrbgems/mruby-enum-ext/test/enum.rb
index daf737d37..9348cb4a5 100644
--- a/mrbgems/mruby-enum-ext/test/enum.rb
+++ b/mrbgems/mruby-enum-ext/test/enum.rb
@@ -135,3 +135,12 @@ assert("Enumerable#find_index") do
assert_equal 34, (1..100).find_index { |i| i % 5 == 0 and i % 7 == 0 }
assert_equal 49 ,(1..100).find_index(50)
end
+
+assert("Enumerable#zip") do
+ a = [ 4, 5, 6 ]
+ b = [ 7, 8, 9 ]
+ assert_equal [[4, 7], [5, 8], [6, 9]], a.zip(b)
+ assert_equal [[1, 4, 7], [2, 5, 8], [3, 6, 9]], [1, 2, 3].zip(a, b)
+ assert_equal [[1, 4, 7], [2, 5, 8]], [1, 2].zip(a, b)
+ assert_equal [[4, 1, 8], [5, 2, nil], [6, nil, nil]], a.zip([1, 2], [8])
+end
diff --git a/mrbgems/mruby-enumerator/mrblib/enumerator.rb b/mrbgems/mruby-enumerator/mrblib/enumerator.rb
index 912683ed9..1494c02c5 100644
--- a/mrbgems/mruby-enumerator/mrblib/enumerator.rb
+++ b/mrbgems/mruby-enumerator/mrblib/enumerator.rb
@@ -297,7 +297,7 @@ class Enumerator
# side-effect
#
def next
- ary2sv next_values, false
+ next_values.__svalue
end
##
@@ -405,7 +405,7 @@ class Enumerator
# p e.next #raises StopIteration
#
def peek
- ary2sv peek_values, true
+ peek_values.__svalue
end
##
@@ -511,22 +511,6 @@ class Enumerator
end
# just for internal
- def ary2sv args, dup
- return args unless args.kind_of? Array
-
- case args.length
- when 0
- nil
- when 1
- args[0]
- else
- return args.dup if dup
- args
- end
- end
- private :ary2sv
-
- # just for internal
class Generator
def initialize &block
raise TypeError, "wrong argument type #{self.class} (expected Proc)" unless block.kind_of? Proc
@@ -623,3 +607,28 @@ module Kernel
end
alias :enum_for :to_enum
end
+
+module Enumerable
+ # use Enumerator to use inifite sequence
+ def zip(*arg)
+ ary = []
+ arg = arg.map{|a|a.each}
+ i = 0
+ self.each do |*val|
+ a = []
+ a.push(val.__svalue)
+ idx = 0
+ while idx < arg.size
+ begin
+ a.push(arg[idx].next)
+ rescue StopIteration
+ a.push(nil)
+ end
+ idx += 1
+ end
+ ary.push(a)
+ i += 1
+ end
+ ary
+ end
+end