category_theory package#
Submodules#
category_theory.applicative module#
- class category_theory.applicative.Just(value: a)[source]#
-
- apply(func: Applicative[Callable[[a], b]]) Maybe[b] [source]#
- class category_theory.applicative.Maybe(value: Any)[source]#
Bases:
Applicative
[a
],Maybe
[a
]
- class category_theory.applicative.Validation(value: Any)[source]#
Bases:
Applicative
category_theory.core module#
- class category_theory.core.Applicative(value: Any)[source]#
Bases:
Functor
[a
]An applicative functor is a functor that
- Parameters:
Functor ([type]) – [description]
- abstract apply(func: Applicative[Callable[[a], b]]) Applicative[b] [source]#
- abstract static pure(value: a) Applicative[a] [source]#
- class category_theory.core.CommutativeMonoid(value: a)[source]#
Bases:
Monoid
[a
]A CommutativeMonoid is just a Monoid where the binary operation is commutative, i.e \(a + b = b + a\)
- class category_theory.core.Functor(value: Any)[source]#
Bases:
ABC
,Generic
[a
],Atomic
A Functor is a mapping between categories.
In programming sense we typically think of a Functor as a container, parameterized by a type a.
Examples for Functors are Lists and Queues which are structures that we typically would think of as containers, but other examples of Functors are Maybe, Either and Promise which are less container like. The key features is that the structure contains something, and given a function, we can apply it to the thing inside our structure. Note that the thing inside the structure has type a.
Example
For example a could be string and the functor could be List, in which case we have a list of strings.
- class category_theory.core.Monoid(value: a)[source]#
Bases:
ABC
,Generic
[a
],Atomic
A monoid is a type \(a\) equipped with a binary operation \(+ : a \times a \rightarrow a\) such that the two following axioms holds:
\(x\) is associative: \((a + b) + c = a + (b + c)\)
There exists an identity element \(e\), such that a + e = e + a = a
In this interface the binary operation used is +, so that any class that wants inherit from Monoid need to implement __add__ and __radd__. For the identity element we require a staticmethod called e.
A key features of the Monoid structure which separates it from a semigroup is the presence of an identity element.
category_theory.functor module#
- class category_theory.functor.List(value: List[a])[source]#
Bases:
Functor
[a
]- map(func: Callable[[a], b]) List[b] [source]#
Lets suppose the functor is a List and a is int, so that we have a list of integers. Then one possible \(f\) could be
\[\begin{split}f : \text{int} \mapsto \text{bool} f(x) = \begin{cases} \text{True, if } x | 2, \\ \text{False, if } x \nmid 2. \end{cases}\end{split}\]In other \(f\) is the function better known as is_even. If The list is given by
>> F = List([1, 2, 3, 3])
then
>> f = lambda x : x % 2 == 0 >> F.map(f) List([False, True, False, True])
- Returns:
A new list after applying the map
- Return type:
List[b]
category_theory.monoid module#
- class category_theory.monoid.All(value: a)[source]#
Bases:
CommutativeMonoid
[bool
]
- class category_theory.monoid.Any(value: a)[source]#
Bases:
CommutativeMonoid
[bool
]
- class category_theory.monoid.IntPlus(value: a)[source]#
Bases:
CommutativeMonoid
[int
]Monoid whose values are integers. Binary operation is the plus operation and identity element is 0
- class category_theory.monoid.IntProd(value: a)[source]#
Bases:
CommutativeMonoid
[int
]Monoid whose values are integers. Binary operation is the multiplication operation and identity element is 1
- class category_theory.monoid.MaybeIntPlus(value: a)[source]#
Bases:
CommutativeMonoid
[int
|None
]Monoid whose values are maybe integers. This means that the value can be int or None. Binary operation is the plus operation is the value is of type int and returns None otherwise. Identity element is MaybeIntPlus(0)
- static e() MaybeIntPlus [source]#
Identity element
- class category_theory.monoid.MaybeIntProd(value: a)[source]#
Bases:
CommutativeMonoid
[int
|None
]Monoid whose values are maybe integers. This means that the value can be int or None. Binary operation is the multiplication operation is the value is of type int and returns None otherwise. Identity element is MaybeIntProd(1)
- static e() MaybeIntProd [source]#
Identity element
category_theory.operations module#
- category_theory.operations.compose(g: Callable[[b], c], f: Callable[[a], b]) Callable[[a], c] [source]#
Compose two functions \(g\) and \(f\), \(g \circ f\).
We have the following two functions
\[\begin{split}g: b \mapsto c \\ f: a \mapsto b\end{split}\]and through this function we create the composition
\[g \circ f: a \mapsto c\]- Parameters:
g (Callable[[b], c]) – Second function to be applied
f (Callable[[a], b]) – First function to be applied
- Returns:
The composition of f and g
- Return type:
Callable[[a], c]
- category_theory.operations.fold(lst: Iterable[Monoid], cls: Type) Any [source]#
Fold an iterable of Monoids together using the identity element as initial.
- Parameters:
lst (Iterable[Monoid]) – An iterable of monoids that should be squashed
cls (Type) – The type of the monoid
- Returns:
The reduction of the iterable.
- Return type:
Any
- category_theory.operations.foldr(lst: Iterable[Monoid], cls: Type) Any [source]#
Same as fold, but from the right
- Parameters:
lst (Iterable[Monoid]) – An iterable of monoids that should be squashed
cls (Type) – The type of the monoid
- Returns:
The reduction of the iterable.
- Return type:
Any
- category_theory.operations.identity(x: a) a [source]#
Identity function
\[\begin{split}f : a \mapsto a \\ f(x) = x\end{split}\]- Parameters:
x (a) – Input variable
- Returns:
Output variable \(f(x)\)
- Return type:
a
- category_theory.operations.is_nothing(x: a | None) TypeGuard[a] [source]#
Check if a variable should be declared as Nothing.
- Parameters:
x (Optional[a]) – The variable
- Returns:
Returns true if the variable is nothing and false otherwise. Note that er make use of TypGuard here which tell the type checker than if the thing we get in is None, then this function will return True
- Return type:
TypeGuard[a]
category_theory.par_operations module#
- category_theory.par_operations.chunkify(chunk_size: int, iterable: Iterable[Any], fillvalue: Any | None = None) Iterable[Iterable[Any]] [source]#
Split iterable into chunks of size chunk_size. If all chunks does not add up, it will use the fillvalue in the remaining spots
- Parameters:
chunk_size (int) – Number of elements in each chunk
iterable (Iterable[Any]) – The iterable that should be chunkified
fillvalue (Any, optional) – A value to put in those places when the chunk size does not add up, by default None
- Returns:
A list of new iterables, each being of size chunk_size.
- Return type:
Iterable[Iterable[Any]]
Example
>> iterable = (1, 2, 3, 4, 5) >> chunkify(3, iterable, fillvalue=None) ((1, 2, 3), (4, 5, None))
- category_theory.par_operations.fold(iterable: Iterable[Monoid], cls: Type, chunk_size=1000) Any [source]#
Fold an iterable of Monoids together using the identity element as initial.
- Parameters:
iterable (Iterable[Monoid]) – An iterable of monoids that should be squashed
cls (Type) – The type of the monoid
- Returns:
The reduction of the iterable.
- Return type:
Any
Module contents#
- category_theory.fold(lst: Iterable[Monoid], cls: Type) Any [source]#
Fold an iterable of Monoids together using the identity element as initial.
- Parameters:
lst (Iterable[Monoid]) – An iterable of monoids that should be squashed
cls (Type) – The type of the monoid
- Returns:
The reduction of the iterable.
- Return type:
Any
- category_theory.foldr(lst: Iterable[Monoid], cls: Type) Any [source]#
Same as fold, but from the right
- Parameters:
lst (Iterable[Monoid]) – An iterable of monoids that should be squashed
cls (Type) – The type of the monoid
- Returns:
The reduction of the iterable.
- Return type:
Any
- category_theory.par_fold(iterable: Iterable[Monoid], cls: Type, chunk_size=1000) Any #
Fold an iterable of Monoids together using the identity element as initial.
- Parameters:
iterable (Iterable[Monoid]) – An iterable of monoids that should be squashed
cls (Type) – The type of the monoid
- Returns:
The reduction of the iterable.
- Return type:
Any