The functional programming library you need
Project description
Haskellian
The python functional programming library you need
pip install haskellian
Why
- Lazy imports + stub files -> great linting and 0 import time (thanks to
lazy-loader
) - Monadic, method chaining style
- Great typing and overloads
from haskellian import either as E, iter as I, promise as P, Pipe, asyn_iter as AI
Pipe[A]
(simplest monad ever)
Pipe('world hello') \
.f(str.title) \
.f(lambda s: s.split(' ')) \
.f(sorted) \
.f(', '.join) \
.value
# 'Hello, World'
Iter[A]
(generators made ergonomic)
I.Iter(range(100000000000)) \
.map(lambda x: 2*x) \
.filter(lambda x: x % 2 == 0) \
.batch(2) \
.tail()
#.sync() # don't recommend it...
# Iter([(4, 6), (8, 10), (12, 14), (16, 18), (20, 22), ...])
Promise[A]
(awaitables made ergonomic)
async def fetch_users() -> list[str]:
...
async def fetch_user(id) -> str:
...
await P.Promise(fetch_users()) \
.bind(lambda ids: P.all(map(fetch_user, ids))) \
.then(sorted)
Either[L, R]
Method-chaining style
from haskellian import Either, Left, Right, either as E
def fetch_users() -> Either[str, list[str]]:
...
def fetch_user(id: str) -> Either[str, dict]:
...
def parse_user(user: dict) -> Either[str, User]:
...
def print_one():
fetch_users() \
.bind(lambda ids: fetch_user(ids[0])) \
.bind(parse_user) \
.fmap(print)
Do-notation style
@E.do()
def print_one():
users = fetch_users().unsafe()
raw_user = fetch_user(users[0]).unsafe()
user = parse_user(raw_user).unsafe()
print(user)
Explanation:
.unsafe()
raises anIsLeft
exception;@E.do()
simply wraps the function in atry...except IsLeft
block, returning the raisedLeft
if so.
AsyncIter[A]
(self-explanatory at this point, right?)
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
haskellian-0.4.9.tar.gz
(20.1 kB
view hashes)
Built Distribution
haskellian-0.4.9-py3-none-any.whl
(34.5 kB
view hashes)
Close
Hashes for haskellian-0.4.9-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | a54ae1e774f3c3405679bdbf04c0cb809111c0469bfed06b578b9e5941abf5c9 |
|
MD5 | 847a97e6f7dfe1436d180c33ec20a8d2 |
|
BLAKE2b-256 | 6548c1b1a028acd41b074ac96cc9b41a7d6dedf178a3fb3f771a2502bb9fd1bc |