我可以问一个是否它是左(或右)? [英] Can I ask an Either whether it is Left (or Right)?

查看:119
本文介绍了我可以问一个是否它是左(或右)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  isLeft = either(const True)(const False)
isRight =或者(const False)(const True)

在标准库中有类似的东西吗?

解决方案

虽然这已经很老了,但在这里发布供参考。



现在位于 Data. 之后的标准库中,因为4.7:



https://hackage.haskell.org/package/base-4.7 .0.0 / docs / Data-Either.html


isLeft ::或者ab - > Bool



如果给定值是一个Left值,则返回True,否则返回False。

isRight ::或者ab - > Bool



如果给定的值是一个Right值,则返回True,否则返回False。


I know I can usually just pattern match, but sometimes I would find these functions useful:

isLeft  = either (const True) (const False)
isRight = either (const False) (const True)

Is there something like that in the standard library?

解决方案

While this is pretty old, posting here for reference.

This is now in the standard library under Data.Either since 4.7:

https://hackage.haskell.org/package/base-4.7.0.0/docs/Data-Either.html

isLeft :: Either a b -> Bool

Return True if the given value is a Left-value, False otherwise.

isRight :: Either a b -> Bool

Return True if the given value is a Right-value, False otherwise.

这篇关于我可以问一个是否它是左(或右)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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