Skip to main content

Simple and ergonomic iterator chaining for Python

Project description

Documentation Status

Iterchain: Iterator chaining for Python

Iterchain is a library intended to make manipulating iterators in Python easier and more ergonomic. The design is heavily inspired by the Rust iterator design, and a lot of the functionality comes from the standard Python itertools library.

Why would I need this?

Say we want to know the sum of all the squares of even numbers up to 100.
How can we do this?

Let's try some straightforward, procedural Python:

>>> total = 0
>>> for i in range(100):
...     if i % 2 is 0:
...         total += i ** 2
...
>>> total
161700

This works, but if you read this for the first time it can take a bit of effort to figure out what's happening, especially in slightly less trivial cases. So, how about we use iterators instead?

Well, let's see:

>>> sum(i**2 for i in range(100) if i % 2 is 0)
161700

That's pretty nice! Much shorter, and much easier to understand.
But there's a problem, this pattern only works for relatively simple manipulations. In those cases you could try using the python map and filter builtins (and the slightly more hidden functools.reduce). They let you construct more complex processing chains.

Let's rewrite our iterator to use those functions instead:

>>> sum(map(lambda x: x**2, filter(lambda x: x % 2 is 0, range(100))))
161700

Okay, now that is a mess...
I don't know about you, but it would take me quite a while to unravel what's happening here. The problem is that the whole expression is inside out. The filter gets applied first, but it's hidden in the middle of the expression, and the sum gets applied last but it is all the way in the front. Makes no sense...

So, how can we improve on this? iterchain of course!
(you probably saw this coming already)

So, let's see how it looks using iterchain:

>>> import iterchain
>>> (iterchain.count(stop=100)
...     .filter(lambda x: x % 2 is 0)
...     .map(lambda x: x**2)
...     .sum())
161700

Isn't this much better? The operations are listed in the order that they're executed, are clearly separated, and you can have as few or as many operations as you want. This is why you should use iterchain!

Iterator manipulation

The heart of this library <3.

Generators

iterchain also provides handy methods that let you build new Iterator instances from scratch. These are contained in the iterchain.generators sub-module, but they're also accessible directly from the iterchain module, which is the preferred way of using them.

For example:

  >>> import iterchain
  >>> iterchain.count().take(4).map(lambda x: x**2).to_list()
  [0, 1, 4, 9]

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

iterchain-0.1.3.tar.gz (4.4 kB view details)

Uploaded Source

Built Distribution

iterchain-0.1.3-py3-none-any.whl (17.3 kB view details)

Uploaded Python 3

File details

Details for the file iterchain-0.1.3.tar.gz.

File metadata

  • Download URL: iterchain-0.1.3.tar.gz
  • Upload date:
  • Size: 4.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.5.4

File hashes

Hashes for iterchain-0.1.3.tar.gz
Algorithm Hash digest
SHA256 bb932c41565a69aa0bdc4397b1df2878ef671d2ba676a5b4e50d414d04335a72
MD5 237e09a0a633d3704abb1d29ad477280
BLAKE2b-256 5e94bc53ba97a798e6586b74c1423ac26c6f7a34d96f9d10453c6d7faf3f44f7

See more details on using hashes here.

File details

Details for the file iterchain-0.1.3-py3-none-any.whl.

File metadata

  • Download URL: iterchain-0.1.3-py3-none-any.whl
  • Upload date:
  • Size: 17.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.5.4

File hashes

Hashes for iterchain-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 2ea3c53f60fac69e5b9317ea2f2167d904ef53e491a4a55b8e7be0f2a61308c7
MD5 486b6ea557cdd7e2f1121237ce413c58
BLAKE2b-256 18e5a80242220b37642f16a3c5d5e3f3c056266098cad7fec9b266cafc6c8ea1

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page