functionalthreading 0.6.6__tar.gz → 1.0.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {functionalthreading-0.6.6 → functionalthreading-1.0.0}/PKG-INFO +91 -5
- {functionalthreading-0.6.6 → functionalthreading-1.0.0}/README.md +90 -4
- functionalthreading-1.0.0/functionalthreading/__init__.py +7 -0
- functionalthreading-1.0.0/functionalthreading/functions/__init__.py +298 -0
- {functionalthreading-0.6.6 → functionalthreading-1.0.0}/functionalthreading.egg-info/PKG-INFO +91 -5
- {functionalthreading-0.6.6 → functionalthreading-1.0.0}/pyproject.toml +1 -1
- functionalthreading-0.6.6/functionalthreading/__init__.py +0 -7
- functionalthreading-0.6.6/functionalthreading/functions/__init__.py +0 -105
- {functionalthreading-0.6.6 → functionalthreading-1.0.0}/LICENSE +0 -0
- {functionalthreading-0.6.6 → functionalthreading-1.0.0}/functionalthreading/classes/__init__.py +0 -0
- {functionalthreading-0.6.6 → functionalthreading-1.0.0}/functionalthreading.egg-info/SOURCES.txt +0 -0
- {functionalthreading-0.6.6 → functionalthreading-1.0.0}/functionalthreading.egg-info/dependency_links.txt +0 -0
- {functionalthreading-0.6.6 → functionalthreading-1.0.0}/functionalthreading.egg-info/top_level.txt +0 -0
- {functionalthreading-0.6.6 → functionalthreading-1.0.0}/setup.cfg +0 -0
- {functionalthreading-0.6.6 → functionalthreading-1.0.0}/setup.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: functionalthreading
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 1.0.0
|
|
4
4
|
Summary: Concurrent functional programming with thread-based parallelism
|
|
5
5
|
Author-email: Richard Tong <richytong@gmail.com>
|
|
6
6
|
License-Expression: Unlicense
|
|
@@ -200,7 +200,32 @@ Used as a placeholder for partial arguments.
|
|
|
200
200
|
|
|
201
201
|
> Added in version 3.14.
|
|
202
202
|
|
|
203
|
+
### always(argument)
|
|
204
|
+
Always return a value.
|
|
205
|
+
|
|
206
|
+
```python
|
|
207
|
+
always5 = always(5)
|
|
208
|
+
|
|
209
|
+
always5()
|
|
210
|
+
```
|
|
211
|
+
|
|
212
|
+
|
|
213
|
+
### thunkify(func, *args, **kwargs)
|
|
214
|
+
Create a thunk from a function and arguments.
|
|
215
|
+
|
|
216
|
+
A thunk is a function that takes no arguments and executes the provided function with the provided arguments each call.
|
|
217
|
+
|
|
218
|
+
```python
|
|
219
|
+
printHello = thunkify(print, 'Hello')
|
|
220
|
+
|
|
221
|
+
printHello()
|
|
222
|
+
printHello()
|
|
223
|
+
printHello()
|
|
224
|
+
```
|
|
225
|
+
|
|
226
|
+
|
|
203
227
|
### chain(argument, *funcs)
|
|
228
|
+
### chain(*funcs)
|
|
204
229
|
Chain functions together.
|
|
205
230
|
|
|
206
231
|
Each function is evaluated in series starting from the first function, passing the return value as the first and only argument to the next function. The return value of the chain is the return value of the last function.
|
|
@@ -245,16 +270,17 @@ chain(
|
|
|
245
270
|
```
|
|
246
271
|
|
|
247
272
|
|
|
248
|
-
### tmap(
|
|
249
|
-
|
|
273
|
+
### tmap(argument, func)
|
|
274
|
+
### tmap(func)
|
|
275
|
+
Map a function concurrently across each element of an list or tuple.
|
|
250
276
|
|
|
251
|
-
|
|
277
|
+
A mapper is a function that specifies a single element of an list or tuple and returns a corresponding element of the resulting list or tuple. Each mapper invocation happens in a separate thread.
|
|
252
278
|
|
|
253
279
|
```python
|
|
254
280
|
squared = tmap([1, 2, 3], lambda n: n ** 2)
|
|
255
281
|
```
|
|
256
282
|
|
|
257
|
-
If the
|
|
283
|
+
If the list or tuple argument is omitted, returns a function of the mapper that expects the argument.
|
|
258
284
|
|
|
259
285
|
```python
|
|
260
286
|
my_mapping_func = tmap(lambda n: n ** 2)
|
|
@@ -262,3 +288,63 @@ squared = my_mapping_func([1, 2, 3])
|
|
|
262
288
|
```
|
|
263
289
|
|
|
264
290
|
|
|
291
|
+
### tforeach(argument, func)
|
|
292
|
+
### tforeach(func)
|
|
293
|
+
Execute a callback concurrently for each element of an list or tuple.
|
|
294
|
+
|
|
295
|
+
A callback is a function that does not necessarily specify a value or return. Each callback invocation happens in a separate thread.
|
|
296
|
+
|
|
297
|
+
```python
|
|
298
|
+
tforeach([1, 2, 3], print)
|
|
299
|
+
```
|
|
300
|
+
|
|
301
|
+
If the list or tuple argument is omitted, returns a function of the callback that expects the argument.
|
|
302
|
+
|
|
303
|
+
```python
|
|
304
|
+
my_foreach_func = tforeach(print)
|
|
305
|
+
my_foreach_func([1, 2, 3])
|
|
306
|
+
```
|
|
307
|
+
|
|
308
|
+
|
|
309
|
+
### tfilter(argument, func)
|
|
310
|
+
### tfilter(func)
|
|
311
|
+
Concurrently filter an list or tuple.
|
|
312
|
+
|
|
313
|
+
A predicate is a function that specifies an element of an list or tuple and returns a boolean value. Elements corresponding to predicate invocations that return `False` are filtered out of the resulting list or tuple, while elements corresponding to predicate invocationsn that return `True` are retained. Each predicate invocation happens in a separate thread.
|
|
314
|
+
|
|
315
|
+
```python
|
|
316
|
+
def is_odd(n):
|
|
317
|
+
return n % 2 == 1
|
|
318
|
+
|
|
319
|
+
odd_numbers = tfilter([1, 2, 3, 4, 5], is_odd)
|
|
320
|
+
```
|
|
321
|
+
|
|
322
|
+
If the list or tuple argument is omitted, returns a function of the predicate that expects the argument.
|
|
323
|
+
|
|
324
|
+
```python
|
|
325
|
+
def is_odd(n):
|
|
326
|
+
return n % 2 == 1
|
|
327
|
+
|
|
328
|
+
filter_odds = tfilter(is_odd)
|
|
329
|
+
odd_numbers = filter_odds([1, 2, 3, 4, 5])
|
|
330
|
+
```
|
|
331
|
+
|
|
332
|
+
|
|
333
|
+
### tflatmap(argument, func)
|
|
334
|
+
### tflatmap(func)
|
|
335
|
+
Apply a flatmapper concurrently to each element of an list or tuple, concatenating the results.
|
|
336
|
+
|
|
337
|
+
A flatmapper is a function that specifies an element of the list or tuple, and returns a list, tuple, or single element. A returned list or tuple is concatenated onto the resulting list or tuple, while a returned single element is appended. Each flatmapper invocation happens in a separate thread.
|
|
338
|
+
|
|
339
|
+
```python
|
|
340
|
+
duplicates = tflatmap([1, 2, 3], lambda n: [n, n, n])
|
|
341
|
+
```
|
|
342
|
+
|
|
343
|
+
If the list or tuple argument is omitted, returns a function of the flatmapping function that expects the argument.
|
|
344
|
+
|
|
345
|
+
```python
|
|
346
|
+
my_flatmapping_func = tflatmap(lambda n: [n, n, n])
|
|
347
|
+
duplicates = my_flatmapping_func([1, 2, 3])
|
|
348
|
+
```
|
|
349
|
+
|
|
350
|
+
|
|
@@ -185,7 +185,32 @@ Used as a placeholder for partial arguments.
|
|
|
185
185
|
|
|
186
186
|
> Added in version 3.14.
|
|
187
187
|
|
|
188
|
+
### always(argument)
|
|
189
|
+
Always return a value.
|
|
190
|
+
|
|
191
|
+
```python
|
|
192
|
+
always5 = always(5)
|
|
193
|
+
|
|
194
|
+
always5()
|
|
195
|
+
```
|
|
196
|
+
|
|
197
|
+
|
|
198
|
+
### thunkify(func, *args, **kwargs)
|
|
199
|
+
Create a thunk from a function and arguments.
|
|
200
|
+
|
|
201
|
+
A thunk is a function that takes no arguments and executes the provided function with the provided arguments each call.
|
|
202
|
+
|
|
203
|
+
```python
|
|
204
|
+
printHello = thunkify(print, 'Hello')
|
|
205
|
+
|
|
206
|
+
printHello()
|
|
207
|
+
printHello()
|
|
208
|
+
printHello()
|
|
209
|
+
```
|
|
210
|
+
|
|
211
|
+
|
|
188
212
|
### chain(argument, *funcs)
|
|
213
|
+
### chain(*funcs)
|
|
189
214
|
Chain functions together.
|
|
190
215
|
|
|
191
216
|
Each function is evaluated in series starting from the first function, passing the return value as the first and only argument to the next function. The return value of the chain is the return value of the last function.
|
|
@@ -230,16 +255,17 @@ chain(
|
|
|
230
255
|
```
|
|
231
256
|
|
|
232
257
|
|
|
233
|
-
### tmap(
|
|
234
|
-
|
|
258
|
+
### tmap(argument, func)
|
|
259
|
+
### tmap(func)
|
|
260
|
+
Map a function concurrently across each element of an list or tuple.
|
|
235
261
|
|
|
236
|
-
|
|
262
|
+
A mapper is a function that specifies a single element of an list or tuple and returns a corresponding element of the resulting list or tuple. Each mapper invocation happens in a separate thread.
|
|
237
263
|
|
|
238
264
|
```python
|
|
239
265
|
squared = tmap([1, 2, 3], lambda n: n ** 2)
|
|
240
266
|
```
|
|
241
267
|
|
|
242
|
-
If the
|
|
268
|
+
If the list or tuple argument is omitted, returns a function of the mapper that expects the argument.
|
|
243
269
|
|
|
244
270
|
```python
|
|
245
271
|
my_mapping_func = tmap(lambda n: n ** 2)
|
|
@@ -247,3 +273,63 @@ squared = my_mapping_func([1, 2, 3])
|
|
|
247
273
|
```
|
|
248
274
|
|
|
249
275
|
|
|
276
|
+
### tforeach(argument, func)
|
|
277
|
+
### tforeach(func)
|
|
278
|
+
Execute a callback concurrently for each element of an list or tuple.
|
|
279
|
+
|
|
280
|
+
A callback is a function that does not necessarily specify a value or return. Each callback invocation happens in a separate thread.
|
|
281
|
+
|
|
282
|
+
```python
|
|
283
|
+
tforeach([1, 2, 3], print)
|
|
284
|
+
```
|
|
285
|
+
|
|
286
|
+
If the list or tuple argument is omitted, returns a function of the callback that expects the argument.
|
|
287
|
+
|
|
288
|
+
```python
|
|
289
|
+
my_foreach_func = tforeach(print)
|
|
290
|
+
my_foreach_func([1, 2, 3])
|
|
291
|
+
```
|
|
292
|
+
|
|
293
|
+
|
|
294
|
+
### tfilter(argument, func)
|
|
295
|
+
### tfilter(func)
|
|
296
|
+
Concurrently filter an list or tuple.
|
|
297
|
+
|
|
298
|
+
A predicate is a function that specifies an element of an list or tuple and returns a boolean value. Elements corresponding to predicate invocations that return `False` are filtered out of the resulting list or tuple, while elements corresponding to predicate invocationsn that return `True` are retained. Each predicate invocation happens in a separate thread.
|
|
299
|
+
|
|
300
|
+
```python
|
|
301
|
+
def is_odd(n):
|
|
302
|
+
return n % 2 == 1
|
|
303
|
+
|
|
304
|
+
odd_numbers = tfilter([1, 2, 3, 4, 5], is_odd)
|
|
305
|
+
```
|
|
306
|
+
|
|
307
|
+
If the list or tuple argument is omitted, returns a function of the predicate that expects the argument.
|
|
308
|
+
|
|
309
|
+
```python
|
|
310
|
+
def is_odd(n):
|
|
311
|
+
return n % 2 == 1
|
|
312
|
+
|
|
313
|
+
filter_odds = tfilter(is_odd)
|
|
314
|
+
odd_numbers = filter_odds([1, 2, 3, 4, 5])
|
|
315
|
+
```
|
|
316
|
+
|
|
317
|
+
|
|
318
|
+
### tflatmap(argument, func)
|
|
319
|
+
### tflatmap(func)
|
|
320
|
+
Apply a flatmapper concurrently to each element of an list or tuple, concatenating the results.
|
|
321
|
+
|
|
322
|
+
A flatmapper is a function that specifies an element of the list or tuple, and returns a list, tuple, or single element. A returned list or tuple is concatenated onto the resulting list or tuple, while a returned single element is appended. Each flatmapper invocation happens in a separate thread.
|
|
323
|
+
|
|
324
|
+
```python
|
|
325
|
+
duplicates = tflatmap([1, 2, 3], lambda n: [n, n, n])
|
|
326
|
+
```
|
|
327
|
+
|
|
328
|
+
If the list or tuple argument is omitted, returns a function of the flatmapping function that expects the argument.
|
|
329
|
+
|
|
330
|
+
```python
|
|
331
|
+
my_flatmapping_func = tflatmap(lambda n: [n, n, n])
|
|
332
|
+
duplicates = my_flatmapping_func([1, 2, 3])
|
|
333
|
+
```
|
|
334
|
+
|
|
335
|
+
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
from functools import partial, Placeholder as _
|
|
2
|
+
from functionalthreading.functions import always, thunkify, chain, tap, tmap, tforeach, tfilter, reduce, tflatmap
|
|
3
|
+
from functionalthreading.classes import Thread
|
|
4
|
+
|
|
5
|
+
__name__ = 'functionalthreading'
|
|
6
|
+
|
|
7
|
+
__all__ = ['Thread', 'partial', '_' ,'chain', 'tap', 'tmap', 'tforeach', 'tfilter', 'reduce', 'tflatmap']
|
|
@@ -0,0 +1,298 @@
|
|
|
1
|
+
from functools import partial, Placeholder as _, reduce as _reduce
|
|
2
|
+
from functionalthreading.classes import Thread
|
|
3
|
+
|
|
4
|
+
def always(argument):
|
|
5
|
+
"""Always return a value.
|
|
6
|
+
|
|
7
|
+
```python
|
|
8
|
+
always5 = always(5)
|
|
9
|
+
|
|
10
|
+
always5()
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
"""
|
|
14
|
+
def inner_func():
|
|
15
|
+
return argument
|
|
16
|
+
return inner_func
|
|
17
|
+
|
|
18
|
+
def thunkify(func, *args, **kwargs):
|
|
19
|
+
"""Create a thunk from a function and arguments.
|
|
20
|
+
|
|
21
|
+
A thunk is a function that takes no arguments and executes the provided function with the provided arguments each call.
|
|
22
|
+
|
|
23
|
+
```python
|
|
24
|
+
printHello = thunkify(print, 'Hello')
|
|
25
|
+
|
|
26
|
+
printHello()
|
|
27
|
+
printHello()
|
|
28
|
+
printHello()
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
"""
|
|
32
|
+
def inner_func():
|
|
33
|
+
return func(*args, **kwargs)
|
|
34
|
+
return inner_func
|
|
35
|
+
|
|
36
|
+
def _chain(argument, funcs):
|
|
37
|
+
ret = argument
|
|
38
|
+
for func in funcs:
|
|
39
|
+
ret = func(ret)
|
|
40
|
+
return ret
|
|
41
|
+
|
|
42
|
+
def chain(argument, *funcs):
|
|
43
|
+
"""Chain functions together.
|
|
44
|
+
|
|
45
|
+
Each function is evaluated in series starting from the first function, passing the return value as the first and only argument to the next function. The return value of the chain is the return value of the last function.
|
|
46
|
+
|
|
47
|
+
```python
|
|
48
|
+
chain(
|
|
49
|
+
2,
|
|
50
|
+
lambda n: n + 1,
|
|
51
|
+
lambda n: n ** 2,
|
|
52
|
+
lambda n: n / 3,
|
|
53
|
+
print
|
|
54
|
+
)
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
If the first non-function argument is omitted, returns a function of chained functions that expects the non-function argument.
|
|
58
|
+
|
|
59
|
+
```python
|
|
60
|
+
my_function_chain = chain(
|
|
61
|
+
lambda n: n + 1,
|
|
62
|
+
lambda n: n ** 2,
|
|
63
|
+
lambda n: n / 3,
|
|
64
|
+
print
|
|
65
|
+
)
|
|
66
|
+
|
|
67
|
+
my_function_chain(2)
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
"""
|
|
71
|
+
if callable(argument):
|
|
72
|
+
return partial(_chain, _, (argument,) + funcs)
|
|
73
|
+
return _chain(argument, funcs)
|
|
74
|
+
|
|
75
|
+
def tap(func):
|
|
76
|
+
"""Call a function with an argument, returning the argument.
|
|
77
|
+
|
|
78
|
+
```python
|
|
79
|
+
chain(
|
|
80
|
+
1,
|
|
81
|
+
lambda n: n + 1,
|
|
82
|
+
tap(print),
|
|
83
|
+
lambda n: n + 2,
|
|
84
|
+
tap(print),
|
|
85
|
+
lambda n: n + 3,
|
|
86
|
+
print
|
|
87
|
+
)
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
"""
|
|
91
|
+
def inner_func(argument):
|
|
92
|
+
func(argument)
|
|
93
|
+
return argument
|
|
94
|
+
return inner_func
|
|
95
|
+
|
|
96
|
+
def _tmap(argument, func):
|
|
97
|
+
length = len(argument)
|
|
98
|
+
threads = []
|
|
99
|
+
index = 0
|
|
100
|
+
args = [argument]
|
|
101
|
+
while index < length:
|
|
102
|
+
t = Thread(target=func, args=[argument[index]])
|
|
103
|
+
t.start()
|
|
104
|
+
threads.append(t)
|
|
105
|
+
index += 1
|
|
106
|
+
result = []
|
|
107
|
+
for t in threads:
|
|
108
|
+
t.join()
|
|
109
|
+
result.append(t.result)
|
|
110
|
+
if isinstance(argument, tuple):
|
|
111
|
+
return tuple(result)
|
|
112
|
+
return result
|
|
113
|
+
|
|
114
|
+
def tmap(*args):
|
|
115
|
+
"""Map a function concurrently across each element of an list or tuple.
|
|
116
|
+
|
|
117
|
+
A mapper is a function that specifies a single element of an list or tuple and returns a corresponding element of the resulting list or tuple. Each mapper invocation happens in a separate thread.
|
|
118
|
+
|
|
119
|
+
```python
|
|
120
|
+
squared = tmap([1, 2, 3], lambda n: n ** 2)
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
If the list or tuple argument is omitted, returns a function of the mapper that expects the argument.
|
|
124
|
+
|
|
125
|
+
```python
|
|
126
|
+
my_mapping_func = tmap(lambda n: n ** 2)
|
|
127
|
+
squared = my_mapping_func([1, 2, 3])
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
"""
|
|
131
|
+
if callable(args[0]):
|
|
132
|
+
return partial(_tmap, _, args[0])
|
|
133
|
+
return _tmap(*args)
|
|
134
|
+
|
|
135
|
+
def _tforeach(argument, func):
|
|
136
|
+
length = len(argument)
|
|
137
|
+
threads = []
|
|
138
|
+
index = 0
|
|
139
|
+
args = [argument]
|
|
140
|
+
while index < length:
|
|
141
|
+
t = Thread(target=func, args=[argument[index]])
|
|
142
|
+
t.start()
|
|
143
|
+
threads.append(t)
|
|
144
|
+
index += 1
|
|
145
|
+
for t in threads:
|
|
146
|
+
t.join()
|
|
147
|
+
return None
|
|
148
|
+
|
|
149
|
+
def tforeach(*args):
|
|
150
|
+
"""Execute a callback concurrently for each element of an list or tuple.
|
|
151
|
+
|
|
152
|
+
A callback is a function that does not necessarily specify a value or return. Each callback invocation happens in a separate thread.
|
|
153
|
+
|
|
154
|
+
```python
|
|
155
|
+
tforeach([1, 2, 3], print)
|
|
156
|
+
```
|
|
157
|
+
|
|
158
|
+
If the list or tuple argument is omitted, returns a function of the callback that expects the argument.
|
|
159
|
+
|
|
160
|
+
```python
|
|
161
|
+
my_foreach_func = tforeach(print)
|
|
162
|
+
my_foreach_func([1, 2, 3])
|
|
163
|
+
```
|
|
164
|
+
|
|
165
|
+
"""
|
|
166
|
+
if callable(args[0]):
|
|
167
|
+
return partial(_tforeach, _, args[0])
|
|
168
|
+
return _tforeach(*args)
|
|
169
|
+
|
|
170
|
+
def _tfilter(argument, func):
|
|
171
|
+
length = len(argument)
|
|
172
|
+
threads = []
|
|
173
|
+
index = 0
|
|
174
|
+
args = [argument]
|
|
175
|
+
while index < length:
|
|
176
|
+
t = Thread(target=func, args=[argument[index]])
|
|
177
|
+
t.start()
|
|
178
|
+
threads.append(t)
|
|
179
|
+
index += 1
|
|
180
|
+
result = []
|
|
181
|
+
index = 0
|
|
182
|
+
while index < length:
|
|
183
|
+
t = threads[index]
|
|
184
|
+
t.join()
|
|
185
|
+
if t.result:
|
|
186
|
+
result.append(argument[index])
|
|
187
|
+
index += 1
|
|
188
|
+
if isinstance(argument, tuple):
|
|
189
|
+
return tuple(result)
|
|
190
|
+
return result
|
|
191
|
+
|
|
192
|
+
def tfilter(*args):
|
|
193
|
+
"""Concurrently filter an list or tuple.
|
|
194
|
+
|
|
195
|
+
A predicate is a function that specifies an element of an list or tuple and returns a boolean value. Elements corresponding to predicate invocations that return `False` are filtered out of the resulting list or tuple, while elements corresponding to predicate invocationsn that return `True` are retained. Each predicate invocation happens in a separate thread.
|
|
196
|
+
|
|
197
|
+
```python
|
|
198
|
+
def is_odd(n):
|
|
199
|
+
return n % 2 == 1
|
|
200
|
+
|
|
201
|
+
odd_numbers = tfilter([1, 2, 3, 4, 5], is_odd)
|
|
202
|
+
```
|
|
203
|
+
|
|
204
|
+
If the list or tuple argument is omitted, returns a function of the predicate that expects the argument.
|
|
205
|
+
|
|
206
|
+
```python
|
|
207
|
+
def is_odd(n):
|
|
208
|
+
return n % 2 == 1
|
|
209
|
+
|
|
210
|
+
filter_odds = tfilter(is_odd)
|
|
211
|
+
odd_numbers = filter_odds([1, 2, 3, 4, 5])
|
|
212
|
+
```
|
|
213
|
+
|
|
214
|
+
"""
|
|
215
|
+
if callable(args[0]):
|
|
216
|
+
return partial(_tfilter, _, args[0])
|
|
217
|
+
return _tfilter(*args)
|
|
218
|
+
|
|
219
|
+
def reduce(*args):
|
|
220
|
+
"""Reduce an list or tuple to a single value (accumulator).
|
|
221
|
+
|
|
222
|
+
A reducer is a function that specifies an accumulator and a given element of an list or tuple, and returns an accumulator. Each reducer invocation happens sequentially.
|
|
223
|
+
|
|
224
|
+
```python
|
|
225
|
+
# add is a reducer
|
|
226
|
+
def add(a, b):
|
|
227
|
+
return a + b
|
|
228
|
+
|
|
229
|
+
sum = reduce([1, 2, 3, 4, 5], add)
|
|
230
|
+
```
|
|
231
|
+
|
|
232
|
+
If an initial value is provided, it is treated as the starting value for the accumulator.
|
|
233
|
+
|
|
234
|
+
```python
|
|
235
|
+
sum = reduce([1, 2, 3, 4, 5], add, 10)
|
|
236
|
+
```
|
|
237
|
+
|
|
238
|
+
If the list or tuple argument is omitted, returns a function of the reducer and initial value that expects the argument.
|
|
239
|
+
|
|
240
|
+
```python
|
|
241
|
+
reducing_func = reduce(add, 10)
|
|
242
|
+
sum = reducing_func([1, 2, 3, 4, 5])
|
|
243
|
+
```
|
|
244
|
+
|
|
245
|
+
"""
|
|
246
|
+
if callable(args[0]):
|
|
247
|
+
if len(args) == 2:
|
|
248
|
+
return partial(_reduce, args[0], _, args[1])
|
|
249
|
+
return partial(_reduce, args[0])
|
|
250
|
+
if len(args) == 3:
|
|
251
|
+
return _reduce(args[1], args[0], args[2])
|
|
252
|
+
return _reduce(args[1], args[0])
|
|
253
|
+
|
|
254
|
+
def _tflatmap(argument, func):
|
|
255
|
+
length = len(argument)
|
|
256
|
+
threads = []
|
|
257
|
+
index = 0
|
|
258
|
+
args = [argument]
|
|
259
|
+
while index < length:
|
|
260
|
+
t = Thread(target=func, args=[argument[index]])
|
|
261
|
+
t.start()
|
|
262
|
+
threads.append(t)
|
|
263
|
+
index += 1
|
|
264
|
+
result = []
|
|
265
|
+
for t in threads:
|
|
266
|
+
t.join()
|
|
267
|
+
if isinstance(t.result, list) or isinstance(t.result, tuple):
|
|
268
|
+
result = result + t.result
|
|
269
|
+
else:
|
|
270
|
+
result.append(t.result)
|
|
271
|
+
if isinstance(argument, tuple):
|
|
272
|
+
return tuple(result)
|
|
273
|
+
return result
|
|
274
|
+
|
|
275
|
+
def tflatmap(*args):
|
|
276
|
+
"""Apply a flatmapper concurrently to each element of an list or tuple, concatenating the results.
|
|
277
|
+
|
|
278
|
+
A flatmapper is a function that specifies an element of the list or tuple, and returns a list, tuple, or single element. A returned list or tuple is concatenated onto the resulting list or tuple, while a returned single element is appended. Each flatmapper invocation happens in a separate thread.
|
|
279
|
+
|
|
280
|
+
```python
|
|
281
|
+
duplicates = tflatmap([1, 2, 3], lambda n: [n, n, n])
|
|
282
|
+
```
|
|
283
|
+
|
|
284
|
+
If the list or tuple argument is omitted, returns a function of the flatmapping function that expects the argument.
|
|
285
|
+
|
|
286
|
+
```python
|
|
287
|
+
my_flatmapping_func = tflatmap(lambda n: [n, n, n])
|
|
288
|
+
duplicates = my_flatmapping_func([1, 2, 3])
|
|
289
|
+
```
|
|
290
|
+
|
|
291
|
+
"""
|
|
292
|
+
if callable(args[0]):
|
|
293
|
+
return partial(_tflatmap, _, args[0])
|
|
294
|
+
return _tflatmap(*args)
|
|
295
|
+
|
|
296
|
+
__name__ = 'functions'
|
|
297
|
+
|
|
298
|
+
__all__ = ['always', 'thunkify', 'chain', 'tap', 'tmap', 'tforeach', 'tfilter', 'reduce', 'tflatmap']
|
{functionalthreading-0.6.6 → functionalthreading-1.0.0}/functionalthreading.egg-info/PKG-INFO
RENAMED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: functionalthreading
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 1.0.0
|
|
4
4
|
Summary: Concurrent functional programming with thread-based parallelism
|
|
5
5
|
Author-email: Richard Tong <richytong@gmail.com>
|
|
6
6
|
License-Expression: Unlicense
|
|
@@ -200,7 +200,32 @@ Used as a placeholder for partial arguments.
|
|
|
200
200
|
|
|
201
201
|
> Added in version 3.14.
|
|
202
202
|
|
|
203
|
+
### always(argument)
|
|
204
|
+
Always return a value.
|
|
205
|
+
|
|
206
|
+
```python
|
|
207
|
+
always5 = always(5)
|
|
208
|
+
|
|
209
|
+
always5()
|
|
210
|
+
```
|
|
211
|
+
|
|
212
|
+
|
|
213
|
+
### thunkify(func, *args, **kwargs)
|
|
214
|
+
Create a thunk from a function and arguments.
|
|
215
|
+
|
|
216
|
+
A thunk is a function that takes no arguments and executes the provided function with the provided arguments each call.
|
|
217
|
+
|
|
218
|
+
```python
|
|
219
|
+
printHello = thunkify(print, 'Hello')
|
|
220
|
+
|
|
221
|
+
printHello()
|
|
222
|
+
printHello()
|
|
223
|
+
printHello()
|
|
224
|
+
```
|
|
225
|
+
|
|
226
|
+
|
|
203
227
|
### chain(argument, *funcs)
|
|
228
|
+
### chain(*funcs)
|
|
204
229
|
Chain functions together.
|
|
205
230
|
|
|
206
231
|
Each function is evaluated in series starting from the first function, passing the return value as the first and only argument to the next function. The return value of the chain is the return value of the last function.
|
|
@@ -245,16 +270,17 @@ chain(
|
|
|
245
270
|
```
|
|
246
271
|
|
|
247
272
|
|
|
248
|
-
### tmap(
|
|
249
|
-
|
|
273
|
+
### tmap(argument, func)
|
|
274
|
+
### tmap(func)
|
|
275
|
+
Map a function concurrently across each element of an list or tuple.
|
|
250
276
|
|
|
251
|
-
|
|
277
|
+
A mapper is a function that specifies a single element of an list or tuple and returns a corresponding element of the resulting list or tuple. Each mapper invocation happens in a separate thread.
|
|
252
278
|
|
|
253
279
|
```python
|
|
254
280
|
squared = tmap([1, 2, 3], lambda n: n ** 2)
|
|
255
281
|
```
|
|
256
282
|
|
|
257
|
-
If the
|
|
283
|
+
If the list or tuple argument is omitted, returns a function of the mapper that expects the argument.
|
|
258
284
|
|
|
259
285
|
```python
|
|
260
286
|
my_mapping_func = tmap(lambda n: n ** 2)
|
|
@@ -262,3 +288,63 @@ squared = my_mapping_func([1, 2, 3])
|
|
|
262
288
|
```
|
|
263
289
|
|
|
264
290
|
|
|
291
|
+
### tforeach(argument, func)
|
|
292
|
+
### tforeach(func)
|
|
293
|
+
Execute a callback concurrently for each element of an list or tuple.
|
|
294
|
+
|
|
295
|
+
A callback is a function that does not necessarily specify a value or return. Each callback invocation happens in a separate thread.
|
|
296
|
+
|
|
297
|
+
```python
|
|
298
|
+
tforeach([1, 2, 3], print)
|
|
299
|
+
```
|
|
300
|
+
|
|
301
|
+
If the list or tuple argument is omitted, returns a function of the callback that expects the argument.
|
|
302
|
+
|
|
303
|
+
```python
|
|
304
|
+
my_foreach_func = tforeach(print)
|
|
305
|
+
my_foreach_func([1, 2, 3])
|
|
306
|
+
```
|
|
307
|
+
|
|
308
|
+
|
|
309
|
+
### tfilter(argument, func)
|
|
310
|
+
### tfilter(func)
|
|
311
|
+
Concurrently filter an list or tuple.
|
|
312
|
+
|
|
313
|
+
A predicate is a function that specifies an element of an list or tuple and returns a boolean value. Elements corresponding to predicate invocations that return `False` are filtered out of the resulting list or tuple, while elements corresponding to predicate invocationsn that return `True` are retained. Each predicate invocation happens in a separate thread.
|
|
314
|
+
|
|
315
|
+
```python
|
|
316
|
+
def is_odd(n):
|
|
317
|
+
return n % 2 == 1
|
|
318
|
+
|
|
319
|
+
odd_numbers = tfilter([1, 2, 3, 4, 5], is_odd)
|
|
320
|
+
```
|
|
321
|
+
|
|
322
|
+
If the list or tuple argument is omitted, returns a function of the predicate that expects the argument.
|
|
323
|
+
|
|
324
|
+
```python
|
|
325
|
+
def is_odd(n):
|
|
326
|
+
return n % 2 == 1
|
|
327
|
+
|
|
328
|
+
filter_odds = tfilter(is_odd)
|
|
329
|
+
odd_numbers = filter_odds([1, 2, 3, 4, 5])
|
|
330
|
+
```
|
|
331
|
+
|
|
332
|
+
|
|
333
|
+
### tflatmap(argument, func)
|
|
334
|
+
### tflatmap(func)
|
|
335
|
+
Apply a flatmapper concurrently to each element of an list or tuple, concatenating the results.
|
|
336
|
+
|
|
337
|
+
A flatmapper is a function that specifies an element of the list or tuple, and returns a list, tuple, or single element. A returned list or tuple is concatenated onto the resulting list or tuple, while a returned single element is appended. Each flatmapper invocation happens in a separate thread.
|
|
338
|
+
|
|
339
|
+
```python
|
|
340
|
+
duplicates = tflatmap([1, 2, 3], lambda n: [n, n, n])
|
|
341
|
+
```
|
|
342
|
+
|
|
343
|
+
If the list or tuple argument is omitted, returns a function of the flatmapping function that expects the argument.
|
|
344
|
+
|
|
345
|
+
```python
|
|
346
|
+
my_flatmapping_func = tflatmap(lambda n: [n, n, n])
|
|
347
|
+
duplicates = my_flatmapping_func([1, 2, 3])
|
|
348
|
+
```
|
|
349
|
+
|
|
350
|
+
|
|
@@ -1,105 +0,0 @@
|
|
|
1
|
-
from functools import partial, Placeholder as _
|
|
2
|
-
from functionalthreading.classes import Thread
|
|
3
|
-
|
|
4
|
-
def _chain(argument, funcs):
|
|
5
|
-
ret = argument
|
|
6
|
-
for func in funcs:
|
|
7
|
-
ret = func(ret)
|
|
8
|
-
return ret
|
|
9
|
-
|
|
10
|
-
def chain(argument, *funcs):
|
|
11
|
-
"""Chain functions together.
|
|
12
|
-
|
|
13
|
-
Each function is evaluated in series starting from the first function, passing the return value as the first and only argument to the next function. The return value of the chain is the return value of the last function.
|
|
14
|
-
|
|
15
|
-
```python
|
|
16
|
-
chain(
|
|
17
|
-
2,
|
|
18
|
-
lambda n: n + 1,
|
|
19
|
-
lambda n: n ** 2,
|
|
20
|
-
lambda n: n / 3,
|
|
21
|
-
print
|
|
22
|
-
)
|
|
23
|
-
```
|
|
24
|
-
|
|
25
|
-
If the first non-function argument is omitted, returns a function of chained functions that expects the non-function argument.
|
|
26
|
-
|
|
27
|
-
```python
|
|
28
|
-
my_function_chain = chain(
|
|
29
|
-
lambda n: n + 1,
|
|
30
|
-
lambda n: n ** 2,
|
|
31
|
-
lambda n: n / 3,
|
|
32
|
-
print
|
|
33
|
-
)
|
|
34
|
-
|
|
35
|
-
my_function_chain(2)
|
|
36
|
-
```
|
|
37
|
-
|
|
38
|
-
"""
|
|
39
|
-
if callable(argument):
|
|
40
|
-
return partial(_chain, _, (argument,) + funcs)
|
|
41
|
-
return _chain(argument, funcs)
|
|
42
|
-
|
|
43
|
-
def tap(func):
|
|
44
|
-
"""Call a function with an argument, returning the argument.
|
|
45
|
-
|
|
46
|
-
```python
|
|
47
|
-
chain(
|
|
48
|
-
1,
|
|
49
|
-
lambda n: n + 1,
|
|
50
|
-
tap(print),
|
|
51
|
-
lambda n: n + 2,
|
|
52
|
-
tap(print),
|
|
53
|
-
lambda n: n + 3,
|
|
54
|
-
print
|
|
55
|
-
)
|
|
56
|
-
```
|
|
57
|
-
|
|
58
|
-
"""
|
|
59
|
-
def inner_func(argument):
|
|
60
|
-
func(argument)
|
|
61
|
-
return argument
|
|
62
|
-
return inner_func
|
|
63
|
-
|
|
64
|
-
def _tmap(array_or_tuple, func):
|
|
65
|
-
length = len(array_or_tuple)
|
|
66
|
-
threads = []
|
|
67
|
-
index = 0
|
|
68
|
-
args = [array_or_tuple]
|
|
69
|
-
while index < length:
|
|
70
|
-
t = Thread(target=func, args=[array_or_tuple[index]])
|
|
71
|
-
t.start()
|
|
72
|
-
threads.append(t)
|
|
73
|
-
index += 1
|
|
74
|
-
result = []
|
|
75
|
-
for t in threads:
|
|
76
|
-
t.join()
|
|
77
|
-
result.append(t.result)
|
|
78
|
-
if isinstance(array_or_tuple, tuple):
|
|
79
|
-
return tuple(result)
|
|
80
|
-
return result
|
|
81
|
-
|
|
82
|
-
def tmap(*args):
|
|
83
|
-
"""Map a function concurrently across each element of an array or tuple.
|
|
84
|
-
|
|
85
|
-
Each function invokation happens in a separate thread.
|
|
86
|
-
|
|
87
|
-
```python
|
|
88
|
-
squared = tmap([1, 2, 3], lambda n: n ** 2)
|
|
89
|
-
```
|
|
90
|
-
|
|
91
|
-
If the array or tuple argument is omitted, returns a function of the mapping function that expects the non-function argument.
|
|
92
|
-
|
|
93
|
-
```python
|
|
94
|
-
my_mapping_func = tmap(lambda n: n ** 2)
|
|
95
|
-
squared = my_mapping_func([1, 2, 3])
|
|
96
|
-
```
|
|
97
|
-
|
|
98
|
-
"""
|
|
99
|
-
if callable(args[0]):
|
|
100
|
-
return partial(_tmap, _, args[0])
|
|
101
|
-
return _tmap(*args)
|
|
102
|
-
|
|
103
|
-
__name__ = 'functions'
|
|
104
|
-
|
|
105
|
-
__all__ = ['chain', 'tap', 'tmap']
|
|
File without changes
|
{functionalthreading-0.6.6 → functionalthreading-1.0.0}/functionalthreading/classes/__init__.py
RENAMED
|
File without changes
|
{functionalthreading-0.6.6 → functionalthreading-1.0.0}/functionalthreading.egg-info/SOURCES.txt
RENAMED
|
File without changes
|
|
File without changes
|
{functionalthreading-0.6.6 → functionalthreading-1.0.0}/functionalthreading.egg-info/top_level.txt
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|