prelude-ts icon indicating copy to clipboard operation
prelude-ts copied to clipboard

Implement sequenceAcc for Option

Open flakey-bit opened this issue 2 years ago • 0 comments

Would it be possible to implement sequenceAcc for Option? Like you have for Either?

Could be used for validation - let's say you have a bunch of functions that - given some input - perform a validation and return Option<TValidationError> .

You want to apply all of those validation functions and get out an Option<TValidationError[]>. If none of the validation functions produce an error then the outcome would be none.

Something like this:

const optionSequenceAcc = <T>(values: Option<T>[]): Option<T[]> => {
  return values.reduce((accum: Option<T[]>, value: Option<T>) => {
    return value.match({
      None: () => accum,
      Some: (error) => Option.some<T[]>([...accum.getOrElse([]), error])
    })
  }, Option.none())
}

flakey-bit avatar Mar 22 '22 19:03 flakey-bit