Python implementation of the Scala standard library.
Project description
category
Installation
pip install works for this library.
pip install pycategory
Usage
Here is an example of how to use the category
package:
curry
from category import curry
@curry
def function(arg1: int, /, arg2: int, arg3: int = 3, *, arg4: int = 4) -> int:
return arg1 + arg2 + arg3 + arg4
function2 = function # (int) -> ((int) -> int)
function1 = function2(1) # (int) -> int
result = function1(2) # int
Pipeline
from category import Pipeline, curry
def squared(value: int) -> int:
return value**2
assert 42**2**2 == ~(Pipeline(42) >> squared >> squared)
@Pipeline
@curry
def cubed(arg1: int, arg2: int, arg3: int) -> int:
return arg1 * arg2 * arg3
assert 42**3 == ~(cubed << 42 << 42 << 42)
Either
from category import Either, EitherDo, Frame, Left, Right
class Error(Frame):
...
@MEither.do
def context(value: int) -> EitherDo[Error, int]:
one = yield from Left[Error, int](Error(unmask=("value",)))
two = 2
three = yield from Right[Error, int](3)
return one + two + three
match context(42).pattern:
case Left(value):
print(f"Left case {value}")
case Right(value):
print(f"Right case {value}")
Option
from category import VOID, Option, OptionDo, Some, Void
@Option.do
def context() -> OptionDo[int]:
one = yield from VOID
two = 2
three = yield from Some[int](3)
return one + two + three
match context().pattern:
case Void():
print("Void case")
case Some(value):
print(f"Some case {value}")
Try
from category import Failure, Success, Try, TryDo
@Try.hold(unmask=("value",))
def hold_context(value: int, /) -> int:
if not value:
raise Exception("error")
return value
@Try.do
def context() -> TryDo[int]:
one = yield from hold_context(0)
two = 2
three = yield from Success[int](3)
return one + two + three
match context().pattern:
case Failure() as failure:
print(f"Failure case {failure.exception}")
case Success(value):
print(f"Success case {value}")
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
pycategory-0.1.1.tar.gz
(14.9 kB
view hashes)
Built Distribution
pycategory-0.1.1-py3-none-any.whl
(18.5 kB
view hashes)
Close
Hashes for pycategory-0.1.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7ec01ace1cc56097986d6cbb39042bcd2f51defe603077ea3929df3ed09cb5b6 |
|
MD5 | c9f3e8672d65117a882d2423ac16cc08 |
|
BLAKE2b-256 | a96ddf51f32510d7f749147932081b5071ee95b37e36ded8151fde2f3996bb86 |