-->

这是否布尔-生产者也许制片功能出现在任何公共库?(Does this Bool-producer t

2019-10-20 10:02发布

我发现自己希望这个小小的功能,但它似乎并没有在Data.Maybe 。 是不是别的地方?

splat :: (a -> Bool) -> a -> Maybe a
splat c a
  | c a       = Just a
  | otherwise = Nothing

Answer 1:

该软件包monadplus正好包含此功能,命名为partial

partial :: (a -> Bool) -> a -> Maybe a


Answer 2:

splat :: MonadPlus m => (a -> Bool) -> a -> m a
splat c x = guard (c x) >> return x

将是一个更短,更一般的定义,如果你决定你想要这个。 但是,仅仅用guard在线无论你需要它可能是更方便呢。



文章来源: Does this Bool-producer to Maybe-producer function appear in any common library?
标签: haskell maybe