如何将 Maybe 值注入 MaybeT
假设我有一些 foo :: Maybe Int
并且我想将它绑定到例如 bar :: Int ->; MaybeT (Writer String) Int
,这样做的惯用方法是什么?
我可以定义自己的 liftMaybe 函数,然后使用它,例如:
let liftMaybe = maybe (fail "Nothing") return in liftMaybe foo >>= bar
但是有没有更惯用(或至少简洁)的方法来做到这一点?
Say I have some foo :: Maybe Int
and I want to bind it for example with bar :: Int -> MaybeT (Writer String) Int
, what would be the idiomatic way to do that?
I could define my own liftMaybe
function, and then use that, like:
let liftMaybe = maybe (fail "Nothing") return in liftMaybe foo >>= bar
But is there a more idiomatic (or at least concise) way to do that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为很遗憾它没有标准名称,但是 a hoogle 搜索,我们看到
relude
包使用hoistMaybe
:更通用的形式是
优于使用
fail
的形式。我只是将它放在某个方便的模块中。I think it's a shame it doesn't have a standard name, however doing a hoogle search, we see that the
relude
packages useshoistMaybe
:A more general form is
which is preferable to the use of
fail
. I'd just put it in a convenient module somewhere.