Skip to content

Instantly share code, notes, and snippets.

@djleonskennedy
Last active March 27, 2019 15:56
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 djleonskennedy/38ad5157449defa8b52975d2955f9714 to your computer and use it in GitHub Desktop.
Save djleonskennedy/38ad5157449defa8b52975d2955f9714 to your computer and use it in GitHub Desktop.
Contravariant Functor how to use it in practice
import Data.Functor.Contravariant
-- newtype Predicate a = Predicate { getPredicate :: a -> Bool }
--instance Contravariant Predicate where
-- contramap f (Predicate p) = Predicate (p . f)
-- | `- First, map the input...
-- `----- then apply the predicate.
fb = Predicate (\x -> x > 20)
-- :t contramap
-- contramap :: Contravariant f => (a -> b) -> f b -> f a
-- :t contramap (\x -> x * 2)
-- contramap (\x -> x * 2) :: (Num b, Contravariant f) => f b -> f b
-- :t contramap (\x -> x * 2) a
-- contramap (\x -> x * 2) a :: (Ord b, Num b) => Predicate b
fa = contramap (\x -> x * 2) fb
faComposed = contramap (\x -> x + 5) . contramap (\x -> x * 2) $ fb -- composable
main = do
print $ getPredicate fa 2 -- Flase
print $ getPredicate faComposes 6 -- True
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment