Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我发现自己想要这个小小的功能,但它似乎不在Data.Maybe. 是不是在别的地方?
Data.Maybe
splat :: (a -> Bool) -> a -> Maybe a splat c a | c a = Just a | otherwise = Nothing
splat :: MonadPlus m => (a -> Bool) -> a -> m a splat c x = guard (c x) >> return x
如果您决定想要这个,那将是一个更短、更笼统的定义。但是,只要guard在需要的地方直接使用它可能会更方便。
guard
包monadplus正好包含这个函数,命名为partial:
partial
partial :: (a -> Bool) -> a -> Maybe a