Skip to content

Commit 292639f

Browse files
authored
Merge pull request #4 from ourstudio-se/3-compose-should-be-class-implement-predicate-class
3 compose should be class implement predicate class
2 parents 2e3a3dc + 54e8eab commit 292639f

4 files changed

Lines changed: 91 additions & 17 deletions

File tree

.github/workflows/publish.yml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
name: Publish to PyPI.org
2+
on:
3+
release:
4+
types: [published]
5+
6+
jobs:
7+
build-publish:
8+
runs-on: ubuntu-latest
9+
steps:
10+
- name: Checkout main
11+
uses: actions/checkout@v3
12+
13+
- name: Install python and build package
14+
uses: actions/setup-python@v4
15+
- run: python3 -m pip install --upgrade build && python3 -m build
16+
17+
- name: Publish package to PyPI
18+
uses: pypa/gh-action-pypi-publish@release/v1
19+
with:
20+
password: ${{ secrets.PYPI_API_TOKEN }}

maz/__init__.py

Lines changed: 59 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,9 @@ def pospartial(function, positional_arguments):
7272
which yield
7373
res == 23+32+19 == 74
7474
75-
Return:
76-
callable
75+
Returns
76+
-------
77+
out : Callable
7778
"""
7879

7980
def wrapper(*args, fn=function, pas=positional_arguments, **kwargs):
@@ -83,8 +84,7 @@ def wrapper(*args, fn=function, pas=positional_arguments, **kwargs):
8384
return fn(*nargs, **kwargs)
8485
return wrapper
8586

86-
def compose_pair(f, g):
87-
87+
class compose_pair:
8888
"""
8989
Composes new function h = f(g(x)), from
9090
f and g.
@@ -96,33 +96,77 @@ def compose_pair(f, g):
9696
>>> h(1)
9797
>>> 4
9898
99-
Return:
100-
Callable
99+
Returns
100+
-------
101+
out : Callable
101102
"""
102-
def composed(*args, f=f, g=g, **kwargs):
103-
return f(g(*args, **kwargs))
104103

105-
return composed
104+
def __init__(self, f, g):
105+
self.f = f
106+
self.g = g
107+
108+
def __call__(self, *args, **kwargs):
109+
return self.f(
110+
self.g(*args, **kwargs)
111+
)
106112

107-
def compose(*functions):
113+
class compose:
108114

109115
"""
110116
Sequence composite functions of `functions`.
111117
E.g. let fns be a list of functions [f, g, h] and
112118
compose(fns) would then represent lambda x : f(g(h(x)))
113119
114-
Example:
120+
Examples
121+
--------
115122
>>> inc = lambda x: x+1
116123
>>> double = lambda x: x*2
117124
>>> f = compose(inc, double, inc)
118125
>>> f(2)
119-
>>> 7
126+
7
120127
121-
Return:
122-
function
128+
Returns
129+
-------
130+
out : Callable
131+
"""
132+
133+
def __init__(self, *functions):
134+
self.fn = functools.reduce(compose_pair, functions)
135+
136+
def __call__(self, *args, **kwargs):
137+
return self.fn(*args, **kwargs)
138+
139+
class fnmap:
140+
141+
"""
142+
Runs an iterable of functions with same arguments.
143+
Returns an iterable of result from each of the functions.
144+
145+
Examples
146+
--------
147+
>>> fn = fnmap(lambda x: x+1, lambda x: x+2)
148+
>>> list(fn(3))
149+
[4, 5]
150+
151+
>>> def add_one(x): return x+1
152+
>>> def mul_two(x): return x*2
153+
>>> fn = fnmap(add_one, mul_two)
154+
>>> list(fn(x=3))
155+
[4, 6]
156+
157+
Returns
158+
-------
159+
out : Callable
123160
"""
124161

125-
return functools.reduce(compose_pair, functions)
162+
def __init__(self, *functions):
163+
self.functions = functions
164+
165+
def __call__(self, *args, **kwargs):
166+
return map(
167+
lambda fn: fn(*args, **kwargs),
168+
self.functions
169+
)
126170

127171
def indexing(lst: list, index_item):
128172

setup.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[metadata]
22
name = maz
3-
version = 0.0.1
3+
version = 0.0.2
44
author = ourstudio
55
author_email = rikard@ourstudio.se
66
description = Functional programming tools.

tests/test_maz.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,4 +63,14 @@ def f(x,y,z):
6363
return x+y+z
6464

6565
_f = maz.pospartial(f, [(1, 32), (2, 19)])
66-
assert _f(23) == 74
66+
assert _f(23) == 74
67+
68+
def test_fnmap():
69+
70+
fn = maz.fnmap(lambda x: x+1, lambda x: x+2)
71+
assert list(fn(3)) == [4, 5]
72+
73+
def add_one(x): return x+1
74+
def mul_two(x): return x*2
75+
fn = maz.fnmap(add_one, mul_two)
76+
assert list(fn(x=3)) == [4, 6]

0 commit comments

Comments
 (0)