Functional programming tools.
Project description
MAZ
A functional programming package.
Install
pip install maz
Main usage
Function composition
You can use maz
to compose function calls such that they call one another in a series.
NOTE: functions are called from right to left.
>>> import maz
>>> succ = lambda x: x+1
>>> add_three = maz.compose(succ,succ,succ)
>>> add_three(4)
>>> 7
Partial function
When you want to fix a parameter in a function, you could just use the functools.partial
. However, that doesn't support fixing a positional argument on a specific index, which are quite common later on when you want to build complex compositions. Here you can use the maz.partialpos
instead
>>> import maz
>>> def add(x,y): return x+y
>>> add_two = maz.partialpos(add, {1:2}) # fixing argument for index 1 to 2 (y=2)
>>> add_two(3)
>>> 5
Complex compositions
To support more complex compositions we've added some special functions such as
import maz
import operator
import itertools
# "invoke" function - a function calling other functions
def add(x,y):
return x+y
maz.invoke(add, [1,2]) # >>> 3
maz.invoke_star(add, 1, 2) # >>> 3
# "fnmap" function will compute all input functions in a
# series with same given arguments
fn = maz.fnmap(add, add, add)
fn(1,2) # >>> [3, 3, 3]
# "ifttt" returns a new function that checks the input somehow
# then does something depending on the result from the check
fn = maz.ifttt(
# if x is greater than 3,
lambda x: x > 3,
# then subtract 1 from x
lambda x: x - 1,
# else add 1 to x
lambda x: x + 1
)
fn(3) # >>> 4
fn(4) # >>> 3
# "starfilter" just as itertools.starmap but a filter instead
# Example, filter tuples who's sum is greater than 4
next(
maz.starfilter(
maz.compose(
lambda i: i > 4,
add
),
itertools.product(range(3), range(3,6))
)
) # >>> (0,5)
# "constant" returns a function which, no matter the argument, returns a constant value.
cnst_fn = maz.constant(True)
cnst_fn() # >>> True
cnst_fn(cnst_fn()) # >>> True
Other functions
We've added a tools
module to provide functions that are in the middle of being simple enough to just write it yourself but tideous enough to not do it.
from maz.tools import reverse_otm_dict
# Reversing a one-to-many relationship map
d = reverse_otm_dict({
"a": [1,2,3],
"b": [1,2,4],
"c": [4,5]
})
# >>> d
# {
# 1: frozenset({"a", "b"}),
# 2: frozenset({"a", "b"}),
# 3: frozenset({"a"}),
# 4: frozenset({"b", "c"}),
# 5: frozenset({"c"}),
# }
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
File details
Details for the file maz-0.0.12.tar.gz
.
File metadata
- Download URL: maz-0.0.12.tar.gz
- Upload date:
- Size: 6.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3aefd6fa0b6168288c5450a495adbf6f5ec336befbe447801bd2ba9882427df2 |
|
MD5 | 135de49c226a15847819407bebaa61f4 |
|
BLAKE2b-256 | 44d18eedd2f26566582cd80d7324105e0530b4696491db8f9af85c2a5b88886f |
File details
Details for the file maz-0.0.12-py3-none-any.whl
.
File metadata
- Download URL: maz-0.0.12-py3-none-any.whl
- Upload date:
- Size: 7.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 | 20f5f9d8dfd644dd8559cc1cafd9102c30acd63e17cc5307252199be822d2d3a |
|
MD5 | 55cbd32ffc7187d6dfbbc891aa2d1218 |
|
BLAKE2b-256 | 4e16ab3d4e62369f66622764d6a9878d59f43a04ea7ce105543e635003dffc19 |