Sophie

Sophie

distrib > Mageia > 7 > i586 > media > core-release > by-pkgid > 016232f1d9a3f7bee85855d35a2bca58 > files > 54

elixir-doc-1.7.2-1.mga7.noarch.rpm

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="x-ua-compatible" content="ie=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="generator" content="ExDoc v0.19.1">
    <title>Enumerable – Elixir v1.7.2</title>
    <link rel="stylesheet" href="dist/app-240d7fc7e5.css" />
      <link rel="canonical" href="https://hexdocs.pm/elixir/v1.7/Enumerable.html" />
    <script src="dist/sidebar_items-cdf4e58b19.js"></script>
    
  </head>
  <body data-type="modules">
    <script>try { if(localStorage.getItem('night-mode')) document.body.className += ' night-mode'; } catch (e) { }</script>
    <div class="main">
<button class="sidebar-button sidebar-toggle">
  <span class="icon-menu" aria-hidden="true"></span>
  <span class="sr-only">Toggle Sidebar</span>
</button>
<button class="sidebar-button night-mode-toggle">
  <span class="icon-theme" aria-hidden="true"></span>
  <span class="sr-only">Toggle Theme</span>
</button>
<section class="sidebar">

  <a href="http://elixir-lang.org/docs.html" class="sidebar-projectLink">
    <div class="sidebar-projectDetails">
      <h1 class="sidebar-projectName">
Elixir      </h1>
      <h2 class="sidebar-projectVersion">
        v1.7.2
      </h2>
    </div>
      <img src="assets/logo.png" alt="Elixir" class="sidebar-projectImage">
  </a>

  <form class="sidebar-search" action="search.html">
    <button type="submit" class="search-button">
      <span class="icon-search" aria-hidden="true"></span>
    </button>
    <input name="q" type="text" id="search-list" class="search-input" placeholder="Search" aria-label="Search" autocomplete="off" />
  </form>

  <ul class="sidebar-listNav">
    <li><a id="extras-list" href="#full-list">Pages</a></li>

      <li><a id="modules-list" href="#full-list">Modules</a></li>

      <li><a id="exceptions-list" href="#full-list">Exceptions</a></li>

  </ul>
  <div class="gradient"></div>
  <ul id="full-list" class="sidebar-fullList"></ul>
</section>

<section class="content">
  <div class="content-outer">
    <div id="content" class="content-inner">


      <h1>
        <small class="visible-xs">Elixir v1.7.2</small>
Enumerable <small>protocol</small>        
          <a href="https://github.com/elixir-lang/elixir/blob/v1.7.2/lib/elixir/lib/enum.ex#L1" title="View Source" class="view-source" rel="help">
            <span class="icon-code" aria-hidden="true"></span>
            <span class="sr-only">View Source</span>
          </a>
      </h1>


        <section id="moduledoc">
