Either
Brian Lonsdorf (the presentators name)
- Typically used for pure error handling
- Like
Maybe
but with an error message embedded - Has two subclasses: Left/Right (as opposed to
Maybe
) - Maps the function over a Right, ignores the Left
// Either
map(function (x) {return x + 1}, Right(2))
//=> Right(3)
map(function (x) {return x + 1}, Left('some message'))
//=> Left('some message')
So if I run in it with Right
it increments, if I run it with Left
it does not run at all like with Maybe
. But this time we can have an error message in that will be returned.
So I can decide somewhere in my app to return a
Left
and my app will stop running altogether. And the error message will propagate to the end.
You may or may not have a left or right. There are someways to get out of this, with lowercase either
. It's for those cases where you don't want to completely stop your app, but want to take some action. This very rare (2% of the cases)
###Either functor
// ###Either
var determineAge = function (user) {
return user.age ? Right(user.age) : Left('couldnt get age')
}
var yearOlder = compose(map(add(1)), determineAge)
yearOld({age: 22})
// → Right(23)
yearOlder({age: null})
// → Left('couldnt get age')
The above example shows how this system works. He's using an Either library.
Right
and Left
are subclasses of an either library.
So, it will either run on the right or it will not run on the left. This is the main thing to remember. If you want to stop the execution of your program, return a left
, if you want to continue, return a right
.
Mentions that Either
is not as useful for asynchronous stuff, this is why he doesn't spend so much time on it.