-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathcompose.py
187 lines (142 loc) · 3.98 KB
/
compose.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
from collections.abc import Callable
from functools import reduce
from typing import Any, TypeVar, TypeVarTuple, overload
_A = TypeVar("_A")
_B = TypeVar("_B")
_C = TypeVar("_C")
_D = TypeVar("_D")
_E = TypeVar("_E")
_F = TypeVar("_F")
_G = TypeVar("_G")
_H = TypeVar("_H")
_T = TypeVar("_T")
_J = TypeVar("_J")
_P = TypeVarTuple("_P")
_Q = TypeVarTuple("_Q")
_X = TypeVarTuple("_X")
_Y = TypeVarTuple("_Y")
_Z = TypeVarTuple("_Z")
@overload
def compose() -> Callable[[_A], _A]:
...
@overload
def compose(__fn1: Callable[[_A], _B]) -> Callable[[_A], _B]:
...
@overload
def compose(__fn1: Callable[[_A], _B], __fn2: Callable[[_B], _C]) -> Callable[[_A], _C]:
...
@overload
def compose(__fn1: Callable[[_A], _B], __fn2: Callable[[_B], _C], __fn3: Callable[[_C], _D]) -> Callable[[_A], _D]:
...
@overload
def compose(
__fn1: Callable[[_A], _B],
__fn2: Callable[[_B], _C],
__fn3: Callable[[_C], _D],
__fn4: Callable[[_D], _E],
) -> Callable[[_A], _E]:
...
@overload
def compose(
__fn1: Callable[[_A], _B],
__fn2: Callable[[_B], _C],
__fn3: Callable[[_C], _D],
__fn4: Callable[[_D], _E],
__fn5: Callable[[_E], _F],
) -> Callable[[_A], _F]:
...
@overload
def compose(
__fn1: Callable[[_A], _B],
__fn2: Callable[[_B], _C],
__fn3: Callable[[_C], _D],
__fn4: Callable[[_D], _E],
__fn5: Callable[[_E], _F],
__fn6: Callable[[_F], _G],
) -> Callable[[_A], _G]:
...
@overload
def compose(
__fn1: Callable[[_A], _B],
__fn2: Callable[[_B], _C],
__fn3: Callable[[_C], _D],
__fn4: Callable[[_D], _E],
__fn5: Callable[[_E], _F],
__fn6: Callable[[_F], _G],
__fn7: Callable[[_G], _H],
) -> Callable[[_A], _H]:
...
@overload
def compose(
__fn1: Callable[[_A], _B],
__fn2: Callable[[_B], _C],
__fn3: Callable[[_C], _D],
__fn4: Callable[[_D], _E],
__fn5: Callable[[_E], _F],
__fn6: Callable[[_F], _G],
__fn7: Callable[[_G], _H],
__fn8: Callable[[_H], _T],
) -> Callable[[_A], _T]:
...
@overload
def compose(
__fn1: Callable[[_A], _B],
__fn2: Callable[[_B], _C],
__fn3: Callable[[_C], _D],
__fn4: Callable[[_D], _E],
__fn5: Callable[[_E], _F],
__fn6: Callable[[_F], _G],
__fn7: Callable[[_G], _H],
__fn8: Callable[[_H], _T],
__fn9: Callable[[_T], _J],
) -> Callable[[_A], _J]:
...
def compose(*fns: Callable[[Any], Any]) -> Callable[[Any], Any]:
"""Compose multiple functions left to right.
Composes zero or more functions into a functional composition. The
functions are composed left to right. A composition of zero
functions gives back the identity function.
>>> x = 42
>>> f = lambda a: a * 10
>>> g = lambda b: b + 3
>>> h = lambda c: c / 2
>>> compose()(x) == x
>>> compose(f)(x) == f(x)
>>> compose(f, g)(x) == g(f(x))
>>> compose(f, g, h)(x) == h(g(f(x)))
...
Returns:
The composed function.
"""
def _compose(source: Any) -> Any:
"""Return a pipeline of composed functions."""
return reduce(lambda acc, f: f(acc), fns, source)
return _compose
@overload
def starcompose() -> Callable[[Any], Any]:
...
@overload
def starcompose(__fn1: Callable[[*_P], _A]) -> Callable[[*_P], _A]:
...
@overload
def starcompose(__fn1: Callable[[*_P], tuple[*_Y]], __fn2: Callable[[*_Y], _B]) -> Callable[[*_P], _B]:
...
@overload
def starcompose(
__fn1: Callable[[*_P], tuple[*_Y]], __fn2: Callable[[*_Y], tuple[*_Z]], __fn3: Callable[[*_Z], _C]
) -> Callable[[*_P], _C]:
...
@overload
def starcompose(
__fn1: Callable[[*_P], tuple[*_Y]],
__fn2: Callable[[*_Y], tuple[*_Z]],
__fn3: Callable[[*_Z], tuple[*_X]],
__fn4: Callable[[*_X], _D],
) -> Callable[[*_P], _D]:
...
def starcompose(*fns: Callable[[Any], Any]) -> Callable[[Any], Any]:
def _compose(source: Any) -> Any:
"""Return a pipeline of composed functions."""
return reduce(lambda fields, f: f(*fields), fns, source)
return _compose
__all__ = ["compose"]