Class: Jrf::Reducers::DecomposableReduce

Inherits:
Reduce
  • Object
show all
Defined in:
lib/jrf/reducers.rb

Overview

A reducer whose partial accumulators can be merged across parallel workers.

Contract:

  • ‘identity` is the neutral element for `merge_fn`: merge(identity, x) == x

  • ‘initial` is always set to `identity` (the accumulator starts from the neutral element)

  • Any bias (e.g. sum’s ‘initial:` keyword) is applied in `finish_fn`, not in the starting accumulator

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Reduce

#finish, #step

Constructor Details

#initialize(identity, merge:, finish_fn: nil, &step_fn) ⇒ DecomposableReduce

Returns a new instance of DecomposableReduce.



32
33
34
35
# File 'lib/jrf/reducers.rb', line 32

def initialize(identity, merge:, finish_fn: nil, &step_fn)
  super(identity, finish_fn: finish_fn, &step_fn)
  @merge_fn = merge
end

Instance Attribute Details

#merge_fnObject (readonly)

Returns the value of attribute merge_fn.



30
31
32
# File 'lib/jrf/reducers.rb', line 30

def merge_fn
  @merge_fn
end

Instance Method Details

#merge_partial(other_acc) ⇒ Object

Merges another partial accumulator into this one.



43
44
45
# File 'lib/jrf/reducers.rb', line 43

def merge_partial(other_acc)
  @acc = @merge_fn.call(@acc, other_acc)
end

#partialObject

Returns the raw accumulator without applying finish_fn.



38
39
40
# File 'lib/jrf/reducers.rb', line 38

def partial
  @acc
end