Files
Nim/sequtils.html
github-actions[bot] de413ab6bb Deploy to GitHub pages
2025-12-25 20:14:24 +00:00

1337 lines
205 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/sequtils</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/sequtils</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" id="see-also_toc" href="#see-also">See also</a></li>
<li>
<a class="reference reference-toplevel" href="#6" id="56">Imports</a>
</li>
<li>
<details open>
<summary><a class="reference reference-toplevel" href="#12" id="62">Procs</a></summary>
<ul class="simple simple-toc-section">
<ul class="simple nested-toc-section">addUnique
<li><a class="reference" href="#addUnique%2Cseq%5BT%5D%2CsinkT" title="addUnique[T](s: var seq[T]; x: sink T)">addUnique[T](s: var seq[T]; x: sink T)</a></li>
</ul>
<ul class="simple nested-toc-section">all
<li><a class="reference" href="#all%2CopenArray%5BT%5D%2Cproc%28T%29" title="all[T](s: openArray[T]; pred: proc (x: T): bool {.closure.}): bool">all[T](s: openArray[T]; pred: proc (x: T): bool {.closure.}): bool</a></li>
</ul>
<ul class="simple nested-toc-section">any
<li><a class="reference" href="#any%2CopenArray%5BT%5D%2Cproc%28T%29" title="any[T](s: openArray[T]; pred: proc (x: T): bool {.closure.}): bool">any[T](s: openArray[T]; pred: proc (x: T): bool {.closure.}): bool</a></li>
</ul>
<ul class="simple nested-toc-section">apply
<li><a class="reference" href="#apply%2CopenArray%5BT%5D%2Cproc%28T%29_3" title="apply[T](s: openArray[T]; op: proc (x: T) {.closure.})">apply[T](s: openArray[T]; op: proc (x: T) {.closure.})</a></li>
<li><a class="reference" href="#apply%2CopenArray%5BT%5D%2Cproc%28T%29_2" title="apply[T](s: var openArray[T]; op: proc (x: T): T {.closure.})">apply[T](s: var openArray[T]; op: proc (x: T): T {.closure.})</a></li>
<li><a class="reference" href="#apply%2CopenArray%5BT%5D%2Cproc%28T%29" title="apply[T](s: var openArray[T]; op: proc (x: var T) {.closure.})">apply[T](s: var openArray[T]; op: proc (x: var T) {.closure.})</a></li>
</ul>
<ul class="simple nested-toc-section">concat
<li><a class="reference" href="#concat%2Cvarargs%5Bseq%5BT%5D%5D" title="concat[T](seqs: varargs[seq[T]]): seq[T]">concat[T](seqs: varargs[seq[T]]): seq[T]</a></li>
</ul>
<ul class="simple nested-toc-section">count
<li><a class="reference" href="#count%2CopenArray%5BT%5D%2CT" title="count[T](s: openArray[T]; x: T): int">count[T](s: openArray[T]; x: T): int</a></li>
</ul>
<ul class="simple nested-toc-section">cycle
<li><a class="reference" href="#cycle%2CopenArray%5BT%5D%2CNatural" title="cycle[T](s: openArray[T]; n: Natural): seq[T]">cycle[T](s: openArray[T]; n: Natural): seq[T]</a></li>
</ul>
<ul class="simple nested-toc-section">deduplicate
<li><a class="reference" href="#deduplicate%2CopenArray%5BT%5D%2Cbool" title="deduplicate[T](s: openArray[T]; isSorted: bool = false): seq[T]">deduplicate[T](s: openArray[T]; isSorted: bool = false): seq[T]</a></li>
</ul>
<ul class="simple nested-toc-section">delete
<li><a class="reference" href="#delete%2Cseq%5BT%5D%2CNatural%2CNatural" title="delete[T](s: var seq[T]; first, last: Natural)">delete[T](s: var seq[T]; first, last: Natural)</a></li>
<li><a class="reference" href="#delete%2Cseq%5BT%5D%2CSlice%5Bint%5D" title="delete[T](s: var seq[T]; slice: Slice[int])">delete[T](s: var seq[T]; slice: Slice[int])</a></li>
</ul>
<ul class="simple nested-toc-section">distribute
<li><a class="reference" href="#distribute%2Cseq%5BT%5D%2CPositive" title="distribute[T](s: seq[T]; num: Positive; spread = true): seq[seq[T]]">distribute[T](s: seq[T]; num: Positive; spread = true): seq[seq[T]]</a></li>
</ul>
<ul class="simple nested-toc-section">filter
<li><a class="reference" href="#filter%2CopenArray%5BT%5D%2Cproc%28T%29" title="filter[T](s: openArray[T]; pred: proc (x: T): bool {.closure.}): seq[T]">filter[T](s: openArray[T]; pred: proc (x: T): bool {.closure.}): seq[T]</a></li>
</ul>
<ul class="simple nested-toc-section">insert
<li><a class="reference" href="#insert%2Cseq%5BT%5D%2CopenArray%5BT%5D%2Cint" title="insert[T](dest: var seq[T]; src: openArray[T]; pos = 0)">insert[T](dest: var seq[T]; src: openArray[T]; pos = 0)</a></li>
</ul>
<ul class="simple nested-toc-section">keepIf
<li><a class="reference" href="#keepIf%2Cseq%5BT%5D%2Cproc%28T%29" title="keepIf[T](s: var seq[T]; pred: proc (x: T): bool {.closure.})">keepIf[T](s: var seq[T]; pred: proc (x: T): bool {.closure.})</a></li>
</ul>
<ul class="simple nested-toc-section">map
<li><a class="reference" href="#map%2CopenArray%5BT%5D%2Cproc%28T%29" title="map[T, S](s: openArray[T]; op: proc (x: T): S {.closure.}): seq[S]">map[T, S](s: openArray[T]; op: proc (x: T): S {.closure.}): seq[S]</a></li>
</ul>
<ul class="simple nested-toc-section">max
<li><a class="reference" href="#max%2CopenArray%5BT%5D%2Cproc%28T%2CT%29" title="max[T](x: openArray[T]; cmp: proc (a, b: T): int): T">max[T](x: openArray[T]; cmp: proc (a, b: T): int): T</a></li>
</ul>
<ul class="simple nested-toc-section">maxIndex
<li><a class="reference" href="#maxIndex%2CopenArray%5BT%5D" title="maxIndex[T](s: openArray[T]): int">maxIndex[T](s: openArray[T]): int</a></li>
<li><a class="reference" href="#maxIndex%2CopenArray%5BT%5D%2Cproc%28T%2CT%29" title="maxIndex[T](s: openArray[T]; cmp: proc (a, b: T): int): int">maxIndex[T](s: openArray[T]; cmp: proc (a, b: T): int): int</a></li>
</ul>
<ul class="simple nested-toc-section">min
<li><a class="reference" href="#min%2CopenArray%5BT%5D%2Cproc%28T%2CT%29" title="min[T](x: openArray[T]; cmp: proc (a, b: T): int): T">min[T](x: openArray[T]; cmp: proc (a, b: T): int): T</a></li>
</ul>
<ul class="simple nested-toc-section">minIndex
<li><a class="reference" href="#minIndex%2CopenArray%5BT%5D" title="minIndex[T](s: openArray[T]): int">minIndex[T](s: openArray[T]): int</a></li>
<li><a class="reference" href="#minIndex%2CopenArray%5BT%5D%2Cproc%28T%2CT%29" title="minIndex[T](s: openArray[T]; cmp: proc (a, b: T): int): int">minIndex[T](s: openArray[T]; cmp: proc (a, b: T): int): int</a></li>
</ul>
<ul class="simple nested-toc-section">minmax
<li><a class="reference" href="#minmax%2CopenArray%5BT%5D" title="minmax[T](x: openArray[T]): (T, T)">minmax[T](x: openArray[T]): (T, T)</a></li>
<li><a class="reference" href="#minmax%2CopenArray%5BT%5D%2Cproc%28T%2CT%29" title="minmax[T](x: openArray[T]; cmp: proc (a, b: T): int): (T, T)">minmax[T](x: openArray[T]; cmp: proc (a, b: T): int): (T, T)</a></li>
</ul>
<ul class="simple nested-toc-section">repeat
<li><a class="reference" href="#repeat%2CT%2CNatural" title="repeat[T](x: T; n: Natural): seq[T]">repeat[T](x: T; n: Natural): seq[T]</a></li>
</ul>
<ul class="simple nested-toc-section">unzip
<li><a class="reference" href="#unzip%2CopenArray%5B%5D" title="unzip[S, T](s: openArray[(S, T)]): (seq[S], seq[T])">unzip[S, T](s: openArray[(S, T)]): (seq[S], seq[T])</a></li>
</ul>
<ul class="simple nested-toc-section">zip
<li><a class="reference" href="#zip%2C%2C" title="zip[S, T](s1: openArray[S]; s2: openArray[T]): seq[(S, T)]">zip[S, T](s1: openArray[S]; s2: openArray[T]): seq[(S, T)]</a></li>
</ul>
</ul>
</details>
</li>
<li>
<details open>
<summary><a class="reference reference-toplevel" href="#15" id="65">Iterators</a></summary>
<ul class="simple simple-toc-section">
<ul class="simple nested-toc-section">filter
<li><a class="reference" href="#filter.i%2CopenArray%5BT%5D%2Cproc%28T%29" title="filter[T](s: openArray[T]; pred: proc (x: T): bool {.closure.}): T">filter[T](s: openArray[T]; pred: proc (x: T): bool {.closure.}): T</a></li>
</ul>
<ul class="simple nested-toc-section">items
<li><a class="reference" href="#items.i" title="items[T](xs: iterator (): T): T">items[T](xs: iterator (): T): T</a></li>
</ul>
</ul>
</details>
</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">mapLiterals
<li><a class="reference" href="#mapLiterals.m%2Cuntyped%2Cuntyped" title="mapLiterals(constructor, op: untyped; nested = true): untyped">mapLiterals(constructor, op: untyped; nested = true): untyped</a></li>
</ul>
</ul>
</details>
</li>
<li>
<details open>
<summary><a class="reference reference-toplevel" href="#18" id="68">Templates</a></summary>
<ul class="simple simple-toc-section">
<ul class="simple nested-toc-section">allIt
<li><a class="reference" href="#allIt.t%2Cuntyped%2Cuntyped" title="allIt(s, pred: untyped): bool">allIt(s, pred: untyped): bool</a></li>
</ul>
<ul class="simple nested-toc-section">anyIt
<li><a class="reference" href="#anyIt.t%2Cuntyped%2Cuntyped" title="anyIt(s, pred: untyped): bool">anyIt(s, pred: untyped): bool</a></li>
</ul>
<ul class="simple nested-toc-section">applyIt
<li><a class="reference" href="#applyIt.t%2Cuntyped%2Cuntyped" title="applyIt(varSeq, op: untyped)">applyIt(varSeq, op: untyped)</a></li>
</ul>
<ul class="simple nested-toc-section">countIt
<li><a class="reference" href="#countIt.t%2Cuntyped%2Cuntyped" title="countIt(s, pred: untyped): int">countIt(s, pred: untyped): int</a></li>
</ul>
<ul class="simple nested-toc-section">filterIt
<li><a class="reference" href="#filterIt.t%2Cuntyped%2Cuntyped" title="filterIt(s, pred: untyped): untyped">filterIt(s, pred: untyped): untyped</a></li>
</ul>
<ul class="simple nested-toc-section">findIt
<li><a class="reference" href="#findIt.t%2Cuntyped%2Cuntyped" title="findIt(s, predicate: untyped): int">findIt(s, predicate: untyped): int</a></li>
</ul>
<ul class="simple nested-toc-section">foldl
<li><a class="reference" href="#foldl.t%2C%2C%2C" title="foldl(sequence, operation, first): untyped">foldl(sequence, operation, first): untyped</a></li>
<li><a class="reference" href="#foldl.t%2Cuntyped%2Cuntyped" title="foldl(sequence, operation: untyped): untyped">foldl(sequence, operation: untyped): untyped</a></li>
</ul>
<ul class="simple nested-toc-section">foldr
<li><a class="reference" href="#foldr.t%2Cuntyped%2Cuntyped" title="foldr(sequence, operation: untyped): untyped">foldr(sequence, operation: untyped): untyped</a></li>
</ul>
<ul class="simple nested-toc-section">keepItIf
<li><a class="reference" href="#keepItIf.t%2Cseq%2Cuntyped" title="keepItIf(varSeq: seq; pred: untyped)">keepItIf(varSeq: seq; pred: untyped)</a></li>
</ul>
<ul class="simple nested-toc-section">mapIt
<li><a class="reference" href="#mapIt.t%2Ctyped%2Cuntyped" title="mapIt(s: typed; op: untyped): untyped">mapIt(s: typed; op: untyped): untyped</a></li>
</ul>
<ul class="simple nested-toc-section">newSeqWith
<li><a class="reference" href="#newSeqWith.t%2Cint%2Cuntyped" title="newSeqWith(len: int; init: untyped): untyped">newSeqWith(len: int; init: untyped): untyped</a></li>
</ul>
<ul class="simple nested-toc-section">toSeq
<li><a class="reference" href="#toSeq.t%2Cuntyped" title="toSeq(iter: untyped): untyped">toSeq(iter: untyped): untyped</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/collections/sequtils.nim#L1" class="link-seesrc" target="_blank">Source</a>&nbsp;&nbsp;
<a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/collections/sequtils.nim#L1" class="link-seesrc" target="_blank" >Edit</a>&nbsp;&nbsp;
<div id="tocRoot"></div>
<p class="module-desc">Although this module has <tt class="docutils literal"><span class="pre"><span class="Identifier">seq</span></span></tt> in its name, it implements operations not only for the <span id="seq_1">seq</span> type, but for three built-in container types under the <tt class="docutils literal"><span class="pre"><span class="Identifier">openArray</span></span></tt> umbrella:<ul class="simple"><li>sequences</li>
<li>strings</li>
<li>array</li>
</ul>
<p>The <tt class="docutils literal"><span class="pre"><span class="Identifier">system</span></span></tt> module defines several common functions, such as:</p>
<ul class="simple"><li><tt class="docutils literal"><span class="pre"><span class="Identifier">newSeq</span><span class="Punctuation">[</span><span class="Identifier">T</span><span class="Punctuation">]</span></span></tt> for creating new sequences of type <tt class="docutils literal"><span class="pre"><span class="Identifier">T</span></span></tt></li>
<li><tt class="docutils literal"><span class="pre"><span class="Operator">@</span></span></tt> for converting arrays and strings to sequences</li>
<li><tt class="docutils literal"><span class="pre"><span class="Identifier">add</span></span></tt> for adding new elements to strings and sequences</li>
<li><tt class="docutils literal"><span class="pre"><span class="Operator">&amp;</span></span></tt> for string and seq concatenation</li>
<li><tt class="docutils literal"><span class="pre"><span class="Keyword">in</span></span></tt> (alias for <tt class="docutils literal"><span class="pre"><span class="Identifier">contains</span></span></tt>) and <tt class="docutils literal"><span class="pre"><span class="Keyword">notin</span></span></tt> for checking if an item is in a container</li>
</ul>
<p>This module builds upon that, providing additional functionality in form of procs, iterators and templates inspired by functional programming languages.</p>
<p>For functional style programming you have different options at your disposal:</p>
<ul class="simple"><li>the <a class="reference external" href="sugar.html#collect.m%2Cuntyped%2Cuntyped">sugar.collect macro</a></li>
<li>pass an <a class="reference external" href="manual.html#procedures-anonymous-procs">anonymous proc</a></li>
<li>import the <a class="reference external" href="sugar.html">sugar module</a> and use the <a class="reference external" href="sugar.html#%3D&gt;.m,untyped,untyped">=&gt; macro</a></li>
<li>use <a class="reference external" href="#18">...It templates</a> (<a class="reference external" href="#mapIt.t,typed,untyped">mapIt</a>, <a class="reference external" href="#filterIt.t,untyped,untyped">filterIt</a>, etc.)</li>
</ul>
<p>Chaining of functions is possible thanks to the <a class="reference external" href="manual.html#procedures-method-call-syntax">method call syntax</a>.</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">sequtils</span>
<span class="Keyword">import</span> <span class="Identifier">std</span><span class="Operator">/</span><span class="Identifier">sugar</span>
<span class="Comment"># Creating a sequence from 1 to 10, multiplying each member by 2,</span>
<span class="Comment"># keeping only the members which are not divisible by 6.</span>
<span class="Keyword">let</span>
<span class="Identifier">foo</span> <span class="Operator">=</span> <span class="Identifier">toSeq</span><span class="Punctuation">(</span><span class="FloatNumber">1.</span><span class="Operator">.</span><span class="DecNumber">10</span><span class="Punctuation">)</span><span class="Operator">.</span><span class="Identifier">map</span><span class="Punctuation">(</span><span class="Identifier">x</span> <span class="Operator">=&gt;</span> <span class="Identifier">x</span> <span class="Operator">*</span> <span class="DecNumber">2</span><span class="Punctuation">)</span><span class="Operator">.</span><span class="Identifier">filter</span><span class="Punctuation">(</span><span class="Identifier">x</span> <span class="Operator">=&gt;</span> <span class="Identifier">x</span> <span class="Keyword">mod</span> <span class="DecNumber">6</span> <span class="Operator">!=</span> <span class="DecNumber">0</span><span class="Punctuation">)</span>
<span class="Identifier">bar</span> <span class="Operator">=</span> <span class="Identifier">toSeq</span><span class="Punctuation">(</span><span class="FloatNumber">1.</span><span class="Operator">.</span><span class="DecNumber">10</span><span class="Punctuation">)</span><span class="Operator">.</span><span class="Identifier">mapIt</span><span class="Punctuation">(</span><span class="Identifier">it</span> <span class="Operator">*</span> <span class="DecNumber">2</span><span class="Punctuation">)</span><span class="Operator">.</span><span class="Identifier">filterIt</span><span class="Punctuation">(</span><span class="Identifier">it</span> <span class="Keyword">mod</span> <span class="DecNumber">6</span> <span class="Operator">!=</span> <span class="DecNumber">0</span><span class="Punctuation">)</span>
<span class="Identifier">baz</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="Keyword">in</span> <span class="FloatNumber">1.</span><span class="Operator">.</span><span class="DecNumber">10</span><span class="Punctuation">:</span>
<span class="Keyword">let</span> <span class="Identifier">j</span> <span class="Operator">=</span> <span class="DecNumber">2</span> <span class="Operator">*</span> <span class="Identifier">i</span>
<span class="Keyword">if</span> <span class="Identifier">j</span> <span class="Keyword">mod</span> <span class="DecNumber">6</span> <span class="Operator">!=</span> <span class="DecNumber">0</span><span class="Punctuation">:</span>
<span class="Identifier">j</span>
<span class="Identifier">doAssert</span> <span class="Identifier">foo</span> <span class="Operator">==</span> <span class="Identifier">bar</span>
<span class="Identifier">doAssert</span> <span class="Identifier">foo</span> <span class="Operator">==</span> <span class="Identifier">baz</span>
<span class="Identifier">doAssert</span> <span class="Identifier">foo</span> <span class="Operator">==</span> <span class="Operator">@</span><span class="Punctuation">[</span><span class="DecNumber">2</span><span class="Punctuation">,</span> <span class="DecNumber">4</span><span class="Punctuation">,</span> <span class="DecNumber">8</span><span class="Punctuation">,</span> <span class="DecNumber">10</span><span class="Punctuation">,</span> <span class="DecNumber">14</span><span class="Punctuation">,</span> <span class="DecNumber">16</span><span class="Punctuation">,</span> <span class="DecNumber">20</span><span class="Punctuation">]</span>
<span class="Identifier">doAssert</span> <span class="Identifier">foo</span><span class="Operator">.</span><span class="Identifier">any</span><span class="Punctuation">(</span><span class="Identifier">x</span> <span class="Operator">=&gt;</span> <span class="Identifier">x</span> <span class="Operator">&gt;</span> <span class="DecNumber">17</span><span class="Punctuation">)</span>
<span class="Identifier">doAssert</span> <span class="Keyword">not</span> <span class="Identifier">bar</span><span class="Operator">.</span><span class="Identifier">allIt</span><span class="Punctuation">(</span><span class="Identifier">it</span> <span class="Operator">&lt;</span> <span class="DecNumber">20</span><span class="Punctuation">)</span>
<span class="Identifier">doAssert</span> <span class="Identifier">foo</span><span class="Operator">.</span><span class="Identifier">foldl</span><span class="Punctuation">(</span><span class="Identifier">a</span> <span class="Operator">+</span> <span class="Identifier">b</span><span class="Punctuation">)</span> <span class="Operator">==</span> <span class="DecNumber">74</span> <span class="Comment"># sum of all members</span></pre>
<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">sequtils</span>
<span class="Keyword">from</span> <span class="Identifier">std</span><span class="Operator">/</span><span class="Identifier">strutils</span> <span class="Keyword">import</span> <span class="Identifier">join</span>
<span class="Keyword">let</span>
<span class="Identifier">vowels</span> <span class="Operator">=</span> <span class="Operator">@</span><span class="StringLit">&quot;aeiou&quot;</span>
<span class="Identifier">foo</span> <span class="Operator">=</span> <span class="StringLit">&quot;sequtils is an awesome module&quot;</span>
<span class="Identifier">doAssert</span> <span class="Punctuation">(</span><span class="Identifier">vowels</span> <span class="Keyword">is</span> <span class="Identifier">seq</span><span class="Punctuation">[</span><span class="Identifier">char</span><span class="Punctuation">]</span><span class="Punctuation">)</span> <span class="Keyword">and</span> <span class="Punctuation">(</span><span class="Identifier">vowels</span> <span class="Operator">==</span> <span class="Operator">@</span><span class="Punctuation">[</span><span class="Punctuation">'</span><span class="Identifier">a</span><span class="CharLit">', '</span><span class="Identifier">e</span><span class="CharLit">', '</span><span class="Identifier">i</span><span class="CharLit">', '</span><span class="Identifier">o</span><span class="CharLit">', '</span><span class="Identifier">u</span><span class="CharLit">'])</span>
<span class="Identifier">doAssert</span> <span class="Identifier">foo</span><span class="Operator">.</span><span class="Identifier">filterIt</span><span class="Punctuation">(</span><span class="Identifier">it</span> <span class="Keyword">notin</span> <span class="Identifier">vowels</span><span class="Punctuation">)</span><span class="Operator">.</span><span class="Identifier">join</span> <span class="Operator">==</span> <span class="StringLit">&quot;sqtls s n wsm mdl&quot;</span></pre>
<h1><a class="toc-backref" id="see-also" href="#see-also">See also</a></h1><ul class="simple"><li><a class="reference external" href="strutils.html">strutils module</a> for common string functions</li>
<li><a class="reference external" href="sugar.html">sugar module</a> for syntactic sugar macros</li>
<li><a class="reference external" href="algorithm.html">algorithm module</a> for common generic algorithms</li>
<li><a class="reference external" href="json.html">json module</a> for a structure which allows heterogeneous members</li>
</ul>
</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="typetraits.html">typetraits</a>, <a class="reference external" href="assertions.html">assertions</a>
</dl>
</div>
<div class="section" id="12">
<h1><a class="toc-backref" href="#12">Procs</a></h1>
<dl class="item">
<div id="addUnique-procs-all">
<div id="addUnique,seq[T],sinkT">
<dt><pre><span class="Keyword">func</span> <a href="#addUnique%2Cseq%5BT%5D%2CsinkT"><span class="Identifier">addUnique</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">]</span><span class="Other">(</span><span class="Identifier">s</span><span class="Other">:</span> <span class="Keyword">var</span> <a href="system.html#seq"><span class="Identifier">seq</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">]</span><span class="Other">;</span> <span class="Identifier">x</span><span class="Other">:</span> <a href="system.html#sink"><span class="Identifier">sink</span></a> <span class="Identifier">T</span><span class="Other">)</span></pre></dt>
<dd>
Adds <tt class="docutils literal"><span class="pre"><span class="Identifier">x</span></span></tt> to the container <tt class="docutils literal"><span class="pre"><span class="Identifier">s</span></span></tt> if it is not already present. Uses <tt class="docutils literal"><span class="pre"><span class="Operator">==</span></span></tt> to check if the item is already present.
<p><strong class="examples_text">Example:</strong></p>
<pre class="listing"><span class="Keyword">var</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="Identifier">a</span><span class="Operator">.</span><span class="Identifier">addUnique</span><span class="Punctuation">(</span><span class="DecNumber">4</span><span class="Punctuation">)</span>
<span class="Identifier">a</span><span class="Operator">.</span><span class="Identifier">addUnique</span><span class="Punctuation">(</span><span class="DecNumber">4</span><span class="Punctuation">)</span>
<span class="Identifier">assert</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></pre>
<a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/collections/sequtils.nim#L149" class="link-seesrc" target="_blank">Source</a>&nbsp;&nbsp;
<a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/collections/sequtils.nim#L149" class="link-seesrc" target="_blank" >Edit</a>&nbsp;&nbsp;
</dd>
</div>
</div>
<div id="all-procs-all">
<div id="all,openArray[T],proc(T)">
<dt><pre><span class="Keyword">proc</span> <a href="#all%2CopenArray%5BT%5D%2Cproc%28T%29"><span class="Identifier">all</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">]</span><span class="Other">(</span><span class="Identifier">s</span><span class="Other">:</span> <a href="system.html#openArray"><span class="Identifier">openArray</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">]</span><span class="Other">;</span> <span class="Identifier">pred</span><span class="Other">:</span> <span class="Keyword">proc</span> <span class="Other">(</span><span class="Identifier">x</span><span class="Other">:</span> <span class="Identifier">T</span><span class="Other">)</span><span class="Other">:</span> <a href="system.html#bool"><span class="Identifier">bool</span></a> {.<span class="Identifier">closure</span>.}<span class="Other">)</span><span class="Other">:</span> <a href="system.html#bool"><span class="Identifier">bool</span></a> {.
<span class="Identifier">effectsOf</span><span class="Other">:</span> <span class="Identifier">pred</span>.}</pre></dt>
<dd>
<p>Iterates through a container and checks if every item fulfills the predicate.</p>
<p><strong>See also:</strong></p>
<ul class="simple"><li><a class="reference external" href="#allIt.t,untyped,untyped">allIt template</a></li>
<li><a class="reference external" href="#any,openArray[T],proc(T)">any proc</a></li>
</ul>
<p><strong class="examples_text">Example:</strong></p>
<pre class="listing"><span class="Keyword">let</span> <span class="Identifier">numbers</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">4</span><span class="Punctuation">,</span> <span class="DecNumber">5</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="DecNumber">7</span><span class="Punctuation">,</span> <span class="DecNumber">4</span><span class="Punctuation">]</span>
<span class="Identifier">assert</span> <span class="Identifier">all</span><span class="Punctuation">(</span><span class="Identifier">numbers</span><span class="Punctuation">,</span> <span class="Keyword">proc</span> <span class="Punctuation">(</span><span class="Identifier">x</span><span class="Punctuation">:</span> <span class="Identifier">int</span><span class="Punctuation">)</span><span class="Punctuation">:</span> <span class="Identifier">bool</span> <span class="Operator">=</span> <span class="Identifier">x</span> <span class="Operator">&lt;</span> <span class="DecNumber">10</span><span class="Punctuation">)</span> <span class="Operator">==</span> <span class="Identifier">true</span>
<span class="Identifier">assert</span> <span class="Identifier">all</span><span class="Punctuation">(</span><span class="Identifier">numbers</span><span class="Punctuation">,</span> <span class="Keyword">proc</span> <span class="Punctuation">(</span><span class="Identifier">x</span><span class="Punctuation">:</span> <span class="Identifier">int</span><span class="Punctuation">)</span><span class="Punctuation">:</span> <span class="Identifier">bool</span> <span class="Operator">=</span> <span class="Identifier">x</span> <span class="Operator">&lt;</span> <span class="DecNumber">9</span><span class="Punctuation">)</span> <span class="Operator">==</span> <span class="Identifier">false</span></pre>
<a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/collections/sequtils.nim#L793" class="link-seesrc" target="_blank">Source</a>&nbsp;&nbsp;
<a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/collections/sequtils.nim#L793" class="link-seesrc" target="_blank" >Edit</a>&nbsp;&nbsp;
</dd>
</div>
</div>
<div id="any-procs-all">
<div id="any,openArray[T],proc(T)">
<dt><pre><span class="Keyword">proc</span> <a href="#any%2CopenArray%5BT%5D%2Cproc%28T%29"><span class="Identifier">any</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">]</span><span class="Other">(</span><span class="Identifier">s</span><span class="Other">:</span> <a href="system.html#openArray"><span class="Identifier">openArray</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">]</span><span class="Other">;</span> <span class="Identifier">pred</span><span class="Other">:</span> <span class="Keyword">proc</span> <span class="Other">(</span><span class="Identifier">x</span><span class="Other">:</span> <span class="Identifier">T</span><span class="Other">)</span><span class="Other">:</span> <a href="system.html#bool"><span class="Identifier">bool</span></a> {.<span class="Identifier">closure</span>.}<span class="Other">)</span><span class="Other">:</span> <a href="system.html#bool"><span class="Identifier">bool</span></a> {.
<span class="Identifier">effectsOf</span><span class="Other">:</span> <span class="Identifier">pred</span>.}</pre></dt>
<dd>
<p>Iterates through a container and checks if at least one item fulfills the predicate.</p>
<p><strong>See also:</strong></p>
<ul class="simple"><li><a class="reference external" href="#anyIt.t,untyped,untyped">anyIt template</a></li>
<li><a class="reference external" href="#all,openArray[T],proc(T)">all proc</a></li>
</ul>
<p><strong class="examples_text">Example:</strong></p>
<pre class="listing"><span class="Keyword">let</span> <span class="Identifier">numbers</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">4</span><span class="Punctuation">,</span> <span class="DecNumber">5</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="DecNumber">7</span><span class="Punctuation">,</span> <span class="DecNumber">4</span><span class="Punctuation">]</span>
<span class="Identifier">assert</span> <span class="Identifier">any</span><span class="Punctuation">(</span><span class="Identifier">numbers</span><span class="Punctuation">,</span> <span class="Keyword">proc</span> <span class="Punctuation">(</span><span class="Identifier">x</span><span class="Punctuation">:</span> <span class="Identifier">int</span><span class="Punctuation">)</span><span class="Punctuation">:</span> <span class="Identifier">bool</span> <span class="Operator">=</span> <span class="Identifier">x</span> <span class="Operator">&gt;</span> <span class="DecNumber">8</span><span class="Punctuation">)</span> <span class="Operator">==</span> <span class="Identifier">true</span>
<span class="Identifier">assert</span> <span class="Identifier">any</span><span class="Punctuation">(</span><span class="Identifier">numbers</span><span class="Punctuation">,</span> <span class="Keyword">proc</span> <span class="Punctuation">(</span><span class="Identifier">x</span><span class="Punctuation">:</span> <span class="Identifier">int</span><span class="Punctuation">)</span><span class="Punctuation">:</span> <span class="Identifier">bool</span> <span class="Operator">=</span> <span class="Identifier">x</span> <span class="Operator">&gt;</span> <span class="DecNumber">9</span><span class="Punctuation">)</span> <span class="Operator">==</span> <span class="Identifier">false</span></pre>
<a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/collections/sequtils.nim#L835" class="link-seesrc" target="_blank">Source</a>&nbsp;&nbsp;
<a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/collections/sequtils.nim#L835" class="link-seesrc" target="_blank" >Edit</a>&nbsp;&nbsp;
</dd>
</div>
</div>
<div id="apply-procs-all">
<div id="apply,openArray[T],proc(T)_3">
<dt><pre><span class="Keyword">proc</span> <a href="#apply%2CopenArray%5BT%5D%2Cproc%28T%29_3"><span class="Identifier">apply</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">]</span><span class="Other">(</span><span class="Identifier">s</span><span class="Other">:</span> <a href="system.html#openArray"><span class="Identifier">openArray</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">]</span><span class="Other">;</span> <span class="Identifier">op</span><span class="Other">:</span> <span class="Keyword">proc</span> <span class="Other">(</span><span class="Identifier">x</span><span class="Other">:</span> <span class="Identifier">T</span><span class="Other">)</span> {.<span class="Identifier">closure</span>.}<span class="Other">)</span> {.<span class="Identifier">inline</span><span class="Other">,</span>
<span class="Identifier">effectsOf</span><span class="Other">:</span> <span class="Identifier">op</span>.}</pre></dt>
<dd>
Same as <tt class="docutils literal"><span class="pre"><span class="Identifier">apply</span></span></tt> but for a proc that does not return anything and does not mutate <tt class="docutils literal"><span class="pre"><span class="Identifier">s</span></span></tt> directly.
<p><strong class="examples_text">Example:</strong></p>
<pre class="listing"><span class="Keyword">var</span> <span class="Identifier">message</span><span class="Punctuation">:</span> <span class="Identifier">string</span>
<span class="Identifier">apply</span><span class="Punctuation">(</span><span class="Punctuation">[</span><span class="DecNumber">0</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="Punctuation">,</span> <span class="Keyword">proc</span><span class="Punctuation">(</span><span class="Identifier">item</span><span class="Punctuation">:</span> <span class="Identifier">int</span><span class="Punctuation">)</span> <span class="Operator">=</span> <span class="Identifier">message</span><span class="Operator">.</span><span class="Identifier">addInt</span> <span class="Identifier">item</span><span class="Punctuation">)</span>
<span class="Identifier">assert</span> <span class="Identifier">message</span> <span class="Operator">==</span> <span class="StringLit">&quot;01234&quot;</span></pre>
<a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/collections/sequtils.nim#L528" class="link-seesrc" target="_blank">Source</a>&nbsp;&nbsp;
<a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/collections/sequtils.nim#L528" class="link-seesrc" target="_blank" >Edit</a>&nbsp;&nbsp;
</dd>
</div>
<div id="apply,openArray[T],proc(T)_2">
<dt><pre><span class="Keyword">proc</span> <a href="#apply%2CopenArray%5BT%5D%2Cproc%28T%29_2"><span class="Identifier">apply</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">]</span><span class="Other">(</span><span class="Identifier">s</span><span class="Other">:</span> <span class="Keyword">var</span> <a href="system.html#openArray"><span class="Identifier">openArray</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">]</span><span class="Other">;</span> <span class="Identifier">op</span><span class="Other">:</span> <span class="Keyword">proc</span> <span class="Other">(</span><span class="Identifier">x</span><span class="Other">:</span> <span class="Identifier">T</span><span class="Other">)</span><span class="Other">:</span> <span class="Identifier">T</span> {.<span class="Identifier">closure</span>.}<span class="Other">)</span> {.<span class="Identifier">inline</span><span class="Other">,</span>
<span class="Identifier">effectsOf</span><span class="Other">:</span> <span class="Identifier">op</span>.}</pre></dt>
<dd>
<p>Applies <tt class="docutils literal"><span class="pre"><span class="Identifier">op</span></span></tt> to every item in <tt class="docutils literal"><span class="pre"><span class="Identifier">s</span></span></tt> modifying it directly.</p>
<p>Note that the container <tt class="docutils literal"><span class="pre"><span class="Identifier">s</span></span></tt> must be declared as a <tt class="docutils literal"><span class="pre"><span class="Keyword">var</span></span></tt> and it is required for your input and output types to be the same, since <tt class="docutils literal"><span class="pre"><span class="Identifier">s</span></span></tt> is modified in-place. The parameter function takes and returns a <tt class="docutils literal"><span class="pre"><span class="Identifier">T</span></span></tt> type variable.</p>
<p><strong>See also:</strong></p>
<ul class="simple"><li><a class="reference external" href="#applyIt.t,untyped,untyped">applyIt template</a></li>
<li><a class="reference external" href="#map,openArray[T],proc(T)">map proc</a></li>
</ul>
<p><strong class="examples_text">Example:</strong></p>
<pre class="listing"><span class="Keyword">var</span> <span class="Identifier">a</span> <span class="Operator">=</span> <span class="Operator">@</span><span class="Punctuation">[</span><span class="StringLit">&quot;1&quot;</span><span class="Punctuation">,</span> <span class="StringLit">&quot;2&quot;</span><span class="Punctuation">,</span> <span class="StringLit">&quot;3&quot;</span><span class="Punctuation">,</span> <span class="StringLit">&quot;4&quot;</span><span class="Punctuation">]</span>
<span class="Identifier">apply</span><span class="Punctuation">(</span><span class="Identifier">a</span><span class="Punctuation">,</span> <span class="Keyword">proc</span><span class="Punctuation">(</span><span class="Identifier">x</span><span class="Punctuation">:</span> <span class="Identifier">string</span><span class="Punctuation">)</span><span class="Punctuation">:</span> <span class="Identifier">string</span> <span class="Operator">=</span> <span class="Identifier">x</span> <span class="Operator">&amp;</span> <span class="StringLit">&quot;42&quot;</span><span class="Punctuation">)</span>
<span class="Identifier">assert</span> <span class="Identifier">a</span> <span class="Operator">==</span> <span class="Operator">@</span><span class="Punctuation">[</span><span class="StringLit">&quot;142&quot;</span><span class="Punctuation">,</span> <span class="StringLit">&quot;242&quot;</span><span class="Punctuation">,</span> <span class="StringLit">&quot;342&quot;</span><span class="Punctuation">,</span> <span class="StringLit">&quot;442&quot;</span><span class="Punctuation">]</span></pre>
<a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/collections/sequtils.nim#L508" class="link-seesrc" target="_blank">Source</a>&nbsp;&nbsp;
<a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/collections/sequtils.nim#L508" class="link-seesrc" target="_blank" >Edit</a>&nbsp;&nbsp;
</dd>
</div>
<div id="apply,openArray[T],proc(T)">
<dt><pre><span class="Keyword">proc</span> <a href="#apply%2CopenArray%5BT%5D%2Cproc%28T%29"><span class="Identifier">apply</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">]</span><span class="Other">(</span><span class="Identifier">s</span><span class="Other">:</span> <span class="Keyword">var</span> <a href="system.html#openArray"><span class="Identifier">openArray</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">]</span><span class="Other">;</span> <span class="Identifier">op</span><span class="Other">:</span> <span class="Keyword">proc</span> <span class="Other">(</span><span class="Identifier">x</span><span class="Other">:</span> <span class="Keyword">var</span> <span class="Identifier">T</span><span class="Other">)</span> {.<span class="Identifier">closure</span>.}<span class="Other">)</span> {.<span class="Identifier">inline</span><span class="Other">,</span>
<span class="Identifier">effectsOf</span><span class="Other">:</span> <span class="Identifier">op</span>.}</pre></dt>
<dd>
<p>Applies <tt class="docutils literal"><span class="pre"><span class="Identifier">op</span></span></tt> to every item in <tt class="docutils literal"><span class="pre"><span class="Identifier">s</span></span></tt>, modifying it directly.</p>
<p>Note that the container <tt class="docutils literal"><span class="pre"><span class="Identifier">s</span></span></tt> must be declared as a <tt class="docutils literal"><span class="pre"><span class="Keyword">var</span></span></tt>, since <tt class="docutils literal"><span class="pre"><span class="Identifier">s</span></span></tt> is modified in-place. The parameter function takes a <tt class="docutils literal"><span class="pre"><span class="Keyword">var</span> <span class="Identifier">T</span></span></tt> type parameter.</p>
<p><strong>See also:</strong></p>
<ul class="simple"><li><a class="reference external" href="#applyIt.t,untyped,untyped">applyIt template</a></li>
<li><a class="reference external" href="#map,openArray[T],proc(T)">map proc</a></li>
</ul>
<p><strong class="examples_text">Example:</strong></p>
<pre class="listing"><span class="Keyword">var</span> <span class="Identifier">a</span> <span class="Operator">=</span> <span class="Operator">@</span><span class="Punctuation">[</span><span class="StringLit">&quot;1&quot;</span><span class="Punctuation">,</span> <span class="StringLit">&quot;2&quot;</span><span class="Punctuation">,</span> <span class="StringLit">&quot;3&quot;</span><span class="Punctuation">,</span> <span class="StringLit">&quot;4&quot;</span><span class="Punctuation">]</span>
<span class="Identifier">apply</span><span class="Punctuation">(</span><span class="Identifier">a</span><span class="Punctuation">,</span> <span class="Keyword">proc</span><span class="Punctuation">(</span><span class="Identifier">x</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="Identifier">x</span> <span class="Operator">&amp;=</span> <span class="StringLit">&quot;42&quot;</span><span class="Punctuation">)</span>
<span class="Identifier">assert</span> <span class="Identifier">a</span> <span class="Operator">==</span> <span class="Operator">@</span><span class="Punctuation">[</span><span class="StringLit">&quot;142&quot;</span><span class="Punctuation">,</span> <span class="StringLit">&quot;242&quot;</span><span class="Punctuation">,</span> <span class="StringLit">&quot;342&quot;</span><span class="Punctuation">,</span> <span class="StringLit">&quot;442&quot;</span><span class="Punctuation">]</span></pre>
<a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/collections/sequtils.nim#L489" class="link-seesrc" target="_blank">Source</a>&nbsp;&nbsp;
<a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/collections/sequtils.nim#L489" class="link-seesrc" target="_blank" >Edit</a>&nbsp;&nbsp;
</dd>
</div>
</div>
<div id="concat-procs-all">
<div id="concat,varargs[seq[T]]">
<dt><pre><span class="Keyword">func</span> <a href="#concat%2Cvarargs%5Bseq%5BT%5D%5D"><span class="Identifier">concat</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">]</span><span class="Other">(</span><span class="Identifier">seqs</span><span class="Other">:</span> <a href="system.html#varargs"><span class="Identifier">varargs</span></a><span class="Other">[</span><a href="system.html#seq"><span class="Identifier">seq</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">]</span><span class="Other">]</span><span class="Other">)</span><span class="Other">:</span> <a href="system.html#seq"><span class="Identifier">seq</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">]</span></pre></dt>
<dd>
<p>Takes several sequences' items and returns them inside a new sequence. All sequences must be of the same type.</p>
<p><strong>See also:</strong></p>
<ul class="simple"><li><a class="reference external" href="#distribute,seq[T],Positive">distribute func</a> for a reverse operation</li>
</ul>
<p><strong class="examples_text">Example:</strong></p>
<pre class="listing"><span class="Keyword">let</span>
<span class="Identifier">s1</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="Identifier">s2</span> <span class="Operator">=</span> <span class="Operator">@</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="Identifier">s3</span> <span class="Operator">=</span> <span class="Operator">@</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="Identifier">total</span> <span class="Operator">=</span> <span class="Identifier">concat</span><span class="Punctuation">(</span><span class="Identifier">s1</span><span class="Punctuation">,</span> <span class="Identifier">s2</span><span class="Punctuation">,</span> <span class="Identifier">s3</span><span class="Punctuation">)</span>
<span class="Identifier">assert</span> <span class="Identifier">total</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></pre>
<a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/collections/sequtils.nim#L124" class="link-seesrc" target="_blank">Source</a>&nbsp;&nbsp;
<a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/collections/sequtils.nim#L124" class="link-seesrc" target="_blank" >Edit</a>&nbsp;&nbsp;
</dd>
</div>
</div>
<div id="count-procs-all">
<div id="count,openArray[T],T">
<dt><pre><span class="Keyword">func</span> <a href="#count%2CopenArray%5BT%5D%2CT"><span class="Identifier">count</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">]</span><span class="Other">(</span><span class="Identifier">s</span><span class="Other">:</span> <a href="system.html#openArray"><span class="Identifier">openArray</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">]</span><span class="Other">;</span> <span class="Identifier">x</span><span class="Other">:</span> <span class="Identifier">T</span><span class="Other">)</span><span class="Other">:</span> <a href="system.html#int"><span class="Identifier">int</span></a></pre></dt>
<dd>
Returns the number of occurrences of the item <tt class="docutils literal"><span class="pre"><span class="Identifier">x</span></span></tt> in the container <tt class="docutils literal"><span class="pre"><span class="Identifier">s</span></span></tt>.
<p><strong class="examples_text">Example:</strong></p>
<pre class="listing"><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">2</span><span class="Punctuation">,</span> <span class="DecNumber">3</span><span class="Punctuation">,</span> <span class="DecNumber">2</span><span class="Punctuation">,</span> <span class="DecNumber">4</span><span class="Punctuation">,</span> <span class="DecNumber">2</span><span class="Punctuation">]</span>
<span class="Identifier">b</span> <span class="Operator">=</span> <span class="StringLit">&quot;abracadabra&quot;</span>
<span class="Identifier">assert</span> <span class="Identifier">count</span><span class="Punctuation">(</span><span class="Identifier">a</span><span class="Punctuation">,</span> <span class="DecNumber">2</span><span class="Punctuation">)</span> <span class="Operator">==</span> <span class="DecNumber">4</span>
<span class="Identifier">assert</span> <span class="Identifier">count</span><span class="Punctuation">(</span><span class="Identifier">a</span><span class="Punctuation">,</span> <span class="DecNumber">99</span><span class="Punctuation">)</span> <span class="Operator">==</span> <span class="DecNumber">0</span>
<span class="Identifier">assert</span> <span class="Identifier">count</span><span class="Punctuation">(</span><span class="Identifier">b</span><span class="Punctuation">,</span> <span class="CharLit">'r'</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/collections/sequtils.nim#L165" class="link-seesrc" target="_blank">Source</a>&nbsp;&nbsp;
<a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/collections/sequtils.nim#L165" class="link-seesrc" target="_blank" >Edit</a>&nbsp;&nbsp;
</dd>
</div>
</div>
<div id="cycle-procs-all">
<div id="cycle,openArray[T],Natural">
<dt><pre><span class="Keyword">func</span> <a href="#cycle%2CopenArray%5BT%5D%2CNatural"><span class="Identifier">cycle</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">]</span><span class="Other">(</span><span class="Identifier">s</span><span class="Other">:</span> <a href="system.html#openArray"><span class="Identifier">openArray</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">]</span><span class="Other">;</span> <span class="Identifier">n</span><span class="Other">:</span> <a href="system.html#Natural"><span class="Identifier">Natural</span></a><span class="Other">)</span><span class="Other">:</span> <a href="system.html#seq"><span class="Identifier">seq</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">]</span></pre></dt>
<dd>
Returns a new sequence with the items of the container <tt class="docutils literal"><span class="pre"><span class="Identifier">s</span></span></tt> repeated <tt class="docutils literal"><span class="pre"><span class="Identifier">n</span></span></tt> times. <tt class="docutils literal"><span class="pre"><span class="Identifier">n</span></span></tt> must be a non-negative number (zero or more).
<p><strong class="examples_text">Example:</strong></p>
<pre class="listing"><span class="Keyword">let</span>
<span class="Identifier">s</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="Identifier">total</span> <span class="Operator">=</span> <span class="Identifier">s</span><span class="Operator">.</span><span class="Identifier">cycle</span><span class="Punctuation">(</span><span class="DecNumber">3</span><span class="Punctuation">)</span>
<span class="Identifier">assert</span> <span class="Identifier">total</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">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">1</span><span class="Punctuation">,</span> <span class="DecNumber">2</span><span class="Punctuation">,</span> <span class="DecNumber">3</span><span class="Punctuation">]</span></pre>
<a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/collections/sequtils.nim#L180" class="link-seesrc" target="_blank">Source</a>&nbsp;&nbsp;
<a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/collections/sequtils.nim#L180" class="link-seesrc" target="_blank" >Edit</a>&nbsp;&nbsp;
</dd>
</div>
</div>
<div id="deduplicate-procs-all">
<div id="deduplicate,openArray[T],bool">
<dt><pre><span class="Keyword">func</span> <a href="#deduplicate%2CopenArray%5BT%5D%2Cbool"><span class="Identifier">deduplicate</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">]</span><span class="Other">(</span><span class="Identifier">s</span><span class="Other">:</span> <a href="system.html#openArray"><span class="Identifier">openArray</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">]</span><span class="Other">;</span> <span class="Identifier">isSorted</span><span class="Other">:</span> <a href="system.html#bool"><span class="Identifier">bool</span></a> <span class="Other">=</span> <span class="Identifier">false</span><span class="Other">)</span><span class="Other">:</span> <a href="system.html#seq"><span class="Identifier">seq</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">]</span></pre></dt>
<dd>
<p>Returns a new sequence without duplicates.</p>
<p>Setting the optional argument <tt class="docutils literal"><span class="pre"><span class="Identifier">isSorted</span></span></tt> to true (default: false) uses a faster algorithm for deduplication. </p>
<p><strong class="examples_text">Example:</strong></p>
<pre class="listing"><span class="Keyword">let</span>
<span class="Identifier">dup1</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">1</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">2</span><span class="Punctuation">,</span> <span class="DecNumber">2</span><span class="Punctuation">,</span> <span class="DecNumber">8</span><span class="Punctuation">,</span> <span class="DecNumber">1</span><span class="Punctuation">,</span> <span class="DecNumber">4</span><span class="Punctuation">]</span>
<span class="Identifier">dup2</span> <span class="Operator">=</span> <span class="Operator">@</span><span class="Punctuation">[</span><span class="StringLit">&quot;a&quot;</span><span class="Punctuation">,</span> <span class="StringLit">&quot;a&quot;</span><span class="Punctuation">,</span> <span class="StringLit">&quot;c&quot;</span><span class="Punctuation">,</span> <span class="StringLit">&quot;d&quot;</span><span class="Punctuation">,</span> <span class="StringLit">&quot;d&quot;</span><span class="Punctuation">]</span>
<span class="Identifier">unique1</span> <span class="Operator">=</span> <span class="Identifier">deduplicate</span><span class="Punctuation">(</span><span class="Identifier">dup1</span><span class="Punctuation">)</span>
<span class="Identifier">unique2</span> <span class="Operator">=</span> <span class="Identifier">deduplicate</span><span class="Punctuation">(</span><span class="Identifier">dup2</span><span class="Punctuation">,</span> <span class="Identifier">isSorted</span> <span class="Operator">=</span> <span class="Identifier">true</span><span class="Punctuation">)</span>
<span class="Identifier">assert</span> <span class="Identifier">unique1</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">3</span><span class="Punctuation">,</span> <span class="DecNumber">4</span><span class="Punctuation">,</span> <span class="DecNumber">2</span><span class="Punctuation">,</span> <span class="DecNumber">8</span><span class="Punctuation">]</span>
<span class="Identifier">assert</span> <span class="Identifier">unique2</span> <span class="Operator">==</span> <span class="Operator">@</span><span class="Punctuation">[</span><span class="StringLit">&quot;a&quot;</span><span class="Punctuation">,</span> <span class="StringLit">&quot;c&quot;</span><span class="Punctuation">,</span> <span class="StringLit">&quot;d&quot;</span><span class="Punctuation">]</span></pre>
<a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/collections/sequtils.nim#L211" class="link-seesrc" target="_blank">Source</a>&nbsp;&nbsp;
<a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/collections/sequtils.nim#L211" class="link-seesrc" target="_blank" >Edit</a>&nbsp;&nbsp;
</dd>
</div>
</div>
<div id="delete-procs-all">
<div id="delete,seq[T],Natural,Natural">
<dt><pre><span class="Keyword">func</span> <a href="#delete%2Cseq%5BT%5D%2CNatural%2CNatural"><span class="Identifier">delete</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">]</span><span class="Other">(</span><span class="Identifier">s</span><span class="Other">:</span> <span class="Keyword">var</span> <a href="system.html#seq"><span class="Identifier">seq</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">]</span><span class="Other">;</span> <span class="Identifier">first</span><span class="Other">,</span> <span class="Identifier">last</span><span class="Other">:</span> <a href="system.html#Natural"><span class="Identifier">Natural</span></a><span class="Other">)</span> {.
<span><span class="Other pragmadots">...</span></span><span class="pragmawrap"><span class="Identifier">deprecated</span><span class="Other">:</span> <span class="StringLit">&quot;use `delete(s, first..last)`&quot;</span></span>.}</pre></dt>
<dd>
<div class="deprecation-message">
<b>Deprecated:</b> use `delete(s, first..last)`
</div>
Deletes the items of a sequence <tt class="docutils literal"><span class="pre"><span class="Identifier">s</span></span></tt> at positions <tt class="docutils literal"><span class="pre"><span class="Identifier">first</span><span class="Operator">..</span><span class="Identifier">last</span></span></tt> (including both ends of the range). This modifies <tt class="docutils literal"><span class="pre"><span class="Identifier">s</span></span></tt> itself, it does not return a copy.
<p><strong class="examples_text">Example: cmd: --warning:deprecated:off</strong></p>
<pre class="listing"><span class="Keyword">let</span> <span class="Identifier">outcome</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">1</span><span class="Punctuation">,</span> <span class="DecNumber">1</span><span class="Punctuation">,</span> <span class="DecNumber">1</span><span class="Punctuation">,</span> <span class="DecNumber">1</span><span class="Punctuation">,</span> <span class="DecNumber">1</span><span class="Punctuation">,</span> <span class="DecNumber">1</span><span class="Punctuation">,</span> <span class="DecNumber">1</span><span class="Punctuation">]</span>
<span class="Keyword">var</span> <span class="Identifier">dest</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">1</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">2</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="DecNumber">2</span><span class="Punctuation">,</span> <span class="DecNumber">2</span><span class="Punctuation">,</span> <span class="DecNumber">1</span><span class="Punctuation">,</span> <span class="DecNumber">1</span><span class="Punctuation">,</span> <span class="DecNumber">1</span><span class="Punctuation">,</span> <span class="DecNumber">1</span><span class="Punctuation">,</span> <span class="DecNumber">1</span><span class="Punctuation">]</span>
<span class="Identifier">dest</span><span class="Operator">.</span><span class="Identifier">delete</span><span class="Punctuation">(</span><span class="DecNumber">3</span><span class="Punctuation">,</span> <span class="DecNumber">8</span><span class="Punctuation">)</span>
<span class="Identifier">assert</span> <span class="Identifier">outcome</span> <span class="Operator">==</span> <span class="Identifier">dest</span></pre>
<a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/collections/sequtils.nim#L657" class="link-seesrc" target="_blank">Source</a>&nbsp;&nbsp;
<a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/collections/sequtils.nim#L657" class="link-seesrc" target="_blank" >Edit</a>&nbsp;&nbsp;
</dd>
</div>
<div id="delete,seq[T],Slice[int]">
<dt><pre><span class="Keyword">func</span> <a href="#delete%2Cseq%5BT%5D%2CSlice%5Bint%5D"><span class="Identifier">delete</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">]</span><span class="Other">(</span><span class="Identifier">s</span><span class="Other">:</span> <span class="Keyword">var</span> <a href="system.html#seq"><span class="Identifier">seq</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">]</span><span class="Other">;</span> <span class="Identifier">slice</span><span class="Other">:</span> <a href="system.html#Slice"><span class="Identifier">Slice</span></a><span class="Other">[</span><a href="system.html#int"><span class="Identifier">int</span></a><span class="Other">]</span><span class="Other">)</span></pre></dt>
<dd>
<p>Deletes the items <tt class="docutils literal"><span class="pre"><span class="Identifier">s</span><span class="Punctuation">[</span><span class="Identifier">slice</span><span class="Punctuation">]</span></span></tt>, raising <tt class="docutils literal"><span class="pre"><span class="Identifier">IndexDefect</span></span></tt> if the slice contains elements out of range.</p>
<p>This operation moves all elements after <tt class="docutils literal"><span class="pre"><span class="Identifier">s</span><span class="Punctuation">[</span><span class="Identifier">slice</span><span class="Punctuation">]</span></span></tt> in linear time.</p>
<p><strong class="examples_text">Example:</strong></p>
<pre class="listing"><span class="Keyword">var</span> <span class="Identifier">a</span> <span class="Operator">=</span> <span class="Operator">@</span><span class="Punctuation">[</span><span class="DecNumber">10</span><span class="Punctuation">,</span> <span class="DecNumber">11</span><span class="Punctuation">,</span> <span class="DecNumber">12</span><span class="Punctuation">,</span> <span class="DecNumber">13</span><span class="Punctuation">,</span> <span class="DecNumber">14</span><span class="Punctuation">]</span>
<span class="Identifier">doAssertRaises</span><span class="Punctuation">(</span><span class="Identifier">IndexDefect</span><span class="Punctuation">)</span><span class="Punctuation">:</span> <span class="Identifier">a</span><span class="Operator">.</span><span class="Identifier">delete</span><span class="Punctuation">(</span><span class="FloatNumber">4.</span><span class="Operator">.</span><span class="DecNumber">5</span><span class="Punctuation">)</span>
<span class="Identifier">assert</span> <span class="Identifier">a</span> <span class="Operator">==</span> <span class="Operator">@</span><span class="Punctuation">[</span><span class="DecNumber">10</span><span class="Punctuation">,</span> <span class="DecNumber">11</span><span class="Punctuation">,</span> <span class="DecNumber">12</span><span class="Punctuation">,</span> <span class="DecNumber">13</span><span class="Punctuation">,</span> <span class="DecNumber">14</span><span class="Punctuation">]</span>
<span class="Identifier">a</span><span class="Operator">.</span><span class="Identifier">delete</span><span class="Punctuation">(</span><span class="FloatNumber">4.</span><span class="Operator">.</span><span class="DecNumber">4</span><span class="Punctuation">)</span>
<span class="Identifier">assert</span> <span class="Identifier">a</span> <span class="Operator">==</span> <span class="Operator">@</span><span class="Punctuation">[</span><span class="DecNumber">10</span><span class="Punctuation">,</span> <span class="DecNumber">11</span><span class="Punctuation">,</span> <span class="DecNumber">12</span><span class="Punctuation">,</span> <span class="DecNumber">13</span><span class="Punctuation">]</span>
<span class="Identifier">a</span><span class="Operator">.</span><span class="Identifier">delete</span><span class="Punctuation">(</span><span class="FloatNumber">1.</span><span class="Operator">.</span><span class="DecNumber">2</span><span class="Punctuation">)</span>
<span class="Identifier">assert</span> <span class="Identifier">a</span> <span class="Operator">==</span> <span class="Operator">@</span><span class="Punctuation">[</span><span class="DecNumber">10</span><span class="Punctuation">,</span> <span class="DecNumber">13</span><span class="Punctuation">]</span>
<span class="Identifier">a</span><span class="Operator">.</span><span class="Identifier">delete</span><span class="Punctuation">(</span><span class="FloatNumber">1.</span><span class="Operator">.&lt;</span><span class="DecNumber">1</span><span class="Punctuation">)</span> <span class="Comment"># empty slice</span>
<span class="Identifier">assert</span> <span class="Identifier">a</span> <span class="Operator">==</span> <span class="Operator">@</span><span class="Punctuation">[</span><span class="DecNumber">10</span><span class="Punctuation">,</span> <span class="DecNumber">13</span><span class="Punctuation">]</span></pre>
<a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/collections/sequtils.nim#L617" class="link-seesrc" target="_blank">Source</a>&nbsp;&nbsp;
<a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/collections/sequtils.nim#L617" class="link-seesrc" target="_blank" >Edit</a>&nbsp;&nbsp;
</dd>
</div>
</div>
<div id="distribute-procs-all">
<div id="distribute,seq[T],Positive">
<dt><pre><span class="Keyword">func</span> <a href="#distribute%2Cseq%5BT%5D%2CPositive"><span class="Identifier">distribute</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">]</span><span class="Other">(</span><span class="Identifier">s</span><span class="Other">:</span> <a href="system.html#seq"><span class="Identifier">seq</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">]</span><span class="Other">;</span> <span class="Identifier">num</span><span class="Other">:</span> <a href="system.html#Positive"><span class="Identifier">Positive</span></a><span class="Other">;</span> <span class="Identifier">spread</span> <span class="Other">=</span> <span class="Identifier">true</span><span class="Other">)</span><span class="Other">:</span> <a href="system.html#seq"><span class="Identifier">seq</span></a><span class="Other">[</span><a href="system.html#seq"><span class="Identifier">seq</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">]</span><span class="Other">]</span></pre></dt>
<dd>
<p>Splits and distributes a sequence <tt class="docutils literal"><span class="pre"><span class="Identifier">s</span></span></tt> into <tt class="docutils literal"><span class="pre"><span class="Identifier">num</span></span></tt> sub-sequences.</p>
<p>Returns a sequence of <tt class="docutils literal"><span class="pre"><span class="Identifier">num</span></span></tt> sequences. For <em>some</em> input values this is the inverse of the <a class="reference external" href="#concat,varargs[seq[T]]">concat</a> func. The input sequence <tt class="docutils literal"><span class="pre"><span class="Identifier">s</span></span></tt> can be empty, which will produce <tt class="docutils literal"><span class="pre"><span class="Identifier">num</span></span></tt> empty sequences.</p>
<p>If <tt class="docutils literal"><span class="pre"><span class="Identifier">spread</span></span></tt> is false and the length of <tt class="docutils literal"><span class="pre"><span class="Identifier">s</span></span></tt> is not a multiple of <tt class="docutils literal"><span class="pre"><span class="Identifier">num</span></span></tt>, the func will max out the first sub-sequence with <tt class="docutils literal"><span class="pre"><span class="DecNumber">1</span> <span class="Operator">+</span> <span class="Identifier">len</span><span class="Punctuation">(</span><span class="Identifier">s</span><span class="Punctuation">)</span> <span class="Keyword">div</span> <span class="Identifier">num</span></span></tt> entries, leaving the remainder of elements to the last sequence.</p>
<p>On the other hand, if <tt class="docutils literal"><span class="pre"><span class="Identifier">spread</span></span></tt> is true, the func will distribute evenly the remainder of the division across all sequences, which makes the result more suited to multithreading where you are passing equal sized work units to a thread pool and want to maximize core usage. </p>
<p><strong class="examples_text">Example:</strong></p>
<pre class="listing"><span class="Keyword">let</span> <span class="Identifier">numbers</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="Identifier">assert</span> <span class="Identifier">numbers</span><span class="Operator">.</span><span class="Identifier">distribute</span><span class="Punctuation">(</span><span class="DecNumber">3</span><span class="Punctuation">)</span> <span class="Operator">==</span> <span class="Operator">@</span><span class="Punctuation">[</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="Punctuation">,</span> <span class="Operator">@</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="Punctuation">,</span> <span class="Operator">@</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="Punctuation">]</span>
<span class="Identifier">assert</span> <span class="Identifier">numbers</span><span class="Operator">.</span><span class="Identifier">distribute</span><span class="Punctuation">(</span><span class="DecNumber">3</span><span class="Punctuation">,</span> <span class="Identifier">false</span><span class="Punctuation">)</span> <span class="Operator">==</span> <span class="Operator">@</span><span class="Punctuation">[</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="Punctuation">,</span> <span class="Operator">@</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="Punctuation">,</span> <span class="Operator">@</span><span class="Punctuation">[</span><span class="DecNumber">7</span><span class="Punctuation">]</span><span class="Punctuation">]</span>
<span class="Identifier">assert</span> <span class="Identifier">numbers</span><span class="Operator">.</span><span class="Identifier">distribute</span><span class="Punctuation">(</span><span class="DecNumber">6</span><span class="Punctuation">)</span><span class="Punctuation">[</span><span class="DecNumber">0</span><span class="Punctuation">]</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="Identifier">assert</span> <span class="Identifier">numbers</span><span class="Operator">.</span><span class="Identifier">distribute</span><span class="Punctuation">(</span><span class="DecNumber">6</span><span class="Punctuation">)</span><span class="Punctuation">[</span><span class="DecNumber">1</span><span class="Punctuation">]</span> <span class="Operator">==</span> <span class="Operator">@</span><span class="Punctuation">[</span><span class="DecNumber">3</span><span class="Punctuation">]</span></pre>
<a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/collections/sequtils.nim#L407" class="link-seesrc" target="_blank">Source</a>&nbsp;&nbsp;
<a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/collections/sequtils.nim#L407" class="link-seesrc" target="_blank" >Edit</a>&nbsp;&nbsp;
</dd>
</div>
</div>
<div id="filter-procs-all">
<div id="filter,openArray[T],proc(T)">
<dt><pre><span class="Keyword">proc</span> <a href="#filter%2CopenArray%5BT%5D%2Cproc%28T%29"><span class="Identifier">filter</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">]</span><span class="Other">(</span><span class="Identifier">s</span><span class="Other">:</span> <a href="system.html#openArray"><span class="Identifier">openArray</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">]</span><span class="Other">;</span> <span class="Identifier">pred</span><span class="Other">:</span> <span class="Keyword">proc</span> <span class="Other">(</span><span class="Identifier">x</span><span class="Other">:</span> <span class="Identifier">T</span><span class="Other">)</span><span class="Other">:</span> <a href="system.html#bool"><span class="Identifier">bool</span></a> {.<span class="Identifier">closure</span>.}<span class="Other">)</span><span class="Other">:</span> <a href="system.html#seq"><span class="Identifier">seq</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">]</span> {.
<span class="Identifier">inline</span><span class="Other">,</span> <span class="Identifier">effectsOf</span><span class="Other">:</span> <span class="Identifier">pred</span>.}</pre></dt>
<dd>
<p>Returns a new sequence with all the items of <tt class="docutils literal"><span class="pre"><span class="Identifier">s</span></span></tt> that fulfill the predicate <tt class="docutils literal"><span class="pre"><span class="Identifier">pred</span></span></tt> (a function that returns a <tt class="docutils literal"><span class="pre"><span class="Identifier">bool</span></span></tt>).</p>
<p>Instead of using <tt class="docutils literal"><span class="pre"><span class="Identifier">map</span></span></tt> and <tt class="docutils literal"><span class="pre"><span class="Identifier">filter</span></span></tt>, consider using the <tt class="docutils literal"><span class="pre"><span class="Identifier">collect</span></span></tt> macro from the <tt class="docutils literal"><span class="pre"><span class="Identifier">sugar</span></span></tt> module.</p>
<p><strong>See also:</strong></p>
<ul class="simple"><li><a class="reference external" href="sugar.html#collect.m%2Cuntyped%2Cuntyped">sugar.collect macro</a></li>
<li><a class="reference external" href="#filterIt.t,untyped,untyped">filterIt template</a></li>
<li><a class="reference external" href="#filter.i,openArray[T],proc(T)">filter iterator</a></li>
<li><a class="reference external" href="#keepIf,seq[T],proc(T)">keepIf proc</a> for the in-place version</li>
</ul>
<p><strong class="examples_text">Example:</strong></p>
<pre class="listing"><span class="Keyword">let</span>
<span class="Identifier">colors</span> <span class="Operator">=</span> <span class="Operator">@</span><span class="Punctuation">[</span><span class="StringLit">&quot;red&quot;</span><span class="Punctuation">,</span> <span class="StringLit">&quot;yellow&quot;</span><span class="Punctuation">,</span> <span class="StringLit">&quot;black&quot;</span><span class="Punctuation">]</span>
<span class="Identifier">f1</span> <span class="Operator">=</span> <span class="Identifier">filter</span><span class="Punctuation">(</span><span class="Identifier">colors</span><span class="Punctuation">,</span> <span class="Keyword">proc</span><span class="Punctuation">(</span><span class="Identifier">x</span><span class="Punctuation">:</span> <span class="Identifier">string</span><span class="Punctuation">)</span><span class="Punctuation">:</span> <span class="Identifier">bool</span> <span class="Operator">=</span> <span class="Identifier">x</span><span class="Operator">.</span><span class="Identifier">len</span> <span class="Operator">&lt;</span> <span class="DecNumber">6</span><span class="Punctuation">)</span>
<span class="Identifier">f2</span> <span class="Operator">=</span> <span class="Identifier">filter</span><span class="Punctuation">(</span><span class="Identifier">colors</span><span class="Punctuation">,</span> <span class="Keyword">proc</span><span class="Punctuation">(</span><span class="Identifier">x</span><span class="Punctuation">:</span> <span class="Identifier">string</span><span class="Punctuation">)</span><span class="Punctuation">:</span> <span class="Identifier">bool</span> <span class="Operator">=</span> <span class="Identifier">x</span><span class="Operator">.</span><span class="Identifier">contains</span><span class="Punctuation">(</span><span class="Punctuation">'</span><span class="Identifier">y</span><span class="CharLit">'))</span>
<span class="Identifier">assert</span> <span class="Identifier">f1</span> <span class="Operator">==</span> <span class="Operator">@</span><span class="Punctuation">[</span><span class="StringLit">&quot;red&quot;</span><span class="Punctuation">,</span> <span class="StringLit">&quot;black&quot;</span><span class="Punctuation">]</span>
<span class="Identifier">assert</span> <span class="Identifier">f2</span> <span class="Operator">==</span> <span class="Operator">@</span><span class="Punctuation">[</span><span class="StringLit">&quot;yellow&quot;</span><span class="Punctuation">]</span></pre>
<a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/collections/sequtils.nim#L560" class="link-seesrc" target="_blank">Source</a>&nbsp;&nbsp;
<a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/collections/sequtils.nim#L560" class="link-seesrc" target="_blank" >Edit</a>&nbsp;&nbsp;
</dd>
</div>
</div>
<div id="insert-procs-all">
<div id="insert,seq[T],openArray[T],int">
<dt><pre><span class="Keyword">func</span> <a href="#insert%2Cseq%5BT%5D%2CopenArray%5BT%5D%2Cint"><span class="Identifier">insert</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">]</span><span class="Other">(</span><span class="Identifier">dest</span><span class="Other">:</span> <span class="Keyword">var</span> <a href="system.html#seq"><span class="Identifier">seq</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">]</span><span class="Other">;</span> <span class="Identifier">src</span><span class="Other">:</span> <a href="system.html#openArray"><span class="Identifier">openArray</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">]</span><span class="Other">;</span> <span class="Identifier">pos</span> <span class="Other">=</span> <span class="DecNumber">0</span><span class="Other">)</span></pre></dt>
<dd>
<p>Inserts items from <tt class="docutils literal"><span class="pre"><span class="Identifier">src</span></span></tt> into <tt class="docutils literal"><span class="pre"><span class="Identifier">dest</span></span></tt> at position <tt class="docutils literal"><span class="pre"><span class="Identifier">pos</span></span></tt>. This modifies <tt class="docutils literal"><span class="pre"><span class="Identifier">dest</span></span></tt> itself, it does not return a copy.</p>
<p>Note that the elements of <tt class="docutils literal"><span class="pre"><span class="Identifier">src</span></span></tt> and <tt class="docutils literal"><span class="pre"><span class="Identifier">dest</span></span></tt> must be of the same type. </p>
<p><strong class="examples_text">Example:</strong></p>
<pre class="listing"><span class="Keyword">var</span> <span class="Identifier">dest</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">1</span><span class="Punctuation">,</span> <span class="DecNumber">1</span><span class="Punctuation">,</span> <span class="DecNumber">1</span><span class="Punctuation">,</span> <span class="DecNumber">1</span><span class="Punctuation">,</span> <span class="DecNumber">1</span><span class="Punctuation">,</span> <span class="DecNumber">1</span><span class="Punctuation">,</span> <span class="DecNumber">1</span><span class="Punctuation">]</span>
<span class="Keyword">let</span>
<span class="Identifier">src</span> <span class="Operator">=</span> <span class="Operator">@</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="DecNumber">2</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="DecNumber">2</span><span class="Punctuation">]</span>
<span class="Identifier">outcome</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">1</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">2</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="DecNumber">2</span><span class="Punctuation">,</span> <span class="DecNumber">2</span><span class="Punctuation">,</span> <span class="DecNumber">1</span><span class="Punctuation">,</span> <span class="DecNumber">1</span><span class="Punctuation">,</span> <span class="DecNumber">1</span><span class="Punctuation">,</span> <span class="DecNumber">1</span><span class="Punctuation">,</span> <span class="DecNumber">1</span><span class="Punctuation">]</span>
<span class="Identifier">dest</span><span class="Operator">.</span><span class="Identifier">insert</span><span class="Punctuation">(</span><span class="Identifier">src</span><span class="Punctuation">,</span> <span class="DecNumber">3</span><span class="Punctuation">)</span>
<span class="Identifier">assert</span> <span class="Identifier">dest</span> <span class="Operator">==</span> <span class="Identifier">outcome</span></pre>
<a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/collections/sequtils.nim#L681" class="link-seesrc" target="_blank">Source</a>&nbsp;&nbsp;
<a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/collections/sequtils.nim#L681" class="link-seesrc" target="_blank" >Edit</a>&nbsp;&nbsp;
</dd>
</div>
</div>
<div id="keepIf-procs-all">
<div id="keepIf,seq[T],proc(T)">
<dt><pre><span class="Keyword">proc</span> <a href="#keepIf%2Cseq%5BT%5D%2Cproc%28T%29"><span class="Identifier">keepIf</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">]</span><span class="Other">(</span><span class="Identifier">s</span><span class="Other">:</span> <span class="Keyword">var</span> <a href="system.html#seq"><span class="Identifier">seq</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">]</span><span class="Other">;</span> <span class="Identifier">pred</span><span class="Other">:</span> <span class="Keyword">proc</span> <span class="Other">(</span><span class="Identifier">x</span><span class="Other">:</span> <span class="Identifier">T</span><span class="Other">)</span><span class="Other">:</span> <a href="system.html#bool"><span class="Identifier">bool</span></a> {.<span class="Identifier">closure</span>.}<span class="Other">)</span> {.<span class="Identifier">inline</span><span class="Other">,</span>
<span class="Identifier">effectsOf</span><span class="Other">:</span> <span class="Identifier">pred</span>.}</pre></dt>
<dd>
<p>Keeps the items in the passed sequence <tt class="docutils literal"><span class="pre"><span class="Identifier">s</span></span></tt> if they fulfill the predicate <tt class="docutils literal"><span class="pre"><span class="Identifier">pred</span></span></tt> (a function that returns a <tt class="docutils literal"><span class="pre"><span class="Identifier">bool</span></span></tt>).</p>
<p>Note that <tt class="docutils literal"><span class="pre"><span class="Identifier">s</span></span></tt> must be declared as a <tt class="docutils literal"><span class="pre"><span class="Keyword">var</span></span></tt>.</p>
<p>Similar to the <a class="reference external" href="#filter,openArray[T],proc(T)">filter proc</a>, but modifies the sequence directly.</p>
<p><strong>See also:</strong></p>
<ul class="simple"><li><a class="reference external" href="#keepItIf.t,seq,untyped">keepItIf template</a></li>
<li><a class="reference external" href="#filter,openArray[T],proc(T)">filter proc</a></li>
</ul>
<p><strong class="examples_text">Example:</strong></p>
<pre class="listing"><span class="Keyword">var</span> <span class="Identifier">floats</span> <span class="Operator">=</span> <span class="Operator">@</span><span class="Punctuation">[</span><span class="FloatNumber">13.0</span><span class="Punctuation">,</span> <span class="FloatNumber">12.5</span><span class="Punctuation">,</span> <span class="FloatNumber">5.8</span><span class="Punctuation">,</span> <span class="FloatNumber">2.0</span><span class="Punctuation">,</span> <span class="FloatNumber">6.1</span><span class="Punctuation">,</span> <span class="FloatNumber">9.9</span><span class="Punctuation">,</span> <span class="FloatNumber">10.1</span><span class="Punctuation">]</span>
<span class="Identifier">keepIf</span><span class="Punctuation">(</span><span class="Identifier">floats</span><span class="Punctuation">,</span> <span class="Keyword">proc</span><span class="Punctuation">(</span><span class="Identifier">x</span><span class="Punctuation">:</span> <span class="Identifier">float</span><span class="Punctuation">)</span><span class="Punctuation">:</span> <span class="Identifier">bool</span> <span class="Operator">=</span> <span class="Identifier">x</span> <span class="Operator">&gt;</span> <span class="DecNumber">10</span><span class="Punctuation">)</span>
<span class="Identifier">assert</span> <span class="Identifier">floats</span> <span class="Operator">==</span> <span class="Operator">@</span><span class="Punctuation">[</span><span class="FloatNumber">13.0</span><span class="Punctuation">,</span> <span class="FloatNumber">12.5</span><span class="Punctuation">,</span> <span class="FloatNumber">10.1</span><span class="Punctuation">]</span></pre>
<a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/collections/sequtils.nim#L587" class="link-seesrc" target="_blank">Source</a>&nbsp;&nbsp;
<a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/collections/sequtils.nim#L587" class="link-seesrc" target="_blank" >Edit</a>&nbsp;&nbsp;
</dd>
</div>
</div>
<div id="map-procs-all">
<div id="map,openArray[T],proc(T)">
<dt><pre><span class="Keyword">proc</span> <a href="#map%2CopenArray%5BT%5D%2Cproc%28T%29"><span class="Identifier">map</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">,</span> <span class="Identifier">S</span><span class="Other">]</span><span class="Other">(</span><span class="Identifier">s</span><span class="Other">:</span> <a href="system.html#openArray"><span class="Identifier">openArray</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">]</span><span class="Other">;</span> <span class="Identifier">op</span><span class="Other">:</span> <span class="Keyword">proc</span> <span class="Other">(</span><span class="Identifier">x</span><span class="Other">:</span> <span class="Identifier">T</span><span class="Other">)</span><span class="Other">:</span> <span class="Identifier">S</span> {.<span class="Identifier">closure</span>.}<span class="Other">)</span><span class="Other">:</span> <a href="system.html#seq"><span class="Identifier">seq</span></a><span class="Other">[</span><span class="Identifier">S</span><span class="Other">]</span> {.
<span class="Identifier">inline</span><span class="Other">,</span> <span class="Identifier">effectsOf</span><span class="Other">:</span> <span class="Identifier">op</span>.}</pre></dt>
<dd>
<p>Returns a new sequence with the results of the <tt class="docutils literal"><span class="pre"><span class="Identifier">op</span></span></tt> proc applied to every item in the container <tt class="docutils literal"><span class="pre"><span class="Identifier">s</span></span></tt>.</p>
<p>Since the input is not modified, you can use it to transform the type of the elements in the input container.</p>
<p>Instead of using <tt class="docutils literal"><span class="pre"><span class="Identifier">map</span></span></tt> and <tt class="docutils literal"><span class="pre"><span class="Identifier">filter</span></span></tt>, consider using the <tt class="docutils literal"><span class="pre"><span class="Identifier">collect</span></span></tt> macro from the <tt class="docutils literal"><span class="pre"><span class="Identifier">sugar</span></span></tt> module.</p>
<p><strong>See also:</strong></p>
<ul class="simple"><li><a class="reference external" href="sugar.html#collect.m%2Cuntyped%2Cuntyped">sugar.collect macro</a></li>
<li><a class="reference external" href="#mapIt.t,typed,untyped">mapIt template</a></li>
<li><a class="reference external" href="#apply,openArray[T],proc(T)_2">apply proc</a> for the in-place version</li>
</ul>
<p><strong class="examples_text">Example:</strong></p>
<pre class="listing"><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="Identifier">b</span> <span class="Operator">=</span> <span class="Identifier">map</span><span class="Punctuation">(</span><span class="Identifier">a</span><span class="Punctuation">,</span> <span class="Keyword">proc</span><span class="Punctuation">(</span><span class="Identifier">x</span><span class="Punctuation">:</span> <span class="Identifier">int</span><span class="Punctuation">)</span><span class="Punctuation">:</span> <span class="Identifier">string</span> <span class="Operator">=</span> <span class="Operator">$</span><span class="Identifier">x</span><span class="Punctuation">)</span>
<span class="Identifier">assert</span> <span class="Identifier">b</span> <span class="Operator">==</span> <span class="Operator">@</span><span class="Punctuation">[</span><span class="StringLit">&quot;1&quot;</span><span class="Punctuation">,</span> <span class="StringLit">&quot;2&quot;</span><span class="Punctuation">,</span> <span class="StringLit">&quot;3&quot;</span><span class="Punctuation">,</span> <span class="StringLit">&quot;4&quot;</span><span class="Punctuation">]</span></pre>
<a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/collections/sequtils.nim#L463" class="link-seesrc" target="_blank">Source</a>&nbsp;&nbsp;
<a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/collections/sequtils.nim#L463" class="link-seesrc" target="_blank" >Edit</a>&nbsp;&nbsp;
</dd>
</div>
</div>
<div id="max-procs-all">
<div id="max,openArray[T],proc(T,T)">
<dt><pre><span class="Keyword">proc</span> <a href="#max%2CopenArray%5BT%5D%2Cproc%28T%2CT%29"><span class="Identifier">max</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">]</span><span class="Other">(</span><span class="Identifier">x</span><span class="Other">:</span> <a href="system.html#openArray"><span class="Identifier">openArray</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">]</span><span class="Other">;</span> <span class="Identifier">cmp</span><span class="Other">:</span> <span class="Keyword">proc</span> <span class="Other">(</span><span class="Identifier">a</span><span class="Other">,</span> <span class="Identifier">b</span><span class="Other">:</span> <span class="Identifier">T</span><span class="Other">)</span><span class="Other">:</span> <a href="system.html#int"><span class="Identifier">int</span></a><span class="Other">)</span><span class="Other">:</span> <span class="Identifier">T</span> {.<span class="Identifier">effectsOf</span><span class="Other">:</span> <span class="Identifier">cmp</span>.}</pre></dt>
<dd>
The maximum value of <tt class="docutils literal"><span class="pre"><span class="Identifier">x</span></span></tt>.
<a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/collections/sequtils.nim#L245" class="link-seesrc" target="_blank">Source</a>&nbsp;&nbsp;
<a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/collections/sequtils.nim#L245" class="link-seesrc" target="_blank" >Edit</a>&nbsp;&nbsp;
</dd>
</div>
</div>
<div id="maxIndex-procs-all">
<div id="maxIndex,openArray[T]">
<dt><pre><span class="Keyword">func</span> <a href="#maxIndex%2CopenArray%5BT%5D"><span class="Identifier">maxIndex</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">]</span><span class="Other">(</span><span class="Identifier">s</span><span class="Other">:</span> <a href="system.html#openArray"><span class="Identifier">openArray</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">]</span><span class="Other">)</span><span class="Other">:</span> <a href="system.html#int"><span class="Identifier">int</span></a></pre></dt>
<dd>
Returns the index of the maximum value of <tt class="docutils literal"><span class="pre"><span class="Identifier">s</span></span></tt>. <tt class="docutils literal"><span class="pre"><span class="Identifier">T</span></span></tt> needs to have a <tt class="docutils literal"><span class="pre"><span class="Operator">&lt;</span></span></tt> operator.
<p><strong class="examples_text">Example:</strong></p>
<pre class="listing"><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="Identifier">b</span> <span class="Operator">=</span> <span class="Operator">@</span><span class="Punctuation">[</span><span class="DecNumber">6</span><span class="Punctuation">,</span> <span class="DecNumber">5</span><span class="Punctuation">,</span> <span class="DecNumber">4</span><span class="Punctuation">,</span> <span class="DecNumber">3</span><span class="Punctuation">]</span>
<span class="Identifier">c</span> <span class="Operator">=</span> <span class="Punctuation">[</span><span class="DecNumber">2</span><span class="Punctuation">,</span> <span class="Operator">-</span><span class="DecNumber">7</span><span class="Punctuation">,</span> <span class="DecNumber">8</span><span class="Punctuation">,</span> <span class="Operator">-</span><span class="DecNumber">5</span><span class="Punctuation">]</span>
<span class="Identifier">d</span> <span class="Operator">=</span> <span class="StringLit">&quot;ziggy&quot;</span>
<span class="Identifier">assert</span> <span class="Identifier">maxIndex</span><span class="Punctuation">(</span><span class="Identifier">a</span><span class="Punctuation">)</span> <span class="Operator">==</span> <span class="DecNumber">3</span>
<span class="Identifier">assert</span> <span class="Identifier">maxIndex</span><span class="Punctuation">(</span><span class="Identifier">b</span><span class="Punctuation">)</span> <span class="Operator">==</span> <span class="DecNumber">0</span>
<span class="Identifier">assert</span> <span class="Identifier">maxIndex</span><span class="Punctuation">(</span><span class="Identifier">c</span><span class="Punctuation">)</span> <span class="Operator">==</span> <span class="DecNumber">2</span>
<span class="Identifier">assert</span> <span class="Identifier">maxIndex</span><span class="Punctuation">(</span><span class="Identifier">d</span><span class="Punctuation">)</span> <span class="Operator">==</span> <span class="DecNumber">0</span></pre>
<a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/collections/sequtils.nim#L282" class="link-seesrc" target="_blank">Source</a>&nbsp;&nbsp;
<a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/collections/sequtils.nim#L282" class="link-seesrc" target="_blank" >Edit</a>&nbsp;&nbsp;
</dd>
</div>
<div id="maxIndex,openArray[T],proc(T,T)">
<dt><pre><span class="Keyword">func</span> <a href="#maxIndex%2CopenArray%5BT%5D%2Cproc%28T%2CT%29"><span class="Identifier">maxIndex</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">]</span><span class="Other">(</span><span class="Identifier">s</span><span class="Other">:</span> <a href="system.html#openArray"><span class="Identifier">openArray</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">]</span><span class="Other">;</span> <span class="Identifier">cmp</span><span class="Other">:</span> <span class="Keyword">proc</span> <span class="Other">(</span><span class="Identifier">a</span><span class="Other">,</span> <span class="Identifier">b</span><span class="Other">:</span> <span class="Identifier">T</span><span class="Other">)</span><span class="Other">:</span> <a href="system.html#int"><span class="Identifier">int</span></a><span class="Other">)</span><span class="Other">:</span> <a href="system.html#int"><span class="Identifier">int</span></a> {.
<span class="Identifier">effectsOf</span><span class="Other">:</span> <span class="Identifier">cmp</span>.}</pre></dt>
<dd>
Returns the index of the maximum value of <tt class="docutils literal"><span class="pre"><span class="Identifier">s</span></span></tt>.
<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">sugar</span>
<span class="Keyword">let</span> <span class="Identifier">s1</span> <span class="Operator">=</span> <span class="Operator">@</span><span class="Punctuation">[</span><span class="StringLit">&quot;foo&quot;</span><span class="Punctuation">,</span><span class="StringLit">&quot;bar&quot;</span><span class="Punctuation">,</span> <span class="StringLit">&quot;hello&quot;</span><span class="Punctuation">]</span>
<span class="Keyword">let</span> <span class="Identifier">s2</span> <span class="Operator">=</span> <span class="Operator">@</span><span class="Punctuation">[</span><span class="FloatNumber">2.</span><span class="Operator">.</span><span class="DecNumber">4</span><span class="Punctuation">,</span> <span class="FloatNumber">1.</span><span class="Operator">.</span><span class="DecNumber">3</span><span class="Punctuation">,</span> <span class="FloatNumber">6.</span><span class="Operator">.</span><span class="DecNumber">10</span><span class="Punctuation">]</span>
<span class="Identifier">assert</span> <span class="Identifier">maxIndex</span><span class="Punctuation">(</span><span class="Identifier">s1</span><span class="Punctuation">,</span> <span class="Keyword">proc</span> <span class="Punctuation">(</span><span class="Identifier">a</span><span class="Punctuation">,</span> <span class="Identifier">b</span><span class="Punctuation">:</span> <span class="Identifier">string</span><span class="Punctuation">)</span><span class="Punctuation">:</span> <span class="Identifier">int</span> <span class="Operator">=</span> <span class="Identifier">a</span><span class="Operator">.</span><span class="Identifier">len</span> <span class="Operator">-</span> <span class="Identifier">b</span><span class="Operator">.</span><span class="Identifier">len</span><span class="Punctuation">)</span> <span class="Operator">==</span> <span class="DecNumber">2</span>
<span class="Identifier">assert</span> <span class="Identifier">maxIndex</span><span class="Punctuation">(</span><span class="Identifier">s2</span><span class="Punctuation">,</span> <span class="Punctuation">(</span><span class="Identifier">a</span><span class="Punctuation">,</span> <span class="Identifier">b</span><span class="Punctuation">)</span> <span class="Operator">=&gt;</span> <span class="Identifier">a</span><span class="Operator">.</span><span class="Identifier">a</span> <span class="Operator">-</span> <span class="Identifier">b</span><span class="Operator">.</span><span class="Identifier">a</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/collections/sequtils.nim#L299" class="link-seesrc" target="_blank">Source</a>&nbsp;&nbsp;
<a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/collections/sequtils.nim#L299" class="link-seesrc" target="_blank" >Edit</a>&nbsp;&nbsp;
</dd>
</div>
</div>
<div id="min-procs-all">
<div id="min,openArray[T],proc(T,T)">
<dt><pre><span class="Keyword">proc</span> <a href="#min%2CopenArray%5BT%5D%2Cproc%28T%2CT%29"><span class="Identifier">min</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">]</span><span class="Other">(</span><span class="Identifier">x</span><span class="Other">:</span> <a href="system.html#openArray"><span class="Identifier">openArray</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">]</span><span class="Other">;</span> <span class="Identifier">cmp</span><span class="Other">:</span> <span class="Keyword">proc</span> <span class="Other">(</span><span class="Identifier">a</span><span class="Other">,</span> <span class="Identifier">b</span><span class="Other">:</span> <span class="Identifier">T</span><span class="Other">)</span><span class="Other">:</span> <a href="system.html#int"><span class="Identifier">int</span></a><span class="Other">)</span><span class="Other">:</span> <span class="Identifier">T</span> {.<span class="Identifier">effectsOf</span><span class="Other">:</span> <span class="Identifier">cmp</span>.}</pre></dt>
<dd>
The minimum value of <tt class="docutils literal"><span class="pre"><span class="Identifier">x</span></span></tt>.
<a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/collections/sequtils.nim#L239" class="link-seesrc" target="_blank">Source</a>&nbsp;&nbsp;
<a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/collections/sequtils.nim#L239" class="link-seesrc" target="_blank" >Edit</a>&nbsp;&nbsp;
</dd>
</div>
</div>
<div id="minIndex-procs-all">
<div id="minIndex,openArray[T]">
<dt><pre><span class="Keyword">func</span> <a href="#minIndex%2CopenArray%5BT%5D"><span class="Identifier">minIndex</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">]</span><span class="Other">(</span><span class="Identifier">s</span><span class="Other">:</span> <a href="system.html#openArray"><span class="Identifier">openArray</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">]</span><span class="Other">)</span><span class="Other">:</span> <a href="system.html#int"><span class="Identifier">int</span></a></pre></dt>
<dd>
Returns the index of the minimum value of <tt class="docutils literal"><span class="pre"><span class="Identifier">s</span></span></tt>. <tt class="docutils literal"><span class="pre"><span class="Identifier">T</span></span></tt> needs to have a <tt class="docutils literal"><span class="pre"><span class="Operator">&lt;</span></span></tt> operator.
<p><strong class="examples_text">Example:</strong></p>
<pre class="listing"><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="Identifier">b</span> <span class="Operator">=</span> <span class="Operator">@</span><span class="Punctuation">[</span><span class="DecNumber">6</span><span class="Punctuation">,</span> <span class="DecNumber">5</span><span class="Punctuation">,</span> <span class="DecNumber">4</span><span class="Punctuation">,</span> <span class="DecNumber">3</span><span class="Punctuation">]</span>
<span class="Identifier">c</span> <span class="Operator">=</span> <span class="Punctuation">[</span><span class="DecNumber">2</span><span class="Punctuation">,</span> <span class="Operator">-</span><span class="DecNumber">7</span><span class="Punctuation">,</span> <span class="DecNumber">8</span><span class="Punctuation">,</span> <span class="Operator">-</span><span class="DecNumber">5</span><span class="Punctuation">]</span>
<span class="Identifier">d</span> <span class="Operator">=</span> <span class="StringLit">&quot;ziggy&quot;</span>
<span class="Identifier">assert</span> <span class="Identifier">minIndex</span><span class="Punctuation">(</span><span class="Identifier">a</span><span class="Punctuation">)</span> <span class="Operator">==</span> <span class="DecNumber">0</span>
<span class="Identifier">assert</span> <span class="Identifier">minIndex</span><span class="Punctuation">(</span><span class="Identifier">b</span><span class="Punctuation">)</span> <span class="Operator">==</span> <span class="DecNumber">3</span>
<span class="Identifier">assert</span> <span class="Identifier">minIndex</span><span class="Punctuation">(</span><span class="Identifier">c</span><span class="Punctuation">)</span> <span class="Operator">==</span> <span class="DecNumber">1</span>
<span class="Identifier">assert</span> <span class="Identifier">minIndex</span><span class="Punctuation">(</span><span class="Identifier">d</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/collections/sequtils.nim#L251" class="link-seesrc" target="_blank">Source</a>&nbsp;&nbsp;
<a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/collections/sequtils.nim#L251" class="link-seesrc" target="_blank" >Edit</a>&nbsp;&nbsp;
</dd>
</div>
<div id="minIndex,openArray[T],proc(T,T)">
<dt><pre><span class="Keyword">func</span> <a href="#minIndex%2CopenArray%5BT%5D%2Cproc%28T%2CT%29"><span class="Identifier">minIndex</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">]</span><span class="Other">(</span><span class="Identifier">s</span><span class="Other">:</span> <a href="system.html#openArray"><span class="Identifier">openArray</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">]</span><span class="Other">;</span> <span class="Identifier">cmp</span><span class="Other">:</span> <span class="Keyword">proc</span> <span class="Other">(</span><span class="Identifier">a</span><span class="Other">,</span> <span class="Identifier">b</span><span class="Other">:</span> <span class="Identifier">T</span><span class="Other">)</span><span class="Other">:</span> <a href="system.html#int"><span class="Identifier">int</span></a><span class="Other">)</span><span class="Other">:</span> <a href="system.html#int"><span class="Identifier">int</span></a> {.
<span class="Identifier">effectsOf</span><span class="Other">:</span> <span class="Identifier">cmp</span>.}</pre></dt>
<dd>
Returns the index of the minimum value of <tt class="docutils literal"><span class="pre"><span class="Identifier">s</span></span></tt>.
<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">sugar</span>
<span class="Keyword">let</span> <span class="Identifier">s1</span> <span class="Operator">=</span> <span class="Operator">@</span><span class="Punctuation">[</span><span class="StringLit">&quot;foo&quot;</span><span class="Punctuation">,</span><span class="StringLit">&quot;bar&quot;</span><span class="Punctuation">,</span> <span class="StringLit">&quot;hello&quot;</span><span class="Punctuation">]</span>
<span class="Keyword">let</span> <span class="Identifier">s2</span> <span class="Operator">=</span> <span class="Operator">@</span><span class="Punctuation">[</span><span class="FloatNumber">2.</span><span class="Operator">.</span><span class="DecNumber">4</span><span class="Punctuation">,</span> <span class="FloatNumber">1.</span><span class="Operator">.</span><span class="DecNumber">3</span><span class="Punctuation">,</span> <span class="FloatNumber">6.</span><span class="Operator">.</span><span class="DecNumber">10</span><span class="Punctuation">]</span>
<span class="Identifier">assert</span> <span class="Identifier">minIndex</span><span class="Punctuation">(</span><span class="Identifier">s1</span><span class="Punctuation">,</span> <span class="Keyword">proc</span> <span class="Punctuation">(</span><span class="Identifier">a</span><span class="Punctuation">,</span> <span class="Identifier">b</span><span class="Punctuation">:</span> <span class="Identifier">string</span><span class="Punctuation">)</span><span class="Punctuation">:</span> <span class="Identifier">int</span> <span class="Operator">=</span> <span class="Identifier">a</span><span class="Operator">.</span><span class="Identifier">len</span> <span class="Operator">-</span> <span class="Identifier">b</span><span class="Operator">.</span><span class="Identifier">len</span><span class="Punctuation">)</span> <span class="Operator">==</span> <span class="DecNumber">0</span>
<span class="Identifier">assert</span> <span class="Identifier">minIndex</span><span class="Punctuation">(</span><span class="Identifier">s2</span><span class="Punctuation">,</span> <span class="Punctuation">(</span><span class="Identifier">a</span><span class="Punctuation">,</span> <span class="Identifier">b</span><span class="Punctuation">)</span> <span class="Operator">=&gt;</span> <span class="Identifier">a</span><span class="Operator">.</span><span class="Identifier">a</span> <span class="Operator">-</span> <span class="Identifier">b</span><span class="Operator">.</span><span class="Identifier">a</span><span class="Punctuation">)</span> <span class="Operator">==</span> <span class="DecNumber">1</span></pre>
<a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/collections/sequtils.nim#L268" class="link-seesrc" target="_blank">Source</a>&nbsp;&nbsp;
<a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/collections/sequtils.nim#L268" class="link-seesrc" target="_blank" >Edit</a>&nbsp;&nbsp;
</dd>
</div>
</div>
<div id="minmax-procs-all">
<div id="minmax,openArray[T]">
<dt><pre><span class="Keyword">func</span> <a href="#minmax%2CopenArray%5BT%5D"><span class="Identifier">minmax</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">]</span><span class="Other">(</span><span class="Identifier">x</span><span class="Other">:</span> <a href="system.html#openArray"><span class="Identifier">openArray</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">]</span><span class="Other">)</span><span class="Other">:</span> <span class="Other">(</span><span class="Identifier">T</span><span class="Other">,</span> <span class="Identifier">T</span><span class="Other">)</span></pre></dt>
<dd>
The minimum and maximum values of <tt class="docutils literal"><span class="pre"><span class="Identifier">x</span></span></tt>. <tt class="docutils literal"><span class="pre"><span class="Identifier">T</span></span></tt> needs to have a <tt class="docutils literal"><span class="pre"><span class="Operator">&lt;</span></span></tt> operator.
<a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/collections/sequtils.nim#L312" class="link-seesrc" target="_blank">Source</a>&nbsp;&nbsp;
<a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/collections/sequtils.nim#L312" class="link-seesrc" target="_blank" >Edit</a>&nbsp;&nbsp;
</dd>
</div>
<div id="minmax,openArray[T],proc(T,T)">
<dt><pre><span class="Keyword">func</span> <a href="#minmax%2CopenArray%5BT%5D%2Cproc%28T%2CT%29"><span class="Identifier">minmax</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">]</span><span class="Other">(</span><span class="Identifier">x</span><span class="Other">:</span> <a href="system.html#openArray"><span class="Identifier">openArray</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">]</span><span class="Other">;</span> <span class="Identifier">cmp</span><span class="Other">:</span> <span class="Keyword">proc</span> <span class="Other">(</span><span class="Identifier">a</span><span class="Other">,</span> <span class="Identifier">b</span><span class="Other">:</span> <span class="Identifier">T</span><span class="Other">)</span><span class="Other">:</span> <a href="system.html#int"><span class="Identifier">int</span></a><span class="Other">)</span><span class="Other">:</span> <span class="Other">(</span><span class="Identifier">T</span><span class="Other">,</span> <span class="Identifier">T</span><span class="Other">)</span> {.
<span class="Identifier">effectsOf</span><span class="Other">:</span> <span class="Identifier">cmp</span>.}</pre></dt>
<dd>
The minimum and maximum values of <tt class="docutils literal"><span class="pre"><span class="Identifier">x</span></span></tt>.
<a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/collections/sequtils.nim#L321" class="link-seesrc" target="_blank">Source</a>&nbsp;&nbsp;
<a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/collections/sequtils.nim#L321" class="link-seesrc" target="_blank" >Edit</a>&nbsp;&nbsp;
</dd>
</div>
</div>
<div id="repeat-procs-all">
<div id="repeat,T,Natural">
<dt><pre><span class="Keyword">proc</span> <a href="#repeat%2CT%2CNatural"><span class="Identifier">repeat</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">]</span><span class="Other">(</span><span class="Identifier">x</span><span class="Other">:</span> <span class="Identifier">T</span><span class="Other">;</span> <span class="Identifier">n</span><span class="Other">:</span> <a href="system.html#Natural"><span class="Identifier">Natural</span></a><span class="Other">)</span><span class="Other">:</span> <a href="system.html#seq"><span class="Identifier">seq</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">]</span></pre></dt>
<dd>
Returns a new sequence with the item <tt class="docutils literal"><span class="pre"><span class="Identifier">x</span></span></tt> repeated <tt class="docutils literal"><span class="pre"><span class="Identifier">n</span></span></tt> times. <tt class="docutils literal"><span class="pre"><span class="Identifier">n</span></span></tt> must be a non-negative number (zero or more).
<p><strong class="examples_text">Example:</strong></p>
<pre class="listing"><span class="Keyword">let</span>
<span class="Identifier">total</span> <span class="Operator">=</span> <span class="Identifier">repeat</span><span class="Punctuation">(</span><span class="DecNumber">5</span><span class="Punctuation">,</span> <span class="DecNumber">3</span><span class="Punctuation">)</span>
<span class="Identifier">assert</span> <span class="Identifier">total</span> <span class="Operator">==</span> <span class="Operator">@</span><span class="Punctuation">[</span><span class="DecNumber">5</span><span class="Punctuation">,</span> <span class="DecNumber">5</span><span class="Punctuation">,</span> <span class="DecNumber">5</span><span class="Punctuation">]</span></pre>
<a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/collections/sequtils.nim#L198" class="link-seesrc" target="_blank">Source</a>&nbsp;&nbsp;
<a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/collections/sequtils.nim#L198" class="link-seesrc" target="_blank" >Edit</a>&nbsp;&nbsp;
</dd>
</div>
</div>
<div id="unzip-procs-all">
<div id="unzip,openArray[]">
<dt><pre><span class="Keyword">proc</span> <a href="#unzip%2CopenArray%5B%5D"><span class="Identifier">unzip</span></a><span class="Other">[</span><span class="Identifier">S</span><span class="Other">,</span> <span class="Identifier">T</span><span class="Other">]</span><span class="Other">(</span><span class="Identifier">s</span><span class="Other">:</span> <a href="system.html#openArray"><span class="Identifier">openArray</span></a><span class="Other">[</span><span class="Other">(</span><span class="Identifier">S</span><span class="Other">,</span> <span class="Identifier">T</span><span class="Other">)</span><span class="Other">]</span><span class="Other">)</span><span class="Other">:</span> <span class="Other">(</span><a href="system.html#seq"><span class="Identifier">seq</span></a><span class="Other">[</span><span class="Identifier">S</span><span class="Other">]</span><span class="Other">,</span> <a href="system.html#seq"><span class="Identifier">seq</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">]</span><span class="Other">)</span></pre></dt>
<dd>
Returns a tuple of two sequences split out from a sequence of 2-field tuples.
<p><strong class="examples_text">Example:</strong></p>
<pre class="listing"><span class="Keyword">let</span>
<span class="Identifier">zipped</span> <span class="Operator">=</span> <span class="Operator">@</span><span class="Punctuation">[</span><span class="Punctuation">(</span><span class="DecNumber">1</span><span class="Punctuation">,</span> <span class="CharLit">'a'</span><span class="Punctuation">)</span><span class="Punctuation">,</span> <span class="Punctuation">(</span><span class="DecNumber">2</span><span class="Punctuation">,</span> <span class="CharLit">'b'</span><span class="Punctuation">)</span><span class="Punctuation">,</span> <span class="Punctuation">(</span><span class="DecNumber">3</span><span class="Punctuation">,</span> <span class="CharLit">'c'</span><span class="Punctuation">)</span><span class="Punctuation">]</span>
<span class="Identifier">unzipped1</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="Identifier">unzipped2</span> <span class="Operator">=</span> <span class="Operator">@</span><span class="Punctuation">[</span><span class="Punctuation">'</span><span class="Identifier">a</span><span class="CharLit">', '</span><span class="Identifier">b</span><span class="CharLit">', '</span><span class="Identifier">c</span><span class="CharLit">']</span>
<span class="Identifier">assert</span> <span class="Identifier">zipped</span><span class="Operator">.</span><span class="Identifier">unzip</span><span class="Punctuation">(</span><span class="Punctuation">)</span> <span class="Operator">==</span> <span class="Punctuation">(</span><span class="Identifier">unzipped1</span><span class="Punctuation">,</span> <span class="Identifier">unzipped2</span><span class="Punctuation">)</span>
<span class="Identifier">assert</span> <span class="Identifier">zip</span><span class="Punctuation">(</span><span class="Identifier">unzipped1</span><span class="Punctuation">,</span> <span class="Identifier">unzipped2</span><span class="Punctuation">)</span><span class="Operator">.</span><span class="Identifier">unzip</span><span class="Punctuation">(</span><span class="Punctuation">)</span> <span class="Operator">==</span> <span class="Punctuation">(</span><span class="Identifier">unzipped1</span><span class="Punctuation">,</span> <span class="Identifier">unzipped2</span><span class="Punctuation">)</span></pre>
<a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/collections/sequtils.nim#L393" class="link-seesrc" target="_blank">Source</a>&nbsp;&nbsp;
<a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/collections/sequtils.nim#L393" class="link-seesrc" target="_blank" >Edit</a>&nbsp;&nbsp;
</dd>
</div>
</div>
<div id="zip-procs-all">
<div id="zip,,">
<dt><pre><span class="Keyword">proc</span> <a href="#zip%2C%2C"><span class="Identifier">zip</span></a><span class="Other">[</span><span class="Identifier">S</span><span class="Other">,</span> <span class="Identifier">T</span><span class="Other">]</span><span class="Other">(</span><span class="Identifier">s1</span><span class="Other">:</span> <a href="system.html#openArray"><span class="Identifier">openArray</span></a><span class="Other">[</span><span class="Identifier">S</span><span class="Other">]</span><span class="Other">;</span> <span class="Identifier">s2</span><span class="Other">:</span> <a href="system.html#openArray"><span class="Identifier">openArray</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">]</span><span class="Other">)</span><span class="Other">:</span> <a href="system.html#seq"><span class="Identifier">seq</span></a><span class="Other">[</span><span class="Other">(</span><span class="Identifier">S</span><span class="Other">,</span> <span class="Identifier">T</span><span class="Other">)</span><span class="Other">]</span></pre></dt>
<dd>
<p>Returns a new sequence with a combination of the two input containers.</p>
<p>The input containers can be of different types. If one container is shorter, the remaining items in the longer container are discarded.</p>
<p><strong>Note</strong>: For Nim 1.0.x and older version, <tt class="docutils literal"><span class="pre"><span class="Identifier">zip</span></span></tt> returned a seq of named tuples with fields <tt class="docutils literal"><span class="pre"><span class="Identifier">a</span></span></tt> and <tt class="docutils literal"><span class="pre"><span class="Identifier">b</span></span></tt>. For Nim versions 1.1.x and newer, <tt class="docutils literal"><span class="pre"><span class="Identifier">zip</span></span></tt> returns a seq of unnamed tuples.</p>
<p><strong class="examples_text">Example:</strong></p>
<pre class="listing"><span class="Keyword">let</span>
<span class="Identifier">short</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="Identifier">long</span> <span class="Operator">=</span> <span class="Operator">@</span><span class="Punctuation">[</span><span class="DecNumber">6</span><span class="Punctuation">,</span> <span class="DecNumber">5</span><span class="Punctuation">,</span> <span class="DecNumber">4</span><span class="Punctuation">,</span> <span class="DecNumber">3</span><span class="Punctuation">,</span> <span class="DecNumber">2</span><span class="Punctuation">,</span> <span class="DecNumber">1</span><span class="Punctuation">]</span>
<span class="Identifier">words</span> <span class="Operator">=</span> <span class="Operator">@</span><span class="Punctuation">[</span><span class="StringLit">&quot;one&quot;</span><span class="Punctuation">,</span> <span class="StringLit">&quot;two&quot;</span><span class="Punctuation">,</span> <span class="StringLit">&quot;three&quot;</span><span class="Punctuation">]</span>
<span class="Identifier">letters</span> <span class="Operator">=</span> <span class="StringLit">&quot;abcd&quot;</span>
<span class="Identifier">zip1</span> <span class="Operator">=</span> <span class="Identifier">zip</span><span class="Punctuation">(</span><span class="Identifier">short</span><span class="Punctuation">,</span> <span class="Identifier">long</span><span class="Punctuation">)</span>
<span class="Identifier">zip2</span> <span class="Operator">=</span> <span class="Identifier">zip</span><span class="Punctuation">(</span><span class="Identifier">short</span><span class="Punctuation">,</span> <span class="Identifier">words</span><span class="Punctuation">)</span>
<span class="Identifier">assert</span> <span class="Identifier">zip1</span> <span class="Operator">==</span> <span class="Operator">@</span><span class="Punctuation">[</span><span class="Punctuation">(</span><span class="DecNumber">1</span><span class="Punctuation">,</span> <span class="DecNumber">6</span><span class="Punctuation">)</span><span class="Punctuation">,</span> <span class="Punctuation">(</span><span class="DecNumber">2</span><span class="Punctuation">,</span> <span class="DecNumber">5</span><span class="Punctuation">)</span><span class="Punctuation">,</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="Punctuation">]</span>
<span class="Identifier">assert</span> <span class="Identifier">zip2</span> <span class="Operator">==</span> <span class="Operator">@</span><span class="Punctuation">[</span><span class="Punctuation">(</span><span class="DecNumber">1</span><span class="Punctuation">,</span> <span class="StringLit">&quot;one&quot;</span><span class="Punctuation">)</span><span class="Punctuation">,</span> <span class="Punctuation">(</span><span class="DecNumber">2</span><span class="Punctuation">,</span> <span class="StringLit">&quot;two&quot;</span><span class="Punctuation">)</span><span class="Punctuation">,</span> <span class="Punctuation">(</span><span class="DecNumber">3</span><span class="Punctuation">,</span> <span class="StringLit">&quot;three&quot;</span><span class="Punctuation">)</span><span class="Punctuation">]</span>
<span class="Identifier">assert</span> <span class="Identifier">zip1</span><span class="Punctuation">[</span><span class="DecNumber">2</span><span class="Punctuation">]</span><span class="Punctuation">[</span><span class="DecNumber">0</span><span class="Punctuation">]</span> <span class="Operator">==</span> <span class="DecNumber">3</span>
<span class="Identifier">assert</span> <span class="Identifier">zip2</span><span class="Punctuation">[</span><span class="DecNumber">1</span><span class="Punctuation">]</span><span class="Punctuation">[</span><span class="DecNumber">1</span><span class="Punctuation">]</span> <span class="Operator">==</span> <span class="StringLit">&quot;two&quot;</span>
<span class="Keyword">when</span> <span class="Punctuation">(</span><span class="Identifier">NimMajor</span><span class="Punctuation">,</span> <span class="Identifier">NimMinor</span><span class="Punctuation">)</span> <span class="Operator">&lt;=</span> <span class="Punctuation">(</span><span class="DecNumber">1</span><span class="Punctuation">,</span> <span class="DecNumber">0</span><span class="Punctuation">)</span><span class="Punctuation">:</span>
<span class="Keyword">let</span>
<span class="Identifier">zip3</span> <span class="Operator">=</span> <span class="Identifier">zip</span><span class="Punctuation">(</span><span class="Identifier">long</span><span class="Punctuation">,</span> <span class="Identifier">letters</span><span class="Punctuation">)</span>
<span class="Identifier">assert</span> <span class="Identifier">zip3</span> <span class="Operator">==</span> <span class="Operator">@</span><span class="Punctuation">[</span><span class="Punctuation">(</span><span class="Identifier">a</span><span class="Punctuation">:</span> <span class="DecNumber">6</span><span class="Punctuation">,</span> <span class="Identifier">b</span><span class="Punctuation">:</span> <span class="CharLit">'a'</span><span class="Punctuation">)</span><span class="Punctuation">,</span> <span class="Punctuation">(</span><span class="DecNumber">5</span><span class="Punctuation">,</span> <span class="CharLit">'b'</span><span class="Punctuation">)</span><span class="Punctuation">,</span> <span class="Punctuation">(</span><span class="DecNumber">4</span><span class="Punctuation">,</span> <span class="CharLit">'c'</span><span class="Punctuation">)</span><span class="Punctuation">,</span> <span class="Punctuation">(</span><span class="DecNumber">3</span><span class="Punctuation">,</span> <span class="CharLit">'d'</span><span class="Punctuation">)</span><span class="Punctuation">]</span>
<span class="Identifier">assert</span> <span class="Identifier">zip3</span><span class="Punctuation">[</span><span class="DecNumber">0</span><span class="Punctuation">]</span><span class="Operator">.</span><span class="Identifier">b</span> <span class="Operator">==</span> <span class="CharLit">'a'</span>
<span class="Keyword">else</span><span class="Punctuation">:</span>
<span class="Keyword">let</span>
<span class="Identifier">zip3</span><span class="Punctuation">:</span> <span class="Identifier">seq</span><span class="Punctuation">[</span><span class="Keyword">tuple</span><span class="Punctuation">[</span><span class="Identifier">num</span><span class="Punctuation">:</span> <span class="Identifier">int</span><span class="Punctuation">,</span> <span class="Identifier">letter</span><span class="Punctuation">:</span> <span class="Identifier">char</span><span class="Punctuation">]</span><span class="Punctuation">]</span> <span class="Operator">=</span> <span class="Identifier">zip</span><span class="Punctuation">(</span><span class="Identifier">long</span><span class="Punctuation">,</span> <span class="Identifier">letters</span><span class="Punctuation">)</span>
<span class="Identifier">assert</span> <span class="Identifier">zip3</span> <span class="Operator">==</span> <span class="Operator">@</span><span class="Punctuation">[</span><span class="Punctuation">(</span><span class="DecNumber">6</span><span class="Punctuation">,</span> <span class="CharLit">'a'</span><span class="Punctuation">)</span><span class="Punctuation">,</span> <span class="Punctuation">(</span><span class="DecNumber">5</span><span class="Punctuation">,</span> <span class="CharLit">'b'</span><span class="Punctuation">)</span><span class="Punctuation">,</span> <span class="Punctuation">(</span><span class="DecNumber">4</span><span class="Punctuation">,</span> <span class="CharLit">'c'</span><span class="Punctuation">)</span><span class="Punctuation">,</span> <span class="Punctuation">(</span><span class="DecNumber">3</span><span class="Punctuation">,</span> <span class="CharLit">'d'</span><span class="Punctuation">)</span><span class="Punctuation">]</span>
<span class="Identifier">assert</span> <span class="Identifier">zip3</span><span class="Punctuation">[</span><span class="DecNumber">0</span><span class="Punctuation">]</span><span class="Operator">.</span><span class="Identifier">letter</span> <span class="Operator">==</span> <span class="CharLit">'a'</span></pre>
<a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/collections/sequtils.nim#L391" class="link-seesrc" target="_blank">Source</a>&nbsp;&nbsp;
<a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/collections/sequtils.nim#L391" class="link-seesrc" target="_blank" >Edit</a>&nbsp;&nbsp;
</dd>
</div>
</div>
</dl>
</div>
<div class="section" id="15">
<h1><a class="toc-backref" href="#15">Iterators</a></h1>
<dl class="item">
<div id="filter-iterators-all">
<div id="filter.i,openArray[T],proc(T)">
<dt><pre><span class="Keyword">iterator</span> <a href="#filter.i%2CopenArray%5BT%5D%2Cproc%28T%29"><span class="Identifier">filter</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">]</span><span class="Other">(</span><span class="Identifier">s</span><span class="Other">:</span> <a href="system.html#openArray"><span class="Identifier">openArray</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">]</span><span class="Other">;</span> <span class="Identifier">pred</span><span class="Other">:</span> <span class="Keyword">proc</span> <span class="Other">(</span><span class="Identifier">x</span><span class="Other">:</span> <span class="Identifier">T</span><span class="Other">)</span><span class="Other">:</span> <a href="system.html#bool"><span class="Identifier">bool</span></a> {.<span class="Identifier">closure</span>.}<span class="Other">)</span><span class="Other">:</span> <span class="Identifier">T</span> {.
<span class="Identifier">effectsOf</span><span class="Other">:</span> <span class="Identifier">pred</span>.}</pre></dt>
<dd>
<p>Iterates through a container <tt class="docutils literal"><span class="pre"><span class="Identifier">s</span></span></tt> and yields every item that fulfills the predicate <tt class="docutils literal"><span class="pre"><span class="Identifier">pred</span></span></tt> (a function that returns a <tt class="docutils literal"><span class="pre"><span class="Identifier">bool</span></span></tt>).</p>
<p>Instead of using <tt class="docutils literal"><span class="pre"><span class="Identifier">map</span></span></tt> and <tt class="docutils literal"><span class="pre"><span class="Identifier">filter</span></span></tt>, consider using the <tt class="docutils literal"><span class="pre"><span class="Identifier">collect</span></span></tt> macro from the <tt class="docutils literal"><span class="pre"><span class="Identifier">sugar</span></span></tt> module.</p>
<p><strong>See also:</strong></p>
<ul class="simple"><li><a class="reference external" href="sugar.html#collect.m%2Cuntyped%2Cuntyped">sugar.collect macro</a></li>
<li><a class="reference external" href="#filter,openArray[T],proc(T)">filter proc</a></li>
<li><a class="reference external" href="#filterIt.t,untyped,untyped">filterIt template</a></li>
</ul>
<p><strong class="examples_text">Example:</strong></p>
<pre class="listing"><span class="Keyword">let</span> <span class="Identifier">numbers</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">4</span><span class="Punctuation">,</span> <span class="DecNumber">5</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="DecNumber">7</span><span class="Punctuation">,</span> <span class="DecNumber">4</span><span class="Punctuation">]</span>
<span class="Keyword">var</span> <span class="Identifier">evens</span> <span class="Operator">=</span> <span class="Identifier">newSeq</span><span class="Punctuation">[</span><span class="Identifier">int</span><span class="Punctuation">]</span><span class="Punctuation">(</span><span class="Punctuation">)</span>
<span class="Keyword">for</span> <span class="Identifier">n</span> <span class="Keyword">in</span> <span class="Identifier">filter</span><span class="Punctuation">(</span><span class="Identifier">numbers</span><span class="Punctuation">,</span> <span class="Keyword">proc</span> <span class="Punctuation">(</span><span class="Identifier">x</span><span class="Punctuation">:</span> <span class="Identifier">int</span><span class="Punctuation">)</span><span class="Punctuation">:</span> <span class="Identifier">bool</span> <span class="Operator">=</span> <span class="Identifier">x</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="Punctuation">:</span>
<span class="Identifier">evens</span><span class="Operator">.</span><span class="Identifier">add</span><span class="Punctuation">(</span><span class="Identifier">n</span><span class="Punctuation">)</span>
<span class="Identifier">assert</span> <span class="Identifier">evens</span> <span class="Operator">==</span> <span class="Operator">@</span><span class="Punctuation">[</span><span class="DecNumber">4</span><span class="Punctuation">,</span> <span class="DecNumber">8</span><span class="Punctuation">,</span> <span class="DecNumber">4</span><span class="Punctuation">]</span></pre>
<a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/collections/sequtils.nim#L537" class="link-seesrc" target="_blank">Source</a>&nbsp;&nbsp;
<a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/collections/sequtils.nim#L537" class="link-seesrc" target="_blank" >Edit</a>&nbsp;&nbsp;
</dd>
</div>
</div>
<div id="items-iterators-all">
<div id="items.i">
<dt><pre><span class="Keyword">iterator</span> <a href="#items.i"><span class="Identifier">items</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">]</span><span class="Other">(</span><span class="Identifier">xs</span><span class="Other">:</span> <span class="Keyword">iterator</span> <span class="Other">(</span><span class="Other">)</span><span class="Other">:</span> <span class="Identifier">T</span><span class="Other">)</span><span class="Other">:</span> <span class="Identifier">T</span></pre></dt>
<dd>
Iterates over each element yielded by a closure iterator. This may not seem particularly useful on its own, but this allows closure iterators to be used by the mapIt, filterIt, allIt, anyIt, etc. templates.
<a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/collections/sequtils.nim#L1222" class="link-seesrc" target="_blank">Source</a>&nbsp;&nbsp;
<a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/collections/sequtils.nim#L1222" class="link-seesrc" target="_blank" >Edit</a>&nbsp;&nbsp;
</dd>
</div>
</div>
</dl>
</div>
<div class="section" id="17">
<h1><a class="toc-backref" href="#17">Macros</a></h1>
<dl class="item">
<div id="mapLiterals-macros-all">
<div id="mapLiterals.m,untyped,untyped">
<dt><pre><span class="Keyword">macro</span> <a href="#mapLiterals.m%2Cuntyped%2Cuntyped"><span class="Identifier">mapLiterals</span></a><span class="Other">(</span><span class="Identifier">constructor</span><span class="Other">,</span> <span class="Identifier">op</span><span class="Other">:</span> <a href="system.html#untyped"><span class="Identifier">untyped</span></a><span class="Other">;</span> <span class="Identifier">nested</span> <span class="Other">=</span> <span class="Identifier">true</span><span class="Other">)</span><span class="Other">:</span> <a href="system.html#untyped"><span class="Identifier">untyped</span></a></pre></dt>
<dd>
Applies <tt class="docutils literal"><span class="pre"><span class="Identifier">op</span></span></tt> to each of the <strong>atomic</strong> literals like <tt class="docutils literal"><span class="pre"><span class="DecNumber">3</span></span></tt> or <tt class="docutils literal"><span class="pre"><span class="StringLit">&quot;abc&quot;</span></span></tt> in the specified <tt class="docutils literal"><span class="pre"><span class="Identifier">constructor</span></span></tt> AST. This can be used to map every array element to some target type:
<p><strong class="examples_text">Example:</strong></p>
<pre class="listing"><span class="Keyword">let</span> <span class="Identifier">x</span> <span class="Operator">=</span> <span class="Identifier">mapLiterals</span><span class="Punctuation">(</span><span class="Punctuation">[</span><span class="FloatNumber">0.1</span><span class="Punctuation">,</span> <span class="FloatNumber">1.2</span><span class="Punctuation">,</span> <span class="FloatNumber">2.3</span><span class="Punctuation">,</span> <span class="FloatNumber">3.4</span><span class="Punctuation">]</span><span class="Punctuation">,</span> <span class="Identifier">int</span><span class="Punctuation">)</span>
<span class="Identifier">doAssert</span> <span class="Identifier">x</span> <span class="Keyword">is</span> <span class="Identifier">array</span><span class="Punctuation">[</span><span class="DecNumber">4</span><span class="Punctuation">,</span> <span class="Identifier">int</span><span class="Punctuation">]</span>
<span class="Identifier">doAssert</span> <span class="Identifier">x</span> <span class="Operator">==</span> <span class="Punctuation">[</span><span class="Identifier">int</span><span class="Punctuation">(</span><span class="FloatNumber">0.1</span><span class="Punctuation">)</span><span class="Punctuation">,</span> <span class="Identifier">int</span><span class="Punctuation">(</span><span class="FloatNumber">1.2</span><span class="Punctuation">)</span><span class="Punctuation">,</span> <span class="Identifier">int</span><span class="Punctuation">(</span><span class="FloatNumber">2.3</span><span class="Punctuation">)</span><span class="Punctuation">,</span> <span class="Identifier">int</span><span class="Punctuation">(</span><span class="FloatNumber">3.4</span><span class="Punctuation">)</span><span class="Punctuation">]</span></pre>If <tt class="docutils literal"><span class="pre"><span class="Identifier">nested</span></span></tt> is true (which is the default), the literals are replaced everywhere in the <tt class="docutils literal"><span class="pre"><span class="Identifier">constructor</span></span></tt> AST, otherwise only the first level is considered:
<p><strong class="examples_text">Example:</strong></p>
<pre class="listing"><span class="Keyword">let</span> <span class="Identifier">a</span> <span class="Operator">=</span> <span class="Identifier">mapLiterals</span><span class="Punctuation">(</span><span class="Punctuation">(</span><span class="FloatNumber">1.2</span><span class="Punctuation">,</span> <span class="Punctuation">(</span><span class="FloatNumber">2.3</span><span class="Punctuation">,</span> <span class="FloatNumber">3.4</span><span class="Punctuation">)</span><span class="Punctuation">,</span> <span class="FloatNumber">4.8</span><span class="Punctuation">)</span><span class="Punctuation">,</span> <span class="Identifier">int</span><span class="Punctuation">)</span>
<span class="Keyword">let</span> <span class="Identifier">b</span> <span class="Operator">=</span> <span class="Identifier">mapLiterals</span><span class="Punctuation">(</span><span class="Punctuation">(</span><span class="FloatNumber">1.2</span><span class="Punctuation">,</span> <span class="Punctuation">(</span><span class="FloatNumber">2.3</span><span class="Punctuation">,</span> <span class="FloatNumber">3.4</span><span class="Punctuation">)</span><span class="Punctuation">,</span> <span class="FloatNumber">4.8</span><span class="Punctuation">)</span><span class="Punctuation">,</span> <span class="Identifier">int</span><span class="Punctuation">,</span> <span class="Identifier">nested</span><span class="Operator">=</span><span class="Identifier">false</span><span class="Punctuation">)</span>
<span class="Identifier">assert</span> <span class="Identifier">a</span> <span class="Operator">==</span> <span class="Punctuation">(</span><span class="DecNumber">1</span><span class="Punctuation">,</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="Punctuation">,</span> <span class="DecNumber">4</span><span class="Punctuation">)</span>
<span class="Identifier">assert</span> <span class="Identifier">b</span> <span class="Operator">==</span> <span class="Punctuation">(</span><span class="DecNumber">1</span><span class="Punctuation">,</span> <span class="Punctuation">(</span><span class="FloatNumber">2.3</span><span class="Punctuation">,</span> <span class="FloatNumber">3.4</span><span class="Punctuation">)</span><span class="Punctuation">,</span> <span class="DecNumber">4</span><span class="Punctuation">)</span>
<span class="Keyword">let</span> <span class="Identifier">c</span> <span class="Operator">=</span> <span class="Identifier">mapLiterals</span><span class="Punctuation">(</span><span class="Punctuation">(</span><span class="DecNumber">1</span><span class="Punctuation">,</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="Punctuation">,</span> <span class="DecNumber">4</span><span class="Punctuation">,</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="Punctuation">)</span><span class="Punctuation">,</span> <span class="Punctuation">`</span><span class="Operator">$</span><span class="Punctuation">`</span><span class="Punctuation">)</span>
<span class="Keyword">let</span> <span class="Identifier">d</span> <span class="Operator">=</span> <span class="Identifier">mapLiterals</span><span class="Punctuation">(</span><span class="Punctuation">(</span><span class="DecNumber">1</span><span class="Punctuation">,</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="Punctuation">,</span> <span class="DecNumber">4</span><span class="Punctuation">,</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="Punctuation">)</span><span class="Punctuation">,</span> <span class="Punctuation">`</span><span class="Operator">$</span><span class="Punctuation">`</span><span class="Punctuation">,</span> <span class="Identifier">nested</span><span class="Operator">=</span><span class="Identifier">false</span><span class="Punctuation">)</span>
<span class="Identifier">assert</span> <span class="Identifier">c</span> <span class="Operator">==</span> <span class="Punctuation">(</span><span class="StringLit">&quot;1&quot;</span><span class="Punctuation">,</span> <span class="Punctuation">(</span><span class="StringLit">&quot;2&quot;</span><span class="Punctuation">,</span> <span class="StringLit">&quot;3&quot;</span><span class="Punctuation">)</span><span class="Punctuation">,</span> <span class="StringLit">&quot;4&quot;</span><span class="Punctuation">,</span> <span class="Punctuation">(</span><span class="StringLit">&quot;5&quot;</span><span class="Punctuation">,</span> <span class="StringLit">&quot;6&quot;</span><span class="Punctuation">)</span><span class="Punctuation">)</span>
<span class="Identifier">assert</span> <span class="Identifier">d</span> <span class="Operator">==</span> <span class="Punctuation">(</span><span class="StringLit">&quot;1&quot;</span><span class="Punctuation">,</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="Punctuation">,</span> <span class="StringLit">&quot;4&quot;</span><span class="Punctuation">,</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="Punctuation">)</span></pre>There are no constraints for the <tt class="docutils literal"><span class="pre"><span class="Identifier">constructor</span></span></tt> AST, it works for nested tuples of arrays of sets etc.
<a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/collections/sequtils.nim#L1196" class="link-seesrc" target="_blank">Source</a>&nbsp;&nbsp;
<a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/collections/sequtils.nim#L1196" class="link-seesrc" target="_blank" >Edit</a>&nbsp;&nbsp;
</dd>
</div>
</div>
</dl>
</div>
<div class="section" id="18">
<h1><a class="toc-backref" href="#18">Templates</a></h1>
<dl class="item">
<div id="allIt-templates-all">
<div id="allIt.t,untyped,untyped">
<dt><pre><span class="Keyword">template</span> <a href="#allIt.t%2Cuntyped%2Cuntyped"><span class="Identifier">allIt</span></a><span class="Other">(</span><span class="Identifier">s</span><span class="Other">,</span> <span class="Identifier">pred</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#bool"><span class="Identifier">bool</span></a></pre></dt>
<dd>
<p>Iterates through a container and checks if every item fulfills the predicate.</p>
<p>Unlike the <a class="reference external" href="#all,openArray[T],proc(T)">all proc</a>, the predicate needs to be an expression using the <tt class="docutils literal"><span class="pre"><span class="Identifier">it</span></span></tt> variable for testing, like: <tt class="docutils literal"><span class="pre"><span class="Identifier">allIt</span><span class="Punctuation">(</span><span class="StringLit">&quot;abba&quot;</span><span class="Punctuation">,</span> <span class="Identifier">it</span> <span class="Operator">==</span> <span class="CharLit">'a'</span><span class="Punctuation">)</span></span></tt>.</p>
<p><strong>See also:</strong></p>
<ul class="simple"><li><a class="reference external" href="#all,openArray[T],proc(T)">all proc</a></li>
<li><a class="reference external" href="#anyIt.t,untyped,untyped">anyIt template</a></li>
</ul>
<p><strong class="examples_text">Example:</strong></p>
<pre class="listing"><span class="Keyword">let</span> <span class="Identifier">numbers</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">4</span><span class="Punctuation">,</span> <span class="DecNumber">5</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="DecNumber">7</span><span class="Punctuation">,</span> <span class="DecNumber">4</span><span class="Punctuation">]</span>
<span class="Identifier">assert</span> <span class="Identifier">numbers</span><span class="Operator">.</span><span class="Identifier">allIt</span><span class="Punctuation">(</span><span class="Identifier">it</span> <span class="Operator">&lt;</span> <span class="DecNumber">10</span><span class="Punctuation">)</span> <span class="Operator">==</span> <span class="Identifier">true</span>
<span class="Identifier">assert</span> <span class="Identifier">numbers</span><span class="Operator">.</span><span class="Identifier">allIt</span><span class="Punctuation">(</span><span class="Identifier">it</span> <span class="Operator">&lt;</span> <span class="DecNumber">9</span><span class="Punctuation">)</span> <span class="Operator">==</span> <span class="Identifier">false</span></pre>
<a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/collections/sequtils.nim#L811" class="link-seesrc" target="_blank">Source</a>&nbsp;&nbsp;
<a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/collections/sequtils.nim#L811" class="link-seesrc" target="_blank" >Edit</a>&nbsp;&nbsp;
</dd>
</div>
</div>
<div id="anyIt-templates-all">
<div id="anyIt.t,untyped,untyped">
<dt><pre><span class="Keyword">template</span> <a href="#anyIt.t%2Cuntyped%2Cuntyped"><span class="Identifier">anyIt</span></a><span class="Other">(</span><span class="Identifier">s</span><span class="Other">,</span> <span class="Identifier">pred</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#bool"><span class="Identifier">bool</span></a></pre></dt>
<dd>
<p>Iterates through a container and checks if at least one item fulfills the predicate.</p>
<p>Unlike the <a class="reference external" href="#any,openArray[T],proc(T)">any proc</a>, the predicate needs to be an expression using the <tt class="docutils literal"><span class="pre"><span class="Identifier">it</span></span></tt> variable for testing, like: <tt class="docutils literal"><span class="pre"><span class="Identifier">anyIt</span><span class="Punctuation">(</span><span class="StringLit">&quot;abba&quot;</span><span class="Punctuation">,</span> <span class="Identifier">it</span> <span class="Operator">==</span> <span class="CharLit">'a'</span><span class="Punctuation">)</span></span></tt>.</p>
<p><strong>See also:</strong></p>
<ul class="simple"><li><a class="reference external" href="#any,openArray[T],proc(T)">any proc</a></li>
<li><a class="reference external" href="#allIt.t,untyped,untyped">allIt template</a></li>
</ul>
<p><strong class="examples_text">Example:</strong></p>
<pre class="listing"><span class="Keyword">let</span> <span class="Identifier">numbers</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">4</span><span class="Punctuation">,</span> <span class="DecNumber">5</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="DecNumber">7</span><span class="Punctuation">,</span> <span class="DecNumber">4</span><span class="Punctuation">]</span>
<span class="Identifier">assert</span> <span class="Identifier">numbers</span><span class="Operator">.</span><span class="Identifier">anyIt</span><span class="Punctuation">(</span><span class="Identifier">it</span> <span class="Operator">&gt;</span> <span class="DecNumber">8</span><span class="Punctuation">)</span> <span class="Operator">==</span> <span class="Identifier">true</span>
<span class="Identifier">assert</span> <span class="Identifier">numbers</span><span class="Operator">.</span><span class="Identifier">anyIt</span><span class="Punctuation">(</span><span class="Identifier">it</span> <span class="Operator">&gt;</span> <span class="DecNumber">9</span><span class="Punctuation">)</span> <span class="Operator">==</span> <span class="Identifier">false</span></pre>
<a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/collections/sequtils.nim#L853" class="link-seesrc" target="_blank">Source</a>&nbsp;&nbsp;
<a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/collections/sequtils.nim#L853" class="link-seesrc" target="_blank" >Edit</a>&nbsp;&nbsp;
</dd>
</div>
</div>
<div id="applyIt-templates-all">
<div id="applyIt.t,untyped,untyped">
<dt><pre><span class="Keyword">template</span> <a href="#applyIt.t%2Cuntyped%2Cuntyped"><span class="Identifier">applyIt</span></a><span class="Other">(</span><span class="Identifier">varSeq</span><span class="Other">,</span> <span class="Identifier">op</span><span class="Other">:</span> <a href="system.html#untyped"><span class="Identifier">untyped</span></a><span class="Other">)</span></pre></dt>
<dd>
<p>Convenience template around the mutable <tt class="docutils literal"><span class="pre"><span class="Identifier">apply</span></span></tt> proc to reduce typing.</p>
<p>The template injects the <tt class="docutils literal"><span class="pre"><span class="Identifier">it</span></span></tt> variable which you can use directly in an expression. The expression has to return the same type as the elements of the sequence you are mutating.</p>
<p><strong>See also:</strong></p>
<ul class="simple"><li><a class="reference external" href="#apply,openArray[T],proc(T)_2">apply proc</a></li>
<li><a class="reference external" href="#mapIt.t,typed,untyped">mapIt template</a></li>
</ul>
<p><strong class="examples_text">Example:</strong></p>
<pre class="listing"><span class="Keyword">var</span> <span class="Identifier">nums</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="Identifier">nums</span><span class="Operator">.</span><span class="Identifier">applyIt</span><span class="Punctuation">(</span><span class="Identifier">it</span> <span class="Operator">*</span> <span class="DecNumber">3</span><span class="Punctuation">)</span>
<span class="Identifier">assert</span> <span class="Identifier">nums</span><span class="Punctuation">[</span><span class="DecNumber">0</span><span class="Punctuation">]</span> <span class="Operator">+</span> <span class="Identifier">nums</span><span class="Punctuation">[</span><span class="DecNumber">3</span><span class="Punctuation">]</span> <span class="Operator">==</span> <span class="DecNumber">15</span></pre>
<a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/collections/sequtils.nim#L1134" class="link-seesrc" target="_blank">Source</a>&nbsp;&nbsp;
<a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/collections/sequtils.nim#L1134" class="link-seesrc" target="_blank" >Edit</a>&nbsp;&nbsp;
</dd>
</div>
</div>
<div id="countIt-templates-all">
<div id="countIt.t,untyped,untyped">
<dt><pre><span class="Keyword">template</span> <a href="#countIt.t%2Cuntyped%2Cuntyped"><span class="Identifier">countIt</span></a><span class="Other">(</span><span class="Identifier">s</span><span class="Other">,</span> <span class="Identifier">pred</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#int"><span class="Identifier">int</span></a></pre></dt>
<dd>
<p>Returns a count of all the items that fulfill the predicate.</p>
<p>The predicate needs to be an expression using the <tt class="docutils literal"><span class="pre"><span class="Identifier">it</span></span></tt> variable for testing, like: <tt class="docutils literal"><span class="pre"><span class="Identifier">countIt</span><span class="Punctuation">(</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="Punctuation">,</span> <span class="Identifier">it</span> <span class="Operator">&gt;</span> <span class="DecNumber">2</span><span class="Punctuation">)</span></span></tt>. </p>
<p><strong class="examples_text">Example:</strong></p>
<pre class="listing"><span class="Keyword">let</span> <span class="Identifier">numbers</span> <span class="Operator">=</span> <span class="Operator">@</span><span class="Punctuation">[</span><span class="Operator">-</span><span class="DecNumber">3</span><span class="Punctuation">,</span> <span class="Operator">-</span><span class="DecNumber">2</span><span class="Punctuation">,</span> <span class="Operator">-</span><span class="DecNumber">1</span><span class="Punctuation">,</span> <span class="DecNumber">0</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="Keyword">iterator</span> <span class="Identifier">iota</span><span class="Punctuation">(</span><span class="Identifier">n</span><span class="Punctuation">:</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="Keyword">for</span> <span class="Identifier">i</span> <span class="Keyword">in</span> <span class="FloatNumber">0.</span><span class="Operator">.&lt;</span><span class="Identifier">n</span><span class="Punctuation">:</span> <span class="Keyword">yield</span> <span class="Identifier">i</span>
<span class="Identifier">assert</span> <span class="Identifier">numbers</span><span class="Operator">.</span><span class="Identifier">countIt</span><span class="Punctuation">(</span><span class="Identifier">it</span> <span class="Operator">&lt;</span> <span class="DecNumber">0</span><span class="Punctuation">)</span> <span class="Operator">==</span> <span class="DecNumber">3</span>
<span class="Identifier">assert</span> <span class="Identifier">countIt</span><span class="Punctuation">(</span><span class="Identifier">iota</span><span class="Punctuation">(</span><span class="DecNumber">10</span><span class="Punctuation">)</span><span class="Punctuation">,</span> <span class="Identifier">it</span> <span class="Operator">&lt;</span> <span class="DecNumber">2</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/collections/sequtils.nim#L775" class="link-seesrc" target="_blank">Source</a>&nbsp;&nbsp;
<a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/collections/sequtils.nim#L775" class="link-seesrc" target="_blank" >Edit</a>&nbsp;&nbsp;
</dd>
</div>
</div>
<div id="filterIt-templates-all">
<div id="filterIt.t,untyped,untyped">
<dt><pre><span class="Keyword">template</span> <a href="#filterIt.t%2Cuntyped%2Cuntyped"><span class="Identifier">filterIt</span></a><span class="Other">(</span><span class="Identifier">s</span><span class="Other">,</span> <span class="Identifier">pred</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>Returns a new sequence with all the items of <tt class="docutils literal"><span class="pre"><span class="Identifier">s</span></span></tt> that fulfill the predicate <tt class="docutils literal"><span class="pre"><span class="Identifier">pred</span></span></tt>.</p>
<p>Unlike the <a class="reference external" href="#filter,openArray[T],proc(T)">filter proc</a> and <a class="reference external" href="#filter.i,openArray[T],proc(T)">filter iterator</a>, the predicate needs to be an expression using the <tt class="docutils literal"><span class="pre"><span class="Identifier">it</span></span></tt> variable for testing, like: <tt class="docutils literal"><span class="pre"><span class="Identifier">filterIt</span><span class="Punctuation">(</span><span class="StringLit">&quot;abcxyz&quot;</span><span class="Punctuation">,</span> <span class="Identifier">it</span> <span class="Operator">==</span> <span class="CharLit">'x'</span><span class="Punctuation">)</span></span></tt>.</p>
<p>Instead of using <tt class="docutils literal"><span class="pre"><span class="Identifier">mapIt</span></span></tt> and <tt class="docutils literal"><span class="pre"><span class="Identifier">filterIt</span></span></tt>, consider using the <tt class="docutils literal"><span class="pre"><span class="Identifier">collect</span></span></tt> macro from the <tt class="docutils literal"><span class="pre"><span class="Identifier">sugar</span></span></tt> module.</p>
<p><strong>See also:</strong></p>
<ul class="simple"><li><a class="reference external" href="sugar.html#collect.m%2Cuntyped%2Cuntyped">sugar.collect macro</a></li>
<li><a class="reference external" href="#filter,openArray[T],proc(T)">filter proc</a></li>
<li><a class="reference external" href="#filter.i,openArray[T],proc(T)">filter iterator</a></li>
</ul>
<p><strong class="examples_text">Example:</strong></p>
<pre class="listing"><span class="Keyword">let</span>
<span class="Identifier">temperatures</span> <span class="Operator">=</span> <span class="Operator">@</span><span class="Punctuation">[</span><span class="Operator">-</span><span class="FloatNumber">272.15</span><span class="Punctuation">,</span> <span class="Operator">-</span><span class="FloatNumber">2.0</span><span class="Punctuation">,</span> <span class="FloatNumber">24.5</span><span class="Punctuation">,</span> <span class="FloatNumber">44.31</span><span class="Punctuation">,</span> <span class="FloatNumber">99.9</span><span class="Punctuation">,</span> <span class="Operator">-</span><span class="FloatNumber">113.44</span><span class="Punctuation">]</span>
<span class="Identifier">acceptable</span> <span class="Operator">=</span> <span class="Identifier">temperatures</span><span class="Operator">.</span><span class="Identifier">filterIt</span><span class="Punctuation">(</span><span class="Identifier">it</span> <span class="Operator">&lt;</span> <span class="DecNumber">50</span> <span class="Keyword">and</span> <span class="Identifier">it</span> <span class="Operator">&gt;</span> <span class="Operator">-</span><span class="DecNumber">10</span><span class="Punctuation">)</span>
<span class="Identifier">notAcceptable</span> <span class="Operator">=</span> <span class="Identifier">temperatures</span><span class="Operator">.</span><span class="Identifier">filterIt</span><span class="Punctuation">(</span><span class="Identifier">it</span> <span class="Operator">&gt;</span> <span class="DecNumber">50</span> <span class="Keyword">or</span> <span class="Identifier">it</span> <span class="Operator">&lt;</span> <span class="Operator">-</span><span class="DecNumber">10</span><span class="Punctuation">)</span>
<span class="Identifier">assert</span> <span class="Identifier">acceptable</span> <span class="Operator">==</span> <span class="Operator">@</span><span class="Punctuation">[</span><span class="Operator">-</span><span class="FloatNumber">2.0</span><span class="Punctuation">,</span> <span class="FloatNumber">24.5</span><span class="Punctuation">,</span> <span class="FloatNumber">44.31</span><span class="Punctuation">]</span>
<span class="Identifier">assert</span> <span class="Identifier">notAcceptable</span> <span class="Operator">==</span> <span class="Operator">@</span><span class="Punctuation">[</span><span class="Operator">-</span><span class="FloatNumber">272.15</span><span class="Punctuation">,</span> <span class="FloatNumber">99.9</span><span class="Punctuation">,</span> <span class="Operator">-</span><span class="FloatNumber">113.44</span><span class="Punctuation">]</span></pre>
<a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/collections/sequtils.nim#L715" class="link-seesrc" target="_blank">Source</a>&nbsp;&nbsp;
<a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/collections/sequtils.nim#L715" class="link-seesrc" target="_blank" >Edit</a>&nbsp;&nbsp;
</dd>
</div>
</div>
<div id="findIt-templates-all">
<div id="findIt.t,untyped,untyped">
<dt><pre><span class="Keyword">template</span> <a href="#findIt.t%2Cuntyped%2Cuntyped"><span class="Identifier">findIt</span></a><span class="Other">(</span><span class="Identifier">s</span><span class="Other">,</span> <span class="Identifier">predicate</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#int"><span class="Identifier">int</span></a></pre></dt>
<dd>
<p>Iterates through a container and returns the index of the first item that fulfills the predicate, or -1</p>
<p>Unlike the <tt class="docutils literal"><span class="pre"><span class="Identifier">find</span></span></tt>, the predicate needs to be an expression using the <tt class="docutils literal"><span class="pre"><span class="Identifier">it</span></span></tt> variable for testing, like: <tt class="docutils literal"><span class="pre"><span class="Identifier">findIt</span><span class="Punctuation">(</span><span class="Punctuation">[</span><span class="DecNumber">3</span><span class="Punctuation">,</span> <span class="DecNumber">2</span><span class="Punctuation">,</span> <span class="DecNumber">1</span><span class="Punctuation">]</span><span class="Punctuation">,</span> <span class="Identifier">it</span> <span class="Operator">==</span> <span class="DecNumber">2</span><span class="Punctuation">)</span></span></tt>.</p>
<a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/collections/sequtils.nim#L329" class="link-seesrc" target="_blank">Source</a>&nbsp;&nbsp;
<a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/collections/sequtils.nim#L329" class="link-seesrc" target="_blank" >Edit</a>&nbsp;&nbsp;
</dd>
</div>
</div>
<div id="foldl-templates-all">
<div id="foldl.t,,,">
<dt><pre><span class="Keyword">template</span> <a href="#foldl.t%2C%2C%2C"><span class="Identifier">foldl</span></a><span class="Other">(</span><span class="Identifier">sequence</span><span class="Other">,</span> <span class="Identifier">operation</span><span class="Other">,</span> <span class="Identifier">first</span><span class="Other">)</span><span class="Other">:</span> <a href="system.html#untyped"><span class="Identifier">untyped</span></a></pre></dt>
<dd>
<p>Template to fold a sequence from left to right, returning the accumulation.</p>
<p>This version of <tt class="docutils literal"><span class="pre"><span class="Identifier">foldl</span></span></tt> gets a <strong>starting parameter</strong>. This makes it possible to accumulate the sequence into a different type than the sequence elements.</p>
<p>The <tt class="docutils literal"><span class="pre"><span class="Identifier">operation</span></span></tt> parameter should be an expression which uses the variables <tt class="docutils literal"><span class="pre"><span class="Identifier">a</span></span></tt> and <tt class="docutils literal"><span class="pre"><span class="Identifier">b</span></span></tt> for each step of the fold. The <tt class="docutils literal"><span class="pre"><span class="Identifier">first</span></span></tt> parameter is the start value (the first <tt class="docutils literal"><span class="pre"><span class="Identifier">a</span></span></tt>) and therefore defines the type of the result.</p>
<p><strong>See also:</strong></p>
<ul class="simple"><li><a class="reference external" href="#foldr.t,untyped,untyped">foldr template</a></li>
</ul>
<p><strong class="examples_text">Example:</strong></p>
<pre class="listing"><span class="Keyword">let</span>
<span class="Identifier">numbers</span> <span class="Operator">=</span> <span class="Operator">@</span><span class="Punctuation">[</span><span class="DecNumber">0</span><span class="Punctuation">,</span> <span class="DecNumber">8</span><span class="Punctuation">,</span> <span class="DecNumber">1</span><span class="Punctuation">,</span> <span class="DecNumber">5</span><span class="Punctuation">]</span>
<span class="Identifier">digits</span> <span class="Operator">=</span> <span class="Identifier">foldl</span><span class="Punctuation">(</span><span class="Identifier">numbers</span><span class="Punctuation">,</span> <span class="Identifier">a</span> <span class="Operator">&amp;</span> <span class="Punctuation">(</span><span class="Identifier">chr</span><span class="Punctuation">(</span><span class="Identifier">b</span> <span class="Operator">+</span> <span class="Identifier">ord</span><span class="Punctuation">(</span><span class="Punctuation">'</span><span class="DecNumber">0'</span><span class="Punctuation">)</span><span class="Punctuation">)</span><span class="Punctuation">)</span><span class="Punctuation">,</span> <span class="StringLit">&quot;&quot;</span><span class="Punctuation">)</span>
<span class="Identifier">assert</span> <span class="Identifier">digits</span> <span class="Operator">==</span> <span class="StringLit">&quot;0815&quot;</span></pre>
<a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/collections/sequtils.nim#L1000" class="link-seesrc" target="_blank">Source</a>&nbsp;&nbsp;
<a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/collections/sequtils.nim#L1000" class="link-seesrc" target="_blank" >Edit</a>&nbsp;&nbsp;
</dd>
</div>
<div id="foldl.t,untyped,untyped">
<dt><pre><span class="Keyword">template</span> <a href="#foldl.t%2Cuntyped%2Cuntyped"><span class="Identifier">foldl</span></a><span class="Other">(</span><span class="Identifier">sequence</span><span class="Other">,</span> <span class="Identifier">operation</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>Template to fold a sequence from left to right, returning the accumulation.</p>
<p>The sequence is required to have at least a single element. Debug versions of your program will assert in this situation but release versions will happily go ahead. If the sequence has a single element it will be returned without applying <tt class="docutils literal"><span class="pre"><span class="Identifier">operation</span></span></tt>.</p>
<p>The <tt class="docutils literal"><span class="pre"><span class="Identifier">operation</span></span></tt> parameter should be an expression which uses the variables <tt class="docutils literal"><span class="pre"><span class="Identifier">a</span></span></tt> and <tt class="docutils literal"><span class="pre"><span class="Identifier">b</span></span></tt> for each step of the fold. Since this is a left fold, for non associative binary operations like subtraction think that the sequence of numbers 1, 2 and 3 will be parenthesized as (((1) - 2) - 3).</p>
<p><strong>See also:</strong></p>
<ul class="simple"><li><a class="reference external" href="#foldl.t,,,">foldl template</a> with a starting parameter</li>
<li><a class="reference external" href="#foldr.t,untyped,untyped">foldr template</a></li>
</ul>
<p><strong class="examples_text">Example:</strong></p>
<pre class="listing"><span class="Keyword">let</span>
<span class="Identifier">numbers</span> <span class="Operator">=</span> <span class="Operator">@</span><span class="Punctuation">[</span><span class="DecNumber">5</span><span class="Punctuation">,</span> <span class="DecNumber">9</span><span class="Punctuation">,</span> <span class="DecNumber">11</span><span class="Punctuation">]</span>
<span class="Identifier">addition</span> <span class="Operator">=</span> <span class="Identifier">foldl</span><span class="Punctuation">(</span><span class="Identifier">numbers</span><span class="Punctuation">,</span> <span class="Identifier">a</span> <span class="Operator">+</span> <span class="Identifier">b</span><span class="Punctuation">)</span>
<span class="Identifier">subtraction</span> <span class="Operator">=</span> <span class="Identifier">foldl</span><span class="Punctuation">(</span><span class="Identifier">numbers</span><span class="Punctuation">,</span> <span class="Identifier">a</span> <span class="Operator">-</span> <span class="Identifier">b</span><span class="Punctuation">)</span>
<span class="Identifier">multiplication</span> <span class="Operator">=</span> <span class="Identifier">foldl</span><span class="Punctuation">(</span><span class="Identifier">numbers</span><span class="Punctuation">,</span> <span class="Identifier">a</span> <span class="Operator">*</span> <span class="Identifier">b</span><span class="Punctuation">)</span>
<span class="Identifier">words</span> <span class="Operator">=</span> <span class="Operator">@</span><span class="Punctuation">[</span><span class="StringLit">&quot;nim&quot;</span><span class="Punctuation">,</span> <span class="StringLit">&quot;is&quot;</span><span class="Punctuation">,</span> <span class="StringLit">&quot;cool&quot;</span><span class="Punctuation">]</span>
<span class="Identifier">concatenation</span> <span class="Operator">=</span> <span class="Identifier">foldl</span><span class="Punctuation">(</span><span class="Identifier">words</span><span class="Punctuation">,</span> <span class="Identifier">a</span> <span class="Operator">&amp;</span> <span class="Identifier">b</span><span class="Punctuation">)</span>
<span class="Identifier">procs</span> <span class="Operator">=</span> <span class="Operator">@</span><span class="Punctuation">[</span><span class="StringLit">&quot;proc&quot;</span><span class="Punctuation">,</span> <span class="StringLit">&quot;Is&quot;</span><span class="Punctuation">,</span> <span class="StringLit">&quot;Also&quot;</span><span class="Punctuation">,</span> <span class="StringLit">&quot;Fine&quot;</span><span class="Punctuation">]</span>
<span class="Keyword">func</span> <span class="Identifier">foo</span><span class="Punctuation">(</span><span class="Identifier">acc</span><span class="Punctuation">,</span> <span class="Identifier">cur</span><span class="Punctuation">:</span> <span class="Identifier">string</span><span class="Punctuation">)</span><span class="Punctuation">:</span> <span class="Identifier">string</span> <span class="Operator">=</span>
<span class="Identifier">result</span> <span class="Operator">=</span> <span class="Identifier">acc</span> <span class="Operator">&amp;</span> <span class="Identifier">cur</span>
<span class="Identifier">assert</span> <span class="Identifier">addition</span> <span class="Operator">==</span> <span class="DecNumber">25</span><span class="Punctuation">,</span> <span class="StringLit">&quot;Addition is (((5)+9)+11)&quot;</span>
<span class="Identifier">assert</span> <span class="Identifier">subtraction</span> <span class="Operator">==</span> <span class="Operator">-</span><span class="DecNumber">15</span><span class="Punctuation">,</span> <span class="StringLit">&quot;Subtraction is (((5)-9)-11)&quot;</span>
<span class="Identifier">assert</span> <span class="Identifier">multiplication</span> <span class="Operator">==</span> <span class="DecNumber">495</span><span class="Punctuation">,</span> <span class="StringLit">&quot;Multiplication is (((5)*9)*11)&quot;</span>
<span class="Identifier">assert</span> <span class="Identifier">concatenation</span> <span class="Operator">==</span> <span class="StringLit">&quot;nimiscool&quot;</span>
<span class="Identifier">assert</span> <span class="Identifier">foldl</span><span class="Punctuation">(</span><span class="Identifier">procs</span><span class="Punctuation">,</span> <span class="Identifier">foo</span><span class="Punctuation">(</span><span class="Identifier">a</span><span class="Punctuation">,</span> <span class="Identifier">b</span><span class="Punctuation">)</span><span class="Punctuation">)</span> <span class="Operator">==</span> <span class="StringLit">&quot;procIsAlsoFine&quot;</span></pre>
<a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/collections/sequtils.nim#L951" class="link-seesrc" target="_blank">Source</a>&nbsp;&nbsp;
<a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/collections/sequtils.nim#L951" class="link-seesrc" target="_blank" >Edit</a>&nbsp;&nbsp;
</dd>
</div>
</div>
<div id="foldr-templates-all">
<div id="foldr.t,untyped,untyped">
<dt><pre><span class="Keyword">template</span> <a href="#foldr.t%2Cuntyped%2Cuntyped"><span class="Identifier">foldr</span></a><span class="Other">(</span><span class="Identifier">sequence</span><span class="Other">,</span> <span class="Identifier">operation</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>Template to fold a sequence from right to left, returning the accumulation.</p>
<p>The sequence is required to have at least a single element. Debug versions of your program will assert in this situation but release versions will happily go ahead. If the sequence has a single element it will be returned without applying <tt class="docutils literal"><span class="pre"><span class="Identifier">operation</span></span></tt>.</p>
<p>The <tt class="docutils literal"><span class="pre"><span class="Identifier">operation</span></span></tt> parameter should be an expression which uses the variables <tt class="docutils literal"><span class="pre"><span class="Identifier">a</span></span></tt> and <tt class="docutils literal"><span class="pre"><span class="Identifier">b</span></span></tt> for each step of the fold. Since this is a right fold, for non associative binary operations like subtraction think that the sequence of numbers 1, 2 and 3 will be parenthesized as (1 - (2 - (3))).</p>
<p><strong>See also:</strong></p>
<ul class="simple"><li><a class="reference external" href="#foldl.t,untyped,untyped">foldl template</a></li>
<li><a class="reference external" href="#foldl.t,,,">foldl template</a> with a starting parameter</li>
</ul>
<p><strong class="examples_text">Example:</strong></p>
<pre class="listing"><span class="Keyword">let</span>
<span class="Identifier">numbers</span> <span class="Operator">=</span> <span class="Operator">@</span><span class="Punctuation">[</span><span class="DecNumber">5</span><span class="Punctuation">,</span> <span class="DecNumber">9</span><span class="Punctuation">,</span> <span class="DecNumber">11</span><span class="Punctuation">]</span>
<span class="Identifier">addition</span> <span class="Operator">=</span> <span class="Identifier">foldr</span><span class="Punctuation">(</span><span class="Identifier">numbers</span><span class="Punctuation">,</span> <span class="Identifier">a</span> <span class="Operator">+</span> <span class="Identifier">b</span><span class="Punctuation">)</span>
<span class="Identifier">subtraction</span> <span class="Operator">=</span> <span class="Identifier">foldr</span><span class="Punctuation">(</span><span class="Identifier">numbers</span><span class="Punctuation">,</span> <span class="Identifier">a</span> <span class="Operator">-</span> <span class="Identifier">b</span><span class="Punctuation">)</span>
<span class="Identifier">multiplication</span> <span class="Operator">=</span> <span class="Identifier">foldr</span><span class="Punctuation">(</span><span class="Identifier">numbers</span><span class="Punctuation">,</span> <span class="Identifier">a</span> <span class="Operator">*</span> <span class="Identifier">b</span><span class="Punctuation">)</span>
<span class="Identifier">words</span> <span class="Operator">=</span> <span class="Operator">@</span><span class="Punctuation">[</span><span class="StringLit">&quot;nim&quot;</span><span class="Punctuation">,</span> <span class="StringLit">&quot;is&quot;</span><span class="Punctuation">,</span> <span class="StringLit">&quot;cool&quot;</span><span class="Punctuation">]</span>
<span class="Identifier">concatenation</span> <span class="Operator">=</span> <span class="Identifier">foldr</span><span class="Punctuation">(</span><span class="Identifier">words</span><span class="Punctuation">,</span> <span class="Identifier">a</span> <span class="Operator">&amp;</span> <span class="Identifier">b</span><span class="Punctuation">)</span>
<span class="Identifier">assert</span> <span class="Identifier">addition</span> <span class="Operator">==</span> <span class="DecNumber">25</span><span class="Punctuation">,</span> <span class="StringLit">&quot;Addition is (5+(9+(11)))&quot;</span>
<span class="Identifier">assert</span> <span class="Identifier">subtraction</span> <span class="Operator">==</span> <span class="DecNumber">7</span><span class="Punctuation">,</span> <span class="StringLit">&quot;Subtraction is (5-(9-(11)))&quot;</span>
<span class="Identifier">assert</span> <span class="Identifier">multiplication</span> <span class="Operator">==</span> <span class="DecNumber">495</span><span class="Punctuation">,</span> <span class="StringLit">&quot;Multiplication is (5*(9*(11)))&quot;</span>
<span class="Identifier">assert</span> <span class="Identifier">concatenation</span> <span class="Operator">==</span> <span class="StringLit">&quot;nimiscool&quot;</span></pre>
<a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/collections/sequtils.nim#L1027" class="link-seesrc" target="_blank">Source</a>&nbsp;&nbsp;
<a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/collections/sequtils.nim#L1027" class="link-seesrc" target="_blank" >Edit</a>&nbsp;&nbsp;
</dd>
</div>
</div>
<div id="keepItIf-templates-all">
<div id="keepItIf.t,seq,untyped">
<dt><pre><span class="Keyword">template</span> <a href="#keepItIf.t%2Cseq%2Cuntyped"><span class="Identifier">keepItIf</span></a><span class="Other">(</span><span class="Identifier">varSeq</span><span class="Other">:</span> <a href="system.html#seq"><span class="Identifier">seq</span></a><span class="Other">;</span> <span class="Identifier">pred</span><span class="Other">:</span> <a href="system.html#untyped"><span class="Identifier">untyped</span></a><span class="Other">)</span></pre></dt>
<dd>
<p>Keeps the items in the passed sequence (must be declared as a <tt class="docutils literal"><span class="pre"><span class="Keyword">var</span></span></tt>) if they fulfill the predicate.</p>
<p>Unlike the <a class="reference external" href="#keepIf,seq[T],proc(T)">keepIf proc</a>, the predicate needs to be an expression using the <tt class="docutils literal"><span class="pre"><span class="Identifier">it</span></span></tt> variable for testing, like: <tt class="docutils literal"><span class="pre"><span class="Identifier">keepItIf</span><span class="Punctuation">(</span><span class="StringLit">&quot;abcxyz&quot;</span><span class="Punctuation">,</span> <span class="Identifier">it</span> <span class="Operator">==</span> <span class="CharLit">'x'</span><span class="Punctuation">)</span></span></tt>.</p>
<p><strong>See also:</strong></p>
<ul class="simple"><li><a class="reference external" href="#keepIf,seq[T],proc(T)">keepIf proc</a></li>
<li><a class="reference external" href="#filterIt.t,untyped,untyped">filterIt template</a></li>
</ul>
<p><strong class="examples_text">Example:</strong></p>
<pre class="listing"><span class="Keyword">var</span> <span class="Identifier">candidates</span> <span class="Operator">=</span> <span class="Operator">@</span><span class="Punctuation">[</span><span class="StringLit">&quot;foo&quot;</span><span class="Punctuation">,</span> <span class="StringLit">&quot;bar&quot;</span><span class="Punctuation">,</span> <span class="StringLit">&quot;baz&quot;</span><span class="Punctuation">,</span> <span class="StringLit">&quot;foobar&quot;</span><span class="Punctuation">]</span>
<span class="Identifier">candidates</span><span class="Operator">.</span><span class="Identifier">keepItIf</span><span class="Punctuation">(</span><span class="Identifier">it</span><span class="Operator">.</span><span class="Identifier">len</span> <span class="Operator">==</span> <span class="DecNumber">3</span> <span class="Keyword">and</span> <span class="Identifier">it</span><span class="Punctuation">[</span><span class="DecNumber">0</span><span class="Punctuation">]</span> <span class="Operator">==</span> <span class="CharLit">'b'</span><span class="Punctuation">)</span>
<span class="Identifier">assert</span> <span class="Identifier">candidates</span> <span class="Operator">==</span> <span class="Operator">@</span><span class="Punctuation">[</span><span class="StringLit">&quot;bar&quot;</span><span class="Punctuation">,</span> <span class="StringLit">&quot;baz&quot;</span><span class="Punctuation">]</span></pre>
<a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/collections/sequtils.nim#L745" class="link-seesrc" target="_blank">Source</a>&nbsp;&nbsp;
<a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/collections/sequtils.nim#L745" class="link-seesrc" target="_blank" >Edit</a>&nbsp;&nbsp;
</dd>
</div>
</div>
<div id="mapIt-templates-all">
<div id="mapIt.t,typed,untyped">
<dt><pre><span class="Keyword">template</span> <a href="#mapIt.t%2Ctyped%2Cuntyped"><span class="Identifier">mapIt</span></a><span class="Other">(</span><span class="Identifier">s</span><span class="Other">:</span> <a href="system.html#typed"><span class="Identifier">typed</span></a><span class="Other">;</span> <span class="Identifier">op</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>Returns a new sequence with the results of the <tt class="docutils literal"><span class="pre"><span class="Identifier">op</span></span></tt> proc applied to every item in the container <tt class="docutils literal"><span class="pre"><span class="Identifier">s</span></span></tt>.</p>
<p>Since the input is not modified you can use it to transform the type of the elements in the input container.</p>
<p>The template injects the <tt class="docutils literal"><span class="pre"><span class="Identifier">it</span></span></tt> variable which you can use directly in an expression.</p>
<p>Instead of using <tt class="docutils literal"><span class="pre"><span class="Identifier">mapIt</span></span></tt> and <tt class="docutils literal"><span class="pre"><span class="Identifier">filterIt</span></span></tt>, consider using the <tt class="docutils literal"><span class="pre"><span class="Identifier">collect</span></span></tt> macro from the <tt class="docutils literal"><span class="pre"><span class="Identifier">sugar</span></span></tt> module.</p>
<p><strong>See also:</strong></p>
<ul class="simple"><li><a class="reference external" href="sugar.html#collect.m%2Cuntyped%2Cuntyped">sugar.collect macro</a></li>
<li><a class="reference external" href="#map,openArray[T],proc(T)">map proc</a></li>
<li><a class="reference external" href="#applyIt.t,untyped,untyped">applyIt template</a> for the in-place version</li>
</ul>
<p><strong class="examples_text">Example:</strong></p>
<pre class="listing"><span class="Keyword">let</span>
<span class="Identifier">nums</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="Identifier">strings</span> <span class="Operator">=</span> <span class="Identifier">nums</span><span class="Operator">.</span><span class="Identifier">mapIt</span><span class="Punctuation">(</span><span class="Operator">$</span><span class="Punctuation">(</span><span class="DecNumber">4</span> <span class="Operator">*</span> <span class="Identifier">it</span><span class="Punctuation">)</span><span class="Punctuation">)</span>
<span class="Identifier">assert</span> <span class="Identifier">strings</span> <span class="Operator">==</span> <span class="Operator">@</span><span class="Punctuation">[</span><span class="StringLit">&quot;4&quot;</span><span class="Punctuation">,</span> <span class="StringLit">&quot;8&quot;</span><span class="Punctuation">,</span> <span class="StringLit">&quot;12&quot;</span><span class="Punctuation">,</span> <span class="StringLit">&quot;16&quot;</span><span class="Punctuation">]</span></pre>
<a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/collections/sequtils.nim#L1069" class="link-seesrc" target="_blank">Source</a>&nbsp;&nbsp;
<a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/collections/sequtils.nim#L1069" class="link-seesrc" target="_blank" >Edit</a>&nbsp;&nbsp;
</dd>
</div>
</div>
<div id="newSeqWith-templates-all">
<div id="newSeqWith.t,int,untyped">
<dt><pre><span class="Keyword">template</span> <a href="#newSeqWith.t%2Cint%2Cuntyped"><span class="Identifier">newSeqWith</span></a><span class="Other">(</span><span class="Identifier">len</span><span class="Other">:</span> <a href="system.html#int"><span class="Identifier">int</span></a><span class="Other">;</span> <span class="Identifier">init</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>Creates a new <tt class="docutils literal"><span class="pre"><span class="Identifier">seq</span></span></tt> of length <tt class="docutils literal"><span class="pre"><span class="Identifier">len</span></span></tt>, calling <tt class="docutils literal"><span class="pre"><span class="Identifier">init</span></span></tt> to initialize each value of the seq.</p>
<p>Useful for creating &quot;2D&quot; seqs - seqs containing other seqs or to populate fields of the created seq.</p>
<p><strong class="examples_text">Example:</strong></p>
<pre class="listing"><span class="Comment">## Creates a seq containing 5 bool seqs, each of length of 3.</span>
<span class="Keyword">var</span> <span class="Identifier">seq2D</span> <span class="Operator">=</span> <span class="Identifier">newSeqWith</span><span class="Punctuation">(</span><span class="DecNumber">5</span><span class="Punctuation">,</span> <span class="Identifier">newSeq</span><span class="Punctuation">[</span><span class="Identifier">bool</span><span class="Punctuation">]</span><span class="Punctuation">(</span><span class="DecNumber">3</span><span class="Punctuation">)</span><span class="Punctuation">)</span>
<span class="Identifier">assert</span> <span class="Identifier">seq2D</span><span class="Operator">.</span><span class="Identifier">len</span> <span class="Operator">==</span> <span class="DecNumber">5</span>
<span class="Identifier">assert</span> <span class="Identifier">seq2D</span><span class="Punctuation">[</span><span class="DecNumber">0</span><span class="Punctuation">]</span><span class="Operator">.</span><span class="Identifier">len</span> <span class="Operator">==</span> <span class="DecNumber">3</span>
<span class="Identifier">assert</span> <span class="Identifier">seq2D</span><span class="Punctuation">[</span><span class="DecNumber">4</span><span class="Punctuation">]</span><span class="Punctuation">[</span><span class="DecNumber">2</span><span class="Punctuation">]</span> <span class="Operator">==</span> <span class="Identifier">false</span>
<span class="Comment">## Creates a seq with random numbers</span>
<span class="Keyword">import</span> <span class="Identifier">std</span><span class="Operator">/</span><span class="Identifier">random</span>
<span class="Keyword">var</span> <span class="Identifier">seqRand</span> <span class="Operator">=</span> <span class="Identifier">newSeqWith</span><span class="Punctuation">(</span><span class="DecNumber">20</span><span class="Punctuation">,</span> <span class="Identifier">rand</span><span class="Punctuation">(</span><span class="FloatNumber">1.0</span><span class="Punctuation">)</span><span class="Punctuation">)</span>
<span class="Identifier">assert</span> <span class="Identifier">seqRand</span><span class="Punctuation">[</span><span class="DecNumber">0</span><span class="Punctuation">]</span> <span class="Operator">!=</span> <span class="Identifier">seqRand</span><span class="Punctuation">[</span><span class="DecNumber">1</span><span class="Punctuation">]</span></pre>
<a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/collections/sequtils.nim#L1155" class="link-seesrc" target="_blank">Source</a>&nbsp;&nbsp;
<a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/collections/sequtils.nim#L1155" class="link-seesrc" target="_blank" >Edit</a>&nbsp;&nbsp;
</dd>
</div>
</div>
<div id="toSeq-templates-all">
<div id="toSeq.t,untyped">
<dt><pre><span class="Keyword">template</span> <a href="#toSeq.t%2Cuntyped"><span class="Identifier">toSeq</span></a><span class="Other">(</span><span class="Identifier">iter</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>
Transforms any iterable (anything that can be iterated over, e.g. with a for-loop) into a sequence.
<p><strong class="examples_text">Example:</strong></p>
<pre class="listing"><span class="Keyword">let</span>
<span class="Identifier">myRange</span> <span class="Operator">=</span> <span class="FloatNumber">1.</span><span class="Operator">.</span><span class="DecNumber">5</span>
<span class="Identifier">mySet</span><span class="Punctuation">:</span> <span class="Identifier">set</span><span class="Punctuation">[</span><span class="Identifier">int8</span><span class="Punctuation">]</span> <span class="Operator">=</span> <span class="Punctuation">{</span><span class="DecNumber">5'i8</span><span class="Punctuation">,</span> <span class="DecNumber">3</span><span class="Punctuation">,</span> <span class="DecNumber">1</span><span class="Punctuation">}</span>
<span class="Identifier">assert</span> <span class="Identifier">typeof</span><span class="Punctuation">(</span><span class="Identifier">myRange</span><span class="Punctuation">)</span> <span class="Keyword">is</span> <span class="Identifier">HSlice</span><span class="Punctuation">[</span><span class="Identifier">system</span><span class="Operator">.</span><span class="Identifier">int</span><span class="Punctuation">,</span> <span class="Identifier">system</span><span class="Operator">.</span><span class="Identifier">int</span><span class="Punctuation">]</span>
<span class="Identifier">assert</span> <span class="Identifier">typeof</span><span class="Punctuation">(</span><span class="Identifier">mySet</span><span class="Punctuation">)</span> <span class="Keyword">is</span> <span class="Identifier">set</span><span class="Punctuation">[</span><span class="Identifier">int8</span><span class="Punctuation">]</span>
<span class="Keyword">let</span>
<span class="Identifier">mySeq1</span> <span class="Operator">=</span> <span class="Identifier">toSeq</span><span class="Punctuation">(</span><span class="Identifier">myRange</span><span class="Punctuation">)</span>
<span class="Identifier">mySeq2</span> <span class="Operator">=</span> <span class="Identifier">toSeq</span><span class="Punctuation">(</span><span class="Identifier">mySet</span><span class="Punctuation">)</span>
<span class="Identifier">assert</span> <span class="Identifier">mySeq1</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="Identifier">assert</span> <span class="Identifier">mySeq2</span> <span class="Operator">==</span> <span class="Operator">@</span><span class="Punctuation">[</span><span class="DecNumber">1'i8</span><span class="Punctuation">,</span> <span class="DecNumber">3</span><span class="Punctuation">,</span> <span class="DecNumber">5</span><span class="Punctuation">]</span></pre>
<a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/collections/sequtils.nim#L913" class="link-seesrc" target="_blank">Source</a>&nbsp;&nbsp;
<a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/collections/sequtils.nim#L913" 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-25 20:13:46 UTC</small>
</div>
</div>
</div>
<script defer data-domain="nim-lang.org" src="https://plausible.io/js/plausible.js"></script>
</body>
</html>