两种操作 [英] Either monadic operations

查看:118
本文介绍了两种操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚开始习惯于处理单子运算. 对于期权类型,托尼·莫里斯的备忘单有助于: 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]
  • 中转换Option[Option[X]]
  • flatMap是一种映射操作,它生成一个Option[Option[X]],然后展平为Option[X]
  • map transforms the value of inside an option
  • flatten permits to transform Option[Option[X]] in Option[X]
  • flatMap is somehow a map operation producing an Option[Option[X]] and then flattened to Option[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应该用于您确实可能会获得两种可能的类型之一的情况,并且现在我正在使用TryValidation(每种用于不同类型的情况),我再也不会使用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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