functionalthreading 0.2.7__tar.gz → 0.3.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.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: functionalthreading
3
- Version: 0.2.7
3
+ Version: 0.3.0
4
4
  Summary: Functional programming with thread-based parallelism
5
5
  Author-email: Richard Tong <richytong@gmail.com>
6
6
  Project-URL: Homepage, https://github.com/richytong/functionalthreading
@@ -1,5 +1,5 @@
1
+ from functools import partial, Placeholder as _
1
2
  from threading import Thread
2
- import itertools
3
3
 
4
4
  def _chain(argument, funcs):
5
5
  ret = argument
@@ -37,10 +37,7 @@ def chain(argument, *funcs):
37
37
 
38
38
  """
39
39
  if callable(argument):
40
- inner_funcs = (argument,) + funcs
41
- def inner_func(inner_argument):
42
- return _chain(inner_argument, inner_funcs)
43
- return inner_func
40
+ return partial(_chain, _, (argument,) + funcs)
44
41
  return _chain(argument, funcs)
45
42
 
46
43
  def tap(func):
@@ -74,16 +71,7 @@ class _Thread(Thread):
74
71
  def run(self):
75
72
  self.value = self.target(*self.args)
76
73
 
77
- def tmap(array_or_tuple, func):
78
- """Map a function concurrently across each element of an array or tuple.
79
-
80
- Each function invokation happens in a separate thread.
81
-
82
- ```python
83
- squared = tmap([1, 2, 3], lambda n: n ** 2)
84
- ```
85
-
86
- """
74
+ def _tmap(array_or_tuple, func):
87
75
  length = len(array_or_tuple)
88
76
  threads = []
89
77
  index = 0
@@ -104,6 +92,27 @@ def tmap(array_or_tuple, func):
104
92
  return tuple(result)
105
93
  return result
106
94
 
95
+ def tmap(*args):
96
+ """Map a function concurrently across each element of an array or tuple.
97
+
98
+ Each function invokation happens in a separate thread.
99
+
100
+ ```python
101
+ squared = tmap([1, 2, 3], lambda n: n ** 2)
102
+ ```
103
+
104
+ If the array or tuple argument is omitted, returns a function of the mapping function that expects the non-function argument.
105
+
106
+ ```python
107
+ my_mapping_func = tmap(lambda n: n ** 2)
108
+ squared = my_mapping_func([1, 2, 3])
109
+ ```
110
+
111
+ """
112
+ if callable(args[0]):
113
+ return partial(_tmap, _, args[0])
114
+ return _tmap(*args)
115
+
107
116
  __name__ = 'functions'
108
117
 
109
118
  __all__ = ['chain', 'tap', 'tmap']
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: functionalthreading
3
- Version: 0.2.7
3
+ Version: 0.3.0
4
4
  Summary: Functional programming with thread-based parallelism
5
5
  Author-email: Richard Tong <richytong@gmail.com>
6
6
  Project-URL: Homepage, https://github.com/richytong/functionalthreading
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "functionalthreading"
3
- version = "0.2.7"
3
+ version = "0.3.0"
4
4
  authors = [
5
5
  { name="Richard Tong", email="richytong@gmail.com" },
6
6
  ]