所有的Either cruft 是怎么回事? [英] What's the deal with all the Either cruft?

查看:63
本文介绍了所有的Either cruft 是怎么回事?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Either 类似乎很有用,使用方法也很明显.但后来我查看了 API 文档,我感到很困惑:

The Either class seems useful and the ways of using it are pretty obvious. But then I look at the API documentation and I'm baffled:

def joinLeft [A1 >: A, B1 >: B, C] (implicit ev: <:<[A1, Either[C, B1]]):
         Either[C, B1]
   Joins an Either through Left.

def joinRight [A1 >: A, B1 >: B, C] (implicit ev: <:<[B1, Either[A1, C]]):
         Either[A1, C]
   Joins an Either through Right.

def left : LeftProjection[A, B]
   Projects this Either as a Left.

def right : RightProjection[A, B]
   Projects this Either as a Right.

如何处理投影以及如何调用连接?

What do I do with a projection and how do I even invoke the joins?

Google 只是将我指向 API 文档.

Google just points me to the API documentation.

这可能只是不注意幕后男人"的情况,但我不这么认为.我认为这很重要.

This might just be a case of "paying no attention to the man behind the curtain", but I don't think so. I think this is important.

推荐答案

leftright 是重要的.Either 在没有投影的情况下很有用(大多数情况下你做模式匹配),但投影非常值得关注,因为它们提供了更丰富的 API.您将很少使用连接.

left and right are the important ones. Either is useful without projections (mostly you do pattern matching), but projections are quite worthy of attention, as they give a much richer API. You will use joins much less.

Either 通常用于表示正确的值或错误".在这方面,它就像一个扩展的 Option .当没有数据而不是 None 时,您会遇到错误.Option 拥有丰富的 API.在 Either 上也可以使用相同的内容,前提是我们知道在任何一个中,哪个是结果,哪个是错误.

Either is often used to mean "a proper value or an error". In this respect, it is like an extended Option . When there is no data, instead of None, you have an error. Option has a rich API. The same can be made available on Either, provided we know, in Either, which one is the result and which one is the error.

leftright 投影说明了这一点.它是Either,加上值分别在左边或右边的附加知识,另一个是错误.

left and right projection says just that. It is the Either, plus the added knowledge that the value is respectively at left or at right, and the other one is the error.

例如,在Option中,你可以映射,所以opt.map(f)返回一个带有f<的Option/code> 应用于 opt 的值,如果它有一个,并且仍然 None 如果 optNone.在左投影上,如果是Left,它将在左边的值上应用f,如果是Right,则保持不变.观察签名:

For instance, in Option, you can map, so opt.map(f) returns an Option with f applied to the value of opt if it has a one, and still None if opt was None. On a left projection, it will apply f on the value at left if it is a Left, and leave it unchanged if it is a Right. Observe the signatures:

  • LeftProjection[A,B]中,map[C](f: A => C):Either[C,B]
  • RightProjection[A,B]中,map[C](f: B => C):Either[A,C].

leftright 只是说明当您想要使用常用 API 例程之一时哪一侧被视为值的方式.

left and right are simply the way to say which side is considered the value when you want to use one of the usual API routines.

替代方案可能是:

  • 设置一个约定,就像在 Haskell 中一样,在那里有很强的语法理由来正确地设置值.当你想在另一边应用一个方法时(例如,你可能想用 map 改变错误),在之前和之后做一个 swap.
  • 带有Left 或Right 的后缀方法名称(可能只是L 和R).这将阻止用于理解.使用 for 推导式(实际上是 flatMap,但 for 表示法非常方便)Either 是(已检查的)异常的替代方法.
  • set a convention, as in Haskell, where there were strong syntactical reasons to put the value at right. When you want to apply a method on the other side (you may well want to change the error with a map for instance), do a swap before and after.
  • postfix method names with Left or Right (maybe just L and R). That would prevent using for comprehension. With for comprehensions (flatMap in fact, but the for notation is quite convenient) Either is an alternative to (checked) exceptions.

现在加入.Left 和 Right 与投影的含义相同,它们与 flatMap 密切相关.考虑 joinLeft.签名可能令人费解:

Now the joins. Left and Right means the same thing as for the projections, and they are closely related to flatMap. Consider joinLeft. The signature may be puzzling:

joinLeft [A1 >: A, B1 >: B, C] (implicit ev: <:<[A1, Either[C, B1]]):
         Either[C, B1]

A1B1 在技术上是必要的,但对理解并不重要,让我们简化一下

A1 and B1 are technically necessary, but not critical to the understanding, let's simplify

joinLeft[C](implicit ev: <:<[A, Either[C, B])

隐含的意思是只有当A 是一个Either[C,B] 时才能调用该方法.该方法通常不适用于 Either[A,B],但仅适用于 Either[Either[C,B], B].与左投影一样,我们认为该值位于左侧(这对于 joinRight 来说是正确的).联接的作用是将其展平(想想 flatMap).当一个连接时,一个人不关心错误(B)是内部还是外部,我们只想要Either[C,B].所以Left(Left(c)) 产生Left(c),Left(Right(b)) 和Right(b) 产生Right(b).与 flatMap 的关系如下:

What the implicit means is that the method can only be called if A is an Either[C,B]. The method is not available on an Either[A,B] in general, but only on an Either[Either[C,B], B]. As with left projection, we consider that the value is at left (that would be right for joinRight). What the join does is flatten this (think flatMap). When one join, one does not care whether the error (B) is inside or outside, we just want Either[C,B]. So Left(Left(c)) yields Left(c), both Left(Right(b)) and Right(b) yield Right(b). The relation with flatMap is as follows:

joinLeft(e) = e.left.flatMap(identity)
e.left.flatMap(f) = e.left.map(f).joinLeft

Option 等价物适用于 Option[Option[A]]Some(Some(x)) 将产生 Some(x) Some(None)None 都会产生 None.可以写成 o.flatMap(identity).请注意,Option[A]Either[A,Unit](如果您使用左投影和连接)以及 Either[Unit, A] 同构(使用正确的投影).

The Option equivalent would work on an Option[Option[A]], Some(Some(x)) would yield Some(x) both Some(None) and None would yield None. It can be written o.flatMap(identity). Note that Option[A] is isomorphic to Either[A,Unit] (if you use left projections and joins) and also to Either[Unit, A] (using right projections).

这篇关于所有的Either cruft 是怎么回事?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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