Skip to content

Instantly share code, notes, and snippets.

@rampion
Created February 20, 2012 20:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rampion/1871093 to your computer and use it in GitHub Desktop.
Save rampion/1871093 to your computer and use it in GitHub Desktop.
module Chainable (
(==.), (.==.), (.==),
(/=.), (./=.), (./=),
(<.), (.<.), (.<),
(>.), (.>.), (.>),
(<=.), (.<=.), (.<=),
(>=.), (.>=.), (.>=),
) where
-- start Eq chain
(==.),(/=.) :: Eq a => a -> a -> Maybe a
(==.) = start (==)
(/=.) = start (/=)
-- continue Eq chain
(.==.),(./=.) :: Eq a => Maybe a -> a -> Maybe a
(.==.) = continue (==)
(./=.) = continue (/=)
-- end Eq chain
(.==),(./=) :: Eq a => Maybe a -> a -> Bool
(.==) = end (==)
(./=) = end (/=)
-- start Ord chain
(<.),(>.),(<=.),(>=.) :: Ord a => a -> a -> Maybe a
(<.) = start (<)
(>.) = start (>)
(<=.) = start (<=)
(>=.) = start (>=)
-- continue Ord chain
(.<.),(.>.),(.<=.),(.>=.) :: Ord a => Maybe a -> a -> Maybe a
(.<.) = continue (<)
(.>.) = continue (>)
(.<=.) = continue (<=)
(.>=.) = continue (>=)
-- end Ord chain
(.<),(.>),(.<=),(.>=) :: Ord a => Maybe a -> a -> Bool
(.<) = end (<)
(.>) = end (>)
(.<=) = end (<=)
(.>=) = end (>=)
-- convenience function
start :: (a -> a -> Bool) -> a -> a -> Maybe a
start f a b = if f a b then Just b else Nothing
continue :: (a -> a -> Bool) -> Maybe a -> a -> Maybe a
continue f Nothing b = Nothing
continue f (Just a) b = start f a b
end :: (a -> a -> Bool) -> Maybe a -> a -> Bool
end f Nothing b = False
end f (Just a) b = f a b
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment