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)

Phantom type for bifunctor composition.

pub type BiCompF(bf, fu, gu)

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)

Phantom type for Either Bifunctor.

pub type EitherBF

Phantom type for Pair Bifunctor.

pub type PairBF

Phantom type for Tuple Bifunctor.

pub type TupleBF

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 either_bifunctor() -> Bifunctor(
  EitherBF,
  a,
  b,
  c,
  d,
  Either(a, b),
  Either(c, d),
)

Either Bifunctor.

Examples

let show_or_double = either_bifunctor().bimap(int.to_string, fn(x) { x * 2 })

Left(10)
|> show_or_double()
// -> should.equal(cat.Left("10"))
Right(10)
|> show_or_double()
// -> Right(20)
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 pair_bifunctor() -> Bifunctor(
  PairBF,
  a,
  b,
  c,
  d,
  Pair(a, b),
  Pair(c, d),
)

Pair Bifunctor.

Examples

Pair(2, 3)
|> pair_bifunctor().bimap(fn(x) { x % 3 }, int.to_string)()
// -> Pair(2, "3")
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")
pub fn tuple_bifunctor() -> Bifunctor(
  TupleBF,
  a,
  b,
  c,
  d,
  #(a, b),
  #(c, d),
)

Tuple Bifunctor.

Examples

#(6, False)
|> tuple_bifunctor().bimap(fn(x) { [x] }, fn(b) { bool.to_string(b) })()
// -> #([6], "False")
Search Document