mirror of
https://github.com/wren-lang/wren.git
synced 2026-01-12 14:48:40 +01:00
225 lines
11 KiB
HTML
225 lines
11 KiB
HTML
<!DOCTYPE html>
|
|
<html>
|
|
<head>
|
|
<meta http-equiv="Content-type" content="text/html;charset=UTF-8" />
|
|
<title>Sequence Class – Wren</title>
|
|
<link rel="stylesheet" type="text/css" href="../../style.css" />
|
|
<link href='//fonts.googleapis.com/css?family=Source+Sans+Pro:400,700,400italic,700italic|Source+Code+Pro:400|Lato:400|Sanchez:400italic,400' rel='stylesheet' type='text/css'>
|
|
<!-- Tell mobile browsers we're optimized for them and they don't need to crop
|
|
the viewport. -->
|
|
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
|
|
</head>
|
|
<body id="top" class="module">
|
|
<header>
|
|
<div class="page">
|
|
<div class="main-column">
|
|
<h1><a href="../../">wren</a></h1>
|
|
<h2>a classy little scripting language</h2>
|
|
</div>
|
|
</div>
|
|
</header>
|
|
<div class="page">
|
|
<nav class="big">
|
|
<ul>
|
|
<li><a href="../">Modules</a></li>
|
|
<li><a href="./">core</a></li>
|
|
</ul>
|
|
<section>
|
|
<h2>core classes</h2>
|
|
<ul>
|
|
<li><a href="bool.html">Bool</a></li>
|
|
<li><a href="class.html">Class</a></li>
|
|
<li><a href="fiber.html">Fiber</a></li>
|
|
<li><a href="fn.html">Fn</a></li>
|
|
<li><a href="list.html">List</a></li>
|
|
<li><a href="map.html">Map</a></li>
|
|
<li><a href="null.html">Null</a></li>
|
|
<li><a href="num.html">Num</a></li>
|
|
<li><a href="object.html">Object</a></li>
|
|
<li><a href="range.html">Range</a></li>
|
|
<li><a href="sequence.html">Sequence</a></li>
|
|
<li><a href="string.html">String</a></li>
|
|
<li><a href="system.html">System</a></li>
|
|
</ul>
|
|
</section>
|
|
</nav>
|
|
<nav class="small">
|
|
<table>
|
|
<tr>
|
|
<td><a href="../">Modules</a></td>
|
|
<td><a href="./">core</a></td>
|
|
</tr>
|
|
<tr>
|
|
<td colspan="2"><h2>core classes</h2></td>
|
|
</tr>
|
|
<tr>
|
|
<td>
|
|
<ul>
|
|
<li><a href="bool.html">Bool</a></li>
|
|
<li><a href="class.html">Class</a></li>
|
|
<li><a href="fiber.html">Fiber</a></li>
|
|
<li><a href="fn.html">Fn</a></li>
|
|
<li><a href="list.html">List</a></li>
|
|
<li><a href="map.html">Map</a></li>
|
|
<li><a href="null.html">Null</a></li>
|
|
</ul>
|
|
</td>
|
|
<td>
|
|
<ul>
|
|
<li><a href="num.html">Num</a></li>
|
|
<li><a href="object.html">Object</a></li>
|
|
<li><a href="range.html">Range</a></li>
|
|
<li><a href="sequence.html">Sequence</a></li>
|
|
<li><a href="string.html">String</a></li>
|
|
<li><a href="system.html">System</a></li>
|
|
</ul>
|
|
</td>
|
|
</tr>
|
|
</table>
|
|
</nav>
|
|
<main>
|
|
<h1>Sequence Class</h1>
|
|
<p>An abstract base class for any iterable object. Any class that implements the
|
|
core <a href="../../control-flow.html#the-iterator-protocol">iterator protocol</a> can extend this to get a number of helpful methods. </p>
|
|
<h2>Methods <a href="#methods" name="methods" class="header-anchor">#</a></h2>
|
|
<h3><strong>all</strong>(predicate) <a href="#all(predicate)" name="all(predicate)" class="header-anchor">#</a></h3>
|
|
<p>Tests whether all the elements in the sequence pass the <code>predicate</code>. </p>
|
|
<p>Iterates over the sequence, passing each element to the function <code>predicate</code>.
|
|
If it returns something <a href="../control-flow.html#truth">false</a>, stops iterating
|
|
and returns the value. Otherwise, returns <code>true</code>. </p>
|
|
<pre class="codehilite"><code class="language-wren">System.print([1, 2, 3].all {|n| n > 2}) //> false
|
|
System.print([1, 2, 3].all {|n| n < 4}) //> true</code></pre>
|
|
|
|
|
|
<h3><strong>any</strong>(predicate) <a href="#any(predicate)" name="any(predicate)" class="header-anchor">#</a></h3>
|
|
<p>Tests whether any element in the sequence passes the <code>predicate</code>. </p>
|
|
<p>Iterates over the sequence, passing each element to the function <code>predicate</code>.
|
|
If it returns something <a href="../../control-flow.html#truth">true</a>, stops iterating and
|
|
returns that value. Otherwise, returns <code>false</code>. </p>
|
|
<pre class="codehilite"><code class="language-wren">System.print([1, 2, 3].any {|n| n < 1}) //> false
|
|
System.print([1, 2, 3].any {|n| n > 2}) //> true</code></pre>
|
|
|
|
|
|
<h3><strong>contains</strong>(element) <a href="#contains(element)" name="contains(element)" class="header-anchor">#</a></h3>
|
|
<p>Returns whether the sequence contains any element equal to the given element. </p>
|
|
<h3><strong>count</strong> <a href="#count" name="count" class="header-anchor">#</a></h3>
|
|
<p>The number of elements in the sequence. </p>
|
|
<p>Unless a more efficient override is available, this will iterate over the
|
|
sequence in order to determine how many elements it contains. </p>
|
|
<h3><strong>count</strong>(predicate) <a href="#count(predicate)" name="count(predicate)" class="header-anchor">#</a></h3>
|
|
<p>Returns the number of elements in the sequence that pass the <code>predicate</code>. </p>
|
|
<p>Iterates over the sequence, passing each element to the function <code>predicate</code>
|
|
and counting the number of times the returned value evaluates to <code>true</code>. </p>
|
|
<pre class="codehilite"><code class="language-wren">System.print([1, 2, 3].count {|n| n > 2}) //> 1
|
|
System.print([1, 2, 3].count {|n| n < 4}) //> 3</code></pre>
|
|
|
|
|
|
<h3><strong>each</strong>(function) <a href="#each(function)" name="each(function)" class="header-anchor">#</a></h3>
|
|
<p>Iterates over the sequence, passing each element to the given <code>function</code>. </p>
|
|
<pre class="codehilite"><code class="language-wren">["one", "two", "three"].each {|word| System.print(word) }</code></pre>
|
|
|
|
|
|
<h3><strong>isEmpty</strong> <a href="#isempty" name="isempty" class="header-anchor">#</a></h3>
|
|
<p>Returns whether the sequence contains any elements. </p>
|
|
<p>This can be more efficient that <code>count == 0</code> because this does not iterate over
|
|
the entire sequence. </p>
|
|
<h3><strong>join</strong>(separator) <a href="#join(separator)" name="join(separator)" class="header-anchor">#</a></h3>
|
|
<p>Converts every element in the sequence to a string and then joins the results
|
|
together into a single string, each separated by <code>separator</code>. </p>
|
|
<p>It is a runtime error if <code>separator</code> is not a string. </p>
|
|
<h3><strong>join</strong>() <a href="#join()" name="join()" class="header-anchor">#</a></h3>
|
|
<p>Converts every element in the sequence to a string and then joins the results
|
|
together into a single string. </p>
|
|
<h3><strong>map</strong>(transformation) <a href="#map(transformation)" name="map(transformation)" class="header-anchor">#</a></h3>
|
|
<p>Creates a new sequence that applies the <code>transformation</code> to each element in the
|
|
original sequence while it is iterated. </p>
|
|
<pre class="codehilite"><code class="language-wren">var doubles = [1, 2, 3].map {|n| n * 2 }
|
|
for (n in doubles) {
|
|
System.print(n) //> 2
|
|
//> 4
|
|
//> 6
|
|
}</code></pre>
|
|
|
|
|
|
<p>The returned sequence is <em>lazy</em>. It only applies the mapping when you iterate
|
|
over the sequence, and it does so by holding a reference to the original
|
|
sequence. </p>
|
|
<p>This means you can use <code>map(_)</code> for things like infinite sequences or sequences
|
|
that have side effects when you iterate over them. But it also means that
|
|
changes to the original sequence will be reflected in the mapped sequence. </p>
|
|
<p>To force eager evaluation, just call <code>.toList</code> on the result. </p>
|
|
<pre class="codehilite"><code class="language-wren">var numbers = [1, 2, 3]
|
|
var doubles = numbers.map {|n| n * 2 }.toList
|
|
numbers.add(4)
|
|
System.print(doubles) //> [2, 4, 6]</code></pre>
|
|
|
|
|
|
<h3><strong>reduce</strong>(function) <a href="#reduce(function)" name="reduce(function)" class="header-anchor">#</a></h3>
|
|
<p>Reduces the sequence down to a single value. <code>function</code> is a function that
|
|
takes two arguments, the accumulator and sequence item and returns the new
|
|
accumulator value. The accumulator is initialized from the first item in the
|
|
sequence. Then, the function is invoked on each remaining item in the sequence,
|
|
iteratively updating the accumulator. </p>
|
|
<p>It is a runtime error to call this on an empty sequence. </p>
|
|
<h3><strong>reduce</strong>(seed, function) <a href="#reduce(seed,-function)" name="reduce(seed,-function)" class="header-anchor">#</a></h3>
|
|
<p>Similar to above, but uses <code>seed</code> for the initial value of the accumulator. If
|
|
the sequence is empty, returns <code>seed</code>. </p>
|
|
<h3><strong>skip</strong>(count) <a href="#skip(count)" name="skip(count)" class="header-anchor">#</a></h3>
|
|
<p>Creates a new sequence that skips the first <code>count</code> elements of the original
|
|
sequence. </p>
|
|
<p>The returned sequence is <em>lazy</em>. The first <code>count</code> elements are only skipped
|
|
once you start to iterate the returned sequence. Changes to the original
|
|
sequence will be reflected in the filtered sequence. </p>
|
|
<h3><strong>take</strong>(count) <a href="#take(count)" name="take(count)" class="header-anchor">#</a></h3>
|
|
<p>Creates a new sequence that iterates only the first <code>count</code> elements of the
|
|
original sequence. </p>
|
|
<p>The returned sequence is <em>lazy</em>. Changes to the original sequence will be
|
|
reflected in the filtered sequence. </p>
|
|
<h3><strong>toList</strong> <a href="#tolist" name="tolist" class="header-anchor">#</a></h3>
|
|
<p>Creates a <a href="list.html">list</a> containing all the elements in the sequence. </p>
|
|
<pre class="codehilite"><code class="language-wren">System.print((1..3).toList) //> [1, 2, 3]</code></pre>
|
|
|
|
|
|
<p>If the sequence is already a list, this creates a copy of it. </p>
|
|
<h3><strong>where</strong>(predicate) <a href="#where(predicate)" name="where(predicate)" class="header-anchor">#</a></h3>
|
|
<p>Creates a new sequence containing only the elements from the original sequence
|
|
that pass the <code>predicate</code>. </p>
|
|
<p>During iteration, each element in the original sequence is passed to the
|
|
function <code>predicate</code>. If it returns <code>false</code>, the element is skipped. </p>
|
|
<pre class="codehilite"><code class="language-wren">var odds = (1..6).where {|n| n % 2 == 1 }
|
|
for (n in odds) {
|
|
System.print(n) //> 1
|
|
//> 3
|
|
//> 5
|
|
}</code></pre>
|
|
|
|
|
|
<p>The returned sequence is <em>lazy</em>. It only applies the filtering when you iterate
|
|
over the sequence, and it does so by holding a reference to the original
|
|
sequence. </p>
|
|
<p>This means you can use <code>where(_)</code> for things like infinite sequences or
|
|
sequences that have side effects when you iterate over them. But it also means
|
|
that changes to the original sequence will be reflected in the filtered
|
|
sequence. </p>
|
|
<p>To force eager evaluation, just call <code>.toList</code> on the result. </p>
|
|
<pre class="codehilite"><code class="language-wren">var numbers = [1, 2, 3, 4, 5, 6]
|
|
var odds = numbers.where {|n| n % 2 == 1 }.toList
|
|
numbers.add(7)
|
|
System.print(odds) //> [1, 3, 5]</code></pre>
|
|
</main>
|
|
</div>
|
|
<footer>
|
|
<div class="page">
|
|
<div class="main-column">
|
|
<p>Wren lives
|
|
<a href="https://github.com/munificent/wren">on GitHub</a>
|
|
— Made with ❤ by
|
|
<a href="http://journal.stuffwithstuff.com/">Bob Nystrom</a> and
|
|
<a href="https://github.com/munificent/wren/blob/master/AUTHORS">friends</a>.
|
|
</p>
|
|
<div class="main-column">
|
|
</div>
|
|
</footer>
|
|
</body>
|
|
</html>
|