Module: LambdaMapReduce

Defined in:
lib/lambda_map_reduce/version.rb,
lib/lambda_map_reduce/map_reduce.rb

Overview

Author:

Constant Summary collapse

VERSION =
'0.0.0'

Class Method Summary collapse

Class Method Details

.map_reduce(items, mapper, reducer) ⇒ Object

Returns an Array of objects after mapping and reducing items. mapper takes a single item and returns an Array of [key, value] pairs. reducer takes a [key, Array of values] pair and returns a single item.



7
8
9
10
11
# File 'lib/lambda_map_reduce/map_reduce.rb', line 7

def self.map_reduce(items, mapper, reducer)
  items.flat_map { |item| mapper.call(item) }.compact
    .each_with_object({}) { |kv, shuffle| (shuffle[kv[0]] ||= []) << kv[1] }
    .map { |key, values| reducer.call(key, values) }.compact
end