A bifunctor is a type constructor with two covariant type parameters that can be "mapped" over. Examples include tuples and Either (a.k.a. Result, disjunction) types.
In haskell, the canonical bifunctors implementation is the
bifunctors package by Edward Kmett. It provides the Bifunctor
class with several common instances, and a number of other useful modules. The basic functions for working with Bifunctor
are first
, second
and bimap
, as seen in the following ghci
transcript:
> import Data.Bifunctor
> let pair = (5, "hi")
> first (*6) pair
(30,"hi")
> second (fmap toUpper) pair
(5,"HI")
> bimap (*6) (fmap toUpper) pair
(30,"HI")