UNPKG

graph-curry

Version:

a Map based implementation of canonical graph data algorithms

283 lines (183 loc) 11.7 kB
<!DOCTYPE html> <html> <head> <title>path.js</title> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <meta name="viewport" content="width=device-width, target-densitydpi=160dpi, initial-scale=1.0; maximum-scale=1.0; user-scalable=0;"> <link rel="stylesheet" media="all" href="docco.css" /> </head> <body> <div id="container"> <div id="background"></div> <ul id="jump_to"> <li> <a class="large" href="javascript:void(0);">Jump To &hellip;</a> <a class="small" href="javascript:void(0);">+</a> <div id="jump_wrapper"> <div id="jump_page_wrapper"> <div id="jump_page"> <a class="source" href="components.html"> components.js </a> <a class="source" href="contract.html"> contract.js </a> <a class="source" href="cut.html"> cut.js </a> <a class="source" href="graph.html"> graph.js </a> <a class="source" href="index.html"> index.js </a> <a class="source" href="path.html"> path.js </a> <a class="source" href="reducers.html"> reducers.js </a> <a class="source" href="search.html"> search.js </a> <a class="source" href="show.html"> show.js </a> <a class="source" href="tree.html"> tree.js </a> </div> </div> </li> </ul> <ul class="sections"> <li id="title"> <div class="annotation"> <h1>path.js</h1> </div> </li> <li id="section-1"> <div class="annotation"> <div class="pilwrap "> <a class="pilcrow" href="#section-1">&#182;</a> </div> </div> <div class="content"><div class='highlight'><pre><span class="hljs-keyword">import</span> { addMap, lastK, } <span class="hljs-keyword">from</span> <span class="hljs-string">'fenugreek-collections'</span>;</pre></div></div> </li> <li id="section-2"> <div class="annotation"> <div class="pilwrap "> <a class="pilcrow" href="#section-2">&#182;</a> </div> <p><strong>pathEntry</strong> <code>:: ( node, Number, Number ) -&gt; {pred, length, weight}</code> returns an object with pred, weight, and length properties</p> </div> <div class="content"><div class='highlight'><pre><span class="hljs-keyword">export</span> <span class="hljs-keyword">const</span> pathEntry = <span class="hljs-function">(<span class="hljs-params">pred = <span class="hljs-literal">null</span>, length = <span class="hljs-number">1</span>, weight = <span class="hljs-number">0</span></span>) =&gt;</span> ({ pred, length, weight }); <span class="hljs-keyword">const</span> pathVal = <span class="hljs-function">(<span class="hljs-params">pred = <span class="hljs-literal">null</span></span>) =&gt;</span> (length = <span class="hljs-number">1</span>) =&gt; <span class="hljs-function">(<span class="hljs-params">weight = <span class="hljs-number">0</span></span>) =&gt;</span> ({ pred, length, weight });</pre></div></div> </li> <li id="section-3"> <div class="annotation"> <div class="pilwrap "> <a class="pilcrow" href="#section-3">&#182;</a> </div> <p><strong>addSrc</strong> <code>:: Map&lt;pathEntry&gt; -&gt; node -&gt; Map&lt;pathEntry&gt;</code> adds a {node:{pred, weight, length}} entry to a path</p> </div> <div class="content"><div class='highlight'><pre><span class="hljs-keyword">export</span> <span class="hljs-keyword">const</span> appendPath = <span class="hljs-function"><span class="hljs-params">path</span> =&gt;</span> src =&gt; addMap(path)(src)(pathEntry(lastK(path), <span class="hljs-number">1</span>, <span class="hljs-number">0</span>));</pre></div></div> </li> <li id="section-4"> <div class="annotation"> <div class="pilwrap "> <a class="pilcrow" href="#section-4">&#182;</a> </div> <p><strong>initPath</strong> <code>:: node -&gt; Map&lt;pathEntry&gt;</code> initializes a new path given a source node</p> </div> <div class="content"><div class='highlight'><pre><span class="hljs-keyword">export</span> <span class="hljs-keyword">const</span> initPath = <span class="hljs-function"><span class="hljs-params">node</span> =&gt;</span> appendPath()(node);</pre></div></div> </li> <li id="section-5"> <div class="annotation"> <div class="pilwrap "> <a class="pilcrow" href="#section-5">&#182;</a> </div> <p><strong>getWeight</strong> <code>:: {weight:Number} -&gt; Number</code> returns an object with pred, weight, and length properties</p> </div> <div class="content"><div class='highlight'><pre><span class="hljs-keyword">export</span> <span class="hljs-keyword">const</span> getWeight = <span class="hljs-function">(<span class="hljs-params">{ weight = <span class="hljs-number">0</span> }</span>) =&gt;</span> weight;</pre></div></div> </li> <li id="section-6"> <div class="annotation"> <div class="pilwrap "> <a class="pilcrow" href="#section-6">&#182;</a> </div> <p><strong>getLength</strong> <code>:: {length:Number} -&gt; Number</code> returns an object with pred, weight, and length properties</p> </div> <div class="content"><div class='highlight'><pre><span class="hljs-keyword">export</span> <span class="hljs-keyword">const</span> getLength = <span class="hljs-function">(<span class="hljs-params">{ length = <span class="hljs-number">1</span> }</span>) =&gt;</span> length;</pre></div></div> </li> <li id="section-7"> <div class="annotation"> <div class="pilwrap "> <a class="pilcrow" href="#section-7">&#182;</a> </div> <p><strong>lastVal</strong> <code>:: Map&lt;pathEntry&gt; -&gt; {pred, length, weight}</code> returns the last entry in the path</p> </div> <div class="content"><div class='highlight'><pre><span class="hljs-keyword">export</span> <span class="hljs-keyword">const</span> lastVal = <span class="hljs-function"><span class="hljs-params">path</span> =&gt;</span> path.get(lastK(path));</pre></div></div> </li> <li id="section-8"> <div class="annotation"> <div class="pilwrap "> <a class="pilcrow" href="#section-8">&#182;</a> </div> <p><strong>lastWeight</strong> <code>:: Map&lt;pathEntry&gt; -&gt; Number</code> returns the last weight in the path</p> </div> <div class="content"><div class='highlight'><pre><span class="hljs-keyword">export</span> <span class="hljs-keyword">const</span> lastWeight = <span class="hljs-function"><span class="hljs-params">path</span> =&gt;</span> getWeight(lastVal(path));</pre></div></div> </li> <li id="section-9"> <div class="annotation"> <div class="pilwrap "> <a class="pilcrow" href="#section-9">&#182;</a> </div> <p><strong>lastLength</strong> <code>:: Map&lt;pathEntry&gt; -&gt; Number</code> returns the last length in the path</p> </div> <div class="content"><div class='highlight'><pre><span class="hljs-keyword">export</span> <span class="hljs-keyword">const</span> lastLength = <span class="hljs-function"><span class="hljs-params">path</span> =&gt;</span> getLength(lastVal(path));</pre></div></div> </li> <li id="section-10"> <div class="annotation"> <div class="pilwrap "> <a class="pilcrow" href="#section-10">&#182;</a> </div> <p><strong>nextWeight</strong> <code>:: Map&lt;pathEntry&gt; -&gt; Number -&gt; Number</code> returns an object with pred, weight, and length properties</p> </div> <div class="content"><div class='highlight'><pre><span class="hljs-keyword">export</span> <span class="hljs-keyword">const</span> nextWeight = <span class="hljs-function"><span class="hljs-params">path</span> =&gt;</span> (w = <span class="hljs-number">0</span>) =&gt; lastWeight(path) + w;</pre></div></div> </li> <li id="section-11"> <div class="annotation"> <div class="pilwrap "> <a class="pilcrow" href="#section-11">&#182;</a> </div> <p><strong>nextLength</strong> <code>:: Map&lt;pathEntry&gt; -&gt; Number -&gt; Number</code> returns an object with pred, weight, and length properties</p> </div> <div class="content"><div class='highlight'><pre><span class="hljs-keyword">export</span> <span class="hljs-keyword">const</span> nextLength = <span class="hljs-function"><span class="hljs-params">path</span> =&gt;</span> lastLength(path) ? lastLength(path) + <span class="hljs-number">1</span> : <span class="hljs-number">1</span>;</pre></div></div> </li> <li id="section-12"> <div class="annotation"> <div class="pilwrap "> <a class="pilcrow" href="#section-12">&#182;</a> </div> <p><strong>nextPath</strong> <code>:: (Map&lt;pathEntry&gt;, [node, Number]) -&gt; Map&lt;pathEntry&gt;</code> returns an object with pred, weight, and length properties</p> </div> <div class="content"><div class='highlight'><pre><span class="hljs-keyword">export</span> <span class="hljs-keyword">const</span> nextPath = <span class="hljs-function">(<span class="hljs-params">path = <span class="hljs-keyword">new</span> <span class="hljs-built_in">Map</span>, [ n, w = <span class="hljs-number">0</span> ]</span>) =&gt;</span> path.set(n, pathVal(lastK(path))(nextLength(path))(nextWeight(path)(w)));</pre></div></div> </li> </ul> </div> </body> </html>