Skip to content

Instantly share code, notes, and snippets.

@Kobold
Created August 8, 2010 04:57
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 Kobold/513609 to your computer and use it in GitHub Desktop.
Save Kobold/513609 to your computer and use it in GitHub Desktop.
-- Playing around with ContraFunctors
-- http://blog.ezyang.com/2010/07/flipping-arrows-in-coburger-king/
class ContraFunctor t where
contramap :: (a -> b) -> t b -> t a
newtype ContraF a b = ContraF (b -> a)
instance ContraFunctor (ContraF a) where
contramap g (ContraF f) = ContraF (f . g)
coreplicate :: a -> ContraF [a] Int
coreplicate x = ContraF (\n -> replicate n x)
apply :: ContraF a b -> b -> a
apply (ContraF f) = f
contraReplace :: b -> [a] -> [b]
contraReplace x = apply (contramap length (coreplicate x))
replace :: a -> [b] -> [a]
replace e xs = replicate (length xs) e
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment