You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Jul 12, 2025. It is now read-only.
Repository navigation
This repository was archived by the owner on Jul 12, 2025. It is now read-only.
How to aggregate all success/failures in list of Validation (sequence?)Β #71
Hi, thanks for the fantastic library and learning resources.
I have Array<Validation<A, B>> and want to aggregate all the successes and failures, e.g. Array<Validation<A, B>> => Validation<Array<A>, Array<B>>.
I thought this was possible using control.monads.sequence, however I seem to get runtime error mr.chain is not a function, so I presume I must be doing something wrong.
I have had success sequencing a list of Eithers, but in my case I want to aggregate the left hand side as well, which from reading this blog post, it appears Validation should help.
@OliverJAsh Validations implement Applicative, rather than Monad, so sequence (which uses the Monad's interface .chain) doesn't work on them.
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 .concat method. You can write a function to do this as:
constValidation=require('folktale/data/validation');constcurry=require('folktale/core/lambda/curry');constintoArray=(x)=>Array.isArray(x) ? x : [x];constcollect=(validations)=>validations.map(x=>x.failureMap(intoArray)).reduce((a,b)=>a.ap(b),Validation.of(curry(validations.length,(...values)=>values)));
Activity
OliverJAsh commentedon Feb 4, 2017
Never mind, got it now!
Is this function in the library anywhere?
vendethiel commentedon Feb 4, 2017
Sounds a bit like http://stackoverflow.com/questions/39227232/futurelisterror-double-to-futurelisterror-listdouble-in-scala/39227649#39227649
robotlolita commentedon Feb 5, 2017
@OliverJAsh Validations implement Applicative, rather than Monad, so
sequence(which uses the Monad's interface.chain) doesn't work on them.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
.concatmethod. You can write a function to do this as:So something like:
Translates to:
(new validation) Adds a function to aggregate lists of validations (#71)
(new validation) Adds a function to aggregate lists of validations (#71)