两种操作 [英] Either monadic operations
问题描述
我刚刚开始习惯于处理单子运算. 对于期权类型,托尼·莫里斯的备忘单有助于: http://blog.tmorris.net/posts/scalaoption-cheat-sheet/
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:
- 地图可转换选项内部的值
- flatten允许在
Option[X]
中转换 - flatMap是一种映射操作,它生成一个
Option[Option[X]]
,然后展平为Option[X]
Option[Option[X]]
- 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.
对于Either来说,似乎有点难以理解,因为Either本身没有正确偏斜,没有map/flatMap操作...并且我们使用投影.
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.
我可以阅读Scaladoc,但它不如选项上的备忘单那么清晰. 有人可以提供任一工作表作弊来描述基本的单子操作吗?
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?
在我看来,Either.joinRight
有点像RightProjection.flatMap
,对于任何一个人来说都等同于Option.flatten
.
It seems to me that Either.joinRight
is a bit like RightProjection.flatMap
and seems to be the equivalent of Option.flatten
for Either.
在我看来,如果Either
被右偏,那么Either.flatten将会是Either.joinRight否?
It seems to me that if Either
was Right biaised, then Either.flatten would be Either.joinRight no?
在此问题中:无论是选项还是理解,我询问对Eiher的理解,答案之一是说我们不能将monad混合,因为将其分解为map/flatMap/filter的方式.
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.
使用这种代码时:
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
这是否意味着我所有3个方法调用都必须始终返回Either[Error,Something]
?
我的意思是,如果我有方法调用Either[Throwable,Something]
,它将无法正常工作?
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?
修改: Try [Something]与右偏的Either [Throwable,Something]完全一样吗?
Is Try[Something] exactly the same as a right-biaised Either[Throwable,Something]?
推荐答案
Either
从未真正成为基于异常处理的结构.它旨在表示一种情况,其中一个函数实际上可能返回两种不同类型中的一种,但是人们开始约定,左类型应该是失败的情况,而右类型是成功的情况.如果要为某些通过/失败类型业务检查逻辑返回偏倚类型,则scalaz
中的Validation
效果很好.如果您有一个可以返回值或Throwable
的函数,那么Try
将是一个不错的选择. Either
应该用于您确实可能会获得两种可能的类型之一的情况,并且现在我正在使用Try
和Validation
(每种用于不同类型的情况),我再也不会使用Either
了.
Either
was never really meant to be an exception handling based structure. It was meant to represent a situation where a function really could possible return one of two distinct types, but people started the convention where the left type is a supposed to be a failed case and the right is success. If you want to return a biased type for some pass/fail type business checks logic, then Validation
from scalaz
works well. If you have a function that could return a value or a Throwable
, then Try
would be a good choice. Either
should be used for situations where you really might get one of two possible types, and now that I am using Try
and Validation
(each for different types of situations), I never use Either
any more.
这篇关于两种操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!