2015-11-09 08:01:19 -08:00
|
|
|
<!DOCTYPE html>
|
|
|
|
|
<html>
|
|
|
|
|
<head>
|
|
|
|
|
<meta http-equiv="Content-type" content="text/html;charset=UTF-8" />
|
|
|
|
|
<title>Sequence Class – Wren</title>
|
2020-06-05 22:25:23 +00:00
|
|
|
<script type="application/javascript" src="../../prism.js" data-manual></script>
|
|
|
|
|
<script type="application/javascript" src="../../wren.js"></script>
|
|
|
|
|
<link rel="stylesheet" type="text/css" href="../../prism.css" />
|
2015-11-09 08:01:19 -08:00
|
|
|
<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">
|
2020-06-05 22:25:23 +00:00
|
|
|
<a href="../../"><img src="../../wren.svg" class="logo"></a>
|
2015-11-09 08:01:19 -08:00
|
|
|
<ul>
|
2020-06-05 22:25:23 +00:00
|
|
|
<li><a href="../">Back to Modules</a></li>
|
2015-11-09 08:01:19 -08:00
|
|
|
</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>
|
2020-06-05 22:25:23 +00:00
|
|
|
<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>
|
2015-11-09 08:01:19 -08:00
|
|
|
<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>
|
2020-06-05 22:25:23 +00:00
|
|
|
<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="snippet">
|
|
|
|
|
System.print([1, 2, 3].all {|n| n > 2}) //> false
|
|
|
|
|
System.print([1, 2, 3].all {|n| n < 4}) //> true
|
|
|
|
|
</pre>
|
2015-11-09 08:01:19 -08:00
|
|
|
|
|
|
|
|
<h3><strong>any</strong>(predicate) <a href="#any(predicate)" name="any(predicate)" class="header-anchor">#</a></h3>
|
2020-06-05 22:25:23 +00:00
|
|
|
<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="snippet">
|
|
|
|
|
System.print([1, 2, 3].any {|n| n < 1}) //> false
|
|
|
|
|
System.print([1, 2, 3].any {|n| n > 2}) //> true
|
|
|
|
|
</pre>
|
2015-11-09 08:01:19 -08:00
|
|
|
|
|
|
|
|
<h3><strong>contains</strong>(element) <a href="#contains(element)" name="contains(element)" class="header-anchor">#</a></h3>
|
2020-06-05 22:25:23 +00:00
|
|
|
<p>Returns whether the sequence contains any element equal to the given element.</p>
|
2015-11-09 08:01:19 -08:00
|
|
|
<h3><strong>count</strong> <a href="#count" name="count" class="header-anchor">#</a></h3>
|
2020-06-05 22:25:23 +00:00
|
|
|
<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>
|
2015-11-09 08:01:19 -08:00
|
|
|
<h3><strong>count</strong>(predicate) <a href="#count(predicate)" name="count(predicate)" class="header-anchor">#</a></h3>
|
2020-06-05 22:25:23 +00:00
|
|
|
<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="snippet">
|
|
|
|
|
System.print([1, 2, 3].count {|n| n > 2}) //> 1
|
|
|
|
|
System.print([1, 2, 3].count {|n| n < 4}) //> 3
|
|
|
|
|
</pre>
|
2015-11-09 08:01:19 -08:00
|
|
|
|
|
|
|
|
<h3><strong>each</strong>(function) <a href="#each(function)" name="each(function)" class="header-anchor">#</a></h3>
|
2020-06-05 22:25:23 +00:00
|
|
|
<p>Iterates over the sequence, passing each element to the given <code>function</code>.</p>
|
|
|
|
|
<pre class="snippet">
|
|
|
|
|
["one", "two", "three"].each {|word| System.print(word) }
|
|
|
|
|
</pre>
|
2015-11-09 08:01:19 -08:00
|
|
|
|
|
|
|
|
<h3><strong>isEmpty</strong> <a href="#isempty" name="isempty" class="header-anchor">#</a></h3>
|
2020-06-05 22:25:23 +00:00
|
|
|
<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>
|
2015-11-09 08:01:19 -08:00
|
|
|
<h3><strong>join</strong>(separator) <a href="#join(separator)" name="join(separator)" class="header-anchor">#</a></h3>
|
2020-06-05 22:25:23 +00:00
|
|
|
<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>
|
2015-11-09 08:01:19 -08:00
|
|
|
<h3><strong>join</strong>() <a href="#join()" name="join()" class="header-anchor">#</a></h3>
|
2020-06-05 22:25:23 +00:00
|
|
|
<p>Converts every element in the sequence to a string and then joins the results
|
|
|
|
|
together into a single string.</p>
|
2015-11-09 08:01:19 -08:00
|
|
|
<h3><strong>map</strong>(transformation) <a href="#map(transformation)" name="map(transformation)" class="header-anchor">#</a></h3>
|
2020-06-05 22:25:23 +00:00
|
|
|
<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="snippet">
|
|
|
|
|
var doubles = [1, 2, 3].map {|n| n * 2 }
|
|
|
|
|
for (n in doubles) {
|
|
|
|
|
System.print(n) //> 2
|
|
|
|
|
//> 4
|
|
|
|
|
//> 6
|
|
|
|
|
}
|
|
|
|
|
</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="snippet">
|
|
|
|
|
var numbers = [1, 2, 3]
|
|
|
|
|
var doubles = numbers.map {|n| n * 2 }.toList
|
|
|
|
|
numbers.add(4)
|
|
|
|
|
System.print(doubles) //> [2, 4, 6]
|
|
|
|
|
</pre>
|
2015-11-09 08:01:19 -08:00
|
|
|
|
|
|
|
|
<h3><strong>reduce</strong>(function) <a href="#reduce(function)" name="reduce(function)" class="header-anchor">#</a></h3>
|
2020-06-05 22:25:23 +00:00
|
|
|
<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>
|
2015-11-09 08:01:19 -08:00
|
|
|
<h3><strong>reduce</strong>(seed, function) <a href="#reduce(seed,-function)" name="reduce(seed,-function)" class="header-anchor">#</a></h3>
|
2020-06-05 22:25:23 +00:00
|
|
|
<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>
|
2017-10-19 07:05:45 -07:00
|
|
|
<h3><strong>skip</strong>(count) <a href="#skip(count)" name="skip(count)" class="header-anchor">#</a></h3>
|
2020-06-05 22:25:23 +00:00
|
|
|
<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>
|
2017-10-19 07:05:45 -07:00
|
|
|
<h3><strong>take</strong>(count) <a href="#take(count)" name="take(count)" class="header-anchor">#</a></h3>
|
2020-06-05 22:25:23 +00:00
|
|
|
<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>
|
2015-11-09 08:01:19 -08:00
|
|
|
<h3><strong>toList</strong> <a href="#tolist" name="tolist" class="header-anchor">#</a></h3>
|
2020-06-05 22:25:23 +00:00
|
|
|
<p>Creates a <a href="list.html">list</a> containing all the elements in the sequence.</p>
|
|
|
|
|
<pre class="snippet">
|
|
|
|
|
System.print((1..3).toList) //> [1, 2, 3]
|
|
|
|
|
</pre>
|
2015-11-09 08:01:19 -08:00
|
|
|
|
2020-06-05 22:25:23 +00:00
|
|
|
<p>If the sequence is already a list, this creates a copy of it.</p>
|
2015-11-09 08:01:19 -08:00
|
|
|
<h3><strong>where</strong>(predicate) <a href="#where(predicate)" name="where(predicate)" class="header-anchor">#</a></h3>
|
2020-06-05 22:25:23 +00:00
|
|
|
<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="snippet">
|
|
|
|
|
var odds = (1..6).where {|n| n % 2 == 1 }
|
|
|
|
|
for (n in odds) {
|
|
|
|
|
System.print(n) //> 1
|
|
|
|
|
//> 3
|
|
|
|
|
//> 5
|
|
|
|
|
}
|
|
|
|
|
</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="snippet">
|
|
|
|
|
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]
|
|
|
|
|
</pre>
|
2015-11-09 08:01:19 -08:00
|
|
|
</main>
|
|
|
|
|
</div>
|
|
|
|
|
<footer>
|
|
|
|
|
<div class="page">
|
|
|
|
|
<div class="main-column">
|
|
|
|
|
<p>Wren lives
|
2019-02-06 02:52:27 +00:00
|
|
|
<a href="https://github.com/wren-lang/wren">on GitHub</a>
|
2015-11-09 08:01:19 -08:00
|
|
|
— Made with ❤ by
|
|
|
|
|
<a href="http://journal.stuffwithstuff.com/">Bob Nystrom</a> and
|
2020-06-12 17:15:45 +00:00
|
|
|
<a href="https://github.com/wren-lang/wren/blob/main/AUTHORS">friends</a>.
|
2015-11-09 08:01:19 -08:00
|
|
|
</p>
|
|
|
|
|
<div class="main-column">
|
|
|
|
|
</div>
|
|
|
|
|
</footer>
|
|
|
|
|
</body>
|
|
|
|
|
</html>
|