cat/instances/contravariant
Op type
and its Contravariant instance.
Types
Type for reverse
functions.
type Op r a = a -> r
Examples
let o = Op(fn(x) { x % 2 == 1 })
o.apply(6)
// -> False
pub type Op(r, a) {
Op(apply: fn(a) -> r)
}
Constructors
-
Op(apply: fn(a) -> r)
Functions
pub fn op_contravariant() -> Contravariant(
OpC(a),
b,
c,
Op(a, b),
Op(a, c),
)
Op Contravariant Instance
.
// Haskell implementation
instance Contravariant (Op r) where
contramap :: (b -> a) -> Op r a -> Op r b
contramap f g = g∘. f
Examples
let f = fn(b) {
case b {
True -> 2
False -> 4
}
}
let original = Op(fn(x) { int.to_string(x * 2) })
let result = op_contravariant().contramap(f)(original)
result.apply(False)
// -> "8"