🐈 Felis Catus – functional bits for Python
Project description
🐈 Felis Catus
Felis Catus is your taxonomic nomenclature, an endothermic quadruped, carnivorous by nature; your visual, olfactory, and auditory senses contribute to your hunting skills and natural defenses. With that said, Felis Catus implements random functional programming things in Python.
Installation
Install from PyPI:
pip install felis-catus
Build and install from source:
pip install git+https://github.com/LeeeeT/felis
Examples
Currying and uncurrying functions with felis.currying
:
from felis.currying import curry, uncurry
@curry
@curry
def curried(a: int, b: str, c: bool) -> None:
pass
uncurried = uncurry(uncurry(curried))
reveal_type(curried) # (bool) -> ((str) -> ((int) -> None))
reveal_type(uncurried) # (int, str, bool) -> None
Safe error handling with felis.either
:
from felis import either
safe_int = either.catch(ValueError)(int)
@either.catch(ZeroDivisionError)
def safe_reciprocal(number: float) -> float:
return 1 / number
safe_reciprocal_of_str = either.compose(safe_reciprocal)(safe_int)
match safe_reciprocal_of_str(input("Enter a number: ")):
case either.Left(error):
print(f"Error: {error}")
case either.Right(reciprocal):
print(f"Reciprocal: {reciprocal}")
Managing IO (or any other lazy computations) with felis.lazy
:
from felis import lazy
from felis.currying import uncurry
main = \
uncurry(lazy.then)(lambda: print("What's your name?"),
uncurry(lazy.bind)(input, lambda name:
lambda: print(f"Hi, {name}!")
))
main()
Finding pythagorean triples (analogue to list comprehension) with felis.list
:
from felis.currying import uncurry
from felis.list import bind, guard, identity, range, then
pythags = \
uncurry(bind)(range(1)(20), lambda z:
uncurry(bind)(range(1)(z), lambda x:
uncurry(bind)(range(x)(z), lambda y:
uncurry(then)(guard(x**2 + y**2 == z**2),
identity((x, y, z)),
))))
print(pythags)
# [(3, 4, 5), (6, 8, 10), (5, 12, 13), (9, 12, 15), (8, 15, 17)]
Parsing a list of natural numbers from a string with felis.parser
:
from felis.parser import bracket, digit, map, run, separated, some, text
natural = map(int)(map("".join)(some(digit)))
naturals_without_brackets = separated(text(", "))(natural)
naturals_with_brackets = bracket(text("["))(text("]"))(naturals_without_brackets)
string = "[12, 345, 6789]"
result = run(naturals_with_brackets)(string)
print(result) # Some(value=[12, 345, 6789])
That's all monads, btw. 🐈
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
Built Distribution
Hashes for felis_catus-0.dev2-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 22c6308447927c999f041a0a2e71ac699627c06c1e8215439860791ff4c5f9b2 |
|
MD5 | c342ef39f7de2a0d69a15fcb2ad322b8 |
|
BLAKE2b-256 | 688f268d8e98c05d43154cd2d41aa959d1c6cd39c47105b0aa58155e2d6822a7 |