-
Notifications
You must be signed in to change notification settings - Fork 0
/
Iterable.html
420 lines (420 loc) · 36.1 KB
/
Iterable.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="generator" content="Asciidoctor 2.0.12">
<meta name="author" content="v0.1">
<title>Iterable class</title>
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Open+Sans:300,300italic,400,400italic,600,600italic%7CNoto+Serif:400,400italic,700,700italic%7CDroid+Sans+Mono:400,700">
<link rel="stylesheet" href="./asciidoctor.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<link rel="stylesheet" href="./rouge-old_github.css">
<meta name="keywords" content="Agate, programming language, scripting, gamedev" />
<meta name="description" content="Agate, a simple and embeddable programming language" />
<link rel="apple-touch-icon" sizes="180x180" href="/apple-touch-icon.png">
<link rel="icon" type="image/png" sizes="32x32" href="/favicon-32x32.png">
<link rel="icon" type="image/png" sizes="16x16" href="/favicon-16x16.png">
<link rel="manifest" href="/site.webmanifest">
<link rel="mask-icon" href="/safari-pinned-tab.svg" color="#5bbad5">
<meta name="msapplication-TileColor" content="#da532c">
<meta name="theme-color" content="#ffffff">
</head>
<body class="article toc2 toc-right">
<div id="header">
<h1><code>Iterable</code> class</h1>
<div class="details">
<span id="author" class="author">v0.1</span><br>
</div>
<div id="toc" class="toc2">
<div id="toctitle">Table of Contents</div>
<ul class="sectlevel1">
<li><a href="#_methods">Methods</a>
<ul class="sectlevel2">
<li><a href="#_accumulate"><strong>accumulate</strong>(val, op)</a></li>
<li><a href="#_all"><strong>all</strong>(predicate)</a></li>
<li><a href="#_any"><strong>any</strong>(predicate)</a></li>
<li><a href="#_contains"><strong>contains</strong>(value)</a></li>
<li><a href="#_count"><strong>count</strong>(predicate)</a></li>
<li><a href="#_each"><strong>each</strong>(fn)</a></li>
<li><a href="#_empty"><strong>empty</strong></a></li>
<li><a href="#_join"><strong>join</strong>()</a></li>
<li><a href="#_join_sep"><strong>join</strong>(sep)</a></li>
<li><a href="#_min"><strong>min</strong></a></li>
<li><a href="#_map"><strong>map</strong>(fn)</a></li>
<li><a href="#_max"><strong>max</strong></a></li>
<li><a href="#_none"><strong>none</strong>(predicate)</a></li>
<li><a href="#_product"><strong>product</strong></a></li>
<li><a href="#_reduce"><strong>reduce</strong>(op)</a></li>
<li><a href="#_size"><strong>size</strong></a></li>
<li><a href="#_skip"><strong>skip</strong>(count)</a></li>
<li><a href="#_sortedcomp_sorted"><strong>sorted</strong>(comp), <strong>sorted</strong>()</a></li>
<li><a href="#_sum"><strong>sum</strong></a></li>
<li><a href="#_take"><strong>take</strong>(count)</a></li>
<li><a href="#_to_a"><strong>to_a</strong></a></li>
<li><a href="#_uniq"><strong>uniq</strong>()</a></li>
<li><a href="#_where"><strong>where</strong>(predicate)</a></li>
</ul>
</li>
</ul>
</div>
</div>
<div id="content">
<div id="preamble">
<div class="sectionbody">
<div class="paragraph">
<p><code>Iterable</code> is meant to use as a mixin for classes that implements the <a href="lang.html#_iterator_protocol">iterator protocol</a>. It provides some useful methods to iterate through the elements of the iterable.</p>
</div>
<div class="paragraph">
<p><a href="core.html">< Back to core unit</a></p>
</div>
</div>
</div>
<div class="sect1">
<h2 id="_methods"><a class="anchor" href="#_methods"></a>Methods</h2>
<div class="sectionbody">
<div class="sect2">
<h3 id="_accumulate"><a class="anchor" href="#_accumulate"></a><strong>accumulate</strong>(val, op)</h3>
<div class="paragraph">
<p>Compute the <a href="https://en.wikipedia.org/wiki/Fold_(higher-order_function)">left fold</a> of the iterable with operator <code>op</code>. <code>op</code> takes two parameters, an accumulator and a new value, and produces a new accumulator. The initial accumulator is <code>val</code>. If the iterable is empty, <code>val</code> is returned.</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="rouge highlight"><code data-lang="agate"><span class="nc">IO</span><span class="p">.</span><span class="n">println</span><span class="p">([</span><span class="mi">1</span><span class="p">,</span><span class="w"> </span><span class="mi">2</span><span class="p">,</span><span class="w"> </span><span class="mi">3</span><span class="p">].</span><span class="n">accumulate</span><span class="p">(</span><span class="mi">4</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="o">|</span><span class="n">a</span><span class="p">,</span><span class="w"> </span><span class="n">b</span><span class="o">|</span><span class="w"> </span><span class="n">a</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="n">b</span><span class="w"> </span><span class="p">})</span><span class="w"> </span><span class="c1">#> 10</span></code></pre>
</div>
</div>
<div class="paragraph">
<p>See also: <a href="#_reduce"><strong>reduce</strong>(op)</a></p>
</div>
</div>
<div class="sect2">
<h3 id="_all"><a class="anchor" href="#_all"></a><strong>all</strong>(predicate)</h3>
<div class="paragraph">
<p>Return <code>true</code> if, for all the elements of the iterable, <code>predicate</code> returns <code>true</code>. If <code>predicate</code> returns a value that is false, then the iteration stops and returns that value. Otherwise, return <code>true</code>.</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="rouge highlight"><code data-lang="agate"><span class="nc">IO</span><span class="p">.</span><span class="n">println</span><span class="p">([</span><span class="w"> </span><span class="mi">1</span><span class="p">,</span><span class="w"> </span><span class="mi">2</span><span class="p">,</span><span class="w"> </span><span class="mi">3</span><span class="w"> </span><span class="p">].</span><span class="n">all</span><span class="w"> </span><span class="p">{</span><span class="o">|</span><span class="n">n</span><span class="o">|</span><span class="w"> </span><span class="n">n</span><span class="w"> </span><span class="o">></span><span class="w"> </span><span class="mi">2</span><span class="w"> </span><span class="p">})</span><span class="w"> </span><span class="c1">#> false</span><span class="err">
</span><span class="nc">IO</span><span class="p">.</span><span class="n">println</span><span class="p">([</span><span class="w"> </span><span class="mi">1</span><span class="p">,</span><span class="w"> </span><span class="mi">2</span><span class="p">,</span><span class="w"> </span><span class="mi">3</span><span class="w"> </span><span class="p">].</span><span class="n">all</span><span class="w"> </span><span class="p">{</span><span class="o">|</span><span class="n">n</span><span class="o">|</span><span class="w"> </span><span class="n">n</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="mi">4</span><span class="w"> </span><span class="p">})</span><span class="w"> </span><span class="c1">#> true</span></code></pre>
</div>
</div>
<div class="paragraph">
<p>See also: <a href="#_any"><strong>any</strong>(predicate)</a>, <a href="#_none"><strong>none</strong>(predicate)</a></p>
</div>
</div>
<div class="sect2">
<h3 id="_any"><a class="anchor" href="#_any"></a><strong>any</strong>(predicate)</h3>
<div class="paragraph">
<p>Return <code>true</code> if, for any element of the iterable, <code>predicate</code> returns <code>true</code>. If <code>predicate</code> returns a value that is true, then the iteration stops and returns that value. Otherwise return <code>false</code>. It is the contrary of <a href="#_none"><strong>none</strong>(predicate)</a>.</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="rouge highlight"><code data-lang="agate"><span class="nc">IO</span><span class="p">.</span><span class="n">println</span><span class="p">([</span><span class="w"> </span><span class="mi">1</span><span class="p">,</span><span class="w"> </span><span class="mi">2</span><span class="p">,</span><span class="w"> </span><span class="mi">3</span><span class="w"> </span><span class="p">].</span><span class="n">any</span><span class="w"> </span><span class="p">{</span><span class="o">|</span><span class="n">n</span><span class="o">|</span><span class="w"> </span><span class="n">n</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="mi">1</span><span class="w"> </span><span class="p">})</span><span class="w"> </span><span class="c1">#> false</span><span class="err">
</span><span class="nc">IO</span><span class="p">.</span><span class="n">println</span><span class="p">([</span><span class="w"> </span><span class="mi">1</span><span class="p">,</span><span class="w"> </span><span class="mi">2</span><span class="p">,</span><span class="w"> </span><span class="mi">3</span><span class="w"> </span><span class="p">].</span><span class="n">any</span><span class="w"> </span><span class="p">{</span><span class="o">|</span><span class="n">n</span><span class="o">|</span><span class="w"> </span><span class="n">n</span><span class="w"> </span><span class="o">></span><span class="w"> </span><span class="mi">2</span><span class="w"> </span><span class="p">})</span><span class="w"> </span><span class="c1">#> true</span></code></pre>
</div>
</div>
<div class="paragraph">
<p>See also: <a href="#_all"><strong>all</strong>(predicate)</a>, <a href="#_none"><strong>none</strong>(predicate)</a></p>
</div>
</div>
<div class="sect2">
<h3 id="_contains"><a class="anchor" href="#_contains"></a><strong>contains</strong>(value)</h3>
<div class="paragraph">
<p>Return <code>true</code> is the iterable contains an element equal to <code>value</code>.</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="rouge highlight"><code data-lang="agate"><span class="nc">IO</span><span class="p">.</span><span class="n">println</span><span class="p">([</span><span class="w"> </span><span class="mi">1</span><span class="p">,</span><span class="w"> </span><span class="mi">2</span><span class="p">,</span><span class="w"> </span><span class="mi">3</span><span class="w"> </span><span class="p">].</span><span class="n">contains</span><span class="p">(</span><span class="mi">2</span><span class="p">))</span><span class="w"> </span><span class="c1">#> true</span><span class="err">
</span><span class="nc">IO</span><span class="p">.</span><span class="n">println</span><span class="p">([</span><span class="w"> </span><span class="mi">1</span><span class="p">,</span><span class="w"> </span><span class="mi">2</span><span class="p">,</span><span class="w"> </span><span class="mi">3</span><span class="w"> </span><span class="p">].</span><span class="n">contains</span><span class="p">(</span><span class="mi">4</span><span class="p">))</span><span class="w"> </span><span class="c1">#> false</span></code></pre>
</div>
</div>
</div>
<div class="sect2">
<h3 id="_count"><a class="anchor" href="#_count"></a><strong>count</strong>(predicate)</h3>
<div class="paragraph">
<p>Return the number of elements in the iterable that satisfies <code>predicate</code>.</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="rouge highlight"><code data-lang="agate"><span class="nc">IO</span><span class="p">.</span><span class="n">println</span><span class="p">([</span><span class="w"> </span><span class="mi">1</span><span class="p">,</span><span class="w"> </span><span class="mi">2</span><span class="p">,</span><span class="w"> </span><span class="mi">3</span><span class="w"> </span><span class="p">].</span><span class="n">count</span><span class="w"> </span><span class="p">{</span><span class="o">|</span><span class="n">n</span><span class="o">|</span><span class="w"> </span><span class="n">n</span><span class="w"> </span><span class="o">></span><span class="w"> </span><span class="mi">2</span><span class="w"> </span><span class="p">})</span><span class="w"> </span><span class="c1">#> 1</span><span class="err">
</span><span class="nc">IO</span><span class="p">.</span><span class="n">println</span><span class="p">([</span><span class="w"> </span><span class="mi">1</span><span class="p">,</span><span class="w"> </span><span class="mi">2</span><span class="p">,</span><span class="w"> </span><span class="mi">3</span><span class="w"> </span><span class="p">].</span><span class="n">count</span><span class="w"> </span><span class="p">{</span><span class="o">|</span><span class="n">n</span><span class="o">|</span><span class="w"> </span><span class="n">n</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="mi">4</span><span class="w"> </span><span class="p">})</span><span class="w"> </span><span class="c1">#> 3</span></code></pre>
</div>
</div>
</div>
<div class="sect2">
<h3 id="_each"><a class="anchor" href="#_each"></a><strong>each</strong>(fn)</h3>
<div class="paragraph">
<p>Iterate through the iterable applying <code>fn</code> to each element of the iterable.</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="rouge highlight"><code data-lang="agate"><span class="p">[</span><span class="w"> </span><span class="mi">1</span><span class="p">,</span><span class="w"> </span><span class="mi">2</span><span class="p">,</span><span class="w"> </span><span class="mi">3</span><span class="w"> </span><span class="p">].</span><span class="n">each</span><span class="w"> </span><span class="p">{</span><span class="o">|</span><span class="n">n</span><span class="o">|</span><span class="w"> </span><span class="nc">IO</span><span class="p">.</span><span class="n">print</span><span class="p">(</span><span class="n">n</span><span class="p">)</span><span class="w"> </span><span class="p">}</span><span class="w"> </span><span class="c1">#> 123</span><span class="err">
</span><span class="nc">IO</span><span class="p">.</span><span class="n">println</span><span class="p">()</span></code></pre>
</div>
</div>
</div>
<div class="sect2">
<h3 id="_empty"><a class="anchor" href="#_empty"></a><strong>empty</strong></h3>
<div class="paragraph">
<p>Return <code>true</code> if the iterable has no elements. It is often more efficient to call this method instead of comparing <a href="#_size"><strong>size</strong></a> to zero.</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="rouge highlight"><code data-lang="agate"><span class="nc">IO</span><span class="p">.</span><span class="n">println</span><span class="p">([].</span><span class="n">empty</span><span class="p">)</span><span class="w"> </span><span class="c1">#> true</span><span class="err">
</span><span class="nc">IO</span><span class="p">.</span><span class="n">println</span><span class="p">((</span><span class="mi">42</span><span class="p">..</span><span class="mi">42</span><span class="p">).</span><span class="n">empty</span><span class="p">)</span><span class="w"> </span><span class="c1">#> false</span></code></pre>
</div>
</div>
<div class="paragraph">
<p>See also: <a href="#_size"><strong>size</strong></a></p>
</div>
</div>
<div class="sect2">
<h3 id="_join"><a class="anchor" href="#_join"></a><strong>join</strong>()</h3>
<div class="paragraph">
<p>Create a <a href="String.html"><code>String</code></a> by concatenating all the elements of the iterable. The elements are converted to strings using <code>to_s</code>.</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="rouge highlight"><code data-lang="agate"><span class="nc">IO</span><span class="p">.</span><span class="n">println</span><span class="p">([</span><span class="mi">1</span><span class="p">,</span><span class="w"> </span><span class="mi">2</span><span class="p">,</span><span class="w"> </span><span class="mi">3</span><span class="p">].</span><span class="n">join</span><span class="p">())</span><span class="w"> </span><span class="c1">#> 123</span></code></pre>
</div>
</div>
</div>
<div class="sect2">
<h3 id="_join_sep"><a class="anchor" href="#_join_sep"></a><strong>join</strong>(sep)</h3>
<div class="paragraph">
<p>Create a <a href="String.html"><code>String</code></a> by concatenating all the elements of the iterable, separated by <code>sep</code>. The elements are converted to strings using <code>to_s</code>. <code>sep</code> can be a <a href="String.html"><code>String</code></a> or a <a href="Char.html"><code>Char</code></a>.</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="rouge highlight"><code data-lang="agate"><span class="nc">IO</span><span class="p">.</span><span class="n">println</span><span class="p">([</span><span class="mi">1</span><span class="p">,</span><span class="w"> </span><span class="mi">2</span><span class="p">,</span><span class="w"> </span><span class="mi">3</span><span class="p">].</span><span class="n">join</span><span class="p">(</span><span class="sc">'|'</span><span class="p">))</span><span class="w"> </span><span class="c1">#> 1|2|3</span></code></pre>
</div>
</div>
</div>
<div class="sect2">
<h3 id="_min"><a class="anchor" href="#_min"></a><strong>min</strong></h3>
<div class="paragraph">
<p>Return the minimum of the iterable according to the <code><</code> operator.</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="rouge highlight"><code data-lang="agate"><span class="nc">IO</span><span class="p">.</span><span class="n">println</span><span class="p">([</span><span class="mi">3</span><span class="p">,</span><span class="w"> </span><span class="mi">1</span><span class="p">,</span><span class="w"> </span><span class="mi">4</span><span class="p">,</span><span class="w"> </span><span class="mi">2</span><span class="p">].</span><span class="n">min</span><span class="p">)</span><span class="w"> </span><span class="c1">#> 1</span></code></pre>
</div>
</div>
<div class="paragraph">
<p>See also: <a href="#_max"><strong>max</strong></a></p>
</div>
</div>
<div class="sect2">
<h3 id="_map"><a class="anchor" href="#_map"></a><strong>map</strong>(fn)</h3>
<div class="paragraph">
<p>Create a lazy iterable that applies <code>fn</code> to each element of the iterable.</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="rouge highlight"><code data-lang="agate"><span class="k">def</span><span class="w"> </span><span class="n">doubles</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">[</span><span class="mi">1</span><span class="p">,</span><span class="w"> </span><span class="mi">2</span><span class="p">,</span><span class="w"> </span><span class="mi">3</span><span class="p">].</span><span class="n">map</span><span class="w"> </span><span class="p">{</span><span class="o">|</span><span class="n">n</span><span class="o">|</span><span class="w"> </span><span class="n">n</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="mi">2</span><span class="w"> </span><span class="p">}</span><span class="err">
</span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="n">n</span><span class="w"> </span><span class="k">in</span><span class="w"> </span><span class="n">doubles</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="err">
</span><span class="w"> </span><span class="nc">IO</span><span class="p">.</span><span class="n">println</span><span class="p">(</span><span class="n">n</span><span class="p">)</span><span class="err">
</span><span class="p">}</span><span class="err">
</span><span class="c1">#> 2</span><span class="err">
</span><span class="c1">#> 4</span><span class="err">
</span><span class="c1">#> 6</span></code></pre>
</div>
</div>
</div>
<div class="sect2">
<h3 id="_max"><a class="anchor" href="#_max"></a><strong>max</strong></h3>
<div class="paragraph">
<p>Return the maximum of the iterable according to the <code><</code> operator.</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="rouge highlight"><code data-lang="agate"><span class="nc">IO</span><span class="p">.</span><span class="n">println</span><span class="p">([</span><span class="mi">3</span><span class="p">,</span><span class="w"> </span><span class="mi">1</span><span class="p">,</span><span class="w"> </span><span class="mi">4</span><span class="p">,</span><span class="w"> </span><span class="mi">2</span><span class="p">].</span><span class="n">max</span><span class="p">)</span><span class="w"> </span><span class="c1">#> 4</span></code></pre>
</div>
</div>
<div class="paragraph">
<p>See also: <a href="#_min"><strong>min</strong></a></p>
</div>
</div>
<div class="sect2">
<h3 id="_none"><a class="anchor" href="#_none"></a><strong>none</strong>(predicate)</h3>
<div class="paragraph">
<p>Return <code>true</code> if, for all element of the iterable, <code>predicate</code> returns <code>false</code>. If <code>predicate</code> returns a value that is true, then the iteration stops and returns the negation of that value. Otherwise return <code>true</code>. It is the contrary of <a href="#_any"><strong>any</strong>(predicate)</a>.</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="rouge highlight"><code data-lang="agate"><span class="nc">IO</span><span class="p">.</span><span class="n">println</span><span class="p">([</span><span class="w"> </span><span class="mi">1</span><span class="p">,</span><span class="w"> </span><span class="mi">2</span><span class="p">,</span><span class="w"> </span><span class="mi">3</span><span class="w"> </span><span class="p">].</span><span class="n">none</span><span class="w"> </span><span class="p">{</span><span class="o">|</span><span class="n">n</span><span class="o">|</span><span class="w"> </span><span class="n">n</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="mi">1</span><span class="w"> </span><span class="p">})</span><span class="w"> </span><span class="c1">#> true</span><span class="err">
</span><span class="nc">IO</span><span class="p">.</span><span class="n">println</span><span class="p">([</span><span class="w"> </span><span class="mi">1</span><span class="p">,</span><span class="w"> </span><span class="mi">2</span><span class="p">,</span><span class="w"> </span><span class="mi">3</span><span class="w"> </span><span class="p">].</span><span class="n">none</span><span class="w"> </span><span class="p">{</span><span class="o">|</span><span class="n">n</span><span class="o">|</span><span class="w"> </span><span class="n">n</span><span class="w"> </span><span class="o">></span><span class="w"> </span><span class="mi">2</span><span class="w"> </span><span class="p">})</span><span class="w"> </span><span class="c1">#> false</span></code></pre>
</div>
</div>
<div class="paragraph">
<p>See also: <a href="#_all"><strong>all</strong>(predicate)</a>, <a href="#_any"><strong>any</strong>(predicate)</a></p>
</div>
</div>
<div class="sect2">
<h3 id="_product"><a class="anchor" href="#_product"></a><strong>product</strong></h3>
<div class="paragraph">
<p>Return the product of the iterable according to the <code>*</code> operator.</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="rouge highlight"><code data-lang="agate"><span class="nc">IO</span><span class="p">.</span><span class="n">println</span><span class="p">([</span><span class="mi">3</span><span class="p">,</span><span class="w"> </span><span class="mi">1</span><span class="p">,</span><span class="w"> </span><span class="mi">4</span><span class="p">,</span><span class="w"> </span><span class="mi">2</span><span class="p">].</span><span class="n">product</span><span class="p">)</span><span class="w"> </span><span class="c1">#> 24</span></code></pre>
</div>
</div>
<div class="paragraph">
<p>See also: <a href="#_sum"><strong>sum</strong></a></p>
</div>
</div>
<div class="sect2">
<h3 id="_reduce"><a class="anchor" href="#_reduce"></a><strong>reduce</strong>(op)</h3>
<div class="paragraph">
<p>Compute the <a href="https://en.wikipedia.org/wiki/Fold_(higher-order_function)">left fold</a> of the iterable with operator <code>op</code>. <code>op</code> takes two parameters, an accumulator and a new value, and produces a new accumulator. The initial accumulator is the first element of the iterable. It is a runtime error to call this method on an empty iterable.</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="rouge highlight"><code data-lang="agate"><span class="nc">IO</span><span class="p">.</span><span class="n">println</span><span class="p">([</span><span class="mi">1</span><span class="p">,</span><span class="w"> </span><span class="mi">2</span><span class="p">,</span><span class="w"> </span><span class="mi">3</span><span class="p">].</span><span class="n">reduce</span><span class="w"> </span><span class="p">{</span><span class="o">|</span><span class="n">a</span><span class="p">,</span><span class="w"> </span><span class="n">b</span><span class="o">|</span><span class="w"> </span><span class="n">a</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="n">b</span><span class="w"> </span><span class="p">})</span><span class="w"> </span><span class="c1">#> 6</span></code></pre>
</div>
</div>
<div class="paragraph">
<p>See also: <a href="#_accumulate"><strong>accumulate</strong>(val, op)</a></p>
</div>
</div>
<div class="sect2">
<h3 id="_size"><a class="anchor" href="#_size"></a><strong>size</strong></h3>
<div class="paragraph">
<p>Return the number of elements in the iterable by iterating the whole iterable. This method may be overriden if it can be computed more efficiently.</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="rouge highlight"><code data-lang="agate"><span class="nc">IO</span><span class="p">.</span><span class="n">println</span><span class="p">((</span><span class="mi">1</span><span class="p">..</span><span class="mi">42</span><span class="p">).</span><span class="n">size</span><span class="p">)</span><span class="w"> </span><span class="c1">#> 42</span></code></pre>
</div>
</div>
<div class="paragraph">
<p>See also: <a href="#_empty"><strong>empty</strong></a></p>
</div>
</div>
<div class="sect2">
<h3 id="_skip"><a class="anchor" href="#_skip"></a><strong>skip</strong>(count)</h3>
<div class="paragraph">
<p>Create a lazy iterable with the <code>count</code> first elements of the iterable skipped.</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="rouge highlight"><code data-lang="agate"><span class="k">def</span><span class="w"> </span><span class="n">seq</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">[</span><span class="mi">1</span><span class="p">,</span><span class="w"> </span><span class="mi">2</span><span class="p">,</span><span class="w"> </span><span class="mi">3</span><span class="p">].</span><span class="n">skip</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span><span class="err">
</span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="n">item</span><span class="w"> </span><span class="k">in</span><span class="w"> </span><span class="n">seq</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="err">
</span><span class="w"> </span><span class="nc">IO</span><span class="p">.</span><span class="n">println</span><span class="p">(</span><span class="n">item</span><span class="p">)</span><span class="err">
</span><span class="p">}</span><span class="err">
</span><span class="c1">#> 2</span><span class="err">
</span><span class="c1">#> 3</span></code></pre>
</div>
</div>
</div>
<div class="sect2">
<h3 id="_sortedcomp_sorted"><a class="anchor" href="#_sortedcomp_sorted"></a><strong>sorted</strong>(comp), <strong>sorted</strong>()</h3>
<div class="paragraph">
<p>Return <code>true</code> if the elements of the iterable are sorted according to comparator <code>comp</code>. If <code>comp</code> is not provided, <code><</code> is used to compare elements.</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="rouge highlight"><code data-lang="agate"><span class="nc">IO</span><span class="p">.</span><span class="n">println</span><span class="p">([</span><span class="mi">1</span><span class="p">,</span><span class="w"> </span><span class="mi">2</span><span class="p">,</span><span class="w"> </span><span class="mi">3</span><span class="p">,</span><span class="w"> </span><span class="mi">4</span><span class="p">].</span><span class="n">sorted</span><span class="p">())</span><span class="w"> </span><span class="c1">#> true</span><span class="err">
</span><span class="nc">IO</span><span class="p">.</span><span class="n">println</span><span class="p">([</span><span class="mi">4</span><span class="p">,</span><span class="w"> </span><span class="mi">2</span><span class="p">,</span><span class="w"> </span><span class="mi">3</span><span class="p">,</span><span class="w"> </span><span class="mi">1</span><span class="p">].</span><span class="n">sorted</span><span class="w"> </span><span class="p">{</span><span class="o">|</span><span class="n">lhs</span><span class="p">,</span><span class="n">rhs</span><span class="o">|</span><span class="w"> </span><span class="n">lhs</span><span class="w"> </span><span class="o">></span><span class="w"> </span><span class="n">rhs</span><span class="w"> </span><span class="p">})</span><span class="w"> </span><span class="c1">#> false</span></code></pre>
</div>
</div>
</div>
<div class="sect2">
<h3 id="_sum"><a class="anchor" href="#_sum"></a><strong>sum</strong></h3>
<div class="paragraph">
<p>Return the sum of the iterable according to the <code>+</code> operator.</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="rouge highlight"><code data-lang="agate"><span class="nc">IO</span><span class="p">.</span><span class="n">println</span><span class="p">([</span><span class="mi">3</span><span class="p">,</span><span class="w"> </span><span class="mi">1</span><span class="p">,</span><span class="w"> </span><span class="mi">4</span><span class="p">,</span><span class="w"> </span><span class="mi">2</span><span class="p">].</span><span class="n">sum</span><span class="p">)</span><span class="w"> </span><span class="c1">#> 10</span></code></pre>
</div>
</div>
<div class="paragraph">
<p>See also: <a href="#_product"><strong>product</strong></a></p>
</div>
</div>
<div class="sect2">
<h3 id="_take"><a class="anchor" href="#_take"></a><strong>take</strong>(count)</h3>
<div class="paragraph">
<p>Create a lazy iterable with only the <code>count</code> first elements of the iterable.</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="rouge highlight"><code data-lang="agate"><span class="k">def</span><span class="w"> </span><span class="n">seq</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">[</span><span class="mi">1</span><span class="p">,</span><span class="w"> </span><span class="mi">2</span><span class="p">,</span><span class="w"> </span><span class="mi">3</span><span class="p">].</span><span class="n">take</span><span class="p">(</span><span class="mi">2</span><span class="p">)</span><span class="err">
</span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="n">item</span><span class="w"> </span><span class="k">in</span><span class="w"> </span><span class="n">seq</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="err">
</span><span class="w"> </span><span class="nc">IO</span><span class="p">.</span><span class="n">println</span><span class="p">(</span><span class="n">item</span><span class="p">)</span><span class="err">
</span><span class="p">}</span><span class="err">
</span><span class="c1">#> 1</span><span class="err">
</span><span class="c1">#> 2</span></code></pre>
</div>
</div>
</div>
<div class="sect2">
<h3 id="_to_a"><a class="anchor" href="#_to_a"></a><strong>to_a</strong></h3>
<div class="paragraph">
<p>Force the evaluation of the whole iterable and put the result of the iteration in an <a href="Array.html"><code>Array</code></a>.</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="rouge highlight"><code data-lang="agate"><span class="nc">IO</span><span class="p">.</span><span class="n">println</span><span class="p">((</span><span class="mi">2</span><span class="p">..</span><span class="mi">5</span><span class="p">).</span><span class="n">to_a</span><span class="p">)</span><span class="w"> </span><span class="c1">#> [2, 3, 4, 5]</span></code></pre>
</div>
</div>
</div>
<div class="sect2">
<h3 id="_uniq"><a class="anchor" href="#_uniq"></a><strong>uniq</strong>()</h3>
<div class="paragraph">
<p>Create a lazy iterable where only the first occurrence of consecutive equal items are kept.</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="rouge highlight"><code data-lang="agate"><span class="nc">IO</span><span class="p">.</span><span class="n">println</span><span class="p">([</span><span class="mi">1</span><span class="p">,</span><span class="w"> </span><span class="mi">1</span><span class="p">,</span><span class="w"> </span><span class="mi">2</span><span class="p">,</span><span class="w"> </span><span class="mi">2</span><span class="p">,</span><span class="w"> </span><span class="mi">2</span><span class="p">,</span><span class="w"> </span><span class="mi">3</span><span class="p">,</span><span class="w"> </span><span class="mi">4</span><span class="p">,</span><span class="w"> </span><span class="mi">4</span><span class="p">].</span><span class="n">uniq</span><span class="p">().</span><span class="n">to_a</span><span class="p">)</span><span class="w"> </span><span class="c1">#> [1, 2, 3, 4]</span></code></pre>
</div>
</div>
</div>
<div class="sect2">
<h3 id="_where"><a class="anchor" href="#_where"></a><strong>where</strong>(predicate)</h3>
<div class="paragraph">
<p>Create a lazy iterable with only the elements for which <code>predicate</code> returns <code>true</code>.</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="rouge highlight"><code data-lang="agate"><span class="k">def</span><span class="w"> </span><span class="n">seq</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">[</span><span class="mi">1</span><span class="p">,</span><span class="w"> </span><span class="mi">2</span><span class="p">,</span><span class="w"> </span><span class="mi">3</span><span class="p">].</span><span class="n">where</span><span class="w"> </span><span class="p">{</span><span class="o">|</span><span class="n">n</span><span class="o">|</span><span class="w"> </span><span class="n">n</span><span class="w"> </span><span class="o">%</span><span class="w"> </span><span class="mi">2</span><span class="w"> </span><span class="o">!=</span><span class="w"> </span><span class="mi">0</span><span class="w"> </span><span class="p">}</span><span class="err">
</span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="n">item</span><span class="w"> </span><span class="k">in</span><span class="w"> </span><span class="n">seq</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="err">
</span><span class="w"> </span><span class="nc">IO</span><span class="p">.</span><span class="n">println</span><span class="p">(</span><span class="n">item</span><span class="p">)</span><span class="err">
</span><span class="p">}</span><span class="err">
</span><span class="c1">#> 1</span><span class="err">
</span><span class="c1">#> 3</span></code></pre>
</div>
</div>
</div>
</div>
</div>
</div>
<script type="text/x-mathjax-config">
MathJax.Hub.Config({
messageStyle: "none",
tex2jax: {
inlineMath: [["\\(", "\\)"]],
displayMath: [["\\[", "\\]"]],
ignoreClass: "nostem|nolatexmath"
},
asciimath2jax: {
delimiters: [["\\$", "\\$"]],
ignoreClass: "nostem|noasciimath"
},
TeX: { equationNumbers: { autoNumber: "none" } }
})
MathJax.Hub.Register.StartupHook("AsciiMath Jax Ready", function () {
MathJax.InputJax.AsciiMath.postfilterHooks.Add(function (data, node) {
if ((node = data.script.parentNode) && (node = node.parentNode) && node.classList.contains("stemblock")) {
data.math.root.display = "block"
}
return data
})
})
</script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.9/MathJax.js?config=TeX-MML-AM_HTMLorMML"></script>
</body>
</html>