-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbasic.html
221 lines (197 loc) · 18.9 KB
/
basic.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
<!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>Basic Functions — 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="Generators" href="generators.html" />
<link rel="prev" title="API Reference" href="reference.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>Basic Functions</span></h2>
</div>
<div class="topnav" role="navigation" aria-label="top navigation">
<p>
«  <a href="reference.html">API Reference</a>
  ::  
<a class="uplink" href="index.html">Contents</a>
  ::  
<a href="generators.html">Generators</a>  »
</p>
</div>
<div class="content" role="main">
<section id="basic-functions">
<h1>Basic Functions<a class="headerlink" href="#basic-functions" title="Permalink to this headline">¶</a></h1>
<p>The section contains functions to create iterators from Lua objects.</p>
<dl class="py function">
<dt class="sig sig-object py" id="fun.iter">
<span class="sig-prename descclassname"><span class="pre">fun.</span></span><span class="sig-name descname"><span class="pre">iter</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">array</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#fun.iter" title="Permalink to this definition">¶</a></dt>
<dt class="sig sig-object py">
<span class="sig-prename descclassname"><span class="pre">fun.</span></span><span class="sig-name descname"><span class="pre">iter</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">map</span></span></em><span class="sig-paren">)</span></dt>
<dt class="sig sig-object py">
<span class="sig-prename descclassname"><span class="pre">fun.</span></span><span class="sig-name descname"><span class="pre">iter</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">string</span></span></em><span class="sig-paren">)</span></dt>
<dt class="sig sig-object py">
<span class="sig-prename descclassname"><span class="pre">fun.</span></span><span class="sig-name descname"><span class="pre">iter</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></dt>
<dd><dl class="field-list simple">
<dt class="field-odd">Returns</dt>
<dd class="field-odd"><p><code class="docutils literal notranslate"><span class="pre">gen,</span> <span class="pre">param,</span> <span class="pre">state</span></code> – <a class="reference internal" href="under_the_hood.html#iterator-triplet"><span class="std std-ref">iterator triplet</span></a></p>
</dd>
</dl>
<p>Make <code class="docutils literal notranslate"><span class="pre">gen,</span> <span class="pre">param,</span> <span class="pre">state</span></code> iterator from the iterable object.
The function is a generalized version of <code class="xref py py-func docutils literal notranslate"><span class="pre">pairs()</span></code> and <code class="xref py py-func docutils literal notranslate"><span class="pre">ipairs()</span></code>.</p>
<p>The function distinguish between arrays and maps using <code class="docutils literal notranslate"><span class="pre">#arg</span> <span class="pre">==</span> <span class="pre">0</span></code>
check to detect maps. For arrays <code class="docutils literal notranslate"><span class="pre">ipairs</span></code> is used. For maps a modified
version of <code class="docutils literal notranslate"><span class="pre">pairs</span></code> is used that also returns keys. Userdata objects
are handled in the same way as tables.</p>
<p>If <code class="docutils literal notranslate"><span class="pre">LUAJIT_ENABLE_LUA52COMPAT</span></code> <a class="footnote-reference brackets" href="#luajit-lua52compat" id="id1">1</a> mode is enabled and
argument has metamethods <code class="docutils literal notranslate"><span class="pre">__pairs</span></code> (for maps) or <code class="docutils literal notranslate"><span class="pre">__ipairs</span></code> for (arrays),
call it with the table or userdata as argument and return the first three
results from the call <a class="footnote-reference brackets" href="#lua52-ipairs" id="id2">2</a>.</p>
<p>All library iterator are suitable to use with Lua’s <code class="docutils literal notranslate"><span class="pre">for</span> <span class="pre">..</span> <span class="pre">in</span></code> loop.</p>
<div class="highlight-lua notranslate"><div class="highlight"><pre><span></span><span class="o">></span> <span class="kr">for</span> <span class="n">_it</span><span class="p">,</span> <span class="n">a</span> <span class="kr">in</span> <span class="n">iter</span><span class="p">({</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">})</span> <span class="kr">do</span> <span class="nb">print</span><span class="p">(</span><span class="n">a</span><span class="p">)</span> <span class="kr">end</span>
<span class="mi">1</span>
<span class="mi">2</span>
<span class="mi">3</span>
<span class="o">></span> <span class="kr">for</span> <span class="n">_it</span><span class="p">,</span> <span class="n">k</span><span class="p">,</span> <span class="n">v</span> <span class="kr">in</span> <span class="n">iter</span><span class="p">({</span> <span class="n">a</span> <span class="o">=</span> <span class="mi">1</span><span class="p">,</span> <span class="n">b</span> <span class="o">=</span> <span class="mi">2</span><span class="p">,</span> <span class="n">c</span> <span class="o">=</span> <span class="mi">3</span><span class="p">})</span> <span class="kr">do</span> <span class="nb">print</span><span class="p">(</span><span class="n">k</span><span class="p">,</span> <span class="n">v</span><span class="p">)</span> <span class="kr">end</span>
<span class="n">b</span> <span class="mi">2</span>
<span class="n">a</span> <span class="mi">1</span>
<span class="n">c</span> <span class="mi">3</span>
<span class="o">></span> <span class="kr">for</span> <span class="n">_it</span><span class="p">,</span> <span class="n">a</span> <span class="kr">in</span> <span class="n">iter</span><span class="p">(</span><span class="s2">"abcde"</span><span class="p">)</span> <span class="kr">do</span> <span class="nb">print</span><span class="p">(</span><span class="n">a</span><span class="p">)</span> <span class="kr">end</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>
</pre></div>
</div>
<p>The first cycle variable <em>_it</em> is needed to store an internal state of
the iterator. The value must be always ignored in loops:</p>
<div class="highlight-lua notranslate"><div class="highlight"><pre><span></span><span class="kr">for</span> <span class="n">_it</span><span class="p">,</span> <span class="n">a</span><span class="p">,</span> <span class="n">b</span> <span class="kr">in</span> <span class="n">iter</span><span class="p">({</span> <span class="n">a</span> <span class="o">=</span> <span class="mi">1</span><span class="p">,</span> <span class="n">b</span> <span class="o">=</span> <span class="mi">2</span><span class="p">,</span> <span class="n">c</span> <span class="o">=</span> <span class="mi">3</span><span class="p">})</span> <span class="kr">do</span> <span class="nb">print</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">end</span>
<span class="c1">-- _it is some internal iterator state - always ignore it</span>
<span class="c1">-- a, b are values return from the iterator</span>
</pre></div>
</div>
<p>Simple iterators like <code class="docutils literal notranslate"><span class="pre">iter({1,</span> <span class="pre">2,</span> <span class="pre">3})</span></code> have simple states, whereas
other iterators like <a class="reference internal" href="compositions.html#fun.zip" title="fun.zip"><code class="xref py py-func docutils literal notranslate"><span class="pre">zip()</span></code></a> or <a class="reference internal" href="compositions.html#fun.chain" title="fun.chain"><code class="xref py py-func docutils literal notranslate"><span class="pre">chain()</span></code></a> have complicated
internal states which values senseless for the end user.</p>
<p>Check out <a class="reference internal" href="under_the_hood.html"><span class="doc">Under the Hood</span></a> section for more details.</p>
<p>There is also the possibility to supply custom iterators to the
function:</p>
<div class="highlight-lua notranslate"><div class="highlight"><pre><span></span><span class="o">></span> <span class="kd">local</span> <span class="kr">function</span> <span class="nf">mypairs_gen</span><span class="p">(</span><span class="n">max</span><span class="p">,</span> <span class="n">state</span><span class="p">)</span>
<span class="kr">if</span> <span class="p">(</span><span class="n">state</span> <span class="o">>=</span> <span class="n">max</span><span class="p">)</span> <span class="kr">then</span>
<span class="kr">return</span> <span class="kc">nil</span>
<span class="kr">end</span>
<span class="kr">return</span> <span class="n">state</span> <span class="o">+</span> <span class="mi">1</span><span class="p">,</span> <span class="n">state</span> <span class="o">+</span> <span class="mi">1</span>
<span class="kr">end</span>
<span class="o">></span> <span class="kd">local</span> <span class="kr">function</span> <span class="nf">mypairs</span><span class="p">(</span><span class="n">max</span><span class="p">)</span>
<span class="kr">return</span> <span class="n">mypairs_gen</span><span class="p">,</span> <span class="n">max</span><span class="p">,</span> <span class="mi">0</span>
<span class="kr">end</span>
<span class="o">></span> <span class="kr">for</span> <span class="n">_it</span><span class="p">,</span> <span class="n">a</span> <span class="kr">in</span> <span class="n">iter</span><span class="p">(</span><span class="n">mypairs</span><span class="p">(</span><span class="mi">10</span><span class="p">))</span> <span class="kr">do</span> <span class="nb">print</span><span class="p">(</span><span class="n">a</span><span class="p">)</span> <span class="kr">end</span>
<span class="mi">1</span>
<span class="mi">2</span>
<span class="mi">3</span>
<span class="mi">4</span>
<span class="mi">5</span>
<span class="mi">6</span>
<span class="mi">7</span>
<span class="mi">8</span>
<span class="mi">9</span>
<span class="mi">10</span>
</pre></div>
</div>
<p>Iterators can return multiple values.</p>
<p>Check out <a class="reference internal" href="under_the_hood.html"><span class="doc">Under the Hood</span></a> section for more details.</p>
<dl class="footnote brackets">
<dt class="label" id="luajit-lua52compat"><span class="brackets"><a class="fn-backref" href="#id1">1</a></span></dt>
<dd><p><a class="reference external" href="http://luajit.org/extensions.html">http://luajit.org/extensions.html</a></p>
</dd>
<dt class="label" id="lua52-ipairs"><span class="brackets"><a class="fn-backref" href="#id2">2</a></span></dt>
<dd><p><a class="reference external" href="http://www.lua.org/manual/5.2/manual.html#pdf-ipairs">http://www.lua.org/manual/5.2/manual.html#pdf-ipairs</a></p>
</dd>
</dl>
</dd></dl>
<dl class="py function">
<dt class="sig sig-object py" id="fun.each">
<span class="sig-prename descclassname"><span class="pre">fun.</span></span><span class="sig-name descname"><span class="pre">each</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">fun</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.each" title="Permalink to this definition">¶</a></dt>
<dt class="sig sig-object py">
<span class="sig-name descname"><span class="pre">iterator:each(fun)</span></span></dt>
<dd><dl class="field-list simple">
<dt class="field-odd">Returns</dt>
<dd class="field-odd"><p>none</p>
</dd>
</dl>
<p>Execute the <em>fun</em> for each iteration value. The function is equivalent to
the code below:</p>
<div class="highlight-lua notranslate"><div class="highlight"><pre><span></span><span class="kr">for</span> <span class="n">_it</span><span class="p">,</span> <span class="p">...</span> <span class="kr">in</span> <span class="n">iter</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> <span class="kr">do</span>
<span class="n">fun</span><span class="p">(...)</span>
<span class="kr">end</span>
</pre></div>
</div>
<p>Examples:</p>
<div class="highlight-lua notranslate"><div class="highlight"><pre><span></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="p">{</span> <span class="n">a</span> <span class="o">=</span> <span class="mi">1</span><span class="p">,</span> <span class="n">b</span> <span class="o">=</span> <span class="mi">2</span><span class="p">,</span> <span class="n">c</span> <span class="o">=</span> <span class="mi">3</span><span class="p">})</span>
<span class="n">b</span> <span class="mi">2</span>
<span class="n">a</span> <span class="mi">1</span>
<span class="n">c</span> <span class="mi">3</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="p">{</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">})</span>
<span class="mi">1</span>
<span class="mi">2</span>
<span class="mi">3</span>
</pre></div>
</div>
<p>The function is used for its side effects. Implementation directly applies
<em>fun</em> to all iteration values without returning a new iterator, in contrast
to functions like <a class="reference internal" href="transformations.html#fun.map" title="fun.map"><code class="xref py py-func docutils literal notranslate"><span class="pre">map()</span></code></a>.</p>
<div class="admonition seealso">
<p class="admonition-title">See also</p>
<p><a class="reference internal" href="transformations.html#fun.map" title="fun.map"><code class="xref py py-func docutils literal notranslate"><span class="pre">map()</span></code></a>, <a class="reference internal" href="reducing.html#fun.reduce" title="fun.reduce"><code class="xref py py-func docutils literal notranslate"><span class="pre">reduce()</span></code></a></p>
</div>
</dd></dl>
<dl class="py function">
<dt class="sig sig-object py" id="fun.for_each">
<span class="sig-prename descclassname"><span class="pre">fun.</span></span><span class="sig-name descname"><span class="pre">for_each</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">fun</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.for_each" title="Permalink to this definition">¶</a></dt>
<dt class="sig sig-object py">
<span class="sig-name descname"><span class="pre">iterator:for_each(fun)</span></span></dt>
<dd><p>An alias for <a class="reference internal" href="#fun.each" title="fun.each"><code class="xref py py-func docutils literal notranslate"><span class="pre">each()</span></code></a>.</p>
</dd></dl>
<dl class="py function">
<dt class="sig sig-object py" id="fun.foreach">
<span class="sig-prename descclassname"><span class="pre">fun.</span></span><span class="sig-name descname"><span class="pre">foreach</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">fun</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.foreach" title="Permalink to this definition">¶</a></dt>
<dt class="sig sig-object py">
<span class="sig-name descname"><span class="pre">iterator:foreach(fun)</span></span></dt>
<dd><p>An alias for <a class="reference internal" href="#fun.each" title="fun.each"><code class="xref py py-func docutils literal notranslate"><span class="pre">each()</span></code></a>.</p>
</dd></dl>
</section>
</div>
<div class="bottomnav" role="navigation" aria-label="bottom navigation">
<p>
«  <a href="reference.html">API Reference</a>
  ::  
<a class="uplink" href="index.html">Contents</a>
  ::  
<a href="generators.html">Generators</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>