<p>Enumerable protocol used by <a href="Enum.html"><code class="inline">Enum</code></a> and <a href="Stream.html"><code class="inline">Stream</code></a> modules.</p>
<p>When you invoke a function in the <a href="Enum.html"><code class="inline">Enum</code></a> module, the first argument
is usually a collection that must implement this protocol.
For example, the expression:</p>
<pre><code class="nohighlight makeup elixir"><span class="nc">Enum</span><span class="o">.</span><span class="n">map</span><span class="p" data-group-id="4863270481-1">(</span><span class="p" data-group-id="4863270481-2">[</span><span class="mi">1</span><span class="p">,</span><span class="w"> </span><span class="mi">2</span><span class="p">,</span><span class="w"> </span><span class="mi">3</span><span class="p" data-group-id="4863270481-2">]</span><span class="p">,</span><span class="w"> </span><span class="o">&amp;</span><span class="p" data-group-id="4863270481-3">(</span><span class="ni">&amp;1</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="mi">2</span><span class="p" data-group-id="4863270481-3">)</span><span class="p" data-group-id="4863270481-1">)</span></code></pre>
<p>invokes <a href="Enumerable.html#reduce/3"><code class="inline">Enumerable.reduce/3</code></a> to perform the reducing operation that
builds a mapped list by calling the mapping function <code class="inline">&amp;(&amp;1 * 2)</code> on
every element in the collection and consuming the element with an
accumulated list.</p>
<p>Internally, <a href="Enum.html#map/2"><code class="inline">Enum.map/2</code></a> is implemented as follows:</p>
<pre><code class="nohighlight makeup elixir"><span class="kd">def</span><span class="w"> </span><span class="nf">map</span><span class="p" data-group-id="3514683386-1">(</span><span class="n">enumerable</span><span class="p">,</span><span class="w"> </span><span class="n">fun</span><span class="p" data-group-id="3514683386-1">)</span><span class="w"> </span><span class="k" data-group-id="3514683386-2">do</span><span class="w">
  </span><span class="n">reducer</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k" data-group-id="3514683386-3">fn</span><span class="w"> </span><span class="n">x</span><span class="p">,</span><span class="w"> </span><span class="n">acc</span><span class="w"> </span><span class="o">-&gt;</span><span class="w"> </span><span class="p" data-group-id="3514683386-4">{</span><span class="ss">:cont</span><span class="p">,</span><span class="w"> </span><span class="p" data-group-id="3514683386-5">[</span><span class="n">fun</span><span class="o">.</span><span class="p" data-group-id="3514683386-6">(</span><span class="n">x</span><span class="p" data-group-id="3514683386-6">)</span><span class="w"> </span><span class="o">|</span><span class="w"> </span><span class="n">acc</span><span class="p" data-group-id="3514683386-5">]</span><span class="p" data-group-id="3514683386-4">}</span><span class="w"> </span><span class="k" data-group-id="3514683386-3">end</span><span class="w">
  </span><span class="nc">Enumerable</span><span class="o">.</span><span class="n">reduce</span><span class="p" data-group-id="3514683386-7">(</span><span class="n">enumerable</span><span class="p">,</span><span class="w"> </span><span class="p" data-group-id="3514683386-8">{</span><span class="ss">:cont</span><span class="p">,</span><span class="w"> </span><span class="p" data-group-id="3514683386-9">[</span><span class="p" data-group-id="3514683386-9">]</span><span class="p" data-group-id="3514683386-8">}</span><span class="p">,</span><span class="w"> </span><span class="n">reducer</span><span class="p" data-group-id="3514683386-7">)</span><span class="w"> </span><span class="o">|&gt;</span><span class="w"> </span><span class="n">elem</span><span class="p" data-group-id="3514683386-10">(</span><span class="mi">1</span><span class="p" data-group-id="3514683386-10">)</span><span class="w"> </span><span class="o">|&gt;</span><span class="w"> </span><span class="ss">:lists</span><span class="o">.</span><span class="n">reverse</span><span class="p" data-group-id="3514683386-11">(</span><span class="p" data-group-id="3514683386-11">)</span><span class="w">
</span><span class="k" data-group-id="3514683386-2">end</span></code></pre>
<p>Notice the user-supplied function is wrapped into a <a href="#t:reducer/0"><code class="inline">reducer/0</code></a> function.
The <a href="#t:reducer/0"><code class="inline">reducer/0</code></a> function must return a tagged tuple after each step,
as described in the <a href="#t:acc/0"><code class="inline">acc/0</code></a> type. At the end, <a href="Enumerable.html#reduce/3"><code class="inline">Enumerable.reduce/3</code></a>
returns <a href="#t:result/0"><code class="inline">result/0</code></a>.</p>
<p>This protocol uses tagged tuples to exchange information between the
reducer function and the data type that implements the protocol. This
allows enumeration of resources, such as files, to be done efficiently
while also guaranteeing the resource will be closed at the end of the
enumeration. This protocol also allows suspension of the enumeration,
which is useful when interleaving between many enumerables is required
(as in zip).</p>
<p>This protocol requires four functions to be implemented, <a href="#reduce/3"><code class="inline">reduce/3</code></a>,
<a href="#count/1"><code class="inline">count/1</code></a>, <a href="#member?/2"><code class="inline">member?/2</code></a>, and <a href="#slice/1"><code class="inline">slice/1</code></a>. The core of the protocol is the
<a href="#reduce/3"><code class="inline">reduce/3</code></a> function. All other functions exist as optimizations paths
for data structures that can implement certain properties in better
than linear time.</p>
        </section>

        <section id="summary" class="details-list">
          <h1 class="section-heading">
            <a class="hover-link" href="#summary">
              <span class="icon-link" aria-hidden="true"></span>
              <span class="sr-only">Link to this section</span>
            </a>
            Summary
          </h1>
  <div class="summary-types summary">
    <h2>
      <a href="#types">Types</a>
    </h2>
