Map Reduce

- - posted in ruby | Comments

This is based on an example I saw while watching the Aaron Patterson Play by Play from Peepcode. The syntax is really nice.

1
2
3
4
5
6
7
records = [
            { :id => 1, :score => 1 },
            { :id => 2, :score => 4 },
            { :id => 3, :score => 9 }
          ]

records.map { |record| record[:score] }.reduce(:+) # => 14

This will work will an collection such as an Array of ActiveRecord objects.

Comments