Open deleite opened 3 years ago
here is what I came up. Please feel free to correct or confirm if this is right :)
const fromPredicate = f => v => { const predicate = f(v); return predicate?Either.of(predicate):left(Error('false!')); }
@deleite this doesn't quite work, if you look at the type signature, the end result should be a Either e a
. There's little we know about e
but we can settle on an Error
or String
here. However, we know that a
must be the same type of the a
also provided in input for the predicate. So instead of Either.of(predicate)
, you should return Either.of(v)
.
You could write it the following way:
// fromPredicate :: (a -> Bool) -> a -> Either e a
const fromPredicate = curry((predicate, a) => predicate(a)
? new Right(a)
: new Left("predicate failed"));
// --- example
const predicate = s => s.startsWith("a", s);
const fruitsA = new List([ "apple", "pear", "banana", "apricot", "orange" ]);
const fruitsB = new List([ "apple", "apricot" ]);
validate(predicate)(fruitsA);
// Left('predicate failed')
validate(predicate)(fruitsB);
// Right({$value: ['apple', 'apricot']})
Could some help me understand the fromPredicate example in the: https://github.com/MostlyAdequate/mostly-adequate-guide/blob/master/ch12.md#effect-assortment
I get that the first one would keep the lefts and rights and the second won't but I would like to see it running so it is easier to get the idea.
Thanks