Skip to content

Instantly share code, notes, and snippets.

@fizruk
Last active March 20, 2017 18:51
Show Gist options
  • Save fizruk/a73cd387bdb46c2857157afb7ab86370 to your computer and use it in GitHub Desktop.
Save fizruk/a73cd387bdb46c2857157afb7ab86370 to your computer and use it in GitHub Desktop.
import Data.Monoid
newtype F a b = F (a -> b)
deriving (Functor)
instance Monoid b => Monoid (F a b) where
mempty = F mempty
mappend (F f) (F g) = F (f <> g)
instance (Bounded a, Enum a) => Foldable (F a) where
foldMap f (F g) = foldMap (f . g) [minBound..maxBound]
instance (Bounded a, Enum a) => Traversable (F a) where
traverse f (F g) = fmap (F . index) (traverse (f . g) [minBound..maxBound])
where
index xs x = xs !! fromEnum x
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment