Files
Nim/sugar.html
github-actions[bot] ef3d9e7b38 Deploy to GitHub pages
2025-12-28 15:55:52 +00:00

379 lines
45 KiB
HTML

<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<!-- This file is generated by Nim. -->
<html xmlns="https://www.w3.org/1999/xhtml" xml:lang="en" lang="en" data-theme="auto">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>std/sugar</title>
<!-- Google fonts -->
<link href='https://fonts.googleapis.com/css?family=Lato:400,600,900' rel='stylesheet' type='text/css'/>
<link href='https://fonts.googleapis.com/css?family=Source+Code+Pro:400,500,600' rel='stylesheet' type='text/css'/>
<!-- Favicon -->
<link rel="shortcut icon" href="data:image/x-icon;base64,AAABAAEAEBAAAAEAIABoBAAAFgAAACgAAAAQAAAAIAAAAAEAIAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AAAAAAUAAAAF////AP///wD///8A////AP///wD///8A////AP///wD///8A////AAAAAAIAAABbAAAAlQAAAKIAAACbAAAAmwAAAKIAAACVAAAAWwAAAAL///8A////AP///wD///8A////AAAAABQAAADAAAAAYwAAAA3///8A////AP///wD///8AAAAADQAAAGMAAADAAAAAFP///wD///8A////AP///wAAAACdAAAAOv///wD///8A////AP///wD///8A////AP///wD///8AAAAAOgAAAJ3///8A////AP///wAAAAAnAAAAcP///wAAAAAoAAAASv///wD///8A////AP///wAAAABKAAAAKP///wAAAABwAAAAJ////wD///8AAAAAgQAAABwAAACIAAAAkAAAAJMAAACtAAAAFQAAABUAAACtAAAAkwAAAJAAAACIAAAAHAAAAIH///8A////AAAAAKQAAACrAAAAaP///wD///8AAAAARQAAANIAAADSAAAARf///wD///8AAAAAaAAAAKsAAACk////AAAAADMAAACcAAAAnQAAABj///8A////AP///wAAAAAYAAAAGP///wD///8A////AAAAABgAAACdAAAAnAAAADMAAAB1AAAAwwAAAP8AAADpAAAAsQAAAE4AAAAb////AP///wAAAAAbAAAATgAAALEAAADpAAAA/wAAAMMAAAB1AAAAtwAAAOkAAAD/AAAA/wAAAP8AAADvAAAA3gAAAN4AAADeAAAA3gAAAO8AAAD/AAAA/wAAAP8AAADpAAAAtwAAAGUAAAA/AAAA3wAAAP8AAAD/AAAA/wAAAP8AAAD/AAAA/wAAAP8AAAD/AAAA/wAAAP8AAADfAAAAPwAAAGX///8A////AAAAAEgAAADtAAAAvwAAAL0AAADGAAAA7wAAAO8AAADGAAAAvQAAAL8AAADtAAAASP///wD///8A////AP///wD///8AAAAAO////wD///8A////AAAAAIcAAACH////AP///wD///8AAAAAO////wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A//8AAP//AAD4HwAA7/cAAN/7AAD//wAAoYUAAJ55AACf+QAAh+EAAAAAAADAAwAA4AcAAP5/AAD//wAA//8AAA=="/>
<link rel="icon" type="image/png" sizes="32x32" href="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAAABmJLR0QA/wD/AP+gvaeTAAAACXBIWXMAAA3XAAAN1wFCKJt4AAAAB3RJTUUH4QQQEwksSS9ZWwAAAk1JREFUWMPtll2ITVEUx39nn/O7Y5qR8f05wtCUUr6ZIS++8pEnkZInPImneaCQ5METNdOkeFBKUhMPRIkHKfEuUZSUlGlKPN2TrgfncpvmnntnmlEyq1Z7t89/rf9a6+y99oZxGZf/XeIq61EdtgKXgdXA0xrYAvBjOIF1AI9zvjcC74BSpndrJPkBWDScTF8Aa4E3wDlgHbASaANmVqlcCnwHvgDvgVfAJ+AikAAvgfVZwLnSVZHZaOuKoQi3ZOMi4NkYkpe1p4J7A8BpYAD49hfIy/oqG0+hLomiKP2L5L+1ubn5115S+3OAn4EnwBlgMzCjyt6ZAnQCJ4A7wOs88iRJHvw50HoujuPBoCKwHWiosy8MdfZnAdcHk8dxXFJ3VQbQlCTJvRBCGdRbD4M6uc5glpY3eAihpN5S5w12diSEcCCEcKUO4ljdr15T76ur1FDDLIQQ3qv71EdDOe3Kxj3leRXyk+pxdWnFWod6Wt2bY3de3aSuUHcPBVimHs7mK9WrmeOF6lR1o9qnzskh2ar2qm1qizpfXaPeVGdlmGN5pb09qMxz1Xb1kLqgzn1RyH7JUXW52lr5e/Kqi9qpto7V1atuUzfnARrV7jEib1T76gG2qxdGmXyiekkt1GswPTtek0aBfJp6YySGBfWg2tPQ0FAYgf1stUfdmdcjarbYJEniKIq6gY/Aw+zWHAC+p2labGpqiorFYgGYCEzN7oQdQClN07O1/EfDyGgC0ALMBdYAi4FyK+4H3gLPsxfR1zRNi+NP7nH5J+QntnXe5B5mpfQAAAAASUVORK5CYII=">
<!-- CSS -->
<link rel="stylesheet" type="text/css" href="nimdoc.out.css?v=2.3.1">
<!-- JS -->
<script type="text/javascript" src="dochack.js?v=2.3.1"></script>
</head>
<body>
<div class="document" id="documentId">
<div class="container">
<h1 class="title">std/sugar</h1>
<div class="row">
<div class="three columns">
<div class="theme-select-wrapper">
<label for="theme-select">Theme:&nbsp;</label>
<select id="theme-select" onchange="setTheme(this.value)">
<option value="auto">🌗 Match OS</option>
<option value="dark">🌑 Dark</option>
<option value="light">🌕 Light</option>
</select>
</div>
<div id="global-links">
<ul class="simple-boot">
<li><a href="manual.html">Manual</a></li>
<li><a href="lib.html">Standard library</a></li>
<li> <a id="indexLink" href="theindex.html">Index</a></li>
<li><a href="compiler/theindex.html">Compiler docs</a></li>
<li><a href="https://nim-lang.github.io/fusion/theindex.html">Fusion docs</a></li>
<li><a href="https://nim-lang.github.io/Nim/">devel</a>, <a href="https://nim-lang.org/documentation.html">stable</a></li>
</ul>
</div>
<div id="searchInputDiv">
Search: <input type="search" id="searchInput"
oninput="search()" />
</div>
<ul class="simple simple-toc" id="toc-list">
<li>
<a class="reference reference-toplevel" href="#6" id="56">Imports</a>
</li>
<li>
<details open>
<summary><a class="reference reference-toplevel" href="#17" id="67">Macros</a></summary>
<ul class="simple simple-toc-section">
<ul class="simple nested-toc-section">->
<li><a class="reference" href="#-%3E.m%2Cuntyped%2Cuntyped" title="`-&gt;`(p, b: untyped): untyped">`-&gt;`(p, b: untyped): untyped</a></li>
</ul>
<ul class="simple nested-toc-section">=>
<li><a class="reference" href="#%3D%3E.m%2Cuntyped%2Cuntyped" title="`=&gt;`(p, b: untyped): untyped">`=&gt;`(p, b: untyped): untyped</a></li>
</ul>
<ul class="simple nested-toc-section">capture
<li><a class="reference" href="#capture.m%2Cvarargs%5Btyped%5D%2Cuntyped" title="capture(locals: varargs[typed]; body: untyped): untyped">capture(locals: varargs[typed]; body: untyped): untyped</a></li>
</ul>
<ul class="simple nested-toc-section">collect
<li><a class="reference" href="#collect.m%2Cuntyped" title="collect(body: untyped): untyped">collect(body: untyped): untyped</a></li>
<li><a class="reference" href="#collect.m%2Cuntyped%2Cuntyped" title="collect(init, body: untyped): untyped">collect(init, body: untyped): untyped</a></li>
</ul>
<ul class="simple nested-toc-section">dump
<li><a class="reference" href="#dump.m%2Cuntyped" title="dump(x: untyped): untyped">dump(x: untyped): untyped</a></li>
</ul>
<ul class="simple nested-toc-section">dumpToString
<li><a class="reference" href="#dumpToString.m%2Cuntyped" title="dumpToString(x: untyped): string">dumpToString(x: untyped): string</a></li>
</ul>
<ul class="simple nested-toc-section">dup
<li><a class="reference" href="#dup.m%2CT%2Cvarargs%5Buntyped%5D" title="dup[T](arg: T; calls: varargs[untyped]): T">dup[T](arg: T; calls: varargs[untyped]): T</a></li>
</ul>
</ul>
</details>
</li>
</ul>
</div>
<div class="nine columns" id="content">
<a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/sugar.nim#L1" class="link-seesrc" target="_blank">Source</a>&nbsp;&nbsp;
<a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/sugar.nim#L1" class="link-seesrc" target="_blank" >Edit</a>&nbsp;&nbsp;
<div id="tocRoot"></div>
<p class="module-desc">This module implements nice syntactic sugar based on Nim's macro system.</p>
<div class="section" id="6">
<h1><a class="toc-backref" href="#6">Imports</a></h1>
<dl class="item">
<a class="reference external" href="since.html">since</a>, <a class="reference external" href="macros.html">macros</a>, <a class="reference external" href="underscored_calls.html">underscored_calls</a>
</dl>
</div>
<div class="section" id="17">
<h1><a class="toc-backref" href="#17">Macros</a></h1>
<dl class="item">
<div id="-&gt;-macros-all">
<div id="->.m,untyped,untyped">
<dt><pre><span class="Keyword">macro</span> <a href="#-%3E.m%2Cuntyped%2Cuntyped"><span class="Identifier">`-&gt;`</span></a><span class="Other">(</span><span class="Identifier">p</span><span class="Other">,</span> <span class="Identifier">b</span><span class="Other">:</span> <a href="system.html#untyped"><span class="Identifier">untyped</span></a><span class="Other">)</span><span class="Other">:</span> <a href="system.html#untyped"><span class="Identifier">untyped</span></a></pre></dt>
<dd>
Syntax sugar for procedure types. It also supports pragmas.<div class="admonition admonition-warning"><span class="admonition-warning-text"><b>Warning:</b></span>
Semicolons can not be used to separate procedure arguments.</div>
<p><strong class="examples_text">Example:</strong></p>
<pre class="listing"><span class="Keyword">proc</span> <span class="Identifier">passTwoAndTwo</span><span class="Punctuation">(</span><span class="Identifier">f</span><span class="Punctuation">:</span> <span class="Punctuation">(</span><span class="Identifier">int</span><span class="Punctuation">,</span> <span class="Identifier">int</span><span class="Punctuation">)</span> <span class="Operator">-&gt;</span> <span class="Identifier">int</span><span class="Punctuation">)</span><span class="Punctuation">:</span> <span class="Identifier">int</span> <span class="Operator">=</span> <span class="Identifier">f</span><span class="Punctuation">(</span><span class="DecNumber">2</span><span class="Punctuation">,</span> <span class="DecNumber">2</span><span class="Punctuation">)</span>
<span class="Comment"># is the same as:</span>
<span class="Comment"># proc passTwoAndTwo(f: proc (x, y: int): int): int = f(2, 2)</span>
<span class="Identifier">assert</span> <span class="Identifier">passTwoAndTwo</span><span class="Punctuation">(</span><span class="Punctuation">(</span><span class="Identifier">x</span><span class="Punctuation">,</span> <span class="Identifier">y</span><span class="Punctuation">)</span> <span class="Operator">=&gt;</span> <span class="Identifier">x</span> <span class="Operator">+</span> <span class="Identifier">y</span><span class="Punctuation">)</span> <span class="Operator">==</span> <span class="DecNumber">4</span>
<span class="Keyword">proc</span> <span class="Identifier">passOne</span><span class="Punctuation">(</span><span class="Identifier">f</span><span class="Punctuation">:</span> <span class="Punctuation">(</span><span class="Identifier">int</span> <span class="Punctuation">{</span><span class="Operator">.</span><span class="Identifier">noSideEffect</span><span class="Operator">.</span><span class="Punctuation">}</span> <span class="Operator">-&gt;</span> <span class="Identifier">int</span><span class="Punctuation">)</span><span class="Punctuation">)</span><span class="Punctuation">:</span> <span class="Identifier">int</span> <span class="Operator">=</span> <span class="Identifier">f</span><span class="Punctuation">(</span><span class="DecNumber">1</span><span class="Punctuation">)</span>
<span class="Comment"># is the same as:</span>
<span class="Comment"># proc passOne(f: proc (x: int): int {.noSideEffect.}): int = f(1)</span>
<span class="Identifier">assert</span> <span class="Identifier">passOne</span><span class="Punctuation">(</span><span class="Identifier">x</span> <span class="Punctuation">{</span><span class="Operator">.</span><span class="Identifier">noSideEffect</span><span class="Operator">.</span><span class="Punctuation">}</span> <span class="Operator">=&gt;</span> <span class="Identifier">x</span> <span class="Operator">+</span> <span class="DecNumber">1</span><span class="Punctuation">)</span> <span class="Operator">==</span> <span class="DecNumber">2</span></pre>
<a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/sugar.nim#L136" class="link-seesrc" target="_blank">Source</a>&nbsp;&nbsp;
<a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/sugar.nim#L136" class="link-seesrc" target="_blank" >Edit</a>&nbsp;&nbsp;
</dd>
</div>
</div>
<div id="=&gt;-macros-all">
<div id="=>.m,untyped,untyped">
<dt><pre><span class="Keyword">macro</span> <a href="#%3D%3E.m%2Cuntyped%2Cuntyped"><span class="Identifier">`=&gt;`</span></a><span class="Other">(</span><span class="Identifier">p</span><span class="Other">,</span> <span class="Identifier">b</span><span class="Other">:</span> <a href="system.html#untyped"><span class="Identifier">untyped</span></a><span class="Other">)</span><span class="Other">:</span> <a href="system.html#untyped"><span class="Identifier">untyped</span></a></pre></dt>
<dd>
Syntax sugar for anonymous procedures. It also supports pragmas.<div class="admonition admonition-warning"><span class="admonition-warning-text"><b>Warning:</b></span>
Semicolons can not be used to separate procedure arguments.</div>
<p><strong class="examples_text">Example:</strong></p>
<pre class="listing"><span class="Keyword">proc</span> <span class="Identifier">passTwoAndTwo</span><span class="Punctuation">(</span><span class="Identifier">f</span><span class="Punctuation">:</span> <span class="Punctuation">(</span><span class="Identifier">int</span><span class="Punctuation">,</span> <span class="Identifier">int</span><span class="Punctuation">)</span> <span class="Operator">-&gt;</span> <span class="Identifier">int</span><span class="Punctuation">)</span><span class="Punctuation">:</span> <span class="Identifier">int</span> <span class="Operator">=</span> <span class="Identifier">f</span><span class="Punctuation">(</span><span class="DecNumber">2</span><span class="Punctuation">,</span> <span class="DecNumber">2</span><span class="Punctuation">)</span>
<span class="Identifier">assert</span> <span class="Identifier">passTwoAndTwo</span><span class="Punctuation">(</span><span class="Punctuation">(</span><span class="Identifier">x</span><span class="Punctuation">,</span> <span class="Identifier">y</span><span class="Punctuation">)</span> <span class="Operator">=&gt;</span> <span class="Identifier">x</span> <span class="Operator">+</span> <span class="Identifier">y</span><span class="Punctuation">)</span> <span class="Operator">==</span> <span class="DecNumber">4</span>
<span class="Keyword">type</span>
<span class="Identifier">Bot</span> <span class="Operator">=</span> <span class="Keyword">object</span>
<span class="Identifier">call</span><span class="Punctuation">:</span> <span class="Punctuation">(</span><span class="Identifier">string</span> <span class="Punctuation">{</span><span class="Operator">.</span><span class="Identifier">noSideEffect</span><span class="Operator">.</span><span class="Punctuation">}</span> <span class="Operator">-&gt;</span> <span class="Identifier">string</span><span class="Punctuation">)</span>
<span class="Keyword">var</span> <span class="Identifier">myBot</span> <span class="Operator">=</span> <span class="Identifier">Bot</span><span class="Punctuation">(</span><span class="Punctuation">)</span>
<span class="Identifier">myBot</span><span class="Operator">.</span><span class="Identifier">call</span> <span class="Operator">=</span> <span class="Punctuation">(</span><span class="Identifier">name</span><span class="Punctuation">:</span> <span class="Identifier">string</span><span class="Punctuation">)</span> <span class="Punctuation">{</span><span class="Operator">.</span><span class="Identifier">noSideEffect</span><span class="Operator">.</span><span class="Punctuation">}</span> <span class="Operator">=&gt;</span> <span class="StringLit">&quot;Hello &quot;</span> <span class="Operator">&amp;</span> <span class="Identifier">name</span> <span class="Operator">&amp;</span> <span class="StringLit">&quot;, I'm a bot.&quot;</span>
<span class="Identifier">assert</span> <span class="Identifier">myBot</span><span class="Operator">.</span><span class="Identifier">call</span><span class="Punctuation">(</span><span class="StringLit">&quot;John&quot;</span><span class="Punctuation">)</span> <span class="Operator">==</span> <span class="StringLit">&quot;Hello John, I'm a bot.&quot;</span>
<span class="Keyword">let</span> <span class="Identifier">f</span> <span class="Operator">=</span> <span class="Punctuation">(</span><span class="Punctuation">)</span> <span class="Operator">=&gt;</span> <span class="Punctuation">(</span><span class="Keyword">discard</span><span class="Punctuation">)</span> <span class="Comment"># simplest proc that returns void</span>
<span class="Identifier">f</span><span class="Punctuation">(</span><span class="Punctuation">)</span></pre>
<a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/sugar.nim#L55" class="link-seesrc" target="_blank">Source</a>&nbsp;&nbsp;
<a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/sugar.nim#L55" class="link-seesrc" target="_blank" >Edit</a>&nbsp;&nbsp;
</dd>
</div>
</div>
<div id="capture-macros-all">
<div id="capture.m,varargs[typed],untyped">
<dt><pre><span class="Keyword">macro</span> <a href="#capture.m%2Cvarargs%5Btyped%5D%2Cuntyped"><span class="Identifier">capture</span></a><span class="Other">(</span><span class="Identifier">locals</span><span class="Other">:</span> <a href="system.html#varargs"><span class="Identifier">varargs</span></a><span class="Other">[</span><a href="system.html#typed"><span class="Identifier">typed</span></a><span class="Other">]</span><span class="Other">;</span> <span class="Identifier">body</span><span class="Other">:</span> <a href="system.html#untyped"><span class="Identifier">untyped</span></a><span class="Other">)</span><span class="Other">:</span> <a href="system.html#untyped"><span class="Identifier">untyped</span></a></pre></dt>
<dd>
Useful when creating a closure in a loop to capture some local loop variables by their current iteration values.
<p><strong class="examples_text">Example:</strong></p>
<pre class="listing"><span class="Keyword">import</span> <span class="Identifier">std</span><span class="Operator">/</span><span class="Identifier">strformat</span>
<span class="Keyword">var</span> <span class="Identifier">myClosure</span><span class="Punctuation">:</span> <span class="Punctuation">(</span><span class="Punctuation">)</span> <span class="Operator">-&gt;</span> <span class="Identifier">string</span>
<span class="Keyword">for</span> <span class="Identifier">i</span> <span class="Keyword">in</span> <span class="FloatNumber">5.</span><span class="Operator">.</span><span class="DecNumber">7</span><span class="Punctuation">:</span>
<span class="Keyword">for</span> <span class="Identifier">j</span> <span class="Keyword">in</span> <span class="FloatNumber">7.</span><span class="Operator">.</span><span class="DecNumber">9</span><span class="Punctuation">:</span>
<span class="Keyword">if</span> <span class="Identifier">i</span> <span class="Operator">*</span> <span class="Identifier">j</span> <span class="Operator">==</span> <span class="DecNumber">42</span><span class="Punctuation">:</span>
<span class="Identifier">capture</span> <span class="Identifier">i</span><span class="Punctuation">,</span> <span class="Identifier">j</span><span class="Punctuation">:</span>
<span class="Identifier">myClosure</span> <span class="Operator">=</span> <span class="Punctuation">(</span><span class="Punctuation">)</span> <span class="Operator">=&gt;</span> <span class="RawData">fmt&quot;{i} * {j} = 42&quot;</span>
<span class="Identifier">assert</span> <span class="Identifier">myClosure</span><span class="Punctuation">(</span><span class="Punctuation">)</span> <span class="Operator">==</span> <span class="StringLit">&quot;6 * 7 = 42&quot;</span></pre>
<a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/sugar.nim#L216" class="link-seesrc" target="_blank">Source</a>&nbsp;&nbsp;
<a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/sugar.nim#L216" class="link-seesrc" target="_blank" >Edit</a>&nbsp;&nbsp;
</dd>
</div>
</div>
<div id="collect-macros-all">
<div id="collect.m,untyped">
<dt><pre><span class="Keyword">macro</span> <a href="#collect.m%2Cuntyped"><span class="Identifier">collect</span></a><span class="Other">(</span><span class="Identifier">body</span><span class="Other">:</span> <a href="system.html#untyped"><span class="Identifier">untyped</span></a><span class="Other">)</span><span class="Other">:</span> <a href="system.html#untyped"><span class="Identifier">untyped</span></a></pre></dt>
<dd>
<p>Same as <tt class="docutils literal"><span class="pre"><span class="Identifier">collect</span></span></tt> but without an <tt class="docutils literal"><span class="pre"><span class="Identifier">init</span></span></tt> parameter.</p>
<p><strong>See also:</strong></p>
<ul class="simple"><li><a class="reference external" href="sequtils.html#toSeq.t%2Cuntyped">sequtils.toSeq proc</a></li>
<li><a class="reference external" href="sequtils.html#mapIt.t%2Ctyped%2Cuntyped">sequtils.mapIt template</a></li>
</ul>
<p><strong class="examples_text">Example:</strong></p>
<pre class="listing"><span class="Keyword">import</span> <span class="Identifier">std</span><span class="Operator">/</span><span class="Punctuation">[</span><span class="Identifier">sets</span><span class="Punctuation">,</span> <span class="Identifier">tables</span><span class="Punctuation">]</span>
<span class="Keyword">let</span> <span class="Identifier">data</span> <span class="Operator">=</span> <span class="Operator">@</span><span class="Punctuation">[</span><span class="StringLit">&quot;bird&quot;</span><span class="Punctuation">,</span> <span class="StringLit">&quot;word&quot;</span><span class="Punctuation">]</span>
<span class="Comment"># seq:</span>
<span class="Keyword">let</span> <span class="Identifier">k</span> <span class="Operator">=</span> <span class="Identifier">collect</span><span class="Punctuation">:</span>
<span class="Keyword">for</span> <span class="Identifier">i</span><span class="Punctuation">,</span> <span class="Identifier">d</span> <span class="Keyword">in</span> <span class="Identifier">data</span><span class="Operator">.</span><span class="Identifier">pairs</span><span class="Punctuation">:</span>
<span class="Keyword">if</span> <span class="Identifier">i</span> <span class="Keyword">mod</span> <span class="DecNumber">2</span> <span class="Operator">==</span> <span class="DecNumber">0</span><span class="Punctuation">:</span> <span class="Identifier">d</span>
<span class="Identifier">assert</span> <span class="Identifier">k</span> <span class="Operator">==</span> <span class="Operator">@</span><span class="Punctuation">[</span><span class="StringLit">&quot;bird&quot;</span><span class="Punctuation">]</span>
<span class="Comment">## HashSet:</span>
<span class="Keyword">let</span> <span class="Identifier">n</span> <span class="Operator">=</span> <span class="Identifier">collect</span><span class="Punctuation">:</span>
<span class="Keyword">for</span> <span class="Identifier">d</span> <span class="Keyword">in</span> <span class="Identifier">data</span><span class="Operator">.</span><span class="Identifier">items</span><span class="Punctuation">:</span> <span class="Punctuation">{</span><span class="Identifier">d</span><span class="Punctuation">}</span>
<span class="Identifier">assert</span> <span class="Identifier">n</span> <span class="Operator">==</span> <span class="Identifier">data</span><span class="Operator">.</span><span class="Identifier">toHashSet</span>
<span class="Comment">## Table:</span>
<span class="Keyword">let</span> <span class="Identifier">m</span> <span class="Operator">=</span> <span class="Identifier">collect</span><span class="Punctuation">:</span>
<span class="Keyword">for</span> <span class="Identifier">i</span><span class="Punctuation">,</span> <span class="Identifier">d</span> <span class="Keyword">in</span> <span class="Identifier">data</span><span class="Operator">.</span><span class="Identifier">pairs</span><span class="Punctuation">:</span> <span class="Punctuation">{</span><span class="Identifier">i</span><span class="Punctuation">:</span> <span class="Identifier">d</span><span class="Punctuation">}</span>
<span class="Identifier">assert</span> <span class="Identifier">m</span> <span class="Operator">==</span> <span class="Punctuation">{</span><span class="DecNumber">0</span><span class="Punctuation">:</span> <span class="StringLit">&quot;bird&quot;</span><span class="Punctuation">,</span> <span class="DecNumber">1</span><span class="Punctuation">:</span> <span class="StringLit">&quot;word&quot;</span><span class="Punctuation">}</span><span class="Operator">.</span><span class="Identifier">toTable</span></pre>
<a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/sugar.nim#L403" class="link-seesrc" target="_blank">Source</a>&nbsp;&nbsp;
<a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/sugar.nim#L403" class="link-seesrc" target="_blank" >Edit</a>&nbsp;&nbsp;
</dd>
</div>
<div id="collect.m,untyped,untyped">
<dt><pre><span class="Keyword">macro</span> <a href="#collect.m%2Cuntyped%2Cuntyped"><span class="Identifier">collect</span></a><span class="Other">(</span><span class="Identifier">init</span><span class="Other">,</span> <span class="Identifier">body</span><span class="Other">:</span> <a href="system.html#untyped"><span class="Identifier">untyped</span></a><span class="Other">)</span><span class="Other">:</span> <a href="system.html#untyped"><span class="Identifier">untyped</span></a></pre></dt>
<dd>
<p>Comprehension for seqs/sets/tables.</p>
<p>The last expression of <tt class="docutils literal"><span class="pre"><span class="Identifier">body</span></span></tt> has special syntax that specifies the collection's add operation. Use <tt class="docutils literal"><span class="pre"><span class="Punctuation">{</span><span class="Identifier">e</span><span class="Punctuation">}</span></span></tt> for set's <tt class="docutils literal"><span class="pre"><span class="Identifier">incl</span></span></tt>, <tt class="docutils literal"><span class="pre"><span class="Punctuation">{</span><span class="Identifier">k</span><span class="Punctuation">:</span> <span class="Identifier">v</span><span class="Punctuation">}</span></span></tt> for table's <tt class="docutils literal"><span class="pre"><span class="Punctuation">[</span><span class="Punctuation">]</span><span class="Operator">=</span></span></tt> and <tt class="docutils literal"><span class="pre"><span class="Identifier">e</span></span></tt> for seq's <tt class="docutils literal"><span class="pre"><span class="Identifier">add</span></span></tt>.</p>
<p><strong class="examples_text">Example:</strong></p>
<pre class="listing"><span class="Keyword">import</span> <span class="Identifier">std</span><span class="Operator">/</span><span class="Punctuation">[</span><span class="Identifier">sets</span><span class="Punctuation">,</span> <span class="Identifier">tables</span><span class="Punctuation">]</span>
<span class="Keyword">let</span> <span class="Identifier">data</span> <span class="Operator">=</span> <span class="Operator">@</span><span class="Punctuation">[</span><span class="StringLit">&quot;bird&quot;</span><span class="Punctuation">,</span> <span class="StringLit">&quot;word&quot;</span><span class="Punctuation">]</span>
<span class="Comment">## seq:</span>
<span class="Keyword">let</span> <span class="Identifier">k</span> <span class="Operator">=</span> <span class="Identifier">collect</span><span class="Punctuation">(</span><span class="Identifier">newSeq</span><span class="Punctuation">)</span><span class="Punctuation">:</span>
<span class="Keyword">for</span> <span class="Identifier">i</span><span class="Punctuation">,</span> <span class="Identifier">d</span> <span class="Keyword">in</span> <span class="Identifier">data</span><span class="Operator">.</span><span class="Identifier">pairs</span><span class="Punctuation">:</span>
<span class="Keyword">if</span> <span class="Identifier">i</span> <span class="Keyword">mod</span> <span class="DecNumber">2</span> <span class="Operator">==</span> <span class="DecNumber">0</span><span class="Punctuation">:</span> <span class="Identifier">d</span>
<span class="Identifier">assert</span> <span class="Identifier">k</span> <span class="Operator">==</span> <span class="Operator">@</span><span class="Punctuation">[</span><span class="StringLit">&quot;bird&quot;</span><span class="Punctuation">]</span>
<span class="Comment">## seq with initialSize:</span>
<span class="Keyword">let</span> <span class="Identifier">x</span> <span class="Operator">=</span> <span class="Identifier">collect</span><span class="Punctuation">(</span><span class="Identifier">newSeqOfCap</span><span class="Punctuation">(</span><span class="DecNumber">4</span><span class="Punctuation">)</span><span class="Punctuation">)</span><span class="Punctuation">:</span>
<span class="Keyword">for</span> <span class="Identifier">i</span><span class="Punctuation">,</span> <span class="Identifier">d</span> <span class="Keyword">in</span> <span class="Identifier">data</span><span class="Operator">.</span><span class="Identifier">pairs</span><span class="Punctuation">:</span>
<span class="Keyword">if</span> <span class="Identifier">i</span> <span class="Keyword">mod</span> <span class="DecNumber">2</span> <span class="Operator">==</span> <span class="DecNumber">0</span><span class="Punctuation">:</span> <span class="Identifier">d</span>
<span class="Identifier">assert</span> <span class="Identifier">x</span> <span class="Operator">==</span> <span class="Operator">@</span><span class="Punctuation">[</span><span class="StringLit">&quot;bird&quot;</span><span class="Punctuation">]</span>
<span class="Comment">## HashSet:</span>
<span class="Keyword">let</span> <span class="Identifier">y</span> <span class="Operator">=</span> <span class="Identifier">collect</span><span class="Punctuation">(</span><span class="Identifier">initHashSet</span><span class="Punctuation">(</span><span class="Punctuation">)</span><span class="Punctuation">)</span><span class="Punctuation">:</span>
<span class="Keyword">for</span> <span class="Identifier">d</span> <span class="Keyword">in</span> <span class="Identifier">data</span><span class="Operator">.</span><span class="Identifier">items</span><span class="Punctuation">:</span> <span class="Punctuation">{</span><span class="Identifier">d</span><span class="Punctuation">}</span>
<span class="Identifier">assert</span> <span class="Identifier">y</span> <span class="Operator">==</span> <span class="Identifier">data</span><span class="Operator">.</span><span class="Identifier">toHashSet</span>
<span class="Comment">## Table:</span>
<span class="Keyword">let</span> <span class="Identifier">z</span> <span class="Operator">=</span> <span class="Identifier">collect</span><span class="Punctuation">(</span><span class="Identifier">initTable</span><span class="Punctuation">(</span><span class="DecNumber">2</span><span class="Punctuation">)</span><span class="Punctuation">)</span><span class="Punctuation">:</span>
<span class="Keyword">for</span> <span class="Identifier">i</span><span class="Punctuation">,</span> <span class="Identifier">d</span> <span class="Keyword">in</span> <span class="Identifier">data</span><span class="Operator">.</span><span class="Identifier">pairs</span><span class="Punctuation">:</span> <span class="Punctuation">{</span><span class="Identifier">i</span><span class="Punctuation">:</span> <span class="Identifier">d</span><span class="Punctuation">}</span>
<span class="Identifier">assert</span> <span class="Identifier">z</span> <span class="Operator">==</span> <span class="Punctuation">{</span><span class="DecNumber">0</span><span class="Punctuation">:</span> <span class="StringLit">&quot;bird&quot;</span><span class="Punctuation">,</span> <span class="DecNumber">1</span><span class="Punctuation">:</span> <span class="StringLit">&quot;word&quot;</span><span class="Punctuation">}</span><span class="Operator">.</span><span class="Identifier">toTable</span></pre>
<a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/sugar.nim#L366" class="link-seesrc" target="_blank">Source</a>&nbsp;&nbsp;
<a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/sugar.nim#L366" class="link-seesrc" target="_blank" >Edit</a>&nbsp;&nbsp;
</dd>
</div>
</div>
<div id="dump-macros-all">
<div id="dump.m,untyped">
<dt><pre><span class="Keyword">macro</span> <a href="#dump.m%2Cuntyped"><span class="Identifier">dump</span></a><span class="Other">(</span><span class="Identifier">x</span><span class="Other">:</span> <a href="system.html#untyped"><span class="Identifier">untyped</span></a><span class="Other">)</span><span class="Other">:</span> <a href="system.html#untyped"><span class="Identifier">untyped</span></a></pre></dt>
<dd>
<p>Dumps the content of an expression, useful for debugging. It accepts any expression and prints a textual representation of the tree representing the expression - as it would appear in source code - together with the value of the expression.</p>
<p>See also: <tt class="docutils literal"><span class="pre"><span class="Identifier">dumpToString</span></span></tt> which is more convenient and useful since it expands intermediate templates/macros, returns a string instead of calling <tt class="docutils literal"><span class="pre"><span class="Identifier">echo</span></span></tt>, and works with statements and expressions.</p>
<p><strong class="examples_text">Example: cmd: -r:off</strong></p>
<pre class="listing"><span class="Keyword">let</span>
<span class="Identifier">x</span> <span class="Operator">=</span> <span class="DecNumber">10</span>
<span class="Identifier">y</span> <span class="Operator">=</span> <span class="DecNumber">20</span>
<span class="Identifier">dump</span><span class="Punctuation">(</span><span class="Identifier">x</span> <span class="Operator">+</span> <span class="Identifier">y</span><span class="Punctuation">)</span> <span class="Comment"># prints: `x + y = 30`</span></pre>
<a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/sugar.nim#L155" class="link-seesrc" target="_blank">Source</a>&nbsp;&nbsp;
<a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/sugar.nim#L155" class="link-seesrc" target="_blank" >Edit</a>&nbsp;&nbsp;
</dd>
</div>
</div>
<div id="dumpToString-macros-all">
<div id="dumpToString.m,untyped">
<dt><pre><span class="Keyword">macro</span> <a href="#dumpToString.m%2Cuntyped"><span class="Identifier">dumpToString</span></a><span class="Other">(</span><span class="Identifier">x</span><span class="Other">:</span> <a href="system.html#untyped"><span class="Identifier">untyped</span></a><span class="Other">)</span><span class="Other">:</span> <a href="system.html#string"><span class="Identifier">string</span></a></pre></dt>
<dd>
Returns the content of a statement or expression <tt class="docutils literal"><span class="pre"><span class="Identifier">x</span></span></tt> after semantic analysis, useful for debugging.
<p><strong class="examples_text">Example:</strong></p>
<pre class="listing"><span class="Keyword">const</span> <span class="Identifier">a</span> <span class="Operator">=</span> <span class="DecNumber">1</span>
<span class="Keyword">let</span> <span class="Identifier">x</span> <span class="Operator">=</span> <span class="DecNumber">10</span>
<span class="Identifier">assert</span> <span class="Identifier">dumpToString</span><span class="Punctuation">(</span><span class="Identifier">a</span> <span class="Operator">+</span> <span class="DecNumber">2</span><span class="Punctuation">)</span> <span class="Operator">==</span> <span class="StringLit">&quot;a + 2: 3 = 3&quot;</span>
<span class="Identifier">assert</span> <span class="Identifier">dumpToString</span><span class="Punctuation">(</span><span class="Identifier">a</span> <span class="Operator">+</span> <span class="Identifier">x</span><span class="Punctuation">)</span> <span class="Operator">==</span> <span class="StringLit">&quot;a + x: 1 + x = 11&quot;</span>
<span class="Keyword">template</span> <span class="Identifier">square</span><span class="Punctuation">(</span><span class="Identifier">x</span><span class="Punctuation">)</span><span class="Punctuation">:</span> <span class="Identifier">untyped</span> <span class="Operator">=</span> <span class="Identifier">x</span> <span class="Operator">*</span> <span class="Identifier">x</span>
<span class="Identifier">assert</span> <span class="Identifier">dumpToString</span><span class="Punctuation">(</span><span class="Identifier">square</span><span class="Punctuation">(</span><span class="Identifier">x</span><span class="Punctuation">)</span><span class="Punctuation">)</span> <span class="Operator">==</span> <span class="StringLit">&quot;square(x): x * x = 100&quot;</span>
<span class="Identifier">assert</span> <span class="Keyword">not</span> <span class="Identifier">compiles</span> <span class="Identifier">dumpToString</span><span class="Punctuation">(</span><span class="DecNumber">1</span> <span class="Operator">+</span> <span class="Identifier">nonexistent</span><span class="Punctuation">)</span>
<span class="Keyword">import</span> <span class="Identifier">std</span><span class="Operator">/</span><span class="Identifier">strutils</span>
<span class="Identifier">assert</span> <span class="StringLit">&quot;failedAssertImpl&quot;</span> <span class="Keyword">in</span> <span class="Identifier">dumpToString</span><span class="Punctuation">(</span><span class="Identifier">assert</span> <span class="Identifier">true</span><span class="Punctuation">)</span> <span class="Comment"># example with a statement</span></pre>
<a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/sugar.nim#L183" class="link-seesrc" target="_blank">Source</a>&nbsp;&nbsp;
<a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/sugar.nim#L183" class="link-seesrc" target="_blank" >Edit</a>&nbsp;&nbsp;
</dd>
</div>
</div>
<div id="dup-macros-all">
<div id="dup.m,T,varargs[untyped]">
<dt><pre><span class="Keyword">macro</span> <a href="#dup.m%2CT%2Cvarargs%5Buntyped%5D"><span class="Identifier">dup</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">]</span><span class="Other">(</span><span class="Identifier">arg</span><span class="Other">:</span> <span class="Identifier">T</span><span class="Other">;</span> <span class="Identifier">calls</span><span class="Other">:</span> <a href="system.html#varargs"><span class="Identifier">varargs</span></a><span class="Other">[</span><a href="system.html#untyped"><span class="Identifier">untyped</span></a><span class="Other">]</span><span class="Other">)</span><span class="Other">:</span> <span class="Identifier">T</span></pre></dt>
<dd>
<p>Turns an <span id="inminusplace_1">in-place</span> algorithm into one that works on a copy and returns this copy, without modifying its input.</p>
<p>This macro also allows for (otherwise in-place) function chaining.</p>
<p><strong>Since:</strong> Version 1.2.</p>
<p><strong class="examples_text">Example:</strong></p>
<pre class="listing"><span class="Keyword">import</span> <span class="Identifier">std</span><span class="Operator">/</span><span class="Identifier">algorithm</span>
<span class="Keyword">let</span> <span class="Identifier">a</span> <span class="Operator">=</span> <span class="Operator">@</span><span class="Punctuation">[</span><span class="DecNumber">1</span><span class="Punctuation">,</span> <span class="DecNumber">2</span><span class="Punctuation">,</span> <span class="DecNumber">3</span><span class="Punctuation">,</span> <span class="DecNumber">4</span><span class="Punctuation">,</span> <span class="DecNumber">5</span><span class="Punctuation">,</span> <span class="DecNumber">6</span><span class="Punctuation">,</span> <span class="DecNumber">7</span><span class="Punctuation">,</span> <span class="DecNumber">8</span><span class="Punctuation">,</span> <span class="DecNumber">9</span><span class="Punctuation">]</span>
<span class="Identifier">assert</span> <span class="Identifier">a</span><span class="Operator">.</span><span class="Identifier">dup</span><span class="Punctuation">(</span><span class="Identifier">sort</span><span class="Punctuation">)</span> <span class="Operator">==</span> <span class="Identifier">sorted</span><span class="Punctuation">(</span><span class="Identifier">a</span><span class="Punctuation">)</span>
<span class="Comment"># Chaining:</span>
<span class="Keyword">var</span> <span class="Identifier">aCopy</span> <span class="Operator">=</span> <span class="Identifier">a</span>
<span class="Identifier">aCopy</span><span class="Operator">.</span><span class="Identifier">insert</span><span class="Punctuation">(</span><span class="DecNumber">10</span><span class="Punctuation">)</span>
<span class="Identifier">assert</span> <span class="Identifier">a</span><span class="Operator">.</span><span class="Identifier">dup</span><span class="Punctuation">(</span><span class="Identifier">insert</span><span class="Punctuation">(</span><span class="DecNumber">10</span><span class="Punctuation">)</span><span class="Punctuation">,</span> <span class="Identifier">sort</span><span class="Punctuation">)</span> <span class="Operator">==</span> <span class="Identifier">sorted</span><span class="Punctuation">(</span><span class="Identifier">aCopy</span><span class="Punctuation">)</span>
<span class="Keyword">let</span> <span class="Identifier">s1</span> <span class="Operator">=</span> <span class="StringLit">&quot;abc&quot;</span>
<span class="Keyword">let</span> <span class="Identifier">s2</span> <span class="Operator">=</span> <span class="StringLit">&quot;xyz&quot;</span>
<span class="Identifier">assert</span> <span class="Identifier">s1</span> <span class="Operator">&amp;</span> <span class="Identifier">s2</span> <span class="Operator">==</span> <span class="Identifier">s1</span><span class="Operator">.</span><span class="Identifier">dup</span><span class="Punctuation">(</span><span class="Operator">&amp;=</span> <span class="Identifier">s2</span><span class="Punctuation">)</span>
<span class="Comment"># An underscore (_) can be used to denote the place of the argument you're passing:</span>
<span class="Identifier">assert</span> <span class="StringLit">&quot;&quot;</span><span class="Operator">.</span><span class="Identifier">dup</span><span class="Punctuation">(</span><span class="Identifier">addQuoted</span><span class="Punctuation">(</span><span class="Identifier">_</span><span class="Punctuation">,</span> <span class="StringLit">&quot;foo&quot;</span><span class="Punctuation">)</span><span class="Punctuation">)</span> <span class="Operator">==</span> <span class="StringLit">&quot;</span><span class="EscapeSequence">\&quot;</span><span class="StringLit">foo</span><span class="EscapeSequence">\&quot;</span><span class="StringLit">&quot;</span>
<span class="Comment"># but `_` is optional here since the substitution is in 1st position:</span>
<span class="Identifier">assert</span> <span class="StringLit">&quot;&quot;</span><span class="Operator">.</span><span class="Identifier">dup</span><span class="Punctuation">(</span><span class="Identifier">addQuoted</span><span class="Punctuation">(</span><span class="StringLit">&quot;foo&quot;</span><span class="Punctuation">)</span><span class="Punctuation">)</span> <span class="Operator">==</span> <span class="StringLit">&quot;</span><span class="EscapeSequence">\&quot;</span><span class="StringLit">foo</span><span class="EscapeSequence">\&quot;</span><span class="StringLit">&quot;</span>
<span class="Keyword">proc</span> <span class="Identifier">makePalindrome</span><span class="Punctuation">(</span><span class="Identifier">s</span><span class="Punctuation">:</span> <span class="Keyword">var</span> <span class="Identifier">string</span><span class="Punctuation">)</span> <span class="Operator">=</span>
<span class="Keyword">for</span> <span class="Identifier">i</span> <span class="Keyword">in</span> <span class="Identifier">countdown</span><span class="Punctuation">(</span><span class="Identifier">s</span><span class="Operator">.</span><span class="Identifier">len</span><span class="Operator">-</span><span class="DecNumber">2</span><span class="Punctuation">,</span> <span class="DecNumber">0</span><span class="Punctuation">)</span><span class="Punctuation">:</span>
<span class="Identifier">s</span><span class="Operator">.</span><span class="Identifier">add</span><span class="Punctuation">(</span><span class="Identifier">s</span><span class="Punctuation">[</span><span class="Identifier">i</span><span class="Punctuation">]</span><span class="Punctuation">)</span>
<span class="Keyword">let</span> <span class="Identifier">c</span> <span class="Operator">=</span> <span class="StringLit">&quot;xyz&quot;</span>
<span class="Comment"># chaining:</span>
<span class="Keyword">let</span> <span class="Identifier">d</span> <span class="Operator">=</span> <span class="Identifier">dup</span> <span class="Identifier">c</span><span class="Punctuation">:</span>
<span class="Identifier">makePalindrome</span> <span class="Comment"># xyzyx</span>
<span class="Identifier">sort</span><span class="Punctuation">(</span><span class="Identifier">_</span><span class="Punctuation">,</span> <span class="Identifier">SortOrder</span><span class="Operator">.</span><span class="Identifier">Descending</span><span class="Punctuation">)</span> <span class="Comment"># zyyxx</span>
<span class="Identifier">makePalindrome</span> <span class="Comment"># zyyxxxyyz</span>
<span class="Identifier">assert</span> <span class="Identifier">d</span> <span class="Operator">==</span> <span class="StringLit">&quot;zyyxxxyyz&quot;</span></pre>
<a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/sugar.nim#L254" class="link-seesrc" target="_blank">Source</a>&nbsp;&nbsp;
<a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/sugar.nim#L254" class="link-seesrc" target="_blank" >Edit</a>&nbsp;&nbsp;
</dd>
</div>
</div>
</dl>
</div>
</div>
</div>
<div class="twelve-columns footer">
<span class="nim-sprite"></span>
<br>
<small style="color: var(--hint);">Made with Nim. Generated: 2025-12-28 15:54:50 UTC</small>
</div>
</div>
</div>
<script defer data-domain="nim-lang.org" src="https://plausible.io/js/plausible.js"></script>
</body>
</html>