mirror of
https://github.com/wren-lang/wren.git
synced 2026-01-12 06:38:45 +01:00
105 lines
7.6 KiB
HTML
105 lines
7.6 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="core">
|
||
<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>
|
||
<ul>
|
||
<li><a href="./">Core Library</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>
|
||
</ul>
|
||
</section>
|
||
</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>
|
||
<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 its return value evaluates to <code>false</code>, stops iterating and returns <code>false</code>.
|
||
Otherwise, returns <code>true</code>.</p>
|
||
<div class="codehilite"><pre><span class="p">[</span><span class="m">1</span><span class="p">,</span> <span class="m">2</span><span class="p">,</span> <span class="m">3</span><span class="p">].</span><span class="n">all</span> <span class="p">{</span><span class="o">|</span><span class="n">n</span><span class="o">|</span> <span class="n">n</span> <span class="o">></span> <span class="m">2</span><span class="p">}</span> <span class="c1">// False.</span>
|
||
<span class="p">[</span><span class="m">1</span><span class="p">,</span> <span class="m">2</span><span class="p">,</span> <span class="m">3</span><span class="p">].</span><span class="n">all</span> <span class="p">{</span><span class="o">|</span><span class="n">n</span><span class="o">|</span> <span class="n">n</span> <span class="o"><</span> <span class="m">4</span><span class="p">}</span> <span class="c1">// True.</span>
|
||
</pre></div>
|
||
|
||
|
||
<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 its return value evaluates to <code>true</code>, stops iterating and returns <code>true</code>.
|
||
Otherwise, returns <code>false</code>.</p>
|
||
<div class="codehilite"><pre><span class="p">[</span><span class="m">1</span><span class="p">,</span> <span class="m">2</span><span class="p">,</span> <span class="m">3</span><span class="p">].</span><span class="n">any</span> <span class="p">{</span><span class="o">|</span><span class="n">n</span><span class="o">|</span> <span class="n">n</span> <span class="o"><</span> <span class="m">1</span><span class="p">}</span> <span class="c1">// False.</span>
|
||
<span class="p">[</span><span class="m">1</span><span class="p">,</span> <span class="m">2</span><span class="p">,</span> <span class="m">3</span><span class="p">].</span><span class="n">any</span> <span class="p">{</span><span class="o">|</span><span class="n">n</span><span class="o">|</span> <span class="n">n</span> <span class="o">></span> <span class="m">2</span><span class="p">}</span> <span class="c1">// True.</span>
|
||
</pre></div>
|
||
|
||
|
||
<h3><strong>join</strong>(sep) <a href="#join(sep)" name="join(sep)" class="header-anchor">#</a></h3>
|
||
<p>Returns a string representation of the list. The string representations of the
|
||
elements in the list is concatenated with intervening occurrences of <code>sep</code>.</p>
|
||
<p>It is a runtime error if <code>sep</code> is not a string.</p>
|
||
<h3><strong>join</strong> <a href="#join" name="join" class="header-anchor">#</a></h3>
|
||
<p>Calls <code>join</code> with the empty string as the separator.</p>
|
||
<h3><strong>map</strong>(transformation) <a href="#map(transformation)" name="map(transformation)" class="header-anchor">#</a></h3>
|
||
<p>Creates a new list by applying <code>transformation</code> to each element in the
|
||
sequence.</p>
|
||
<p>Iterates over the sequence, passing each element to the function
|
||
<code>transformation</code>. Generates a new list from the result of each of those calls.</p>
|
||
<div class="codehilite"><pre><span class="p">[</span><span class="m">1</span><span class="p">,</span> <span class="m">2</span><span class="p">,</span> <span class="m">3</span><span class="p">].</span><span class="n">map</span> <span class="p">{</span><span class="o">|</span><span class="n">n</span><span class="o">|</span> <span class="n">n</span> <span class="o">*</span> <span class="m">2</span><span class="p">}</span> <span class="c1">// [2, 4, 6].</span>
|
||
</pre></div>
|
||
|
||
|
||
<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>where</strong>(predicate) <a href="#where(predicate)" name="where(predicate)" class="header-anchor">#</a></h3>
|
||
<p>Produces a new list containing only the 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>.
|
||
If it returns <code>true</code>, adds the element to the result list.</p>
|
||
<div class="codehilite"><pre><span class="p">(</span><span class="mf">1..10</span><span class="p">).</span><span class="n">where</span> <span class="p">{</span><span class="o">|</span><span class="n">n</span><span class="o">|</span> <span class="n">n</span> <span class="o">%</span> <span class="mi">2</span> <span class="o">==</span> <span class="mi">1</span><span class="p">}</span> <span class="c1">// [1, 3, 5, 7, 9].</span>
|
||
</pre></div>
|
||
</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>.</p>
|
||
<div class="main-column">
|
||
</div>
|
||
</footer>
|
||
</body>
|
||
</html> |