A library to inject common functional programming operations as methods into iterable objects.
Project description
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
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distributions
Built Distribution
Hashes for funcipy-0.5-py2.py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4cbf0d1375aa90c2804c09ec0a8ef1d442105deedc1883022ffa706335167e4b |
|
MD5 | 20c03c1817bb94225d27cd9f3b435cfe |
|
BLAKE2b-256 | 4631be018a6c7f22e2ee7393fcda321e3b0d1bfb49c40761efaacdd339f067b0 |