graph-curry
Version:
a Map based implementation of canonical graph data algorithms
283 lines (183 loc) • 11.7 kB
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 …</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">¶</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">¶</a>
</div>
<p><strong>pathEntry</strong> <code>:: ( node, Number, Number ) -> {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>) =></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>) =></span> (length = <span class="hljs-number">1</span>) => <span class="hljs-function">(<span class="hljs-params">weight = <span class="hljs-number">0</span></span>) =></span>
({ pred, length, weight });</pre></div></div>
</li>
<li id="section-3">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-3">¶</a>
</div>
<p><strong>addSrc</strong> <code>:: Map<pathEntry> -> node -> Map<pathEntry></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> =></span> src => 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">¶</a>
</div>
<p><strong>initPath</strong> <code>:: node -> Map<pathEntry></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> =></span> appendPath()(node);</pre></div></div>
</li>
<li id="section-5">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-5">¶</a>
</div>
<p><strong>getWeight</strong> <code>:: {weight:Number} -> 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>) =></span> weight;</pre></div></div>
</li>
<li id="section-6">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-6">¶</a>
</div>
<p><strong>getLength</strong> <code>:: {length:Number} -> 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>) =></span> length;</pre></div></div>
</li>
<li id="section-7">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-7">¶</a>
</div>
<p><strong>lastVal</strong> <code>:: Map<pathEntry> -> {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> =></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">¶</a>
</div>
<p><strong>lastWeight</strong> <code>:: Map<pathEntry> -> 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> =></span> getWeight(lastVal(path));</pre></div></div>
</li>
<li id="section-9">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-9">¶</a>
</div>
<p><strong>lastLength</strong> <code>:: Map<pathEntry> -> 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> =></span> getLength(lastVal(path));</pre></div></div>
</li>
<li id="section-10">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-10">¶</a>
</div>
<p><strong>nextWeight</strong> <code>:: Map<pathEntry> -> Number -> 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> =></span> (w = <span class="hljs-number">0</span>) => lastWeight(path) + w;</pre></div></div>
</li>
<li id="section-11">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-11">¶</a>
</div>
<p><strong>nextLength</strong> <code>:: Map<pathEntry> -> Number -> 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> =></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">¶</a>
</div>
<p><strong>nextPath</strong> <code>:: (Map<pathEntry>, [node, Number]) -> Map<pathEntry></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>) =></span>
path.set(n, pathVal(lastK(path))(nextLength(path))(nextWeight(path)(w)));</pre></div></div>
</li>
</ul>
</div>
</body>
</html>