Files
wren/modules/core/map.html
2021-02-06 15:58:10 +00:00

154 lines
7.3 KiB
HTML

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-type" content="text/html;charset=UTF-8" />
<title>Map Class &ndash; Wren</title>
<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" />
<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">
<a href="../../"><img src="../../wren.svg" class="logo"></a>
<ul>
<li><a href="../">Back to Modules</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>Map Class</h1>
<p>Extends <a href="sequence.html">Sequence</a>.</p>
<p>An associative collection that maps keys to values. More details <a href="../../maps.html">here</a>.</p>
<h2>Static Method <a href="#static-method" name="static-method" class="header-anchor">#</a></h2>
<h3>Map.<strong>new</strong>() <a href="#map.new()" name="map.new()" class="header-anchor">#</a></h3>
<p>Creates a new empty map. Equivalent to <code>{}</code>.</p>
<h2>Methods <a href="#methods" name="methods" class="header-anchor">#</a></h2>
<h3><strong>clear</strong>() <a href="#clear()" name="clear()" class="header-anchor">#</a></h3>
<p>Removes all entries from the map.</p>
<h3><strong>containsKey</strong>(key) <a href="#containskey(key)" name="containskey(key)" class="header-anchor">#</a></h3>
<p>Returns <code>true</code> if the map contains <code>key</code> or <code>false</code> otherwise.</p>
<h3><strong>count</strong> <a href="#count" name="count" class="header-anchor">#</a></h3>
<p>The number of entries in the map.</p>
<h3><strong>keys</strong> <a href="#keys" name="keys" class="header-anchor">#</a></h3>
<p>A <a href="sequence.html">Sequence</a> that can be used to iterate over the keys in the
map. Note that iteration order is undefined. All keys will be iterated over,
but may be in any order, and may even change between invocations of Wren.</p>
<h3><strong>remove</strong>(key) <a href="#remove(key)" name="remove(key)" class="header-anchor">#</a></h3>
<p>Removes <code>key</code> and the value associated with it from the map. Returns the value.</p>
<p>If the key was not present, returns <code>null</code>.</p>
<h3><strong>values</strong> <a href="#values" name="values" class="header-anchor">#</a></h3>
<p>A <a href="sequence.html">Sequence</a> that can be used to iterate over the values in the
map. Note that iteration order is undefined. All values will be iterated over,
but may be in any order, and may even change between invocations of Wren.</p>
<p>If multiple keys are associated with the same value, the value will appear
multiple times in the sequence.</p>
<h3><strong>[</strong>key<strong>]</strong> operator <a href="#[key]-operator" name="[key]-operator" class="header-anchor">#</a></h3>
<p>Gets the value associated with <code>key</code> in the map. If <code>key</code> is not present in the
map, returns <code>null</code>.</p>
<pre class="snippet">
var map = {"george": "harrison", "ringo": "starr"}
System.print(map["ringo"]) //> starr
System.print(map["pete"]) //> null
</pre>
<h3><strong>[</strong>key<strong>]=</strong>(value) operator <a href="#[key]=(value)-operator" name="[key]=(value)-operator" class="header-anchor">#</a></h3>
<p>Associates <code>value</code> with <code>key</code> in the map. If <code>key</code> was already in the map, this
replaces the previous association.</p>
<p>It is a runtime error if the key is not a <a href="bool.html">Bool</a>,
<a href="class.html">Class</a>, <a href="null.html">Null</a>, <a href="num.html">Num</a>, <a href="range.html">Range</a>,
or <a href="string.html">String</a>.</p>
<h3><strong>iterate</strong>(iterator), <strong>iteratorValue</strong>(iterator) <a href="#iterate(iterator),-iteratorvalue(iterator)" name="iterate(iterator),-iteratorvalue(iterator)" class="header-anchor">#</a></h3>
<p>Implements the <a href="../../control-flow.html#the-iterator-protocol">iterator protocol</a> for iterating over the keys and values of a map at the same time.</p>
<p>When a map (as opposed to its keys or values separately) is iterated over, each key/value pair is wrapped in a <code>MapEntry</code> object. <code>MapEntry</code> is a small helper class which has read-only <code>key</code> and <code>value</code> properties and a familiar <code>toString</code> representation.</p>
<pre class="snippet">
var map = {"paul": "mccartney"}
for (entry in map) {
System.print(entry.type) // MapEntry
System.print(entry.key + " " + entry.value) // paul mccartney
System.print(entry) // paul:mccartney
}
</pre>
<p>All map entries will be iterated over, but may be in any order, and may even change between invocations of Wren.</p>
</main>
</div>
<footer>
<div class="page">
<div class="main-column">
<p>Wren lives
<a href="https://github.com/wren-lang/wren">on GitHub</a>
&mdash; Made with &#x2764; by
<a href="http://journal.stuffwithstuff.com/">Bob Nystrom</a> and
<a href="https://github.com/wren-lang/wren/blob/main/AUTHORS">friends</a>.
</p>
<div class="main-column">
</div>
</footer>
</body>
</html>