Haskell Language Bifunctor first and second


If mapping covariantly over only the first argument, or only the second argument, is desired, then first or second ought to be used (in lieu of bimap).

first :: Bifunctor f => (a -> c) -> f a b -> f c b
first f = bimap f id

second :: Bifunctor f => (b -> d) -> f a b -> f a d
second g = bimap id g

For example,

ghci> second (+ 2) (Right 40)
Right 42
ghci> second (+ 2) (Left "uh oh")
Left "uh oh"