<div class="summary-row">
  <div class="summary-signature">
    <a href="#t:acc/0">acc()</a>
  </div>
    <div class="summary-synopsis"><p>The accumulator value for each step</p>
</div>
</div>
<div class="summary-row">
  <div class="summary-signature">
    <a href="#t:continuation/0">continuation()</a>
  </div>
    <div class="summary-synopsis"><p>A partially applied reduce function</p>
</div>
</div>
<div class="summary-row">
  <div class="summary-signature">
    <a href="#t:reducer/0">reducer()</a>
  </div>
    <div class="summary-synopsis"><p>The reducer function</p>
</div>
</div>
<div class="summary-row">
  <div class="summary-signature">
    <a href="#t:result/0">result()</a>
  </div>
    <div class="summary-synopsis"><p>The result of the reduce operation</p>
</div>
</div>
<div class="summary-row">
  <div class="summary-signature">
    <a href="#t:slicing_fun/0">slicing_fun()</a>
  </div>
    <div class="summary-synopsis"><p>A slicing function that receives the initial position and the
number of elements in the slice</p>
</div>
</div>
<div class="summary-row">
  <div class="summary-signature">
    <a href="#t:t/0">t()</a>
  </div>
</div>
  </div>
          
  <div class="summary-functions summary">
    <h2>
      <a href="#functions">Functions</a>
    </h2>
<div class="summary-row">
  <div class="summary-signature">
    <a href="#count/1">count(enumerable)</a>
  </div>
    <div class="summary-synopsis"><p>Retrieves the number of elements in the enumerable</p>
</div>
</div>
<div class="summary-row">
  <div class="summary-signature">
    <a href="#member?/2">member?(enumerable, element)</a>
  </div>
    <div class="summary-synopsis"><p>Checks if an element exists within the enumerable</p>
</div>
</div>
<div class="summary-row">
  <div class="summary-signature">
    <a href="#reduce/3">reduce(enumerable, acc, fun)</a>
  </div>
    <div class="summary-synopsis"><p>Reduces the enumerable into an element</p>
</div>
</div>
<div class="summary-row">
  <div class="summary-signature">
    <a href="#slice/1">slice(enumerable)</a>
  </div>
    <div class="summary-synopsis"><p>Returns a function that slices the data structure contiguously</p>
</div>
</div>
  </div>
          
        </section>

        <section id="types" class="details-list">
          <h1 class="section-heading">
            <a class="hover-link" href="#types">
              <span class="icon-link" aria-hidden="true"></span>
              <span class="sr-only">Link to this section</span>
            </a>
            Types
          </h1>
          <div class="types-list">
<div class="detail" id="t:acc/0">
    <div class="detail-header">
    <a href="#t:acc/0" class="detail-link" title="Link to this type">
      <span class="icon-link" aria-hidden="true"></span>
      <span class="sr-only">Link to this type</span>
    </a>
    <span class="signature">acc()</span>
      <a href="https://github.com/elixir-lang/elixir/blob/v1.7.2/lib/elixir/lib/enum.ex#L59" class="view-source" rel="help" title="View Source">
       <span class="icon-code" aria-hidden="true"></span>
       <span class="sr-only">View Source</span>
     </a>
        
      <div class="specs">
          <pre>acc() :: {:cont, <a href="typespecs.html#built-in-types">term</a>()} | {:halt, <a href="typespecs.html#built-in-types">term</a>()} | {:suspend, <a href="typespecs.html#built-in-types">term</a>()}</pre>
      </div>
  </div>
  <section class="docstring">
<p>The accumulator value for each step.</p>
<p>It must be a tagged tuple with one of the following “tags”:</p>
<ul>
<li><code class="inline">:cont</code>    - the enumeration should continue
</li>
<li><code class="inline">:halt</code>    - the enumeration should halt immediately
</li>
<li><code class="inline">:suspend</code> - the enumeration should be suspended immediately
</li>
</ul>
<p>Depending on the accumulator value, the result returned by
<a href="Enumerable.html#reduce/3"><code class="inline">Enumerable.reduce/3</code></a> will change. Please check the <a href="#t:result/0"><code class="inline">result/0</code></a>
type documentation for more information.</p>
<p>In case a <a href="#t:reducer/0"><code class="inline">reducer/0</code></a> function returns a <code class="inline">:suspend</code> accumulator,
it must be explicitly handled by the caller and never leak.</p>
  </section>
