cat/profunctor
Profunctor
type {minimal implementation - dimap
}
Default implementations: lmap
and rmap
.
Profunctor instance for function type
.
Types
Profunctor
.
A type constructor p
of two arguments, which is contra-functorial
in the first
argument and functorial
in the second
.
// Haskel definition
class Profunctor p where
dimap :: (a -> b) -> (c -> d) -> p b c -> p a d
dimap f g = lmap f . rmap g
lmap :: (a -> b) -> p b c -> p a c
lmap f = dimap f id
rmap :: (c -> d) -> p a c -> p a d
rmap = dimap id
pub type Profunctor(p, a, b, c, d, pbc, pad) {
Profunctor(dimap: fn(fn(a) -> b, fn(c) -> d) -> fn(pbc) -> pad)
}
Constructors
-
Profunctor(dimap: fn(fn(a) -> b, fn(c) -> d) -> fn(pbc) -> pad)
Functions
pub fn function_profunctor() -> Profunctor(
ArrowPro,
a,
b,
c,
d,
fn(b) -> c,
fn(a) -> d,
)
Profunctor
instance for the function type
(->).
// Haskell implementation
instance Profunctor (->) where
dimap ab cd bc = cd . bc . ab
lmap = flip (.)
rmap = (.)
Examples
// Function a -> b ([Int] -> Int)
let f = list.fold(_, 0, fn(x, y) { x + y })
// Function c -> d (Bool -> String)
let g = bool.to_string
// Profunctor pbc: Function b -> c (Int -> Bool)
let h = fn(x) { x % 2 == 0 }
// Profunctor pad: Function a -> d ([Int] -> String)
let z = function_profunctor().dimap(f, g)(h)
[1, 2, 3]
|> z
// -> "True"
[1, 2]
|> z
// -> "False"
pub fn lmap(
profunctor: Profunctor(a, b, c, d, d, e, f),
) -> fn(fn(b) -> c) -> fn(e) -> f
Default function lmap
for a given Profunctor instance.
Examples
// Function a -> b ([Int] -> Int)
let f = list.fold(_, 0, fn(x, y) { x + y })
// Profunctor pbc: Function b -> c (Int -> Bool)
let h = fn(x) { x % 2 == 0 }
// Profunctor pac: Function a -> c ([Int] -> Bool)
let z = lmap(function_profunctor())(f)(h)
[1, 2, 3]
|> z
// -> True
[1, 2]
|> z
// -> False
pub fn rmap(
profunctor: Profunctor(a, b, b, c, d, e, f),
) -> fn(fn(c) -> d) -> fn(e) -> f
Default function rmap
for a given Profunctor instance.
Examples
// Function c -> d (Bool -> String)
let g = bool.to_string
// Profunctor pac: Function a -> c ([Int] -> Bool)
let h = fn(x) { list.length(x) % 2 == 0 }
// Profunctor pad: Function ([Int] -> String)
let z = rmap(function_profunctor())(g)(h)
[1, 2, 3]
|> z
// -> "False"
[1, 2]
|> z
// -> "True"