我发现自己希望这个小小的功能,但它似乎并没有在Data.Maybe
。 是不是别的地方?
splat :: (a -> Bool) -> a -> Maybe a
splat c a
| c a = Just a
| otherwise = Nothing
我发现自己希望这个小小的功能,但它似乎并没有在Data.Maybe
。 是不是别的地方?
splat :: (a -> Bool) -> a -> Maybe a
splat c a
| c a = Just a
| otherwise = Nothing
该软件包monadplus正好包含此功能,命名为partial
:
partial :: (a -> Bool) -> a -> Maybe a
splat :: MonadPlus m => (a -> Bool) -> a -> m a
splat c x = guard (c x) >> return x
将是一个更短,更一般的定义,如果你决定你想要这个。 但是,仅仅用guard
在线无论你需要它可能是更方便呢。