You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
|`from_iterable`| • `iterable` - An iterable to be used in the iterator chain | Starts the iterator chain with the supplied iterable. Chaining and terminating methods can now be called on the result. |
45
+
|`from_iterable_parallel`| • `iterable` - An iterable to be used in the iterator chain<br/>• `chunksize` - Keyword. How big of chunks to split the iterator up across the parallel execution units. If unspecified or None, the chunk size will start at 1 and send that many elements to each execution unit. The chunk size will then increment in powers of two and send that many items to each execution unit. This is repeated until the iterator is exhausted. This value is used as the default chunksize for all the following parallel based methods. A specific parallel based method's chunksize can be overrided by supplying the `chunksize` keyword to that method. | Starts the iterator chain with the supplied iterable. Chaining and terminating methods can now be called on the result. Certain chaining and terminating methods will occur in parallel. Parallel means separate processes to get around Python's GIL. |
44
46
45
47
46
48
### Continuing the chain
@@ -55,33 +57,44 @@ called.
55
57
an actual value. This value will depend on all the previous chaining methods being executed first.
56
58
57
59
#### Chaining methods
58
-
| Method | Arguments | Description |
60
+
| Method | Arguments | Description |
59
61
| --- | --- | --- |
60
62
|`map`| • `function` - A function that takes a single argument | Will run the `function` across all the elements in the iterator. |
61
63
|`filter`| • `function` - A function that takes a single argument | Will run the `function` on every element. `function` should return a truthy or falsy value. On true, the element will stay; on false, the element will be removed. |
62
64
|`skip`| • `number` - An integer | The `number` number of elements will be skipped over and effectively removed. |
63
65
|`distinct`|| Any duplicates will be removed. |
64
66
|`limit`| • `max_size` - An integer | The iterator will stop after `max_size` elements. Any elements afterward are effectively removed. |
65
67
|`flatten`|| Any element that is an iterable itself will have its elements iterated over first before continuing with the remaining elements. Strings (`str`) do not count as an iterable for this method. Dictionaries flatten to its item tuples. |
66
-
|`sort`| • `key` - Keyword. A function of one argument that is used to extract a comparison key from each element<br/>• `cmp` - Keyword. A Python 2.x "cmp" function that takes two arguments<br/>• `reverse` - Keyword. If set to `True`, the elements will be sorted in the reverse order. | Sorts the iterator based on the elements' values. Use `key` or `cmp` to make a custom comparison. If `key` is specified, `cmp` cannot be used. This method is expensive because it must serialize all the values into a sequence. |
67
-
|`reverse`|| Reverses the iterator. The last time will be first, and the first item will be last. This method is expensive because it must serialize all the values into a list. |
68
+
|`sort`| • `key` - Keyword. A function of one argument that is used to extract a comparison key from each element<br/>• `cmp` - Keyword. A Python 2.x "cmp" function that takes two arguments<br/>• `reverse` - Keyword. If set to `True`, the elements will be sorted in the reverse order | Sorts the iterator based on the elements' values. Use `key` or `cmp` to make a custom comparison. If `key` is specified, `cmp` cannot be used. This method is expensive because it must serialize all the values into a sequence. |
69
+
|`reverse`|| Reverses the iterator. The last item will be first, and the first item will be last. This method is expensive because it must serialize all the values into a list. |
70
+
71
+
##### Parallel Versions
72
+
| Method | Arguments | Description |
73
+
| --- | --- | --- |
74
+
|`map`| • `function` - A function that takes a single argument<br/>• `chunksize` - Keyword. Overrides the chunksize supplied to the original `from_iterable_parallel`| Will run the `function` across all the elements in the iterator in parallel. |
75
+
|`filter`| • `function` - A function that takes a single argument<br/>• `chunksize` - Keyword. Overrides the chunksize supplied to the original `from_iterable_parallel`| Will run the `function` on every element in parallel. `function` should return a truthy or falsy value. On true, the element will stay; on false, the element will be removed. |
68
76
69
77
#### Terminating methods
70
-
| Method | Arguments | Description |
78
+
| Method | Arguments | Description |
71
79
| --- | --- | --- |
72
80
|`list`|| Serializes the iterator chain into a `list` and returns it. |
73
-
|`count`|| Returns the number of elements in the iterator |
81
+
|`count`|| Returns the number of elements in the iterator.|
74
82
|`first`| • `default` - Keyword. Any value. | Returns just the first item in the iterator. If the iterator is empty, the `default` is returned. |
75
83
|`last`| • `default` - Keyword. Any value. | Returns just the last item in the iterator. If the iterator is empty, the `default` is returned. |
76
84
|`max`| • `default` - Keyword. Any value. | Returns the largest valued element in the iterator. If the iterator is empty, the `default` is returned. |
77
85
|`min`| • `default` - Keyword. Any value. | Returns the smallest valued element in the iterator. If the iterator is empty, the `default` is returned. |
78
86
|`sum`| • `default` - Keyword. Any value. | Sums all the elements in the iterator together. If any of the elements are un-summable, the `default` is returned. |
79
-
|`reduce`| • `function` - A function that takes two arguments| Applies the function to two elements in the iterator cumulatively. Subsequent calls to `function` uses the previous return value from `function` as the first argument and the next element in the iterator as the second argument. The final value is returned. |
87
+
|`reduce`| • `function` - A function that takes two arguments<br/>• `initial` - Keyword. Any value. | Applies the function to two elements in the iterator cumulatively. Subsequent calls to `function` uses the previous return value from `function` as the first argument and the next element in the iterator as the second argument. The final value is returned. If `initial` is present, it is placed before the items of the sequence in the calculation, and serves as a default when the sequence is empty. |
80
88
|`for_each`| • `function` - A function that takes one argument and returns nothing | Executes `function` on every element in the iterator. There is no return value. If you are wanting to return a list of values based on the function, use `.map(_function_).list()`. |
81
89
|`all_match`| • `function` - A function that takes one argument and returns a boolean | Returns `True` only if _all_ the elements return `True` after applying the `function` to them. Else returns `False`. |
82
90
|`any_match`| • `function` - A function that takes one argument and returns a boolean | Returns `True` if just one element return `True` after applying the `function` to it. If all elements result in `False`, `False` is returned. |
83
91
|`none_match`| • `function` - A function that takes one argument and returns a boolean | Returns `True` only if _all_ the elements return `False` after applying the `function` to them. Else returns `True`. |
84
92
93
+
##### Parallel Versions
94
+
| Method | Arguments | Description |
95
+
| --- | --- | --- |
96
+
|`for_each`| • `function` - A function that takes one argument and returns nothing<br/>• `chunksize` - Keyword. Overrides the chunksize supplied to the original `from_iterable_parallel`| Executes `function` on every element in the iterator in parallel. There is no return value. If you are wanting to return a list of values based on the function, use `.map(function).list()`. |
Starts the iterator chain with the supplied iterable. Chaining and terminating methods can now be called on the result. Certain chaining and terminating methods will occur in parallel. Parallel means separate processes to get around Python's GIL.
20
+
21
+
:param iterable: An iterable to be used in the iterator chain.
22
+
:param chunksize: How big of chunks to split the iterator up across the parallel execution units. If unspecified or None, the chunk size will start at 1 and send that many elements to each execution unit. The chunk size will then increment in powers of two and send that many items to each execution unit. This is repeated until the iterator is exhausted. This value is used as the default chunksize for all the following parallel based methods. A specific parallel based method's chunksize can be overrided by supplying the `chunksize` keyword to that method.
23
+
:return: An intermediate object that subsequent chaining and terminating methods can be called on.
:param function: A function that takes a single argument.
16
+
:return: An intermediate object that subsequent chaining and terminating methods can be called on.
17
+
"""
18
+
iterator=map(function, self._iterator)
17
19
return_IntermediateIteratorChain(iterator)
18
20
19
21
deffilter(self, function):
22
+
"""
23
+
Will run the `function` on every element. `function` should return a truthy or falsy value. On true, the element will stay; on false, the element will be removed.
24
+
25
+
:param function: A function that takes a single argument.
26
+
:return: An intermediate object that subsequent chaining and terminating methods can be called on.
27
+
"""
20
28
iterator=filter(function, self._iterator)
21
29
return_IntermediateIteratorChain(iterator)
22
30
31
+
defskip(self, number):
32
+
"""
33
+
The `number` number of elements will be skipped over and effectively removed.
34
+
35
+
:param number: An integer.
36
+
:return: An intermediate object that subsequent chaining and terminating methods can be called on.
Any element that is an iterable itself will have its elements iterated over first before continuing with the remaining elements. Strings (`str`) do not count as an iterable for this method. Dictionaries flatten to its item tuples.
94
+
95
+
:return: An intermediate object that subsequent chaining and terminating methods can be called on.
96
+
"""
57
97
iterator=self._flatten(self._iterator)
58
98
return_IntermediateIteratorChain(iterator)
59
99
60
100
defsort(self, key=None, cmp=None, reverse=False):
101
+
"""
102
+
Sorts the iterator based on the elements' values. Use `key` or `cmp` to make a custom comparison. If `key` is specified, `cmp` cannot be used. This method is expensive because it must serialize all the values into a sequence.
103
+
104
+
:param key: Keyword. A function of one argument that is used to extract a comparison key from each element.
105
+
:param cmp: Keyword. A Python 2.x "cmp" function that takes two arguments.
106
+
:param reverse: Keyword. If set to `True`, the elements will be sorted in the reverse order.
107
+
:return: An intermediate object that subsequent chaining and terminating methods can be called on.
Reverses the iterator. The last item will be first, and the first item will be last. This method is expensive because it must serialize all the values into a list.
120
+
121
+
:return: An intermediate object that subsequent chaining and terminating methods can be called on.
122
+
"""
123
+
iterator=self._reverse()
69
124
return_IntermediateIteratorChain(iterator)
70
125
126
+
def_reverse(self):
127
+
forward=list(self._iterator)
128
+
returnreversed(forward)
129
+
71
130
# Termination methods
72
131
deflist(self):
132
+
"""
133
+
Serializes the iterator chain into a `list` and returns it.
134
+
135
+
:return: A list whose elements come from the iterator.
136
+
"""
73
137
returnlist(self._iterator)
74
138
75
139
defcount(self):
140
+
"""
141
+
Returns the number of elements in the iterator
142
+
143
+
:return: An integer.
144
+
"""
76
145
returnsum(1for_inself._iterator)
77
146
78
147
deffirst(self, default=None):
148
+
"""
149
+
Returns just the first item in the iterator. If the iterator is empty, the `default` is returned.
Returns the largest valued element in the iterator. If the iterator is empty, the `default` is returned.
172
+
173
+
:param default: Keyword. Any value.
174
+
:return: The largest element.
175
+
"""
89
176
returnmax(self._iterator, default=default)
90
177
91
178
defmin(self, default=None):
179
+
"""
180
+
Returns the smallest valued element in the iterator. If the iterator is empty, the `default` is returned.
181
+
182
+
:param default: Keyword. Any value.
183
+
:return: The smallest element.
184
+
"""
92
185
returnmin(self._iterator, default=default)
93
186
94
187
defsum(self, default=None):
188
+
"""
189
+
Sums all the elements in the iterator together. If any of the elements are un-summable, the `default` is returned.
190
+
191
+
:param default: Keyword. Any value.
192
+
:return: The sum of all the elements.
193
+
"""
95
194
try:
96
195
total=sum(self._iterator)
97
196
exceptTypeError:
98
197
total=default
99
198
returntotal
100
199
101
-
defreduce(self, function):
102
-
returnfunctools.reduce(function, self._iterator)
200
+
defreduce(self, function, initial=None):
201
+
"""
202
+
Applies the function to two elements in the iterator cumulatively. Subsequent calls to `function` uses the previous return value from `function` as the first argument and the next element in the iterator as the second argument. The final value is returned. If `initial` is present, it is placed before the items of the sequence in the calculation, and serves as a default when the sequence is empty.
203
+
204
+
:param function: A function that takes two arguments.
Executes `function` on every element in the iterator. There is no return value. If you are wanting to return a list of values based on the function, use `.map(function).list()`.
216
+
217
+
:param function: A function that takes one argument and returns nothing.
218
+
"""
105
219
foriteminself._iterator:
106
220
function(item)
107
221
108
222
defall_match(self, function):
223
+
"""
224
+
Returns `True` only if all the elements return `True` after applying the `function` to them. Else returns `False`.
225
+
226
+
:param function: A function that takes one argument and returns a boolean.
227
+
:return: True or False
228
+
"""
109
229
returnall(map(function, self._iterator))
110
230
111
231
defany_match(self, function):
232
+
"""
233
+
Returns `True` if just one element return `True` after applying the `function` to it. If all elements result in `False`, `False` is returned.
234
+
235
+
:param function: A function that takes one argument and returns a boolean.
236
+
:return: True or False
237
+
"""
112
238
returnany(map(function, self._iterator))
113
239
114
240
defnone_match(self, function):
241
+
"""
242
+
Returns `True` only if all the elements return `False` after applying the `function` to them. Else returns `True`.
243
+
244
+
:param function: A function that takes one argument and returns a boolean.
0 commit comments