Created
June 2, 2015 20:29
-
-
Save TGOlson/9b80f8153e2e411a7511 to your computer and use it in GitHub Desktop.
Finder Monoid
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import Data.Monoid | |
| import Data.Maybe | |
| import Data.List | |
| data Finder a = Finder ([a] -> Maybe a) | |
| instance Monoid (Finder a) where | |
| mempty = Finder (const Nothing) | |
| (Finder f1) `mappend` (Finder f2) = Finder (\xs -> if isNothing (f1 xs) then f2 xs else f1 xs) | |
| useFinder :: Finder a -> [a] -> Maybe a | |
| useFinder (Finder f) = f | |
| makeFinder :: (a -> Bool) -> Finder a | |
| makeFinder = Finder . find | |
| -- Example | |
| finders = map makeFinder [(<1), (==7), (>8)] | |
| finder = mconcat finders | |
| xs = [2, 3, 4, 6, 9] | |
| -- useFinder finder xs | |
| -- => Just 9 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment