cat/bifunctor
Bifunctor
type {minimal implementation - bimap
}.
Default functions: first
and second
(defined in terms of bimap).
Bifunctor composition
.
Types
Bifunctor composition type.
newtype BiComp bf fu gu a b = BiComp (bf (fu a ) (gu b))
pub type BiComp(bf, fu, gu, a, b, fua, gub, bifgab) {
BiComp(bifgab)
}
Constructors
-
BiComp(bifgab)
Bifunctor
type in gleam.
// Haskell type class
class Bifunctor f where
bimap :: (a -> c) -> (b -> d) -> f a b -> f c d
first :: (a -> c) -> f a b -> f c b
first g = bimap g id
second :: (b -> d) -> f a b -> f a d
second = bimap id
pub type Bifunctor(f, a, b, c, d, fab, fcd) {
Bifunctor(bimap: fn(fn(a) -> c, fn(b) -> d) -> fn(fab) -> fcd)
}
Constructors
-
Bifunctor(bimap: fn(fn(a) -> c, fn(b) -> d) -> fn(fab) -> fcd)
Functions
pub fn bifunctor_compose(
bf_instance: Bifunctor(a, b, c, d, e, f, g),
fu_instance: Functor(h, i, j, b, d),
gu_instance: Functor(k, l, m, c, e),
) -> Bifunctor(BiCompF(a, h, k), i, l, j, m, f, g)
Composition of a Bifunctor
with 2 Functors
.
// Haskell instance
instance (Bifunctor bf, Functor fu, Functor gu) =>
Bifunctor (BiComp bf fu gu) where
bimap f1 f2 (BiComp x) = BiComp ((bimap (fmap f1) (fmap f2)) x)
Examples
Right(Identity(3))
|> bifunctor_compose(either_bifunctor(), const_functor(), identity_functor())
.bimap(fn(_) { panic }, fn(x) { x % 2 == 0 })
// -> Right(Identity(False))
pub fn first(
bifunctor: Bifunctor(a, b, c, d, c, e, f),
) -> fn(fn(b) -> d) -> fn(e) -> f
Default function first
for a given Bifunctor instance.
Examples
let first_show =
int.to_string
|> {
pair_bifunctor()
|> first
}
first_show(Pair(8, 9))
// -> Pair("8", 9)
pub fn second(
bifunctor: Bifunctor(a, b, c, b, d, e, f),
) -> fn(fn(c) -> d) -> fn(e) -> f
Default function second
for a given Bifunctor instance.
Examples
let second_show = second(either_bifunctor())(int.to_string)
Left(8)
|> second_show
// -> Left(8)
Right(9)
|> second_show
// -> Right("9")