Created
August 8, 2010 04:57
-
-
Save Kobold/513609 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- 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