Hyperlinkv0.8.0-beta.28

UndefinedOr

UndefinedOr.makeReducerFailFastfunctioneffect/UndefinedOr.ts:228
<A>(reducer: Reducer.Reducer<A>): Reducer.Reducer<A | undefined>

Creates a Reducer for A | undefined by wrapping an existing reducer with fail-fast semantics.

When to use

Use to wrap an existing Reducer so any undefined value aborts the entire reduction result.

Details

  • Initial value is the wrapped reducer's initialValue
  • Combining two defined values delegates to the wrapped reducer
  • If the accumulator or next value is undefined, the reduction returns undefined
export function makeReducerFailFast<A>(reducer: Reducer.Reducer<A>): Reducer.Reducer<A | undefined> {
  const combine = makeCombinerFailFast(reducer).combine
  const initialValue = reducer.initialValue as A | undefined
  return Reducer.make(combine, initialValue, (collection) => {
    let out = initialValue
    for (const value of collection) {
      out = combine(out, value)
      if (out === undefined) return out
    }
    return out
  })
}