public
Created

  • Download Gist
Chainable.hs
Haskell
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
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

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.