Skip to content

Instantly share code, notes, and snippets.

@TGOlson
Created June 2, 2015 20:29
Show Gist options
  • Select an option

  • Save TGOlson/9b80f8153e2e411a7511 to your computer and use it in GitHub Desktop.

Select an option

Save TGOlson/9b80f8153e2e411a7511 to your computer and use it in GitHub Desktop.
Finder Monoid
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