Write iterator.map(func) instead of map(func, iterator)
Project description
functional-piped
Python has native support for some functional programming functions such as map
and filter
.
This library allows you to use them in a "piped" way,
i.e. s(iterable).map(func)
instead of map(func, iterable)
,
because in any slightly more complex scenarios, the former is much more readable.
For example,
(s(iterable)
.map(func0)
.filter(func1)
.to(list))
makes much more sense than
list(
filter(
func1,
map(func0, iterable)
)
)
Installation
pip install functional-piped
Usage
>>> from funcpipe import Stream as s
Then you can use .map
, .filter
, .reduce
, and .foreach
to manipulate your iterable in a functional programming way.
If the result is still an iterable, you can use .to()
to collect it into any data type
>>> s([1, 2, 3]).map(lambda x: x + 1).to(list)
[2, 3, 4]
>>> s([1, 2, 3]).map(lambda x: x + 1).filter(lambda x: x % 2).to(list)
[2]
>>> s([1, 2, 3]).map(lambda x: x + 1).to(set)
{2, 3, 4}
>>> (s([1, 2, 3])
... .map(lambda x: x + 1) # [2, 3, 4]
... .filter(lambda x: x % 2 == 0) # [2, 4]
... .reduce(lambda x, y: x + y)) # 2 + 4 = 6
6
>>> s([1, 2, 3]).foreach(print)
1
2
3
Iterable Reusability
s(obj)
behaves exactly the same as obj
in terms of "reusability" when calling iterator/iterable
related methods.
If obj
is an iterable, not iterator:
>>> obj = [1, 2, 3]
>>> stream = s(obj)
>>> stream.map(lambda x: x + 1).to(list)
[2, 3, 4]
>>> stream.map(lambda x: x + 1).to(list)
[2, 3, 4]
If obj
is an iterator:
>>> obj = range(1, 4)
>>> stream = s(obj)
>>> stream.map(lambda x: x + 1).to(list)
[2, 3, 4]
>>> stream.map(lambda x: x + 1).to(list)
[]
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
Hashes for functional_piped-0.0.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1499fe6927bef64daec54766d4725dad28f76af0824865ebb13689a1ea5248ea |
|
MD5 | d1e144fde5052f82271f54fc34e7e9ed |
|
BLAKE2b-256 | dc0d763d66321518ca1ac5abd0a52a5ac4e603ef7b280337ff1234fd1d4043f8 |