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
Iter[A]
(generators made ergonomic)
from haskellian import Iter
Iter(range(100000000000)) \
.map(lambda x: 2*x) \
.filter(lambda x: x % 2 == 0) \
.batch(2) \
.skip(1)
#.sync() # don't recommend it...
# Iter([(4, 6), (8, 10), (12, 14), (16, 18), (20, 22), ...])
Promise[A]
(awaitables made ergonomic)
from haskellian import Promise, promise as P
async def fetch_users() -> list[str]:
...
async def fetch_user(id) -> str:
...
await 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.
AsynIter[A]
from haskellian import AsynIter, asyn_iter as AI
@AI.lift
async def gen():
for i in range(10):
yield i
await gen().map(lambda x: 2*x).filter(lambda x: x % 2 == 0).batch(2).skip(1).sync()
Dict[A]
from haskellian import Dict
Dict({'a': 1, 'b': 2}) \
.filter(lambda v: v % 2 == 0) \
.map(lambda v: 2*v) \
# Dict({'b': 4})
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.5.0.tar.gz
(20.5 kB
view hashes)
Built Distribution
haskellian-0.5.0-py3-none-any.whl
(33.9 kB
view hashes)
Close
Hashes for haskellian-0.5.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 93982d3e02a57ed219f3641df2fb290bd09abf483f6ae2a9c355f7e77f2c51fc |
|
MD5 | 0b6e3842c085b2ed17c300f8d91dd7d6 |
|
BLAKE2b-256 | 32b2acb9925f5896c5f51eec17996bfcb12c8c8396578b3914d4d1eafa679580 |