Hyperlinkv0.8.0-beta.28

Option

Option.makeReducerfunctioneffect/Option.ts:2571
<A>(combiner: Combiner.Combiner<A>): Reducer.Reducer<Option<A>>

Creates a Reducer for Option<A> that prioritizes the first non-None value and combines values when both are Some.

When to use

Use to build an Option reducer that falls back to the first available value when either side may be absent.

Details

  • None + NoneNone
  • Some(a) + NoneSome(a)
  • None + Some(b)Some(b)
  • Some(a) + Some(b)Some(combine(a, b))
  • Initial value is None

Example (Reducing with first-wins semantics)

import { Number, Option } from "effect"

const reducer = Option.makeReducer(Number.ReducerSum)
console.log(reducer.combineAll([Option.some(1), Option.none(), Option.some(2)]))
// Output: { _id: 'Option', _tag: 'Some', value: 3 }
Source effect/Option.ts:25717 lines
export function makeReducer<A>(combiner: Combiner.Combiner<A>): Reducer.Reducer<Option<A>> {
  return Reducer.make((self, that) => {
    if (isNone(self)) return that
    if (isNone(that)) return self
    return some(combiner.combine(self.value, that.value))
  }, none())
}