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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
|
Reducing
========
.. module:: fun
The section contains functions to analyze iteration values and recombine
through use of a given combining operation the results of recursively processing
its constituent parts, building up a return value
.. contents::
.. note:: An attempt to use infinity iterators with the most function from
the module causes an infinite loop.
Folds
-----
.. function:: foldl(accfun, initval, gen, param, state)
iterator:reduce(accfun, initval)
:param accfun: an accumulating function
:type param: (function(prevval, ...) -> val)
:param initval: an initial value that passed to **accfun** on the first
iteration
The function reduces the iterator from left to right using the binary
operator **accfun** and the initial value **initval**.
Equivalent to::
local val = initval
for _k, ... in gen, param, state do
val = accfun(val, ...)
end
return val
Examples:
.. code-block:: lua
> print(foldl(function(acc, x) return acc + x end, 0, range(5)))
15
> print(foldl(operator.add, 0, range(5)))
15
> print(foldl(function(acc, x, y) return acc + x * y; end, 0,
zip(range(1, 5), {4, 3, 2, 1})))
20
.. function:: reduce(accfun, initval, gen, param, state)
iterator:reduce(accfun, initval)
An alias to :func:`foldl`.
.. function:: length(gen, param, state)
iterator:length()
:returns: a number of elements in ``gen, param, state`` iterator.
Return a number of elements in ``gen, param, state`` iterator.
This function is equivalent to ``#obj`` for basic array and string iterators.
Examples:
.. code-block:: lua
> print(length({"a", "b", "c", "d", "e"}))
5
> print(length({}))
0
> print(length(range(0)))
0
.. warning:: An attempt to call this function on an infinite iterator will
result an infinite loop.
.. note:: This function has ``O(n)`` complexity for all iterators except
basic array and string iterators.
.. function:: totable(gen, param, state)
:returns: a new table (array) from iterated values.
The function reduces the iterator from left to right using ``table.insert``.
Examples:
.. code-block:: lua
> local tab = totable("abcdef")
> print(type(tab), #tab)
table 6
> each(print, tab)
a
b
c
d
e
f
.. function:: tomap(gen, param, state)
:returns: a new table (map) from iterated values.
The function reduces the iterator from left to right using
``tab[val1] = val2`` expression.
Examples:
.. code-block:: lua
> local tab = tomap(zip(range(1, 7), 'abcdef'))
> print(type(tab), #tab)
table 6
> each(print, iter(tab))
a
b
c
d
e
f
Predicates
----------
.. function:: is_prefix_of(iterator1, iterator2)
iterator1:is_prefix_of(iterator2)
The function takes two iterators and returns ``true`` if the first iterator
is a prefix of the second.
Examples:
.. code-block:: lua
> print(is_prefix_of({"a"}, {"a", "b", "c"}))
true
> print(is_prefix_of(range(6), range(5)))
false
.. function:: is_null(gen, param, state)
iterator:is_null()
:returns: true when `gen, param, state`` iterator is empty or finished.
:returns: false otherwise.
Example::
> print(is_null({"a", "b", "c", "d", "e"}))
false
> print(is_null({}))
true
> print(is_null(range(0)))
true
.. function:: all(predicate, gen, param, state)
iterator:all(predicate)
:param predicate: a predicate
Returns true if all return values of iterator satisfy the **predicate**.
Examples:
.. code-block:: lua
> print(all(function(x) return x end, {true, true, true, true}))
true
> print(all(function(x) return x end, {true, true, true, false}))
false
.. function:: every(predicate, gen, param, state)
An alias for :func:`all`.
.. function:: any(predicate, gen, param, state)
iterator:any(predicate)
:param predicate: a predicate
Returns ``true`` if at least one return values of iterator satisfy the
**predicate**. The iteration stops on the first such value. Therefore,
infinity iterators that have at least one satisfying value might work.
Examples:
.. code-block:: lua
> print(any(function(x) return x end, {false, false, false, false}))
false
> print(any(function(x) return x end, {false, false, false, true}))
true
.. function:: some(predicate, gen, param, state)
An alias for :func:`any`.
Special folds
-------------
.. function:: sum(gen, param, state)
iterator:sum()
Sum up all iteration values. An optimized alias for::
foldl(operator.add, 0, gen, param, state)
For an empty iterator ``0`` is returned.
Examples:
.. code-block:: lua
> print(sum(range(5)))
15
.. function:: product(gen, param, state)
iterator:product()
Multiply all iteration values. An optimized alias for::
foldl(operator.mul, 1, gen, param, state)
For an empty iterator ``1`` is returned.
Examples:
.. code-block:: lua
> print(product(range(1, 5)))
120
.. function:: min(gen, param, state)
iterator:min()
Return a minimum value from the iterator using :func:`operator.min` or ``<``
for numbers and other types respectivly. The iterator must be
non-null, otherwise an error is raised.
Examples:
.. code-block:: lua
> print(min(range(1, 10, 1)))
1
> print(min({"f", "d", "c", "d", "e"}))
c
> print(min({}))
error: min: iterator is empty
.. function:: minimum(gen, param, state)
An alias for :func:`min`.
.. function:: min_by(cmp, gen, param, state)
iterator:min_by(cmp)
Return a minimum value from the iterator using the **cmp** as a ``<``
operator. The iterator must be non-null, otherwise an error is raised.
Examples:
.. code-block:: lua
> function min_cmp(a, b) if -a < -b then return a else return b end end
> print(min_by(min_cmp, range(1, 10, 1)))
9
.. function:: minimum_by(cmp, gen, param, state)
An alias for :func:`min_by`.
.. function:: max(gen, param, state)
iterator:max()
Return a maximum value from the iterator using :func:`operator.max` or ``>``
for numbers and other types respectivly.
The iterator must be non-null, otherwise an error is raised.
Examples:
.. code-block:: lua
> print(max(range(1, 10, 1)))
9
> print(max({"f", "d", "c", "d", "e"}))
f
> print(max({}))
error: max: iterator is empty
.. function:: maximum(gen, param, state)
An alias for :func:`max`.
.. function:: max_by(cmp, gen, param, state)
iterator:max_by(cmp)
Return a maximum value from the iterator using the **cmp** as a `>`
operator. The iterator must be non-null, otherwise an error is raised.
Examples:
.. code-block:: lua
> function max_cmp(a, b) if -a > -b then return a else return b end end
> print(max_by(max_cmp, range(1, 10, 1)))
1
.. function:: maximum_by(cmp, gen, param, state)
An alias for :func:`max_by`.
|