我可以问一个Either是左(还是右)吗?

发布于 2024-12-02 05:00:55 字数 172 浏览 1 评论 0原文

我知道我通常可以进行模式匹配,但有时我会发现这些函数很有用:

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

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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

公布 2024-12-09 05:00:55

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

从 4.7 开始,它现在位于 Data.Either 下的标准库中:

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

isLeft :: 要么 ab ->布尔

如果给定值是左值,则返回 True,否则返回 False。

isRight :: 要么 ab ->布尔

如果给定值是右值,则返回 True,否则返回 False。

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.

茶花眉 2024-12-09 05:00:55

正如人们指出的那样,标准库中没有这样的函数,您可以通过各种方式实现自己的函数。

但是,请注意,Hoogle,因为即使您不知道函数的名称,您也可以通过其类型来搜索它。

Hoogle 也足够聪明,知道参数顺序并不重要,并且它还会显示类型与您搜索的类型相似(例如更通用)的结果。

在这种情况下,搜索 Either ab -> Bool 不会产生任何有希望的匹配,所以这是一个很好的指示,表明它不存在于标准库中。

As people have been pointing out, there is no such function in the standard library, and you can implement your own in various ways.

However, note that questions of the form "Is X in the standard library?" are most easily answered by Hoogle, since even if you don't know the name of a function, you can search for it by its type.

Hoogle is also smart enough to know that the argument order does not matter, and it will also show results whose types are similar (e.g. more generic) than the type you searched for.

In this case, searching for Either a b -> Bool does not yield any promising matches, so that's a good indicator that it does not exist in the standard library.

離人涙 2024-12-09 05:00:55

不,但你可以写:

import Data.Either

isLeft = null . rights . return
isRight = null . lefts . return

No, but you can write:

import Data.Either

isLeft = null . rights . return
isRight = null . lefts . return
挽梦忆笙歌 2024-12-09 05:00:55

不,没有,据我所知。

但您可以更轻松地定义这些函数*:

isLeft (Left _) = True
isLeft _        = False

当然,isRight 也是如此。

编辑:*好吧,我想这是否更容易是有争议的,因为它需要更多的代码行......

No, there isn't, afaik.

But you can define these functions even easier*:

isLeft (Left _) = True
isLeft _        = False

the same goes for isRight, of course.

EDIT: * Okay, I guess it's arguable if that's easier or not, since it requires more lines of code...

风为裳 2024-12-09 05:00:55

据我所知,标准库中没有这样的东西。不过,您可以轻松地自己定义它。

either l _ (Left  a) = l a
either _ r (Right b) = r b

isLeft (Left _) = True
isLeft _        = False

isRight (Right _) = True
isRight _         = False

As far as I know, there's nothing like this in the standard library. You can define it yourself easily, however.

either l _ (Left  a) = l a
either _ r (Right b) = r b

isLeft (Left _) = True
isLeft _        = False

isRight (Right _) = True
isRight _         = False
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文