(,)
is an example of a type that has a Bifunctor
instance.
instance Bifunctor (,) where
bimap f g (x, y) = (f x, g y)
bimap
takes a pair of functions and applies them to the tuple's respective components.
bimap (+ 2) (++ "nie") (3, "john") --> (5,"johnnie")
bimap ceiling length (3.5 :: Double, "john" :: String) --> (4,4)
Either
Either
's instance of Bifunctor
selects one of the two functions to apply depending on whether the value is Left
or Right
.
instance Bifunctor Either where
bimap f g (Left x) = Left (f x)
bimap f g (Right y) = Right (g y)