-
Notifications
You must be signed in to change notification settings - Fork 2
Consider providing a concat
function
#14
Comments
The Applicative instance for Validation already does that, it's the major difference between Validation and Either: var v1 = Validation.Failure([1])
var v2 = Validation.Failure([2])
v1.ap(v2)
// => Validation.Failure([1, 2])
Validation.Success(x => 1).ap(v1)
// => Validation.Failure([1])
Validation.Success(x => x + 1).ap(Validation.Success(1))
// => Validation.Success(2) edit: The example in the README shows a more detailed scenario for this |
Hi! As you show, it is possible to achieve the desired behavior when Validation.Success(R.construct(FormBean))
.ap(validateFirstParam(body.firstParam))
.ap(validateSecondParam(body.secondParam)); However, what if I have some unrelated values – each of which wrapped in a const v1 = Validation.Success(12);
const v2 = Validation.Failure(['error']); Since Thanks! |
You can't use it directly without transforming the data, yes. I plan to include a function like this in the current redesign (origamitower/folktale#5) of the Folktale libraries, but that's going to take a while still (a couple of months, maybe?) In the mean time, you can use the following function for this: // Any function that curries with an explicit arity
var curry = require('core.lambda').curry;
//: (Array<Validation<Any, Error:Any>>, a) -> Validation<Array<Any>, Array<Error:Any>>
function collectFailures(validations) {
const ap = (acc, validation) => acc.ap(validation);
const toList = (...args) => args;
const liftToArray = (value) => Array.isArray(value)? value : [value];
return validations.map(validation => validation.failureMap(liftToArray))
.reduce(ap, Success(curry(validations.length, toList)))
}
// Then use it like:
const v1 = Validation.Success(12);
const v2 = Validation.Failure(['error']);
const v3 = Validation.Failure('another error');
collectFailures([v1, v2]) // => Success([12])
collectFailures([v2, v3]) // => Failure(['error', 'another error']) |
Hi,
It would be handy to have
Validation
provide aconcat
function. Something like:Thanks.
The text was updated successfully, but these errors were encountered: