问题标签 [maybe]

For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.

0 投票
3 回答
3431 浏览

haskell - 处理 Maybe Bool 值

让我们说,我有两个Maybe Bool价值,我想实现以下功能:

  • 如果两者都是Just值,我想||在它们之间执行值。
  • 如果其中一个是值,Nothing另一个是Just值,那么我希望该Just值作为输出。
  • 如果它们都是Nothing,那么我想要Just False作为输出。

我知道这可以使用模式匹配来实现。但是是否可以使用任何一元函数来获得结果?

liftM2适用于这种情况:

但是当任何一个输入是(我想要另一个值)时liftM2会产生。IE:NothingNothingJust

但我想Just False在上述情况下。

是否可以使用任何一元函数来做到这一点?

0 投票
2 回答
105 浏览

haskell - “应用默认值”的抽象形式

我想轻松地从“失败”数据类型中取出一个值,或者在失败的情况下使用默认值。

这是我的实现Maybe

predTrue如果name映射到xin则返回myMap

但是就像在 Haskell 中通常的情况一样,有一种我不知道的更抽象的方法来做到这一点。任何人?

0 投票
2 回答
163 浏览

haskell - Haskell Monad 等价物

如果我们选择它们作为 的数据类型,monadPlusSDif这两个函数是否等效?MaybeMonadPlus

0 投票
2 回答
101 浏览

haskell - Just and dot in winghci

为什么这个工作...

...但不是这个?

我尝试将截断解析为一个简单的 Double -> Int:

……但这似乎不是问题……

非常感谢!

0 投票
3 回答
3492 浏览

haskell - Multiplying the value within two "Maybe" monads?

I'm currently in the process of trying to learn Haskell, and ran into an odd issue regarding the Maybe monad which I can't seem to figure out.

As an experiment, I'm currently trying to take a string, convert each letter to an arbitrary number, and multiply/combine them together. Here's what I have so far:

When I try running this, it returns the following error:

I'm not 100% sure why this error is occurring, or what it exactly means, though I suspect it might be because I'm trying to multiply two Maybe monads together -- if I change the definition of test to the following, the program compiles and runs fine:

I also tried changing the type declaration to the below, but that didn't work either.

I'm not really sure how to go about fixing this error. I'm fairly new to Haskell, so it might just be something really simple that I'm missing, or that I've structured everything completely wrong, but this is stumping me. What am I doing wrong?

0 投票
3 回答
326 浏览

haskell - Haskell 中的偏函数图:a -> Maybe b -> [a] -> [(a, b)]

给定一个偏函数f和一个参数列表,xs我正在寻找定义的对列表。这似乎是一件很自然的事情,但到目前为止我还没有优雅地表达它。我想知道 Maybe/Monad/Applicative/... 区域是否有什么可以提供帮助的?以下工作,但似乎有点明确。(x, f(x))f

是否liftMaybe存在其他名称?我发现了其中一些的以下重新表述:

0 投票
1 回答
192 浏览

haskell - 为什么这两个 Haskell “展开”功能不同?

我正在学习 Haskell,现在我正在用 Maybe 类做一个练习。我必须创建一个函数,将 f("Maybe function") 重复应用于 a(及其以下结果),直到f a 返回Nothing。例如 f a0 = Just a1,f a1= Just a2,...,f an = Nothing。然后

我已经尝试过,并且我得到了:

问题是解决方案是:

而且我的程序不像解决方案那样工作。也许我使用了错误的“案例”,但我不确定。

0 投票
1 回答
917 浏览

haskell - 不在范围内:readMaybe,我应该导入哪个库?

我正在尝试使用函数 readMaybe,它应该在 Text.Read 库中,但是当我编译时收到此消息:

谁能告诉我我做错了什么?谢谢 ;)

0 投票
2 回答
1428 浏览

haskell - 压扁可能

我经常有一个具有“也许没什么 someFunc”模式的代码:

这里parseUSDate有类型Text -> Maybe Date

Aeson 解析显然返回Maybe Text

所以在我看来,我需要通过Maybe这里的 2 层。而且我不知道如何以任何其他方式做到这一点,除了maybe Nothing someFunc模式。

我是否缺少一些明显的“扁平化”或我可以在这里使用的任何功能?

编辑:感谢 Alexey 的回答。

这正是我想要的。这是最终结果:

0 投票
2 回答
4157 浏览

scala - Compose partial functions

I have two PartialFunctions f and g. They have no side effects and are quick to execute. What's the best way to compose them into another partial function h such that h.isDefinedAt(x) iff f.isDefinedAt(x) && g.isDefinedAt(f(x))?

It's also OK if h is a function returning an Option rather than a partial function.

I'm disappointed that f andThen g does not do what I want: