A library to inject common functional programming operations as methods into iterable objects. Currently, it injects filter, flat_map, foldl, foldr, map, and reduce operations along with sum, count, any, all, max, min, and zip operations.
Getting the library
The library can be installed via pip install funcipy. Similarly, it can be uninstalled via pip uninstall funcipy.
Usage
When you want to inject common functional programming operations as methods into an object Obj, invoke funcipy.funcify function with Obj as the argument. If Obj is iterable, then the function will return an object that
provides the same interface as the input object and
has functional programming operations as methods.
Otherwise, Obj is returned as is.
Here are few example invocations.
from funcipy import funcify
import functools
import itertools
import operator
i = range(5, 15)
print("Map function: " + ' '.join(map(str, i)))
tmp1 = funcify(i)
print("Map function applied to funcified object: " + ' '.join(map(str, tmp1)))
print("Map method: " + ' '.join(tmp1.map(str)))
print("Map and Filter Method chaining: " +
' '.join(tmp1.filter(lambda x: x % 2).map(str)))
print("Reduce function: " + str(functools.reduce(operator.add, i, 5)))
print("Reduce method: " + str(tmp1.reduce(operator.add, 5)))
print("Reduce function: " + str(functools.reduce(operator.sub, i)))
print("Foldl method: " + str(tmp1.foldl(operator.sub)))
print("Foldr method: " + str(tmp1.foldr(operator.sub)))
print("Flat-map operation: " + ' '.join(itertools.chain.from_iterable(
map(str, i))))
print("Flat-map method: " + ' '.join(funcify(i).flat_map(str)))
print("Sum function: " + str(sum(i)))
print("Sum method: " + str(tmp1.sum()))
print("Count function: " + str(sum(1 for _ in filter(lambda x: x > 10, i))))
print("Count method: " + str(tmp1.count(lambda x: x > 10)))
print("Max function: " + str(max(i)))
print("Max method: " + str(tmp1.max()))
print("Min function: " + str(min(i)))
print("Min method: " + str(tmp1.min()))
print("Any function: " + str(any(map(lambda x: x > 10, i))))
print("Any method: " + str(tmp1.map(lambda x: x > 10).any()))
print("All function: " + str(all(map(lambda x: x > 10, i))))
print("All method: " + str(tmp1.map(lambda x: x > 10).all()))
j = range(0, 7)
print("Zip function: " + ' '.join(map(str, zip(i, j))))
print("Zip method: " + ' '.join(tmp1.zip(j).map(str)))
Attribution
Copyright (c) 2017, Venkatesh-Prasad Ranganath
Licensed under BSD 3-clause “New” or “Revised” License (https://choosealicense.com/licenses/bsd-3-clause/)
Authors: Venkatesh-Prasad Ranganath
Metadata
Release files for funcipy 0.5
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| funcipy-0.5-py2.py3-none-any.whl | Python 3, Python 2 | none | any | Details |
Release files / funcipy-0.5-py2.py3-none-any.whl
| Download URL | funcipy-0.5-py2.py3-none-any.whl |
|---|---|
| Size | 5.9 kB |
| Tags | Python 2 Python 3 |
|
SHA-256 checksum How to use checksums |
4cbf0d1375aa90c2804c09ec0a8ef1d442105deedc1883022ffa706335167e4b
|
|
BLAKE2b-256 checksum How to use checksums |
4631be018a6c7f22e2ee7393fcda321e3b0d1bfb49c40761efaacdd339f067b0
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |