Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

solving a problem with map reduce

Tags:

ruby

mapreduce

I want to simulate in ruby my implementation of the map and reduce functions for a system like hadoop to verify that the idea works at least.

I have the following problem. I have two lists of elements:

List1
3 - A
4 - B
5 - C
7 - D
8 - F

List2
2 - A
8 - B
6 - C
9 - D
4 - E

I need to build a common list that includes the sum of the numbers associated with the alphabets common in the two lists:

commonList
5 - A
12 - B
11 - C
16 - D

I want to make a ruby script with the map and reduce operations to solve this problem. I am unsure how to tackle on this problem or what procedure to follow to simulate this in a ruby script.

Any help appreciated.

like image 678
Flethuseo Avatar asked Apr 10 '11 15:04

Flethuseo


1 Answers

Using irb (ruby-1.9.2-p180):

list = [ {a:2, b:1, d:3}, {a:3, b:2, c:3}, {a:4, b:1, c:3} ]
 => [{:a=>2, :b=>1, :d=>3}, {:a=>3, :b=>2, :c=>3}, {:a=>4, :b=>1, :c=>3}] 

Hash[list.map(&:keys).inject(&:&).map{|key| [key,list.map{|arr| arr[key]}.inject(&:+)]}]
 => {:a=>9, :b=>4} 

this solution works with multiple arrays (2+) it finds common keys and sums them returning a hash of results

to find common keys (gather keys and find common part):

list.map(&:keys).inject(&:&)

to find sum for key (select values by keys and sum them):

list.map{|arr| arr[key]}.inject(&:+)

to build Hash from array of pairs [[:a,9], [:b,4]]:

results = [[:a,9], [:b,4]]
Hash[ results ]

I love ruby for this one liners!

like image 105
mpapis Avatar answered Sep 21 '22 04:09

mpapis