<p>I'm wondering if there's a way to do what I can do below with Python, in Ruby:</p>

<pre><code>sum = reduce(lambda x, y: x + y, map(lambda x, y: x * y, weights, data))
</code></pre>

<p>I have two arrays of equal sizes with the weights and data but I can't seem to find a function similar to map in Ruby, reduce I have working.</p>

[{'answer_id': 3314, 'author': 'Curt Hagenlocher', 'author_id': 533, 'author_profile': 'https://Stackoverflow.com/users/533', 'pm_score': 2, 'selected': False, 'text': "<p>The Array.zip function does an elementwise combination of arrays. It's not quite as clean as the Python syntax, but here's one approach you could use:</p>\n\n<pre><code>weights = [1, 2, 3]\ndata = [4, 5, 6]\nresult = Array.new\na.zip(b) { |x, y| result &lt;&lt; x * y } # For just the one operation\n\nsum = 0\na.zip(b) { |x, y| sum += x * y } # For both operations\n</code></pre>\n"}, {'answer_id': 3318, 'author': 'Joey deVilla', 'author_id': 216, 'author_profile': 'https://Stackoverflow.com/users/216', 'pm_score': 1, 'selected': False, 'text': '<p>Ruby has a <code>map</code> method (a.k.a. the <code>collect</code> method), which can be applied to any <code>Enumerable</code> object. If <code>numbers</code> is an array of numbers, the following line in Ruby:</p>\n\n<pre><code>numbers.map{|x| x + 5}\n</code></pre>\n\n<p>is the equivalent of the following line in Python:</p>\n\n<pre><code>map(lambda x: x + 5, numbers)\n</code></pre>\n\n<p>For more details, see <a href="http://www.globalnerdy.com/2008/06/25/enumerating-enumerable-enumerablecollectenumerablemap/" rel="nofollow noreferrer" title="Enumerable#collect / Enumerable#map">here</a> or <a href="http://www.ruby-doc.org/core/classes/Enumerable.html#M001146" rel="nofollow noreferrer">here</a>.</p>\n'}, {'answer_id': 4263, 'author': 'Michiel de Mare', 'author_id': 136, 'author_profile': 'https://Stackoverflow.com/users/136', 'pm_score': 3, 'selected': False, 'text': '<p>In Ruby 1.9:</p>\n\n<pre><code>weights.zip(data).map{|a,b| a*b}.reduce(:+)\n</code></pre>\n\n<p>In Ruby 1.8:</p>\n\n<pre><code>weights.zip(data).inject(0) {|sum,(w,d)| sum + w*d }\n</code></pre>\n'}, {'answer_id': 4270, 'author': 'Lily Ballard', 'author_id': 582, 'author_profile': 'https://Stackoverflow.com/users/582', 'pm_score': 5, 'selected': True, 'text': '<p>@Michiel de Mare</p>\n\n<p>Your Ruby 1.9 example can be shortened a bit further:</p>\n\n<pre><code>weights.zip(data).map(:*).reduce(:+)\n</code></pre>\n\n<p>Also note that in Ruby 1.8, if you require ActiveSupport (from Rails) you can use:</p>\n\n<pre><code>weights.zip(data).map(&amp;:*).reduce(&amp;:+)\n</code></pre>\n'}, {'answer_id': 5124209, 'author': 'Chris Turner', 'author_id': 635050, 'author_profile': 'https://Stackoverflow.com/users/635050', 'pm_score': 0, 'selected': False, 'text': '<p>An alternative for the map that works for more than 2 arrays as well:</p>\n\n<pre><code>def dot(*arrays)\n  arrays.transpose.map {|vals| yield vals}\nend\n\ndot(weights,data) {|a,b| a*b} \n\n# OR, if you have a third array\n\ndot(weights,data,offsets) {|a,b,c| (a*b)+c}\n</code></pre>\n\n<p>This could also be added to Array:</p>\n\n<pre><code>class Array\n  def dot\n    self.transpose.map{|vals| yield vals}\n  end\nend\n\n[weights,data].dot {|a,b| a*b}\n\n#OR\n\n[weights,data,offsets].dot {|a,b,c| (a*b)+c}\n</code></pre>\n'}, {'answer_id': 21015028, 'author': 'Cary Swoveland', 'author_id': 256970, 'author_profile': 'https://Stackoverflow.com/users/256970', 'pm_score': 0, 'selected': False, 'text': "<pre><code>weights = [1,2,3]\ndata    = [10,50,30]\n\nrequire 'matrix'\nVector[*weights].inner_product Vector[*data] # =&gt; 200 \n</code></pre>\n"}]
['https://Stackoverflow.com/questions/3281', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/484/']