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"}),
# }
Release files for maz 0.0.12
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| maz-0.0.12.tar.gz | 6.8 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| maz-0.0.12-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 14.6 kB
Release files / maz-0.0.12.tar.gz
| Download URL | maz-0.0.12.tar.gz |
|---|---|
| Size | 6.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
3aefd6fa0b6168288c5450a495adbf6f5ec336befbe447801bd2ba9882427df2
|
|
BLAKE2b-256 checksum How to use checksums |
44d18eedd2f26566582cd80d7324105e0530b4696491db8f9af85c2a5b88886f
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.11.7
|
Release files / maz-0.0.12-py3-none-any.whl
| Download URL | maz-0.0.12-py3-none-any.whl |
|---|---|
| Size | 7.9 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
20f5f9d8dfd644dd8559cc1cafd9102c30acd63e17cc5307252199be822d2d3a
|
|
BLAKE2b-256 checksum How to use checksums |
4e16ab3d4e62369f66622764d6a9878d59f43a04ea7ce105543e635003dffc19
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.11.7
|