prelude-ts
prelude-ts copied to clipboard
Implement sequenceAcc for Option
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())
}