-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreducing.html
459 lines (409 loc) · 43.1 KB
/
reducing.html
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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" /><meta name="generator" content="Docutils 0.17.1: http://docutils.sourceforge.net/" />
<title>Reducing — Lua Functional 0.1.3 documentation</title>
<link rel="stylesheet" type="text/css" href="_static/pygments.css" />
<link rel="stylesheet" type="text/css" href="_static/haiku.css" />
<script data-url_root="./" id="documentation_options" src="_static/documentation_options.js"></script>
<script src="_static/jquery.js"></script>
<script src="_static/underscore.js"></script>
<script src="_static/doctools.js"></script>
<link rel="author" title="About these documents" href="about.html" />
<link rel="index" title="Index" href="genindex.html" />
<link rel="search" title="Search" href="search.html" />
<link rel="next" title="Transformations" href="transformations.html" />
<link rel="prev" title="Filtering" href="filtering.html" />
</head><body>
<div class="header" role="banner"><img class="rightlogo" src="_static/logo.png" alt="Logo"/><h1 class="heading"><a href="index.html">
<span>Lua Functional 0.1.3 documentation</span></a></h1>
<h2 class="heading"><span>Reducing</span></h2>
</div>
<div class="topnav" role="navigation" aria-label="top navigation">
<p>
«  <a href="filtering.html">Filtering</a>
  ::  
<a class="uplink" href="index.html">Contents</a>
  ::  
<a href="transformations.html">Transformations</a>  »
</p>
</div>
<div class="content" role="main">
<section id="reducing">
<h1><a class="toc-backref" href="#id1">Reducing</a><a class="headerlink" href="#reducing" title="Permalink to this headline">¶</a></h1>
<p>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</p>
<div class="contents topic" id="contents">
<p class="topic-title">Contents</p>
<ul class="simple">
<li><p><a class="reference internal" href="#reducing" id="id1">Reducing</a></p>
<ul>
<li><p><a class="reference internal" href="#folds" id="id2">Folds</a></p></li>
<li><p><a class="reference internal" href="#predicates" id="id3">Predicates</a></p></li>
<li><p><a class="reference internal" href="#special-folds" id="id4">Special folds</a></p></li>
</ul>
</li>
</ul>
</div>
<div class="admonition note">
<p class="admonition-title">Note</p>
<p>An attempt to use infinity iterators with the most function from
the module causes an infinite loop.</p>
</div>
<section id="folds">
<h2><a class="toc-backref" href="#id2">Folds</a><a class="headerlink" href="#folds" title="Permalink to this headline">¶</a></h2>
<dl class="py function">
<dt class="sig sig-object py" id="fun.foldl">
<span class="sig-prename descclassname"><span class="pre">fun.</span></span><span class="sig-name descname"><span class="pre">foldl</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">accfun</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">initval</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">gen</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">param</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">state</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#fun.foldl" title="Permalink to this definition">¶</a></dt>
<dt class="sig sig-object py">
<span class="sig-name descname"><span class="pre">iterator:reduce(accfun,</span> <span class="pre">initval)</span></span></dt>
<dd><dl class="field-list simple">
<dt class="field-odd">Parameters</dt>
<dd class="field-odd"><ul class="simple">
<li><p><strong>accfun</strong> – an accumulating function</p></li>
<li><p><strong>initval</strong> – an initial value that passed to <strong>accfun</strong> on the first
iteration</p></li>
</ul>
</dd>
</dl>
<p>The function reduces the iterator from left to right using the binary
operator <strong>accfun</strong> and the initial value <strong>initval</strong>.
Equivalent to:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">local</span> <span class="n">val</span> <span class="o">=</span> <span class="n">initval</span>
<span class="k">for</span> <span class="n">_k</span><span class="p">,</span> <span class="o">...</span> <span class="ow">in</span> <span class="n">gen</span><span class="p">,</span> <span class="n">param</span><span class="p">,</span> <span class="n">state</span> <span class="n">do</span>
<span class="n">val</span> <span class="o">=</span> <span class="n">accfun</span><span class="p">(</span><span class="n">val</span><span class="p">,</span> <span class="o">...</span><span class="p">)</span>
<span class="n">end</span>
<span class="k">return</span> <span class="n">val</span>
</pre></div>
</div>
<p>Examples:</p>
<div class="highlight-lua notranslate"><div class="highlight"><pre><span></span><span class="o">></span> <span class="nb">print</span><span class="p">(</span><span class="n">foldl</span><span class="p">(</span><span class="kr">function</span><span class="p">(</span><span class="n">acc</span><span class="p">,</span> <span class="n">x</span><span class="p">)</span> <span class="kr">return</span> <span class="n">acc</span> <span class="o">+</span> <span class="n">x</span> <span class="kr">end</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="n">range</span><span class="p">(</span><span class="mi">5</span><span class="p">)))</span>
<span class="mi">15</span>
<span class="o">></span> <span class="nb">print</span><span class="p">(</span><span class="n">foldl</span><span class="p">(</span><span class="n">operator</span><span class="p">.</span><span class="n">add</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="n">range</span><span class="p">(</span><span class="mi">5</span><span class="p">)))</span>
<span class="mi">15</span>
<span class="o">></span> <span class="nb">print</span><span class="p">(</span><span class="n">foldl</span><span class="p">(</span><span class="kr">function</span><span class="p">(</span><span class="n">acc</span><span class="p">,</span> <span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">)</span> <span class="kr">return</span> <span class="n">acc</span> <span class="o">+</span> <span class="n">x</span> <span class="o">*</span> <span class="n">y</span><span class="p">;</span> <span class="kr">end</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span>
<span class="n">zip</span><span class="p">(</span><span class="n">range</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">5</span><span class="p">),</span> <span class="p">{</span><span class="mi">4</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">1</span><span class="p">})))</span>
<span class="mi">20</span>
</pre></div>
</div>
</dd></dl>
<dl class="py function">
<dt class="sig sig-object py" id="fun.reduce">
<span class="sig-prename descclassname"><span class="pre">fun.</span></span><span class="sig-name descname"><span class="pre">reduce</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">accfun</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">initval</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">gen</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">param</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">state</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#fun.reduce" title="Permalink to this definition">¶</a></dt>
<dt class="sig sig-object py">
<span class="sig-name descname"><span class="pre">iterator:reduce(accfun,</span> <span class="pre">initval)</span></span></dt>
<dd><p>An alias to <a class="reference internal" href="#fun.foldl" title="fun.foldl"><code class="xref py py-func docutils literal notranslate"><span class="pre">foldl()</span></code></a>.</p>
</dd></dl>
<dl class="py function">
<dt class="sig sig-object py" id="fun.length">
<span class="sig-prename descclassname"><span class="pre">fun.</span></span><span class="sig-name descname"><span class="pre">length</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">gen</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">param</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">state</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#fun.length" title="Permalink to this definition">¶</a></dt>
<dt class="sig sig-object py">
<span class="sig-name descname"><span class="pre">iterator:length()</span></span></dt>
<dd><p>Return a number of remaining elements in <code class="docutils literal notranslate"><span class="pre">gen,</span> <span class="pre">param,</span> <span class="pre">state</span></code> iterator.</p>
<p>Examples:</p>
<div class="highlight-lua notranslate"><div class="highlight"><pre><span></span><span class="o">></span> <span class="nb">print</span><span class="p">(</span><span class="n">length</span><span class="p">({</span><span class="s2">"a"</span><span class="p">,</span> <span class="s2">"b"</span><span class="p">,</span> <span class="s2">"c"</span><span class="p">,</span> <span class="s2">"d"</span><span class="p">,</span> <span class="s2">"e"</span><span class="p">}))</span>
<span class="mi">5</span>
<span class="o">></span> <span class="nb">print</span><span class="p">(</span><span class="n">length</span><span class="p">(</span><span class="n">drop_n</span><span class="p">(</span><span class="mi">3</span><span class="p">,</span> <span class="p">{</span><span class="s2">"a"</span><span class="p">,</span> <span class="s2">"b"</span><span class="p">,</span> <span class="s2">"c"</span><span class="p">,</span> <span class="s2">"d"</span><span class="p">,</span> <span class="s2">"e"</span><span class="p">})))</span>
<span class="mi">2</span>
<span class="o">></span> <span class="nb">print</span><span class="p">(</span><span class="n">length</span><span class="p">({}))</span>
<span class="mi">0</span>
<span class="o">></span> <span class="nb">print</span><span class="p">(</span><span class="n">length</span><span class="p">(</span><span class="n">range</span><span class="p">(</span><span class="mi">0</span><span class="p">)))</span>
<span class="mi">0</span>
</pre></div>
</div>
<div class="admonition warning">
<p class="admonition-title">Warning</p>
<p>An attempt to call this function on an infinite iterator will
result an infinite loop.</p>
</div>
<div class="admonition note">
<p class="admonition-title">Note</p>
<p>This function has <code class="docutils literal notranslate"><span class="pre">O(n)</span></code> complexity for all iterators except
basic array and string iterators, where it has <code class="docutils literal notranslate"><span class="pre">O(1)</span></code> complexity.</p>
</div>
</dd></dl>
<dl class="py function">
<dt class="sig sig-object py" id="fun.totable">
<span class="sig-prename descclassname"><span class="pre">fun.</span></span><span class="sig-name descname"><span class="pre">totable</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">gen</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">param</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">state</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#fun.totable" title="Permalink to this definition">¶</a></dt>
<dd><dl class="field-list simple">
<dt class="field-odd">Returns</dt>
<dd class="field-odd"><p>a new table (array) from iterated values.</p>
</dd>
</dl>
<p>The function reduces the iterator from left to right using <code class="docutils literal notranslate"><span class="pre">table.insert</span></code>.</p>
<p>Examples:</p>
<div class="highlight-lua notranslate"><div class="highlight"><pre><span></span><span class="o">></span> <span class="kd">local</span> <span class="n">tab</span> <span class="o">=</span> <span class="n">totable</span><span class="p">(</span><span class="s2">"abcdef"</span><span class="p">)</span>
<span class="o">></span> <span class="nb">print</span><span class="p">(</span><span class="nb">type</span><span class="p">(</span><span class="n">tab</span><span class="p">),</span> <span class="o">#</span><span class="n">tab</span><span class="p">)</span>
<span class="n">table</span> <span class="mi">6</span>
<span class="o">></span> <span class="n">each</span><span class="p">(</span><span class="nb">print</span><span class="p">,</span> <span class="n">tab</span><span class="p">)</span>
<span class="n">a</span>
<span class="n">b</span>
<span class="n">c</span>
<span class="n">d</span>
<span class="n">e</span>
<span class="n">f</span>
</pre></div>
</div>
</dd></dl>
<dl class="py function">
<dt class="sig sig-object py" id="fun.tomap">
<span class="sig-prename descclassname"><span class="pre">fun.</span></span><span class="sig-name descname"><span class="pre">tomap</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">gen</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">param</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">state</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#fun.tomap" title="Permalink to this definition">¶</a></dt>
<dd><dl class="field-list simple">
<dt class="field-odd">Returns</dt>
<dd class="field-odd"><p>a new table (map) from iterated values.</p>
</dd>
</dl>
<p>The function reduces the iterator from left to right using
<code class="docutils literal notranslate"><span class="pre">tab[val1]</span> <span class="pre">=</span> <span class="pre">val2</span></code> expression.</p>
<p>Examples:</p>
<div class="highlight-lua notranslate"><div class="highlight"><pre><span></span><span class="o">></span> <span class="kd">local</span> <span class="n">tab</span> <span class="o">=</span> <span class="n">tomap</span><span class="p">(</span><span class="n">zip</span><span class="p">(</span><span class="n">range</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">7</span><span class="p">),</span> <span class="s1">'abcdef'</span><span class="p">))</span>
<span class="o">></span> <span class="nb">print</span><span class="p">(</span><span class="nb">type</span><span class="p">(</span><span class="n">tab</span><span class="p">),</span> <span class="o">#</span><span class="n">tab</span><span class="p">)</span>
<span class="n">table</span> <span class="mi">6</span>
<span class="o">></span> <span class="n">each</span><span class="p">(</span><span class="nb">print</span><span class="p">,</span> <span class="n">iter</span><span class="p">(</span><span class="n">tab</span><span class="p">))</span>
<span class="n">a</span>
<span class="n">b</span>
<span class="n">c</span>
<span class="n">d</span>
<span class="n">e</span>
<span class="n">f</span>
</pre></div>
</div>
</dd></dl>
</section>
<section id="predicates">
<h2><a class="toc-backref" href="#id3">Predicates</a><a class="headerlink" href="#predicates" title="Permalink to this headline">¶</a></h2>
<dl class="py function">
<dt class="sig sig-object py" id="fun.is_prefix_of">
<span class="sig-prename descclassname"><span class="pre">fun.</span></span><span class="sig-name descname"><span class="pre">is_prefix_of</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">iterator1</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">iterator2</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#fun.is_prefix_of" title="Permalink to this definition">¶</a></dt>
<dt class="sig sig-object py">
<span class="sig-name descname"><span class="pre">iterator1:is_prefix_of(iterator2)</span></span></dt>
<dd><p>The function takes two iterators and returns <code class="docutils literal notranslate"><span class="pre">true</span></code> if the first iterator
is a prefix of the second.</p>
<p>Examples:</p>
<div class="highlight-lua notranslate"><div class="highlight"><pre><span></span><span class="o">></span> <span class="nb">print</span><span class="p">(</span><span class="n">is_prefix_of</span><span class="p">({</span><span class="s2">"a"</span><span class="p">},</span> <span class="p">{</span><span class="s2">"a"</span><span class="p">,</span> <span class="s2">"b"</span><span class="p">,</span> <span class="s2">"c"</span><span class="p">}))</span>
<span class="kc">true</span>
<span class="o">></span> <span class="nb">print</span><span class="p">(</span><span class="n">is_prefix_of</span><span class="p">(</span><span class="n">range</span><span class="p">(</span><span class="mi">6</span><span class="p">),</span> <span class="n">range</span><span class="p">(</span><span class="mi">5</span><span class="p">)))</span>
<span class="kc">false</span>
</pre></div>
</div>
</dd></dl>
<dl class="py function">
<dt class="sig sig-object py" id="fun.is_null">
<span class="sig-prename descclassname"><span class="pre">fun.</span></span><span class="sig-name descname"><span class="pre">is_null</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">gen</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">param</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">state</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#fun.is_null" title="Permalink to this definition">¶</a></dt>
<dt class="sig sig-object py">
<span class="sig-name descname"><span class="pre">iterator:is_null()</span></span></dt>
<dd><dl class="field-list simple">
<dt class="field-odd">Returns</dt>
<dd class="field-odd"><p>true when <cite>gen, param, state`</cite> iterator is empty or finished.</p>
</dd>
<dt class="field-even">Returns</dt>
<dd class="field-even"><p>false otherwise.</p>
</dd>
</dl>
<p>Example:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="o">></span> <span class="nb">print</span><span class="p">(</span><span class="n">is_null</span><span class="p">({</span><span class="s2">"a"</span><span class="p">,</span> <span class="s2">"b"</span><span class="p">,</span> <span class="s2">"c"</span><span class="p">,</span> <span class="s2">"d"</span><span class="p">,</span> <span class="s2">"e"</span><span class="p">}))</span>
<span class="n">false</span>
<span class="o">></span> <span class="nb">print</span><span class="p">(</span><span class="n">is_null</span><span class="p">({}))</span>
<span class="n">true</span>
<span class="o">></span> <span class="nb">print</span><span class="p">(</span><span class="n">is_null</span><span class="p">(</span><span class="nb">range</span><span class="p">(</span><span class="mi">0</span><span class="p">)))</span>
<span class="n">true</span>
</pre></div>
</div>
</dd></dl>
<dl class="py function">
<dt class="sig sig-object py" id="fun.all">
<span class="sig-prename descclassname"><span class="pre">fun.</span></span><span class="sig-name descname"><span class="pre">all</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">predicate</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">gen</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">param</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">state</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#fun.all" title="Permalink to this definition">¶</a></dt>
<dt class="sig sig-object py">
<span class="sig-name descname"><span class="pre">iterator:all(predicate)</span></span></dt>
<dd><dl class="field-list simple">
<dt class="field-odd">Parameters</dt>
<dd class="field-odd"><p><strong>predicate</strong> – a predicate</p>
</dd>
</dl>
<p>Returns true if all return values of iterator satisfy the <strong>predicate</strong>.</p>
<p>Examples:</p>
<div class="highlight-lua notranslate"><div class="highlight"><pre><span></span><span class="o">></span> <span class="nb">print</span><span class="p">(</span><span class="n">all</span><span class="p">(</span><span class="kr">function</span><span class="p">(</span><span class="n">x</span><span class="p">)</span> <span class="kr">return</span> <span class="n">x</span> <span class="kr">end</span><span class="p">,</span> <span class="p">{</span><span class="kc">true</span><span class="p">,</span> <span class="kc">true</span><span class="p">,</span> <span class="kc">true</span><span class="p">,</span> <span class="kc">true</span><span class="p">}))</span>
<span class="kc">true</span>
<span class="o">></span> <span class="nb">print</span><span class="p">(</span><span class="n">all</span><span class="p">(</span><span class="kr">function</span><span class="p">(</span><span class="n">x</span><span class="p">)</span> <span class="kr">return</span> <span class="n">x</span> <span class="kr">end</span><span class="p">,</span> <span class="p">{</span><span class="kc">true</span><span class="p">,</span> <span class="kc">true</span><span class="p">,</span> <span class="kc">true</span><span class="p">,</span> <span class="kc">false</span><span class="p">}))</span>
<span class="kc">false</span>
</pre></div>
</div>
</dd></dl>
<dl class="py function">
<dt class="sig sig-object py" id="fun.every">
<span class="sig-prename descclassname"><span class="pre">fun.</span></span><span class="sig-name descname"><span class="pre">every</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">predicate</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">gen</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">param</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">state</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#fun.every" title="Permalink to this definition">¶</a></dt>
<dd><p>An alias for <a class="reference internal" href="#fun.all" title="fun.all"><code class="xref py py-func docutils literal notranslate"><span class="pre">all()</span></code></a>.</p>
</dd></dl>
<dl class="py function">
<dt class="sig sig-object py" id="fun.any">
<span class="sig-prename descclassname"><span class="pre">fun.</span></span><span class="sig-name descname"><span class="pre">any</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">predicate</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">gen</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">param</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">state</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#fun.any" title="Permalink to this definition">¶</a></dt>
<dt class="sig sig-object py">
<span class="sig-name descname"><span class="pre">iterator:any(predicate)</span></span></dt>
<dd><dl class="field-list simple">
<dt class="field-odd">Parameters</dt>
<dd class="field-odd"><p><strong>predicate</strong> – a predicate</p>
</dd>
</dl>
<p>Returns <code class="docutils literal notranslate"><span class="pre">true</span></code> if at least one return values of iterator satisfy the
<strong>predicate</strong>. The iteration stops on the first such value. Therefore,
infinity iterators that have at least one satisfying value might work.</p>
<p>Examples:</p>
<div class="highlight-lua notranslate"><div class="highlight"><pre><span></span><span class="o">></span> <span class="nb">print</span><span class="p">(</span><span class="n">any</span><span class="p">(</span><span class="kr">function</span><span class="p">(</span><span class="n">x</span><span class="p">)</span> <span class="kr">return</span> <span class="n">x</span> <span class="kr">end</span><span class="p">,</span> <span class="p">{</span><span class="kc">false</span><span class="p">,</span> <span class="kc">false</span><span class="p">,</span> <span class="kc">false</span><span class="p">,</span> <span class="kc">false</span><span class="p">}))</span>
<span class="kc">false</span>
<span class="o">></span> <span class="nb">print</span><span class="p">(</span><span class="n">any</span><span class="p">(</span><span class="kr">function</span><span class="p">(</span><span class="n">x</span><span class="p">)</span> <span class="kr">return</span> <span class="n">x</span> <span class="kr">end</span><span class="p">,</span> <span class="p">{</span><span class="kc">false</span><span class="p">,</span> <span class="kc">false</span><span class="p">,</span> <span class="kc">false</span><span class="p">,</span> <span class="kc">true</span><span class="p">}))</span>
<span class="kc">true</span>
</pre></div>
</div>
</dd></dl>
<dl class="py function">
<dt class="sig sig-object py" id="fun.some">
<span class="sig-prename descclassname"><span class="pre">fun.</span></span><span class="sig-name descname"><span class="pre">some</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">predicate</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">gen</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">param</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">state</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#fun.some" title="Permalink to this definition">¶</a></dt>
<dd><p>An alias for <a class="reference internal" href="#fun.any" title="fun.any"><code class="xref py py-func docutils literal notranslate"><span class="pre">any()</span></code></a>.</p>
</dd></dl>
</section>
<section id="special-folds">
<h2><a class="toc-backref" href="#id4">Special folds</a><a class="headerlink" href="#special-folds" title="Permalink to this headline">¶</a></h2>
<dl class="py function">
<dt class="sig sig-object py" id="fun.sum">
<span class="sig-prename descclassname"><span class="pre">fun.</span></span><span class="sig-name descname"><span class="pre">sum</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">gen</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">param</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">state</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#fun.sum" title="Permalink to this definition">¶</a></dt>
<dt class="sig sig-object py">
<span class="sig-name descname"><span class="pre">iterator:sum()</span></span></dt>
<dd><p>Sum up all iteration values. An optimized alias for:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">foldl</span><span class="p">(</span><span class="n">operator</span><span class="o">.</span><span class="n">add</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="n">gen</span><span class="p">,</span> <span class="n">param</span><span class="p">,</span> <span class="n">state</span><span class="p">)</span>
</pre></div>
</div>
<p>For an empty iterator <code class="docutils literal notranslate"><span class="pre">0</span></code> is returned.</p>
<p>Examples:</p>
<div class="highlight-lua notranslate"><div class="highlight"><pre><span></span><span class="o">></span> <span class="nb">print</span><span class="p">(</span><span class="n">sum</span><span class="p">(</span><span class="n">range</span><span class="p">(</span><span class="mi">5</span><span class="p">)))</span>
<span class="mi">15</span>
</pre></div>
</div>
</dd></dl>
<dl class="py function">
<dt class="sig sig-object py" id="fun.product">
<span class="sig-prename descclassname"><span class="pre">fun.</span></span><span class="sig-name descname"><span class="pre">product</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">gen</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">param</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">state</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#fun.product" title="Permalink to this definition">¶</a></dt>
<dt class="sig sig-object py">
<span class="sig-name descname"><span class="pre">iterator:product()</span></span></dt>
<dd><p>Multiply all iteration values. An optimized alias for:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">foldl</span><span class="p">(</span><span class="n">operator</span><span class="o">.</span><span class="n">mul</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="n">gen</span><span class="p">,</span> <span class="n">param</span><span class="p">,</span> <span class="n">state</span><span class="p">)</span>
</pre></div>
</div>
<p>For an empty iterator <code class="docutils literal notranslate"><span class="pre">1</span></code> is returned.</p>
<p>Examples:</p>
<div class="highlight-lua notranslate"><div class="highlight"><pre><span></span><span class="o">></span> <span class="nb">print</span><span class="p">(</span><span class="n">product</span><span class="p">(</span><span class="n">range</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">5</span><span class="p">)))</span>
<span class="mi">120</span>
</pre></div>
</div>
</dd></dl>
<dl class="py function">
<dt class="sig sig-object py" id="fun.min">
<span class="sig-prename descclassname"><span class="pre">fun.</span></span><span class="sig-name descname"><span class="pre">min</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">gen</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">param</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">state</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#fun.min" title="Permalink to this definition">¶</a></dt>
<dt class="sig sig-object py">
<span class="sig-name descname"><span class="pre">iterator:min()</span></span></dt>
<dd><p>Return a minimum value from the iterator using <code class="xref py py-func docutils literal notranslate"><span class="pre">operator.min()</span></code> or <code class="docutils literal notranslate"><span class="pre"><</span></code>
for numbers and other types respectively. The iterator must be
non-null, otherwise an error is raised.</p>
<p>Examples:</p>
<div class="highlight-lua notranslate"><div class="highlight"><pre><span></span><span class="o">></span> <span class="nb">print</span><span class="p">(</span><span class="n">min</span><span class="p">(</span><span class="n">range</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">10</span><span class="p">,</span> <span class="mi">1</span><span class="p">)))</span>
<span class="mi">1</span>
<span class="o">></span> <span class="nb">print</span><span class="p">(</span><span class="n">min</span><span class="p">({</span><span class="s2">"f"</span><span class="p">,</span> <span class="s2">"d"</span><span class="p">,</span> <span class="s2">"c"</span><span class="p">,</span> <span class="s2">"d"</span><span class="p">,</span> <span class="s2">"e"</span><span class="p">}))</span>
<span class="n">c</span>
<span class="o">></span> <span class="nb">print</span><span class="p">(</span><span class="n">min</span><span class="p">({}))</span>
<span class="nb">error</span><span class="p">:</span> <span class="n">min</span><span class="p">:</span> <span class="n">iterator</span> <span class="n">is</span> <span class="n">empty</span>
</pre></div>
</div>
</dd></dl>
<dl class="py function">
<dt class="sig sig-object py" id="fun.minimum">
<span class="sig-prename descclassname"><span class="pre">fun.</span></span><span class="sig-name descname"><span class="pre">minimum</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">gen</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">param</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">state</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#fun.minimum" title="Permalink to this definition">¶</a></dt>
<dd><p>An alias for <a class="reference internal" href="#fun.min" title="fun.min"><code class="xref py py-func docutils literal notranslate"><span class="pre">min()</span></code></a>.</p>
</dd></dl>
<dl class="py function">
<dt class="sig sig-object py" id="fun.min_by">
<span class="sig-prename descclassname"><span class="pre">fun.</span></span><span class="sig-name descname"><span class="pre">min_by</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">cmp</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">gen</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">param</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">state</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#fun.min_by" title="Permalink to this definition">¶</a></dt>
<dt class="sig sig-object py">
<span class="sig-name descname"><span class="pre">iterator:min_by(cmp)</span></span></dt>
<dd><p>Return a minimum value from the iterator using the <strong>cmp</strong> as a <code class="docutils literal notranslate"><span class="pre"><</span></code>
operator. The iterator must be non-null, otherwise an error is raised.</p>
<p>Examples:</p>
<div class="highlight-lua notranslate"><div class="highlight"><pre><span></span><span class="o">></span> <span class="kr">function</span> <span class="nf">min_cmp</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">)</span> <span class="kr">if</span> <span class="o">-</span><span class="n">a</span> <span class="o"><</span> <span class="o">-</span><span class="n">b</span> <span class="kr">then</span> <span class="kr">return</span> <span class="n">a</span> <span class="kr">else</span> <span class="kr">return</span> <span class="n">b</span> <span class="kr">end</span> <span class="kr">end</span>
<span class="o">></span> <span class="nb">print</span><span class="p">(</span><span class="n">min_by</span><span class="p">(</span><span class="n">min_cmp</span><span class="p">,</span> <span class="n">range</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">10</span><span class="p">,</span> <span class="mi">1</span><span class="p">)))</span>
<span class="mi">9</span>
</pre></div>
</div>
</dd></dl>
<dl class="py function">
<dt class="sig sig-object py" id="fun.minimum_by">
<span class="sig-prename descclassname"><span class="pre">fun.</span></span><span class="sig-name descname"><span class="pre">minimum_by</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">cmp</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">gen</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">param</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">state</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#fun.minimum_by" title="Permalink to this definition">¶</a></dt>
<dd><p>An alias for <a class="reference internal" href="#fun.min_by" title="fun.min_by"><code class="xref py py-func docutils literal notranslate"><span class="pre">min_by()</span></code></a>.</p>
</dd></dl>
<dl class="py function">
<dt class="sig sig-object py" id="fun.max">
<span class="sig-prename descclassname"><span class="pre">fun.</span></span><span class="sig-name descname"><span class="pre">max</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">gen</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">param</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">state</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#fun.max" title="Permalink to this definition">¶</a></dt>
<dt class="sig sig-object py">
<span class="sig-name descname"><span class="pre">iterator:max()</span></span></dt>
<dd><p>Return a maximum value from the iterator using <code class="xref py py-func docutils literal notranslate"><span class="pre">operator.max()</span></code> or <code class="docutils literal notranslate"><span class="pre">></span></code>
for numbers and other types respectively.</p>
<p>The iterator must be non-null, otherwise an error is raised.</p>
<p>Examples:</p>
<div class="highlight-lua notranslate"><div class="highlight"><pre><span></span><span class="o">></span> <span class="nb">print</span><span class="p">(</span><span class="n">max</span><span class="p">(</span><span class="n">range</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">10</span><span class="p">,</span> <span class="mi">1</span><span class="p">)))</span>
<span class="mi">9</span>
<span class="o">></span> <span class="nb">print</span><span class="p">(</span><span class="n">max</span><span class="p">({</span><span class="s2">"f"</span><span class="p">,</span> <span class="s2">"d"</span><span class="p">,</span> <span class="s2">"c"</span><span class="p">,</span> <span class="s2">"d"</span><span class="p">,</span> <span class="s2">"e"</span><span class="p">}))</span>
<span class="n">f</span>
<span class="o">></span> <span class="nb">print</span><span class="p">(</span><span class="n">max</span><span class="p">({}))</span>
<span class="nb">error</span><span class="p">:</span> <span class="n">max</span><span class="p">:</span> <span class="n">iterator</span> <span class="n">is</span> <span class="n">empty</span>
</pre></div>
</div>
</dd></dl>
<dl class="py function">
<dt class="sig sig-object py" id="fun.maximum">
<span class="sig-prename descclassname"><span class="pre">fun.</span></span><span class="sig-name descname"><span class="pre">maximum</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">gen</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">param</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">state</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#fun.maximum" title="Permalink to this definition">¶</a></dt>
<dd><p>An alias for <a class="reference internal" href="#fun.max" title="fun.max"><code class="xref py py-func docutils literal notranslate"><span class="pre">max()</span></code></a>.</p>
</dd></dl>
<dl class="py function">
<dt class="sig sig-object py" id="fun.max_by">
<span class="sig-prename descclassname"><span class="pre">fun.</span></span><span class="sig-name descname"><span class="pre">max_by</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">cmp</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">gen</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">param</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">state</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#fun.max_by" title="Permalink to this definition">¶</a></dt>
<dt class="sig sig-object py">
<span class="sig-name descname"><span class="pre">iterator:max_by(cmp)</span></span></dt>
<dd><p>Return a maximum value from the iterator using the <strong>cmp</strong> as a <cite>></cite>
operator. The iterator must be non-null, otherwise an error is raised.</p>
<p>Examples:</p>
<div class="highlight-lua notranslate"><div class="highlight"><pre><span></span><span class="o">></span> <span class="kr">function</span> <span class="nf">max_cmp</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">)</span> <span class="kr">if</span> <span class="o">-</span><span class="n">a</span> <span class="o">></span> <span class="o">-</span><span class="n">b</span> <span class="kr">then</span> <span class="kr">return</span> <span class="n">a</span> <span class="kr">else</span> <span class="kr">return</span> <span class="n">b</span> <span class="kr">end</span> <span class="kr">end</span>
<span class="o">></span> <span class="nb">print</span><span class="p">(</span><span class="n">max_by</span><span class="p">(</span><span class="n">max_cmp</span><span class="p">,</span> <span class="n">range</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">10</span><span class="p">,</span> <span class="mi">1</span><span class="p">)))</span>
<span class="mi">1</span>
</pre></div>
</div>
</dd></dl>
<dl class="py function">
<dt class="sig sig-object py" id="fun.maximum_by">
<span class="sig-prename descclassname"><span class="pre">fun.</span></span><span class="sig-name descname"><span class="pre">maximum_by</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">cmp</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">gen</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">param</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">state</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#fun.maximum_by" title="Permalink to this definition">¶</a></dt>
<dd><p>An alias for <a class="reference internal" href="#fun.max_by" title="fun.max_by"><code class="xref py py-func docutils literal notranslate"><span class="pre">max_by()</span></code></a>.</p>
</dd></dl>
</section>
</section>
</div>
<div class="bottomnav" role="navigation" aria-label="bottom navigation">
<p>
«  <a href="filtering.html">Filtering</a>
  ::  
<a class="uplink" href="index.html">Contents</a>
  ::  
<a href="transformations.html">Transformations</a>  »
</p>
</div>
<div class="footer" role="contentinfo">
© Copyright 2013-2021, Roman Tsisyk.
Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 4.4.0.
</div>
<script type="text/javascript">
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','https://www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-45899190-2', 'auto');
ga('send', 'pageview');
</script>
</body>
</html>