-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathstd.functional.html
More file actions
1015 lines (820 loc) · 30.5 KB
/
std.functional.html
File metadata and controls
1015 lines (820 loc) · 30.5 KB
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
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<head>
<title>stdlib 41.2.2 Reference</title>
<link rel="stylesheet" href="../ldoc.css" type="text/css" />
</head>
<body>
<div id="container">
<div id="product">
<div id="product_logo"></div>
<div id="product_name"><big><b></b></big></div>
<div id="product_description"></div>
</div> <!-- id="product" -->
<div id="main">
<!-- Menu -->
<div id="navigation">
<br/>
<h1>stdlib 41.2.2</h1>
<ul>
<li><a href="../index.html">Index</a></li>
</ul>
<h2>Contents</h2>
<ul>
<li><a href="#Functions">Functions</a></li>
<li><a href="#Types">Types </a></li>
</ul>
<h2>Modules</h2>
<ul class="nowrap">
<li><a href="../modules/std.html">std</a></li>
<li><a href="../modules/std.debug.html">std.debug</a></li>
<li><strong>std.functional</strong></li>
<li><a href="../modules/std.io.html">std.io</a></li>
<li><a href="../modules/std.math.html">std.math</a></li>
<li><a href="../modules/std.operator.html">std.operator</a></li>
<li><a href="../modules/std.package.html">std.package</a></li>
<li><a href="../modules/std.strict.html">std.strict</a></li>
<li><a href="../modules/std.string.html">std.string</a></li>
<li><a href="../modules/std.table.html">std.table</a></li>
</ul>
<h2>Classes</h2>
<ul class="nowrap">
<li><a href="../classes/std.tree.html">std.tree</a></li>
<li><a href="../classes/std.container.html">std.container</a></li>
<li><a href="../classes/std.object.html">std.object</a></li>
<li><a href="../classes/std.list.html">std.list</a></li>
<li><a href="../classes/std.optparse.html">std.optparse</a></li>
<li><a href="../classes/std.set.html">std.set</a></li>
<li><a href="../classes/std.strbuf.html">std.strbuf</a></li>
</ul>
</div>
<div id="content">
<h1>Module <code>std.functional</code></h1>
<p>Functional programming.</p>
<p> A selection of higher-order functions to enable a functional style of
programming in Lua.</p>
<h2><a href="#Functions">Functions</a></h2>
<table class="function_list">
<tr>
<td class="name" nowrap><a href="#bind">bind (fn, argt)</a></td>
<td class="summary">Partially apply a function.</td>
</tr>
<tr>
<td class="name" nowrap><a href="#callable">callable (x)</a></td>
<td class="summary">Identify callable types.</td>
</tr>
<tr>
<td class="name" nowrap><a href="#case">case (with, branches)</a></td>
<td class="summary">A rudimentary case statement.</td>
</tr>
<tr>
<td class="name" nowrap><a href="#collect">collect ([ifn=std.npairs], ...)</a></td>
<td class="summary">Collect the results of an iterator.</td>
</tr>
<tr>
<td class="name" nowrap><a href="#compose">compose (...)</a></td>
<td class="summary">Compose functions.</td>
</tr>
<tr>
<td class="name" nowrap><a href="#cond">cond (expr, branch, ...)</a></td>
<td class="summary">A rudimentary condition-case statement.</td>
</tr>
<tr>
<td class="name" nowrap><a href="#curry">curry (fn, n)</a></td>
<td class="summary">Curry a function.</td>
</tr>
<tr>
<td class="name" nowrap><a href="#filter">filter (pfn[, ifn=std.pairs], ...)</a></td>
<td class="summary">Filter an iterator with a predicate.</td>
</tr>
<tr>
<td class="name" nowrap><a href="#foldl">foldl (fn[, d=t[1], t)</a></td>
<td class="summary">Fold a binary function left associatively.</td>
</tr>
<tr>
<td class="name" nowrap><a href="#foldr">foldr (fn[, d=t[1], t)</a></td>
<td class="summary">Fold a binary function right associatively.</td>
</tr>
<tr>
<td class="name" nowrap><a href="#id">id (...)</a></td>
<td class="summary">Identity function.</td>
</tr>
<tr>
<td class="name" nowrap><a href="#lambda">lambda (s)</a></td>
<td class="summary">Compile a lambda string into a Lua function.</td>
</tr>
<tr>
<td class="name" nowrap><a href="#map">map (fn[, ifn=std.pairs], ...)</a></td>
<td class="summary">Map a function over an iterator.</td>
</tr>
<tr>
<td class="name" nowrap><a href="#map_with">map_with (fn, tt)</a></td>
<td class="summary">Map a function over a table of argument lists.</td>
</tr>
<tr>
<td class="name" nowrap><a href="#memoize">memoize (fn[, normfn=std.tostring])</a></td>
<td class="summary">Memoize a function, by wrapping it in a functable.</td>
</tr>
<tr>
<td class="name" nowrap><a href="#nop">nop ()</a></td>
<td class="summary">No operation.</td>
</tr>
<tr>
<td class="name" nowrap><a href="#reduce">reduce (fn, d[, ifn=std.pairs], ...)</a></td>
<td class="summary">Fold a binary function into an iterator.</td>
</tr>
<tr>
<td class="name" nowrap><a href="#zip">zip (tt)</a></td>
<td class="summary">Zip a table of tables.</td>
</tr>
<tr>
<td class="name" nowrap><a href="#zip_with">zip_with (fn, tt)</a></td>
<td class="summary">Zip a list of tables together with a function.</td>
</tr>
</table>
<h2><a href="#Types">Types </a></h2>
<table class="function_list">
<tr>
<td class="name" nowrap><a href="#normalize">normalize (...)</a></td>
<td class="summary">Signature of a <a href="../modules/std.functional.html#memoize">memoize</a> argument normalization callback function.</td>
</tr>
<tr>
<td class="name" nowrap><a href="#predicate">predicate (...)</a></td>
<td class="summary">Signature of a <a href="../modules/std.functional.html#filter">filter</a> predicate callback function.</td>
</tr>
</table>
<br/>
<br/>
<h2 class="section-header "><a name="Functions"></a>Functions</h2>
Methods
<dl class="function">
<dt>
<a name = "bind"></a>
<strong>bind (fn, argt)</strong>
</dt>
<dd>
Partially apply a function.
<h3>Parameters:</h3>
<ul>
<li><span class="parameter">fn</span>
<span class="types"><span class="type">func</span></span>
function to apply partially
</li>
<li><span class="parameter">argt</span>
<span class="types"><a class="type" href="https://www.lua.org/manual/5.3/manual.html#6.6">table</a></span>
table of <em>fn</em> arguments to bind
</li>
</ul>
<h3>Returns:</h3>
<ol>
function with <em>argt</em> arguments already bound
</ol>
<h3>Usage:</h3>
<ul>
<pre class="example">cube = bind (std.operator.pow, {[<span class="number">2</span>] = <span class="number">3</span>})</pre>
</ul>
</dd>
<dt>
<a name = "callable"></a>
<strong>callable (x)</strong>
</dt>
<dd>
Identify callable types.
<h3>Parameters:</h3>
<ul>
<li><span class="parameter">x</span>
an object or primitive
</li>
</ul>
<h3>Returns:</h3>
<ol>
<code>true</code> if <em>x</em> can be called, otherwise <code>false</code>
</ol>
<h3>Usage:</h3>
<ul>
<pre class="example"><span class="keyword">if</span> callable (functable) <span class="keyword">then</span> functable (args) <span class="keyword">end</span></pre>
</ul>
</dd>
<dt>
<a name = "case"></a>
<strong>case (with, branches)</strong>
</dt>
<dd>
A rudimentary case statement.
Match <em>with</em> against keys in <em>branches</em> table.
<h3>Parameters:</h3>
<ul>
<li><span class="parameter">with</span>
expression to match
</li>
<li><span class="parameter">branches</span>
<span class="types"><a class="type" href="https://www.lua.org/manual/5.3/manual.html#6.6">table</a></span>
map possible matches to functions
</li>
</ul>
<h3>Returns:</h3>
<ol>
the value associated with a matching key, or the first non-key
value if no key matches. Function or functable valued matches are
called using <em>with</em> as the sole argument, and the result of that call
returned; otherwise the matching value associated with the matching
key is returned directly; or else <code>nil</code> if there is no match and no
default.
</ol>
<h3>See also:</h3>
<ul>
<a href="../modules/std.functional.html#cond">cond</a>
</ul>
<h3>Usage:</h3>
<ul>
<pre class="example"><span class="keyword">return</span> case (<span class="global">type</span> (object), {
<span class="global">table</span> = <span class="string">"table"</span>,
<span class="global">string</span> = <span class="keyword">function</span> () <span class="keyword">return</span> <span class="string">"string"</span> <span class="keyword">end</span>,
<span class="keyword">function</span> (s) <span class="global">error</span> (<span class="string">"unhandled type: "</span> .. s) <span class="keyword">end</span>,
})</pre>
</ul>
</dd>
<dt>
<a name = "collect"></a>
<strong>collect ([ifn=std.npairs], ...)</strong>
</dt>
<dd>
Collect the results of an iterator.
<h3>Parameters:</h3>
<ul>
<li><span class="parameter">ifn</span>
<span class="types"><span class="type">func</span></span>
iterator function
(<em>default</em> std.npairs)
</li>
<li><span class="parameter">...</span>
<em>ifn</em> arguments
</li>
</ul>
<h3>Returns:</h3>
<ol>
<span class="types"><a class="type" href="https://www.lua.org/manual/5.3/manual.html#6.6">table</a></span>
of results from running <em>ifn</em> on <em>args</em>
</ol>
<h3>See also:</h3>
<ul>
<li><a href="../modules/std.functional.html#filter">filter</a></li>
<li><a href="../modules/std.functional.html#map">map</a></li>
</ul>
<h3>Usage:</h3>
<ul>
<pre class="example"><span class="comment">--> {"a", "b", "c"}
</span>collect {<span class="string">"a"</span>, <span class="string">"b"</span>, <span class="string">"c"</span>, x=<span class="number">1</span>, y=<span class="number">2</span>, z=<span class="number">5</span>}</pre>
</ul>
</dd>
<dt>
<a name = "compose"></a>
<strong>compose (...)</strong>
</dt>
<dd>
Compose functions.
<h3>Parameters:</h3>
<ul>
<li><span class="parameter">...</span>
<span class="types"><span class="type">func</span></span>
functions to compose
</li>
</ul>
<h3>Returns:</h3>
<ol>
<span class="types"><span class="type">function</span></span>
composition of fnN .. fn1: note that this is the
reverse of what you might expect, but means that code like:</p>
<pre><code> functional.compose (function (x) return f (x) end,
function (x) return g (x) end))
</code></pre>
<p> can be read from top to bottom.
</ol>
<h3>Usage:</h3>
<ul>
<pre class="example">vpairs = compose (<span class="global">table</span>.invert, <span class="global">ipairs</span>)
<span class="keyword">for</span> v, i <span class="keyword">in</span> vpairs {<span class="string">"a"</span>, <span class="string">"b"</span>, <span class="string">"c"</span>} <span class="keyword">do</span> process (v, i) <span class="keyword">end</span></pre>
</ul>
</dd>
<dt>
<a name = "cond"></a>
<strong>cond (expr, branch, ...)</strong>
</dt>
<dd>
A rudimentary condition-case statement.
If <em>expr</em> is "truthy" return <em>branch</em> if given, otherwise <em>expr</em>
itself. If the return value is a function or functable, then call it
with <em>expr</em> as the sole argument and return the result; otherwise
return it explicitly. If <em>expr</em> is "falsey", then recurse with the
first two arguments stripped.
<h3>Parameters:</h3>
<ul>
<li><span class="parameter">expr</span>
a Lua expression
</li>
<li><span class="parameter">branch</span>
a function, functable or value to use if <em>expr</em> is
"truthy"
</li>
<li><span class="parameter">...</span>
additional arguments to retry if <em>expr</em> is "falsey"
</li>
</ul>
<h3>See also:</h3>
<ul>
<a href="../modules/std.functional.html#case">case</a>
</ul>
<h3>Usage:</h3>
<ul>
<pre class="example"><span class="comment">-- recursively calculate the nth triangular number
</span><span class="keyword">function</span> triangle (n)
<span class="keyword">return</span> cond (
n <= <span class="number">0</span>, <span class="number">0</span>,
n == <span class="number">1</span>, <span class="number">1</span>,
<span class="keyword">function</span> () <span class="keyword">return</span> n + triangle (n - <span class="number">1</span>) <span class="keyword">end</span>)
<span class="keyword">end</span></pre>
</ul>
</dd>
<dt>
<a name = "curry"></a>
<strong>curry (fn, n)</strong>
</dt>
<dd>
Curry a function.
<h3>Parameters:</h3>
<ul>
<li><span class="parameter">fn</span>
<span class="types"><span class="type">func</span></span>
function to curry
</li>
<li><span class="parameter">n</span>
<span class="types"><span class="type">int</span></span>
number of arguments
</li>
</ul>
<h3>Returns:</h3>
<ol>
<span class="types"><span class="type">function</span></span>
curried version of <em>fn</em>
</ol>
<h3>Usage:</h3>
<ul>
<pre class="example">add = curry (<span class="keyword">function</span> (x, y) <span class="keyword">return</span> x + y <span class="keyword">end</span>, <span class="number">2</span>)
incr, decr = add (<span class="number">1</span>), add (-<span class="number">1</span>)</pre>
</ul>
</dd>
<dt>
<a name = "filter"></a>
<strong>filter (pfn[, ifn=std.pairs], ...)</strong>
</dt>
<dd>
Filter an iterator with a predicate.
<h3>Parameters:</h3>
<ul>
<li><span class="parameter">pfn</span>
<span class="types"><a class="type" href="../modules/std.functional.html#predicate">predicate</a></span>
predicate function
</li>
<li><span class="parameter">ifn</span>
<span class="types"><span class="type">func</span></span>
iterator function
(<em>default</em> std.pairs)
</li>
<li><span class="parameter">...</span>
iterator arguments
</li>
</ul>
<h3>Returns:</h3>
<ol>
<span class="types"><a class="type" href="https://www.lua.org/manual/5.3/manual.html#6.6">table</a></span>
elements e for which <code>pfn (e)</code> is not "falsey".
</ol>
<h3>See also:</h3>
<ul>
<li><a href="../modules/std.functional.html#collect">collect</a></li>
<li><a href="../modules/std.functional.html#map">map</a></li>
</ul>
<h3>Usage:</h3>
<ul>
<pre class="example"><span class="comment">--> {2, 4}
</span>filter (lambda <span class="string">'|e|e%2==0'</span>, std.elems, {<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>})</pre>
</ul>
</dd>
<dt>
<a name = "foldl"></a>
<strong>foldl (fn[, d=t[1], t)</strong>
</dt>
<dd>
Fold a binary function left associatively.
If parameter <em>d</em> is omitted, the first element of <em>t</em> is used,
and <em>t</em> treated as if it had been passed without that element.
<h3>Parameters:</h3>
<ul>
<li><span class="parameter">fn</span>
<span class="types"><span class="type">func</span></span>
binary function
</li>
<li><span class="parameter">d</span>
initial left-most argument
(<em>default</em> t[1)
</li>
<li><span class="parameter">t</span>
<span class="types"><a class="type" href="https://www.lua.org/manual/5.3/manual.html#6.6">table</a></span>
a table
</li>
</ul>
<h3>Returns:</h3>
<ol>
result
</ol>
<h3>See also:</h3>
<ul>
<li><a href="../modules/std.functional.html#foldr">foldr</a></li>
<li><a href="../modules/std.functional.html#reduce">reduce</a></li>
</ul>
<h3>Usage:</h3>
<ul>
<pre class="example">foldl (std.operator.quot, {<span class="number">10000</span>, <span class="number">100</span>, <span class="number">10</span>}) == (<span class="number">10000</span> / <span class="number">100</span>) / <span class="number">10</span></pre>
</ul>
</dd>
<dt>
<a name = "foldr"></a>
<strong>foldr (fn[, d=t[1], t)</strong>
</dt>
<dd>
Fold a binary function right associatively.
If parameter <em>d</em> is omitted, the last element of <em>t</em> is used,
and <em>t</em> treated as if it had been passed without that element.
<h3>Parameters:</h3>
<ul>
<li><span class="parameter">fn</span>
<span class="types"><span class="type">func</span></span>
binary function
</li>
<li><span class="parameter">d</span>
initial right-most argument
(<em>default</em> t[1)
</li>
<li><span class="parameter">t</span>
<span class="types"><a class="type" href="https://www.lua.org/manual/5.3/manual.html#6.6">table</a></span>
a table
</li>
</ul>
<h3>Returns:</h3>
<ol>
result
</ol>
<h3>See also:</h3>
<ul>
<li><a href="../modules/std.functional.html#foldl">foldl</a></li>
<li><a href="../modules/std.functional.html#reduce">reduce</a></li>
</ul>
<h3>Usage:</h3>
<ul>
<pre class="example">foldr (std.operator.quot, {<span class="number">10000</span>, <span class="number">100</span>, <span class="number">10</span>}) == <span class="number">10000</span> / (<span class="number">100</span> / <span class="number">10</span>)</pre>
</ul>
</dd>
<dt>
<a name = "id"></a>
<strong>id (...)</strong>
</dt>
<dd>
Identity function.
<h3>Parameters:</h3>
<ul>
<li><span class="parameter">...</span>
arguments
</li>
</ul>
<h3>Returns:</h3>
<ol>
<em>arguments</em>
</ol>
</dd>
<dt>
<a name = "lambda"></a>
<strong>lambda (s)</strong>
</dt>
<dd>
Compile a lambda string into a Lua function. </p>
<p> A valid lambda string takes one of the following forms:</p>
<ol>
<li><code>'=expression'</code>: equivalent to <code>function (...) return expression end</code></li>
<li><code>'|args|expression'</code>: equivalent to <code>function (args) return expression end</code></li>
</ol>
<p> The first form (starting with <code>'='</code>) automatically assigns the first
nine arguments to parameters <code>'_1'</code> through <code>'_9'</code> for use within the
expression body. The parameter <code>'_1'</code> is aliased to <code>'_'</code>, and if the
first non-whitespace of the whole expression is <code>'_'</code>, then the
leading <code>'='</code> can be omitted.</p>
<p> The results are memoized, so recompiling a previously compiled
lambda string is extremely fast.
<h3>Parameters:</h3>
<ul>
<li><span class="parameter">s</span>
<span class="types"><a class="type" href="https://www.lua.org/manual/5.3/manual.html#6.4">string</a></span>
a lambda string
</li>
</ul>
<h3>Returns:</h3>
<ol>
<span class="types"><span class="type">functable</span></span>
compiled lambda string, can be called like a function
</ol>
<h3>Usage:</h3>
<ul>
<pre class="example"><span class="comment">-- The following are equivalent:
</span>lambda <span class="string">'= _1 < _2'</span>
lambda <span class="string">'|a,b| a<b'</span></pre>
</ul>
</dd>
<dt>
<a name = "map"></a>
<strong>map (fn[, ifn=std.pairs], ...)</strong>
</dt>
<dd>
Map a function over an iterator.
<h3>Parameters:</h3>
<ul>
<li><span class="parameter">fn</span>
<span class="types"><span class="type">func</span></span>
map function
</li>
<li><span class="parameter">ifn</span>
<span class="types"><span class="type">func</span></span>
iterator function
(<em>default</em> std.pairs)
</li>
<li><span class="parameter">...</span>
iterator arguments
</li>
</ul>
<h3>Returns:</h3>
<ol>
<span class="types"><a class="type" href="https://www.lua.org/manual/5.3/manual.html#6.6">table</a></span>
results
</ol>
<h3>See also:</h3>
<ul>
<li><a href="../modules/std.functional.html#filter">filter</a></li>
<li><a href="../modules/std.functional.html#map_with">map_with</a></li>
<li><a href="../modules/std.functional.html#zip">zip</a></li>
</ul>
<h3>Usage:</h3>
<ul>
<pre class="example"><span class="comment">--> {1, 4, 9, 16}
</span>map (lambda <span class="string">'=_1*_1'</span>, std.ielems, {<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>})</pre>
</ul>
</dd>
<dt>
<a name = "map_with"></a>
<strong>map_with (fn, tt)</strong>
</dt>
<dd>
Map a function over a table of argument lists.
<h3>Parameters:</h3>
<ul>
<li><span class="parameter">fn</span>
<span class="types"><span class="type">func</span></span>
map function
</li>
<li><span class="parameter">tt</span>
<span class="types"><a class="type" href="https://www.lua.org/manual/5.3/manual.html#6.6">table</a></span>
a table of <em>fn</em> argument lists
</li>
</ul>
<h3>Returns:</h3>
<ol>
<span class="types"><a class="type" href="https://www.lua.org/manual/5.3/manual.html#6.6">table</a></span>
new table of <em>fn</em> results
</ol>
<h3>See also:</h3>
<ul>
<li><a href="../modules/std.functional.html#map">map</a></li>
<li><a href="../modules/std.functional.html#zip_with">zip_with</a></li>
</ul>
<h3>Usage:</h3>
<ul>
<pre class="example"><span class="comment">--> {"123", "45"}, {a="123", b="45"}
</span>conc = bind (map_with, {lambda <span class="string">'|...|table.concat {...}'</span>})
conc {{<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>}, {<span class="number">4</span>, <span class="number">5</span>}}, conc {a={<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, x=<span class="string">"y"</span>}, b={<span class="number">4</span>, <span class="number">5</span>, z=<span class="number">6</span>}}</pre>
</ul>
</dd>
<dt>
<a name = "memoize"></a>
<strong>memoize (fn[, normfn=std.tostring])</strong>
</dt>
<dd>
Memoize a function, by wrapping it in a functable. </p>
<p> To ensure that memoize always returns the same results for the same
arguments, it passes arguments to <em>fn</em>. You can specify a more
sophisticated function if memoize should handle complicated argument
equivalencies.
<h3>Parameters:</h3>
<ul>
<li><span class="parameter">fn</span>
<span class="types"><span class="type">func</span></span>
pure function: a function with no side effects
</li>
<li><span class="parameter">normfn</span>
<span class="types"><a class="type" href="../modules/std.functional.html#normalize">normalize</a></span>
function to normalize arguments
(<em>default</em> std.tostring)
</li>
</ul>
<h3>Returns:</h3>
<ol>
<span class="types"><span class="type">functable</span></span>
memoized function
</ol>
<h3>Usage:</h3>
<ul>
<pre class="example"><span class="keyword">local</span> fast = memoize (<span class="keyword">function</span> (...) <span class="comment">--[[ slow code ]]</span> <span class="keyword">end</span>)</pre>
</ul>
</dd>
<dt>
<a name = "nop"></a>
<strong>nop ()</strong>
</dt>
<dd>
No operation.
This function ignores all arguments, and returns no values.
<h3>See also:</h3>
<ul>
<a href="../modules/std.functional.html#id">id</a>
</ul>
<h3>Usage:</h3>
<ul>
<pre class="example"><span class="keyword">if</span> unsupported <span class="keyword">then</span> vtable[<span class="string">"memrmem"</span>] = nop <span class="keyword">end</span></pre>
</ul>
</dd>
<dt>
<a name = "reduce"></a>
<strong>reduce (fn, d[, ifn=std.pairs], ...)</strong>
</dt>
<dd>
Fold a binary function into an iterator.
<h3>Parameters:</h3>
<ul>
<li><span class="parameter">fn</span>
<span class="types"><span class="type">func</span></span>
reduce function
</li>
<li><span class="parameter">d</span>
initial first argument
</li>
<li><span class="parameter">ifn</span>
<span class="types"><span class="type">func</span></span>
iterator function
(<em>default</em> std.pairs)
</li>
<li><span class="parameter">...</span>
iterator arguments
</li>
</ul>
<h3>Returns:</h3>
<ol>
result
</ol>
<h3>See also:</h3>
<ul>
<li><a href="../modules/std.functional.html#foldl">foldl</a></li>
<li><a href="../modules/std.functional.html#foldr">foldr</a></li>
</ul>
<h3>Usage:</h3>
<ul>
<pre class="example"><span class="comment">--> 2 ^ 3 ^ 4 ==> 4096
</span>reduce (std.operator.pow, <span class="number">2</span>, std.ielems, {<span class="number">3</span>, <span class="number">4</span>})</pre>
</ul>
</dd>
<dt>
<a name = "zip"></a>
<strong>zip (tt)</strong>
</dt>
<dd>
Zip a table of tables.
Make a new table, with lists of elements at the same index in the
original table. This function is effectively its own inverse.
<h3>Parameters:</h3>
<ul>
<li><span class="parameter">tt</span>
<span class="types"><a class="type" href="https://www.lua.org/manual/5.3/manual.html#6.6">table</a></span>
a table of tables
</li>
</ul>
<h3>Returns:</h3>
<ol>
<span class="types"><a class="type" href="https://www.lua.org/manual/5.3/manual.html#6.6">table</a></span>
new table with lists of elements of the same key
from <em>tt</em>
</ol>
<h3>See also:</h3>
<ul>
<li><a href="../modules/std.functional.html#map">map</a></li>
<li><a href="../modules/std.functional.html#zip_with">zip_with</a></li>
</ul>
<h3>Usage:</h3>
<ul>
<pre class="example"><span class="comment">--> {{1, 3, 5}, {2, 4}}, {a={x=1, y=3, z=5}, b={x=2, y=4}}
</span>zip {{<span class="number">1</span>, <span class="number">2</span>}, {<span class="number">3</span>, <span class="number">4</span>}, {<span class="number">5</span>}}, zip {x={a=<span class="number">1</span>, b=<span class="number">2</span>}, y={a=<span class="number">3</span>, b=<span class="number">4</span>}, z={a=<span class="number">5</span>}}</pre>
</ul>
</dd>
<dt>
<a name = "zip_with"></a>
<strong>zip_with (fn, tt)</strong>
</dt>
<dd>
Zip a list of tables together with a function.
<h3>Parameters:</h3>
<ul>
<li><span class="parameter">fn</span>
<span class="types"><span class="type">function</span></span>
function
</li>
<li><span class="parameter">tt</span>
<span class="types"><a class="type" href="https://www.lua.org/manual/5.3/manual.html#6.6">table</a></span>
table of tables
</li>
</ul>
<h3>Returns:</h3>
<ol>
<span class="types"><a class="type" href="https://www.lua.org/manual/5.3/manual.html#6.6">table</a></span>
a new table of results from calls to <em>fn</em> with arguments
made from all elements the same key in the original tables; effectively
the "columns" in a simple list
of lists.
</ol>
<h3>See also:</h3>
<ul>
<li><a href="../modules/std.functional.html#map_with">map_with</a></li>
<li><a href="../modules/std.functional.html#zip">zip</a></li>
</ul>
<h3>Usage:</h3>
<ul>
<pre class="example"><span class="comment">--> {"135", "24"}, {a="1", b="25"}
</span>conc = bind (zip_with, {lambda <span class="string">'|...|table.concat {...}'</span>})
conc {{<span class="number">1</span>, <span class="number">2</span>}, {<span class="number">3</span>, <span class="number">4</span>}, {<span class="number">5</span>}}, conc {{a=<span class="number">1</span>, b=<span class="number">2</span>}, x={a=<span class="number">3</span>, b=<span class="number">4</span>}, {b=<span class="number">5</span>}}</pre>
</ul>
</dd>
</dl>
<h2 class="section-header "><a name="Types"></a>Types </h2>
<dl class="function">
<dt>
<a name = "normalize"></a>
<strong>normalize (...)</strong>
</dt>
<dd>
Signature of a <a href="../modules/std.functional.html#memoize">memoize</a> argument normalization callback function.
<h3>Parameters:</h3>
<ul>
<li><span class="parameter">...</span>
arguments
</li>
</ul>
<h3>Returns:</h3>
<ol>
<span class="types"><a class="type" href="https://www.lua.org/manual/5.3/manual.html#6.4">string</a></span>
normalized arguments
</ol>
<h3>Usage:</h3>
<ul>
<pre class="example"><span class="keyword">local</span> normalize = <span class="keyword">function</span> (name, value, props) <span class="keyword">return</span> name <span class="keyword">end</span>
<span class="keyword">local</span> intern = std.functional.memoize (mksymbol, normalize)</pre>
</ul>
</dd>
<dt>
<a name = "predicate"></a>
<strong>predicate (...)</strong>
</dt>
<dd>
Signature of a <a href="../modules/std.functional.html#filter">filter</a> predicate callback function.
<h3>Parameters:</h3>
<ul>
<li><span class="parameter">...</span>
arguments
</li>
</ul>
<h3>Returns:</h3>
<ol>
<span class="types"><span class="type">boolean</span></span>
"truthy" if the predicate condition succeeds,
"falsey" otherwise
</ol>
<h3>Usage:</h3>
<ul>
<pre class="example"><span class="keyword">local</span> predicate = lambda <span class="string">'|k,v|type(v)=="string"'</span>
<span class="keyword">local</span> strvalues = filter (predicate, std.<span class="global">pairs</span>, {name=<span class="string">"Roberto"</span>, id=<span class="number">12345</span>})</pre>