</div>
<div class="detail" id="t:continuation/0">
    <div class="detail-header">
    <a href="#t:continuation/0" class="detail-link" title="Link to this type">
      <span class="icon-link" aria-hidden="true"></span>
      <span class="sr-only">Link to this type</span>
    </a>
    <span class="signature">continuation()</span>
      <a href="https://github.com/elixir-lang/elixir/blob/v1.7.2/lib/elixir/lib/enum.ex#L103" class="view-source" rel="help" title="View Source">
       <span class="icon-code" aria-hidden="true"></span>
       <span class="sr-only">View Source</span>
     </a>
        
      <div class="specs">
          <pre>continuation() :: (<a href="#t:acc/0">acc</a>() -> <a href="#t:result/0">result</a>())</pre>
      </div>
  </div>
  <section class="docstring">
<p>A partially applied reduce function.</p>
<p>The continuation is the closure returned as a result when
the enumeration is suspended. When invoked, it expects
a new accumulator and it returns the result.</p>
<p>A continuation can be trivially implemented as long as the reduce
function is defined in a tail recursive fashion. If the function
is tail recursive, all the state is passed as arguments, so
the continuation is the reducing function partially applied.</p>
  </section>
</div>
<div class="detail" id="t:reducer/0">
    <div class="detail-header">
    <a href="#t:reducer/0" class="detail-link" title="Link to this type">
      <span class="icon-link" aria-hidden="true"></span>
      <span class="sr-only">Link to this type</span>
    </a>
    <span class="signature">reducer()</span>
      <a href="https://github.com/elixir-lang/elixir/blob/v1.7.2/lib/elixir/lib/enum.ex#L69" class="view-source" rel="help" title="View Source">
       <span class="icon-code" aria-hidden="true"></span>
       <span class="sr-only">View Source</span>
     </a>
        
      <div class="specs">
          <pre>reducer() :: (<a href="typespecs.html#built-in-types">term</a>(), <a href="typespecs.html#built-in-types">term</a>() -> <a href="#t:acc/0">acc</a>())</pre>
      </div>
  </div>
  <section class="docstring">
<p>The reducer function.</p>
<p>Should be called with the enumerable element and the
accumulator contents.</p>
<p>Returns the accumulator for the next enumeration step.</p>
  </section>
</div>
<div class="detail" id="t:result/0">
    <div class="detail-header">
    <a href="#t:result/0" class="detail-link" title="Link to this type">
      <span class="icon-link" aria-hidden="true"></span>
      <span class="sr-only">Link to this type</span>
    </a>
    <span class="signature">result()</span>
      <a href="https://github.com/elixir-lang/elixir/blob/v1.7.2/lib/elixir/lib/enum.ex#L86" class="view-source" rel="help" title="View Source">
       <span class="icon-code" aria-hidden="true"></span>
       <span class="sr-only">View Source</span>
     </a>
        
      <div class="specs">
          <pre>result() ::
  {:done, <a href="typespecs.html#built-in-types">term</a>()} | {:halted, <a href="typespecs.html#built-in-types">term</a>()} | {:suspended, <a href="typespecs.html#built-in-types">term</a>(), <a href="#t:continuation/0">continuation</a>()}</pre>
      </div>
  </div>
  <section class="docstring">
<p>The result of the reduce operation.</p>
<p>It may be <em>done</em> when the enumeration is finished by reaching
its end, or <em>halted</em>/<em>suspended</em> when the enumeration was halted
or suspended by the <a href="#t:reducer/0"><code class="inline">reducer/0</code></a> function.</p>
<p>In case a <a href="#t:reducer/0"><code class="inline">reducer/0</code></a> function returns the <code class="inline">:suspend</code> accumulator, the
<code class="inline">:suspended</code> tuple must be explicitly handled by the caller and
never leak. In practice, this means regular enumeration functions
just need to be concerned about <code class="inline">:done</code> and <code class="inline">:halted</code> results.</p>
<p>Furthermore, a <code class="inline">:suspend</code> call must always be followed by another call,
eventually halting or continuing until the end.</p>
  </section>
