Skip to content

Instantly share code, notes, and snippets.

@jchia
Last active June 20, 2018 15:33
Show Gist options
  • Save jchia/7daa8f4800748ed643c17646e8f7677c to your computer and use it in GitHub Desktop.
Save jchia/7daa8f4800748ed643c17646e8f7677c to your computer and use it in GitHub Desktop.
data Foo = Foo { alpha :: Int, beta :: Int, gamma :: Int
, delta :: Int, eta :: Int } deriving (Eq, Ord, Show)
plusOn :: Num a => (Foo -> a) -> Foo -> Foo -> a
plusOn = on (+)
maxOn :: Ord a => (Foo -> a) -> Foo -> Foo -> a
maxOn = on max
newtype AddFoo = AddFoo { getAddFoo :: Foo }
instance Semigroup AddFoo where
AddFoo x <> AddFoo y =
AddFoo $ Foo (plusOn alpha x y) (maxOn beta x y) (plusOn gamma x y)
(maxOn delta x y) (plusOn eta x y)
instance Monoid AddFoo where
mempty = AddFoo (Foo 0 0 0 0 0)
mappend = (<>)
newtype MaxFoo = MaxFoo { getMaxFoo :: Foo }
instance Semigroup MaxFoo where
MaxFoo x <> MaxFoo y =
MaxFoo $ Foo (maxOn alpha x y) (maxOn beta x y) (maxOn gamma x y)
(maxOn delta x y) (maxOn eta x y)
instance Monoid MaxFoo where
mempty = MaxFoo (Foo 0 0 0 0 0)
mappend = (<>)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment