Chainable lazy iterators
Project description
chainit
Documentation available here: https://lukapeschke.github.io/chainit/
This library provides the ChainIt class, a wrapper around stdlib's
itertools module, allowing to chain
operations on iterables, resulting in easier-to-read code.
import typing as t
def fib() -> t.Iterable[int]:
a, b = 0, 1
while True:
yield a
a, b = b, a + b
# Allows to write things like this...
(
ChainIt(fib())
.filter(lambda x: x % 2 == 0)
.map(lambda x: x // 2)
.flat_map(range)
.take_while(lambda x: x < 6)
.collect_list()
)
# ...rather than like this
from itertools import chain as ichain, islice, takewhile
list(
takewhile(
lambda x: x < 6,
ichain.from_iterable(
map(lambda x: range(x // 2), filter(lambda x: x % 2 == 0, fib()))
),
)
)
Installation
pip install chainit
Examples
Decorator
In addition to ChainIt, the library provides a chainit decorator. It makes a function returning
an iterable return a ChainIt instead:
@chainit
def fac():
n = 0
fac = 1
while True:
yield fac
n += 1
fac *= n
assert fac().enumerate().take(5).collect() == ((0, 1), (1, 1), (2, 2), (3, 6), (4, 24))
Using a ChainIt instance as an iterable
assert list(fac().take(3)) == [1, 1, 2]
for idx, x in fac().enumerate():
if idx > 3:
break
print(x)
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
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file chainit-0.4.0.tar.gz.
File metadata
- Download URL: chainit-0.4.0.tar.gz
- Upload date:
- Size: 4.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
bd2e7308cf32c8d1ae04a2c8c9fbbde8aa021815f29e0d8bf69caf6c6bc23579
|
|
| MD5 |
f886d35cc380c2c7d635775f2d9c8440
|
|
| BLAKE2b-256 |
b0aff900e8c7ecbfc900fdd78b7620bad6e57301f5dc62b093ff2a0e808fabea
|
File details
Details for the file chainit-0.4.0-py3-none-any.whl.
File metadata
- Download URL: chainit-0.4.0-py3-none-any.whl
- Upload date:
- Size: 5.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
642e4c45a563b2b9b76c0d1ed07185691ec7fe68ddcd7b96e5ffb7f79643ab51
|
|
| MD5 |
4f4d5bcf4aed372fc32834627307e0a4
|
|
| BLAKE2b-256 |
81e2d0aaf7cea405ef0628b7d86b6a5e8adabaab5781b12fe03bf01583066d7b
|