Last active
March 27, 2019 15:56
-
-
Save djleonskennedy/38ad5157449defa8b52975d2955f9714 to your computer and use it in GitHub Desktop.
Contravariant Functor how to use it in practice
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
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