</div>
<div class="detail" id="t:slicing_fun/0">
    <div class="detail-header">
    <a href="#t:slicing_fun/0" class="detail-link" title="Link to this type">
      <span class="icon-link" aria-hidden="true"></span>
      <span class="sr-only">Link to this type</span>
    </a>
    <span class="signature">slicing_fun()</span>
      <a href="https://github.com/elixir-lang/elixir/blob/v1.7.2/lib/elixir/lib/enum.ex#L117" class="view-source" rel="help" title="View Source">
       <span class="icon-code" aria-hidden="true"></span>
       <span class="sr-only">View Source</span>
     </a>
        
      <div class="specs">
          <pre>slicing_fun() ::
  (start :: <a href="typespecs.html#basic-types">non_neg_integer</a>(), length :: <a href="typespecs.html#basic-types">pos_integer</a>() -> [<a href="typespecs.html#built-in-types">term</a>()])</pre>
      </div>
  </div>
  <section class="docstring">
<p>A slicing function that receives the initial position and the
number of elements in the slice.</p>
<p>The <code class="inline">start</code> position is a number <code class="inline">&gt;= 0</code> and guaranteed to
exist in the enumerable. The length is a number <code class="inline">&gt;= 1</code> in a way
that <code class="inline">start + length &lt;= count</code>, where <code class="inline">count</code> is the maximum
amount of elements in the enumerable.</p>
<p>The function should return a non empty list where
the amount of elements is equal to <code class="inline">length</code>.</p>
  </section>
</div>
<div class="detail" id="t:t/0">
    <div class="detail-header">
    <a href="#t:t/0" class="detail-link" title="Link to this type">
      <span class="icon-link" aria-hidden="true"></span>
      <span class="sr-only">Link to this type</span>
    </a>
    <span class="signature">t()</span>
      <a href="https://github.com/elixir-lang/elixir/blob/v1.7.2/lib/elixir/lib/enum.ex#L1" class="view-source" rel="help" title="View Source">
       <span class="icon-code" aria-hidden="true"></span>
       <span class="sr-only">View Source</span>
     </a>
        
      <div class="specs">
          <pre>t() :: <a href="typespecs.html#built-in-types">term</a>()</pre>
      </div>
  </div>
  <section class="docstring">
  </section>
</div>
          </div>
        </section>


        <section id="functions" class="details-list">
          <h1 class="section-heading">
            <a class="hover-link" href="#functions">
              <span class="icon-link" aria-hidden="true"></span>
              <span class="sr-only">Link to this section</span>
            </a>
            Functions
          </h1>
<div class="detail" id="count/1">
    <div class="detail-header">
    <a href="#count/1" class="detail-link" title="Link to this function">
      <span class="icon-link" aria-hidden="true"></span>
      <span class="sr-only">Link to this function</span>
    </a>
    <span class="signature">count(enumerable)</span>
      <a href="https://github.com/elixir-lang/elixir/blob/v1.7.2/lib/elixir/lib/enum.ex#L153" class="view-source" rel="help" title="View Source">
       <span class="icon-code" aria-hidden="true"></span>
       <span class="sr-only">View Source</span>
     </a>
          </div>
  <section class="docstring">
<p>Retrieves the number of elements in the enumerable.</p>
<p>It should return <code class="inline">{:ok, count}</code> if you can count the number of elements
in the enumerable.</p>
<p>Otherwise it should return <code class="inline">{:error, __MODULE__}</code> and a default algorithm
built on top of <a href="#reduce/3"><code class="inline">reduce/3</code></a> that runs in linear time will be used.</p>
  </section>
</div>
<div class="detail" id="member?/2">
    <div class="detail-header">
    <a href="#member?/2" class="detail-link" title="Link to this function">
      <span class="icon-link" aria-hidden="true"></span>
      <span class="sr-only">Link to this function</span>
    </a>
    <span class="signature">member?(enumerable, element)</span>
      <a href="https://github.com/elixir-lang/elixir/blob/v1.7.2/lib/elixir/lib/enum.ex#L166" class="view-source" rel="help" title="View Source">
       <span class="icon-code" aria-hidden="true"></span>
       <span class="sr-only">View Source</span>
     </a>
          </div>
  <section class="docstring">
<p>Checks if an element exists within the enumerable.</p>
<p>It should return <code class="inline">{:ok, boolean}</code> if you can check the membership of a
given element in the enumerable with <a href="Kernel.html#===/2"><code class="inline">===/2</code></a> without traversing the whole
enumerable.</p>
<p>Otherwise it should return <code class="inline">{:error, __MODULE__}</code> and a default algorithm
built on top of <a href="#reduce/3"><code class="inline">reduce/3</code></a> that runs in linear time will be used.</p>
  </section>
