Skip to main content

A wrapper for FP style iterator manipulation

Project description

IterWrapper

This is a wrapper for python iterators to give it a style like those in Rust among with other methods, to improve the consistency and code readablity of iterator manipulation.

A example for this is something like :

from iterwrapper import IterWrapper as iw
l = (
    iw(range(0,10))
    .map(lambda x:x+1)
    .filter(lambda x:x%2==0)
    .fold(lambda c, x: c+x**2, d=0)
)

print(l) # 220 = 2^2 + 4^2 + 6^2 + 8^2 + 10^2

comparing to the equivalent representation in vanilla python :

l = sum(map(lambda x: x**2, filter(lambda x:x%2==0, map(lambda x: x+1, range(0,10)))))
# or
r = range(0, 10)
m = map(lambda x: x+1, r)
f = filter(lambda x: x%2==0, m)
c = map(lambda x: x**2, f)
l = sum(c)

Another one:

from iterwrapper import IterWrapper as iw
l = (
    iw(range(0,10))
    .map(lambda x: x+1)
    .filter(lambda x: x%2==0)
    .map(str)
    .collect(', '.join)
)

print(l) # "2, 4, 6, 8, 10"

# Comparing to

l = ', '.join(map(str, filter(lambda x: x%2==0, map(lambda x: x+1, range(0,10)))))

# or

r = range(0, 10)
m = map(lambda x: x+1, r)
f = filter(lambda x: x%2==0, m)
ms = map(str, f)
l = ', '.join(ms)

The main goal of this only is to be a convenient wrapper to manipulate iterators, so it comes with high compatibility since most of the method is done by accessing the wrapped iterator, and most of the method just use the wrapper as a container.

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

iterwrapper-0.1.4.tar.gz (6.5 kB view hashes)

Uploaded Source

Built Distribution

iterwrapper-0.1.4-py3-none-any.whl (20.0 kB view hashes)

Uploaded Python 3

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