I just start to be used to deal with monadic operations. For the Option type, this Cheat Sheet of Tony Morris helped: http://blog.tmorris.net/posts/scalaoption-cheat-sheet/
So in the end it seems easy to understand that:
- map transforms the value of inside an option
- flatten permits to transform
Option[Option[X]]
inOption[X]
- flatMap is somehow a map operation producing an
Option[Option[X]]
and then flattened toOption[X]
At least it is what I understand until now.
For Either, it seems a bit more difficult to understand since Either itself is not right biaised, does not have map / flatMap operations... and we use projection.
I can read the Scaladoc but it is not as clear as the Cheat Sheet on Options. Can someone provide an Either Sheet Cheat to describe the basic monadic operations?
It seems to me that Either.joinRight
is a bit like RightProjection.flatMap
and seems to be the equivalent of Option.flatten
for Either.
It seems to me that if Either
was Right biaised, then Either.flatten would be Either.joinRight no?
In this question: Either, Options and for comprehensions I ask about for comprehension with Eiher, and one of the answers says that we can't mix monads because of the way it is desugared into map/flatMap/filter.
When using this kind of code:
def updateUserStats(user: User): Either[Error,User] = for {
stampleCount <- stampleRepository.getStampleCount(user).right
userUpdated <- Right(copyUserWithStats(user,stampleCount)).right
userSaved <- userService.update(userUpdated).right
} yield userSaved
Does this mean that all my 3 method calls must always return Either[Error,Something]
?
I mean if I have a method call Either[Throwable,Something]
it won't work right?
Edit: Is Try[Something] exactly the same as a right-biaised Either[Throwable,Something]?