Join GitHub today
How to aggregate all success/failures in list of Validation (sequence?) #71
Hi, thanks for the fantastic library and learning resources.
I thought this was possible using
I have had success sequencing a list of
Never mind, got it now!
// Array<Validation<A, B>> => Validation<Array<A>, Array<B>> const sequenceValidations = validations => ( validations .reduce((acc, validation) => ( Validation.Success(a => b => a.concat(b)).ap(acc).ap(validation) ), Validation.of()) )
Is this function in the library anywhere?
@OliverJAsh Validations implement Applicative, rather than Monad, so
There isn't a function to aggregate Validations yet. Validations themselves already aggregate failures when you use .apply(), but the failure needs to have a
const Validation = require('folktale/data/validation'); const curry = require('folktale/core/lambda/curry'); const intoArray = (x) => Array.isArray(x) ? x : [x]; const collect = (validations) => validations.map(x => x.failureMap(intoArray)).reduce( (a, b) => a.ap(b), Validation.of(curry(validations.length, (...values) => values)) );
So something like:
collect([a, b, c]);
Validation.of((a, b, c) => [a, b, c]) .ap(a.failureMap(intoArray)) .ap(b.failureMap(intoArray)) .ap(c.failureMap(intoArray))