mirror of
https://github.com/c-cube/ocaml-containers.git
synced 2025-12-06 11:15:31 -05:00
2 lines
No EOL
6 KiB
HTML
2 lines
No EOL
6 KiB
HTML
<!DOCTYPE html>
|
||
<html xmlns="http://www.w3.org/1999/xhtml"><head><title>Traverse (containers.CCList.Traverse)</title><link rel="stylesheet" href="../../../_odoc-theme/odoc.css"/><meta charset="utf-8"/><meta name="generator" content="odoc 2.2.0"/><meta name="viewport" content="width=device-width,initial-scale=1.0"/><script src="../../../highlight.pack.js"></script><script>hljs.initHighlightingOnLoad();</script></head><body class="odoc"><nav class="odoc-nav"><a href="../index.html">Up</a> – <a href="../../index.html">containers</a> » <a href="../index.html">CCList</a> » Traverse</nav><header class="odoc-preamble"><h1>Module <code><span>CCList.Traverse</span></code></h1></header><nav class="odoc-toc"><ul><li><a href="#monadic-traverse">Monadic Traverse</a></li><li><a href="#parameters">Parameters</a></li><li><a href="#signature">Signature</a></li></ul></nav><div class="odoc-content"><h3 id="monadic-traverse"><a href="#monadic-traverse" class="anchor"></a>Monadic Traverse</h3><p>This allows the traversal of a <code>'a t list</code> where <code>_ t</code> is also a monad.</p><p>For example, a <code>'a option list</code> will be traversed by extracting a value from each option, returning <code>Some [x1;…;x_n]</code>; but if one of the option is <code>None</code> then the whole result is <code>None</code>.</p><p>Another example is with <code>result</code>: <code>('a, 'err) result list</code> can be transformed into <code>('a list, 'err) result</code> by returning the first error, or <code>Ok [x1; …; xn]</code> if all the elements were successful.</p><p>This describes the behavior of <code>sequence_m</code>; <code>map_m</code> is a combination of <a href="../index.html#val-map"><code>map</code></a> and <code>sequence_m</code>; <code>map_m_par</code> is like <code>map_m</code> but useful for some pseudo monads like Lwt.</p><h2 id="parameters"><a href="#parameters" class="anchor"></a>Parameters</h2><div class="odoc-spec"><div class="spec parameter anchored" id="argument-1-M"><a href="#argument-1-M" class="anchor"></a><code><span><span class="keyword">module</span> </span><span><a href="argument-1-M/index.html">M</a></span><span> : <a href="../module-type-MONAD/index.html">MONAD</a></span></code></div></div><h2 id="signature"><a href="#signature" class="anchor"></a>Signature</h2><div class="odoc-spec"><div class="spec value anchored" id="val-sequence_m"><a href="#val-sequence_m" class="anchor"></a><code><span><span class="keyword">val</span> sequence_m : <span><span><span><span class="type-var">'a</span> <a href="argument-1-M/index.html#type-t">M.t</a></span> <a href="../index.html#type-t">t</a></span> <span class="arrow">-></span></span> <span><span><span class="type-var">'a</span> <a href="../index.html#type-t">t</a></span> <a href="argument-1-M/index.html#type-t">M.t</a></span></span></code></div><div class="spec-doc"><p>Traverse the list of monadic values using <a href="argument-1-M/index.html#val-(>>=)"><code>M.(>>=)</code></a>, re-combining elements with <code>(::)</code>. See the documentation of the <a href="#"><code>Traverse</code></a> functor itself.</p></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-fold_m"><a href="#val-fold_m" class="anchor"></a><code><span><span class="keyword">val</span> fold_m : <span><span>(<span><span class="type-var">'b</span> <span class="arrow">-></span></span> <span><span class="type-var">'a</span> <span class="arrow">-></span></span> <span><span class="type-var">'b</span> <a href="argument-1-M/index.html#type-t">M.t</a></span>)</span> <span class="arrow">-></span></span> <span><span class="type-var">'b</span> <span class="arrow">-></span></span> <span><span><span class="type-var">'a</span> <a href="../index.html#type-t">t</a></span> <span class="arrow">-></span></span> <span><span class="type-var">'b</span> <a href="argument-1-M/index.html#type-t">M.t</a></span></span></code></div><div class="spec-doc"><p>Fold a function with a monadic effect through a list.</p></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-map_m"><a href="#val-map_m" class="anchor"></a><code><span><span class="keyword">val</span> map_m : <span><span>(<span><span class="type-var">'a</span> <span class="arrow">-></span></span> <span><span class="type-var">'b</span> <a href="argument-1-M/index.html#type-t">M.t</a></span>)</span> <span class="arrow">-></span></span> <span><span><span class="type-var">'a</span> <a href="../index.html#type-t">t</a></span> <span class="arrow">-></span></span> <span><span><span class="type-var">'b</span> <a href="../index.html#type-t">t</a></span> <a href="argument-1-M/index.html#type-t">M.t</a></span></span></code></div><div class="spec-doc"><p>Combination of <a href="../index.html#val-map"><code>map</code></a> and <a href="#val-sequence_m"><code>sequence_m</code></a>.</p></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-map_m_par"><a href="#val-map_m_par" class="anchor"></a><code><span><span class="keyword">val</span> map_m_par : <span><span>(<span><span class="type-var">'a</span> <span class="arrow">-></span></span> <span><span class="type-var">'b</span> <a href="argument-1-M/index.html#type-t">M.t</a></span>)</span> <span class="arrow">-></span></span> <span><span><span class="type-var">'a</span> <a href="../index.html#type-t">t</a></span> <span class="arrow">-></span></span> <span><span><span class="type-var">'b</span> <a href="../index.html#type-t">t</a></span> <a href="argument-1-M/index.html#type-t">M.t</a></span></span></code></div><div class="spec-doc"><p><code>map_m_par f (x :: l)</code> is like <a href="#val-map_m"><code>map_m</code></a> but <code>f x</code> and <code>f l</code> are evaluated "in parallel" before combining their result (for instance in Lwt).</p><p>Basically, when encoutering <code>x :: tl</code>, this computes <code>f x</code> and <code>map_m_par f tl</code>, and only then is <code>M.(>>=)</code> used to combine the two results into a new list.</p></div></div></div></body></html> |