Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Data.Bifunctor

Functors of two arguments: map both payloads of a two-parameter type at once.

bimap transforms each side of a p(a, b) with its own function, and the one-sided maps are the two specializations with the identity on the other side. The carrier here is Result, whose two sides are the success and the error; map_first is then Data.Result‘s map_result and map_second its map_err, stated once against the class so a function generic in the bifunctor works for any future two-parameter carrier. Effects ride the mapped functions’ shared row, like fmap. Opt-in: not in Base.

Type Classes

Bifunctor

class Bifunctor(p)
  bimap : ((a) -> c ! {| e}, (b) -> d ! {| e}, p(a, b)) -> p(c, d) ! {| e}

A two-argument functor: bimap(id, id, p) is p, and bimap composes sidewise.

Instances

bifunctorResult

instance bifunctorResult : Bifunctor(Result)

Functions and Values

map_first

map_first : forall e0 a b c d. ((a) -> b ! {e0}, c(a, d)) -> c(b, d) ! {e0} given Data.Bifunctor.Bifunctor(c)

Map the first side only.

(map_first(\(x) -> x + 1, Ok(1)), map_first(\(x) -> x + 1, Err("e")))
(Ok(2), Err(e))

map_second

map_second : forall e0 a b c d. ((a) -> b ! {e0}, c(d, a)) -> c(d, b) ! {e0} given Data.Bifunctor.Bifunctor(c)

Map the second side only.

(map_second(\(m) -> concat(m, "!"), Err("bad")), map_second(\(m) -> m, Ok(1)))
(Err(bad!), Ok(1))