Write s(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, .foreach, and .zip
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
Using .zip and .star
One common use case in Python is
a_list = [...]
b_list = [...]
for a, b in zip(a_list, b_list):
...
To write equivalent code, you can use s(a_list).zip(b_list) to zip the two lists together,
and use the .star attribute to apply .foreach to the unpacked zipped values
>>> s([1, 2]).zip([3, 4]).to(list)
[(1, 3), (2, 4)]
>>> s([1, 2]).zip([3, 4]).star.foreach(lambda x, y: print(f"{x} + {y}"))
1 + 3
2 + 4
The same thing applies to map and filter:
>>> s([1, 2]).zip([3, 4]).star.map(lambda x, y: x + y).to(list)
[4, 6]
If you don't use .star, the callback of each function will receive a tuple:
>>> s([1, 2]).zip([3, 4]).map(lambda x: x[0] + x[1]).to(list)
[4, 6]
Naming of .star comes from the itertools.starmap, but this concept also
applies to .filter and .foreach, so instead of creating a .starmap method for Stream, we use the above mentioned syntax there.
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 = iter(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
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 functional-piped-0.1.0.tar.gz.
File metadata
- Download URL: functional-piped-0.1.0.tar.gz
- Upload date:
- Size: 4.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3b9bf214e3f295d73899a5381d374f1cc9b1c7a41a7242f3fd50142db2b856a8
|
|
| MD5 |
f21a406a9d519b8a4e0d7be9926436bd
|
|
| BLAKE2b-256 |
5f8519a8390e1034ac7ed71ec743e381653609cb31c8ee6dbbdf7c415d96ba8f
|
File details
Details for the file functional_piped-0.1.0-py3-none-any.whl.
File metadata
- Download URL: functional_piped-0.1.0-py3-none-any.whl
- Upload date:
- Size: 4.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
122372d76d17b560b5bed3a92369e06742393a6d9c99976431ec510420c803d8
|
|
| MD5 |
6fb8c6276b1c4c8838ec6819cde681c2
|
|
| BLAKE2b-256 |
166e63c7b9b68431af0d321b55a3d8865dabd1a232a178623f687a878e18f77f
|