cartesian
- class Hom(*xs, **ys)
Hom functor: maps type pairs to their callable types.
Class Methods
new :
... -> Typefmap :
(A -> B) -> (X -> A) -> X -> Bcofmap :
(X -> Y) -> (Y -> A) -> X -> Acompose :
(A -> B) -> (B -> C) -> A -> Ceval :
A -> (A -> B) -> B
The type
Hom(A, B)describes callables with input inAand output inB, it can be used as a decorator to type a function definition.Example:
>>> @Hom(Int, Str): ... def bar(n): ... return n * "|" ... >>> bar Int -> Str : bar >>> bar(8) Str : '||||||||'
- Object
alias of
HomObject
- classmethod compose(f, *fs)
Pipe a collection of functions.
The usual composition of two functions
f @ gis obtained asHom.compose(g, f). Applied on an inputx, this returned pipe satisfies:>>> Hom.compose(f, *fs)(x) == Hom.compose(*fs)(f(x)) True
Note: Composition is made associative by storing the sequence of functions in a flat tuple. This also avoids nesting function closures.
- classmethod curry(f, xs)
Curried function applied to n-ary input
xsfor n < arity.Example:
>>> Int.add Int -> Int -> Int: add >>> Int.add(2, 3) Int: 5 >>> Int.add(2): Int -> Int: add 2 >>> Int.add(2)(3) Int: 3
- classmethod eval(x, f)
Evaluate
fon inputx.- Parameters:
x (A)
f (callable[A, B])
- Return type:
B
- src
alias of
Type
- tgt
alias of
Type
- class Prod(*xs, **ys)
Class Methods
new :
... -> Typefmap :
((A -> B), ...) -> (A, ...:src) -> (B, ...:tgt)
- class Object(*xs)
Product base type:
tuplealias.
- Unit
alias of
()
- classmethod branch(f, *fs)
Universal property of categorical products.
Given a collection of arrows with the same source
X, return an arrow fromXto the product of targets.(X -> A, X -> B, …) -> X -> (A, B, …)
The terminal arrow to
(A, B, ...)has the input maps as projections.- Parameters:
f (Callable)
fs (Callable)
- Return type:
Callable
- classmethod fmap(*fs)
Map a collection of functions to their joint action.
Given maps
f : X -> A,g : Y -> B, … return the product map(f, g, …) : (X, Y, …) -> (A, B, …)
- Parameters:
fs (Callable)
- Return type:
Callable
- class Writer(W, bases=(), dct=None)
Constructor of
Writerfunctors.Given a writing type
W, the functorWriter Wmaps any typeAto the pair type(W, A).When
Wis a monoid, the functorWriter Wis also a monad:- unitA -> (W, A)
- a -> (1, a)
- join(W, (W, A)) -> (W, A)
- (v, (w, a)) -> (v
opw, a)
A particular example is
Str, which is useful for writing logs or error messages.- Parameters:
W (type)
- class WriterFunctor(*xs, **ys)
Base class for
Writerfunctors.- class Object(*xs)
- classmethod fmap(*fs)
Map a collection of functions to their joint action.
Given maps
f : X -> A,g : Y -> B, … return the product map(f, g, …) : (X, Y, …) -> (A, B, …)
- class Either(*xs, **ys)
Direct sum of types (union).
Class Methods
new :
... -> Typefmap :
(A -> B) -> (A) -> (B)unit :
A -> (A)lift2 :
(A -> B -> C) -> (A) -> (B) -> (C)join :
((A)) -> (A)bind :
(A) -> (A -> (B)) -> (B)
Example:
>>> Either(Int, Str) Either : (Int | Str) >>> (Int | Str) is Either(Int, Str) True >>> x = List(int | str)(["Hello", 12, "World!", 8]) >>> foo = Hom(str, int)(len) >>> bar = Hom(int, str)(lambda x: "|" * x) ### functorial map: type-wise application >>> Either.fmap(bar, foo) (int | str | ...) -> (str | int | ...) : (λ | len | ...) >>> List.fmap(Either.fmap(bar, foo))(x) List (str | int | ...) : [1 : 5, 0 : ||||||||||||, 1 : 6, 0 : ||||||||]
- classmethod fmap(*fs)
Apply a mapping on each case.
- classmethod gather(f, *fs)
Universal property of categorical sums.
Given a collection of arrows with the same target
Y, return an arrow from the sum of sources toY.(A -> Y, B -> Y, …) -> (A | B | …) -> Y
The initial arrow from
(A | B | ...)has the input maps as as restrictions.- Parameters:
f (Callable)
fs (Callable)
- Return type:
Callable
- classmethod join(EEx)
Access unwrapped either value.
- Parameters:
EEx (cls(cls('A', ...), ...))
- Return type:
cls(‘A’, …)
- classmethod new(*As)
Type union.