昨天蛋疼了 写了个Ruby的并行迭代
[code="ruby"]
a=[1,2,3] b=[4,5,6]
def both(*enumerables)
enumerators = enumerables.map { |e| e.to_enum }
loop { yield enumerators.map { |e| e.next } }
end
both(a,b) { |i| print(i) }
[/code]
输出结果
[code="ruby"][1,4] [2,5] [3,6][/code]
我想要用这个both迭代器实现两个数组中元素逐个操作,就是类似
[code="ruby"]
i=0
while(i<a.length) { puts(a[i]*b[i])}
[/code]
这样的操作,请问能实现吗?