</div>
<div class="detail" id="reduce/3">
    <div class="detail-header">
    <a href="#reduce/3" class="detail-link" title="Link to this function">
      <span class="icon-link" aria-hidden="true"></span>
      <span class="sr-only">Link to this function</span>
    </a>
    <span class="signature">reduce(enumerable, acc, fun)</span>
      <a href="https://github.com/elixir-lang/elixir/blob/v1.7.2/lib/elixir/lib/enum.ex#L141" class="view-source" rel="help" title="View Source">
       <span class="icon-code" aria-hidden="true"></span>
       <span class="sr-only">View Source</span>
     </a>
          </div>
  <section class="docstring">
<p>Reduces the enumerable into an element.</p>
<p>Most of the operations in <a href="Enum.html"><code class="inline">Enum</code></a> are implemented in terms of reduce.
This function should apply the given <a href="#t:reducer/0"><code class="inline">reducer/0</code></a> function to each
item in the enumerable and proceed as expected by the returned
accumulator.</p>
<p>See the documentation of the types <a href="#t:result/0"><code class="inline">result/0</code></a> and <a href="#t:acc/0"><code class="inline">acc/0</code></a> for
more information.</p>
<h2 id="reduce/3-examples" class="section-heading">
  <a href="#reduce/3-examples" class="hover-link"><span class="icon-link" aria-hidden="true"></span></a>
  Examples
</h2>

<p>As an example, here is the implementation of <code class="inline">reduce</code> for lists:</p>
<pre><code class="nohighlight makeup elixir"><span class="kd">def</span><span class="w"> </span><span class="nf">reduce</span><span class="p" data-group-id="0558425247-1">(</span><span class="c">_list</span><span class="p">,</span><span class="w"> </span><span class="p" data-group-id="0558425247-2">{</span><span class="ss">:halt</span><span class="p">,</span><span class="w"> </span><span class="n">acc</span><span class="p" data-group-id="0558425247-2">}</span><span class="p">,</span><span class="w"> </span><span class="c">_fun</span><span class="p" data-group-id="0558425247-1">)</span><span class="p">,</span><span class="w"> </span><span class="ss">do</span><span class="p">:</span><span class="w"> </span><span class="p" data-group-id="0558425247-3">{</span><span class="ss">:halted</span><span class="p">,</span><span class="w"> </span><span class="n">acc</span><span class="p" data-group-id="0558425247-3">}</span><span class="w">
</span><span class="kd">def</span><span class="w"> </span><span class="nf">reduce</span><span class="p" data-group-id="0558425247-4">(</span><span class="n">list</span><span class="p">,</span><span class="w"> </span><span class="p" data-group-id="0558425247-5">{</span><span class="ss">:suspend</span><span class="p">,</span><span class="w"> </span><span class="n">acc</span><span class="p" data-group-id="0558425247-5">}</span><span class="p">,</span><span class="w"> </span><span class="n">fun</span><span class="p" data-group-id="0558425247-4">)</span><span class="p">,</span><span class="w"> </span><span class="ss">do</span><span class="p">:</span><span class="w"> </span><span class="p" data-group-id="0558425247-6">{</span><span class="ss">:suspended</span><span class="p">,</span><span class="w"> </span><span class="n">acc</span><span class="p">,</span><span class="w"> </span><span class="o">&amp;</span><span class="n">reduce</span><span class="p" data-group-id="0558425247-7">(</span><span class="n">list</span><span class="p">,</span><span class="w"> </span><span class="ni">&amp;1</span><span class="p">,</span><span class="w"> </span><span class="n">fun</span><span class="p" data-group-id="0558425247-7">)</span><span class="p" data-group-id="0558425247-6">}</span><span class="w">
</span><span class="kd">def</span><span class="w"> </span><span class="nf">reduce</span><span class="p" data-group-id="0558425247-8">(</span><span class="p" data-group-id="0558425247-9">[</span><span class="p" data-group-id="0558425247-9">]</span><span class="p">,</span><span class="w"> </span><span class="p" data-group-id="0558425247-10">{</span><span class="ss">:cont</span><span class="p">,</span><span class="w"> </span><span class="n">acc</span><span class="p" data-group-id="0558425247-10">}</span><span class="p">,</span><span class="w"> </span><span class="c">_fun</span><span class="p" data-group-id="0558425247-8">)</span><span class="p">,</span><span class="w"> </span><span class="ss">do</span><span class="p">:</span><span class="w"> </span><span class="p" data-group-id="0558425247-11">{</span><span class="ss">:done</span><span class="p">,</span><span class="w"> </span><span class="n">acc</span><span class="p" data-group-id="0558425247-11">}</span><span class="w">
</span><span class="kd">def</span><span class="w"> </span><span class="nf">reduce</span><span class="p" data-group-id="0558425247-12">(</span><span class="p" data-group-id="0558425247-13">[</span><span class="n">head</span><span class="w"> </span><span class="o">|</span><span class="w"> </span><span class="n">tail</span><span class="p" data-group-id="0558425247-13">]</span><span class="p">,</span><span class="w"> </span><span class="p" data-group-id="0558425247-14">{</span><span class="ss">:cont</span><span class="p">,</span><span class="w"> </span><span class="n">acc</span><span class="p" data-group-id="0558425247-14">}</span><span class="p">,</span><span class="w"> </span><span class="n">fun</span><span class="p" data-group-id="0558425247-12">)</span><span class="p">,</span><span class="w"> </span><span class="ss">do</span><span class="p">:</span><span class="w"> </span><span class="n">reduce</span><span class="p" data-group-id="0558425247-15">(</span><span class="n">tail</span><span class="p">,</span><span class="w"> </span><span class="n">fun</span><span class="o">.</span><span class="p" data-group-id="0558425247-16">(</span><span class="n">head</span><span class="p">,</span><span class="w"> </span><span class="n">acc</span><span class="p" data-group-id="0558425247-16">)</span><span class="p">,</span><span class="w"> </span><span class="n">fun</span><span class="p" data-group-id="0558425247-15">)</span></code></pre>
  </section>
