# Map more than one array at once

**URL:** <https://www.ruby-forum.com/t/map-more-than-one-array-at-once/109860>\
**Category:** Ruby\
**Created:** [July 19, 2007, 6:11pm UTC](https://www.ruby-forum.com/t/map-more-than-one-array-at-once/109860 "2007-07-19T18:11:12Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![wil](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/59ef9b/32.png) [@wil](https://www.ruby-forum.com/u/wil)\
**Post date:** [July 19, 2007, 6:11pm UTC](https://www.ruby-forum.com/t/map-more-than-one-array-at-once/109860/1 "2007-07-19T18:11:12Z")

</div>

Hi all,

I’ve been using Ruby for a while now, and have enjoyed it immensely.  
However, I haven’t yet found how to map more than one array at once.  
The common example is calculating the dot product. You’d need to  
iterate through each array at the same time to multiply the elements.  
However, map only operates on one list at a time. I wanted to avoid  
For Loops. Therefore, I end up having to do something like this  
instead:

def dot\_prod(a,b)  
return 0 if a.length != b.length  
c = []  
a.each\_with\_index { |e, i| c \<\< e \* b[i] }  
c.inject { |e, t| t += e }  
end

Ugly. or use recursion:

def dot\_prod(a, b)  
return 0 if a.length != b.length  
a.empty? ? 0 : a.first \* b.first + dot\_prod(a[1…-1], b[1…-1])  
end

Better, but a map/inject solution would be desired. Does anyone else  
know of a way to iterate through two arrays without the use of an  
index? Or do people just roll their own multiple array map function?

---

<div class="post-metadata">

**Author:** ![wil](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/59ef9b/32.png) [@wil](https://www.ruby-forum.com/u/wil)\
**Post date:** [July 19, 2007, 6:17pm UTC](https://www.ruby-forum.com/t/map-more-than-one-array-at-once/109860/2 "2007-07-19T18:17:08Z")

</div>

Wil wrote:

> Hi all,
> 
> Better, but a map/inject solution would be desired. Does anyone else  
> know of a way to iterate through two arrays without the use of an  
> index?

def dot\_prod(a, b)  
acc = 0  
a.zip(b) do |a1, b1|  
acc += a1\*b1  
end  
acc  
end

best,  
Dan

---

<div class="post-metadata">

**Author:** ![wil](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/59ef9b/32.png) [@wil](https://www.ruby-forum.com/u/wil)\
**Post date:** [July 19, 2007, 7:19pm UTC](https://www.ruby-forum.com/t/map-more-than-one-array-at-once/109860/3 "2007-07-19T19:19:20Z")

</div>

Wil wrote:

> Hi all,
> 
> I’ve been using Ruby for a while now, and have enjoyed it immensely.  
> However, I haven’t yet found how to map more than one array at once.  
> The common example is calculating the dot product.

Here’s an example using inject, no need for a temp variable:

irb(main):001:0\> def dot\_prod a,b  
irb(main):002:1\> a.zip(b).inject(0) do |dp,(a,b)|  
irb(main):003:2\* dp + a\*b  
irb(main):004:2\> end  
irb(main):005:1\> end  
=\> nil  
irb(main):006:0\> dot\_prod([1,2,3],[4,5,6])  
=\> 32

---

<div class="post-metadata">

**Author:** ![wil](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/59ef9b/32.png) [@wil](https://www.ruby-forum.com/u/wil)\
**Post date:** [July 19, 2007, 7:39pm UTC](https://www.ruby-forum.com/t/map-more-than-one-array-at-once/109860/4 "2007-07-19T19:39:30Z")

</div>

Alle giovedì 19 luglio 2007, Wil ha scritto:

> def dot\_prod(a,b)  
> a.empty? ? 0 : a.first \* b.first + dot\_prod(a[1…-1], b[1…-1])  
> end
> 
> Better, but a map/inject solution would be desired. Does anyone else  
> know of a way to iterate through two arrays without the use of an  
> index? Or do people just roll their own multiple array map function?

require ‘generator’

a = [1, 2, 3]  
b = [‘a’, ‘b’, ‘c’]  
c = [:x, :y, :z]

s = SyncEnumerator.new( a, b, c)  
s.each{|i| puts “first: #{i[0]}, second: #{i[1]}, third: #{i[3]}”}  
=\>  
first: 1, second: a, third: x  
first: 2, second: b, third: y  
first: 3, second: c, third: z

To define the dot product:

def dot\_prod(a,b)  
raise ArgumentError unless a.size == b.size  
SyncEnumerator.new(a,b).inject(0){|res, i| res + i[0] \* i[1]}  
end

Stefano