</div>
<div class="detail" id="slice/1">
    <div class="detail-header">
    <a href="#slice/1" class="detail-link" title="Link to this function">
      <span class="icon-link" aria-hidden="true"></span>
      <span class="sr-only">Link to this function</span>
    </a>
    <span class="signature">slice(enumerable)</span>
      <a href="https://github.com/elixir-lang/elixir/blob/v1.7.2/lib/elixir/lib/enum.ex#L193" class="view-source" rel="help" title="View Source">
       <span class="icon-code" aria-hidden="true"></span>
       <span class="sr-only">View Source</span>
     </a>
          </div>
  <section class="docstring">
<p>Returns a function that slices the data structure contiguously.</p>
<p>It should return <code class="inline">{:ok, size, slicing_fun}</code> if the enumerable has
a known bound and can access a position in the enumerable without
traversing all previous elements.</p>
<p>Otherwise it should return <code class="inline">{:error, __MODULE__}</code> and a default
algorithm built on top of <a href="#reduce/3"><code class="inline">reduce/3</code></a> that runs in linear time will be
used.</p>
<h2 id="slice/1-differences-to-count-1" class="section-heading">
  <a href="#slice/1-differences-to-count-1" class="hover-link"><span class="icon-link" aria-hidden="true"></span></a>
  Differences to <a href="#count/1"><code class="inline">count/1</code></a>
</h2>

<p>The <code class="inline">size</code> value returned by this function is used for boundary checks,
therefore it is extremely important that this function only returns <code class="inline">:ok</code>
if retrieving the <code class="inline">size</code> of the enumerable is cheap, fast and takes constant
time. Otherwise the simplest of operations, such as <code class="inline">Enum.at(enumerable, 0)</code>,
will become too expensive.</p>
<p>On the other hand, the <a href="#count/1"><code class="inline">count/1</code></a> function in this protocol should be
implemented whenever you can count the number of elements in the collection.</p>
  </section>
</div>
        </section>

          <footer class="footer">
        <p>
          <span class="line">
            Built using
            <a href="https://github.com/elixir-lang/ex_doc" title="ExDoc" target="_blank" rel="help noopener">ExDoc</a> (v0.19.1),
          </span>
          <span class="line">
            designed by
            <a href="https://twitter.com/dignifiedquire" target="_blank" rel="noopener" title="@dignifiedquire">Friedel Ziegelmayer</a>.
            </span>
        </p>
      </footer>
    </div>
  </div>
</section>
</div>
  <script src="dist/app-a0c90688fa.js"></script>
  
  </body>
</html>