Skip to content

Commit

Permalink
feat(do): Add the do operator
Browse files Browse the repository at this point in the history
The `do` operator injects a side-effectual operation in the middle of
an otherwise pure maybe chain. This is incredibly useful when trying
to debug using the console. It also provides a convenient mechanism
for adding _fire and foget_ side-effects, although I would recommend
not abusing do for this purpose.
  • Loading branch information
kofno committed Jun 9, 2018
1 parent a2bf999 commit a5c61d8
Show file tree
Hide file tree
Showing 14 changed files with 240 additions and 40 deletions.
2 changes: 1 addition & 1 deletion docs/assets/js/search.js

Large diffs are not rendered by default.

62 changes: 54 additions & 8 deletions docs/classes/_just_.just.html
Original file line number Diff line number Diff line change
Expand Up @@ -927,6 +927,9 @@
<li class=" tsd-kind-method tsd-parent-kind-class tsd-has-type-parameter tsd-is-overwrite">
<a href="_just_.just.html#cata" class="tsd-kind-icon">cata</a>
</li>
<li class=" tsd-kind-method tsd-parent-kind-class tsd-is-overwrite">
<a href="_just_.just.html#do" class="tsd-kind-icon">do</a>
</li>
<li class=" tsd-kind-method tsd-parent-kind-class tsd-is-overwrite">
<a href="_just_.just.html#getorelse" class="tsd-kind-icon">get<wbr>OrElse</a>
</li>
Expand Down Expand Up @@ -988,6 +991,7 @@ <h3>Methods</h3>
<li class="tsd-kind-method tsd-parent-kind-class tsd-has-type-parameter tsd-is-overwrite"><a href="_just_.just.html#andthen" class="tsd-kind-icon">and<wbr>Then</a></li>
<li class="tsd-kind-method tsd-parent-kind-class tsd-has-type-parameter tsd-is-overwrite"><a href="_just_.just.html#assign" class="tsd-kind-icon">assign</a></li>
<li class="tsd-kind-method tsd-parent-kind-class tsd-has-type-parameter tsd-is-overwrite"><a href="_just_.just.html#cata" class="tsd-kind-icon">cata</a></li>
<li class="tsd-kind-method tsd-parent-kind-class tsd-is-overwrite"><a href="_just_.just.html#do" class="tsd-kind-icon">do</a></li>
<li class="tsd-kind-method tsd-parent-kind-class tsd-is-overwrite"><a href="_just_.just.html#getorelse" class="tsd-kind-icon">get<wbr>OrElse</a></li>
<li class="tsd-kind-method tsd-parent-kind-class tsd-is-overwrite"><a href="_just_.just.html#getorelsevalue" class="tsd-kind-icon">get<wbr>OrElse<wbr>Value</a></li>
<li class="tsd-kind-method tsd-parent-kind-class tsd-has-type-parameter tsd-is-overwrite"><a href="_just_.just.html#map" class="tsd-kind-icon">map</a></li>
Expand All @@ -1008,7 +1012,7 @@ <h3>constructor</h3>
<li class="tsd-description">
<aside class="tsd-sources">
<ul>
<li>Defined in <a href="https://github.com/kofno/maybeasy/blob/f30571a/src/Just.ts#L4">Just.ts:4</a></li>
<li>Defined in <a href="https://github.com/kofno/maybeasy/blob/a2bf999/src/Just.ts#L4">Just.ts:4</a></li>
</ul>
</aside>
<h4 class="tsd-parameters-title">Parameters</h4>
Expand All @@ -1030,7 +1034,7 @@ <h3><span class="tsd-flag ts-flagPrivate">Private</span> value</h3>
<div class="tsd-signature tsd-kind-icon">value<span class="tsd-signature-symbol">:</span> <span class="tsd-signature-type">A</span></div>
<aside class="tsd-sources">
<ul>
<li>Defined in <a href="https://github.com/kofno/maybeasy/blob/f30571a/src/Just.ts#L5">Just.ts:5</a></li>
<li>Defined in <a href="https://github.com/kofno/maybeasy/blob/a2bf999/src/Just.ts#L5">Just.ts:5</a></li>
</ul>
</aside>
</section>
Expand All @@ -1048,7 +1052,7 @@ <h3>and<wbr>Then</h3>
<aside class="tsd-sources">
<p>Overrides <a href="_maybe_.maybe.html">Maybe</a>.<a href="_maybe_.maybe.html#andthen">andThen</a></p>
<ul>
<li>Defined in <a href="https://github.com/kofno/maybeasy/blob/f30571a/src/Just.ts#L21">Just.ts:21</a></li>
<li>Defined in <a href="https://github.com/kofno/maybeasy/blob/a2bf999/src/Just.ts#L21">Just.ts:21</a></li>
</ul>
</aside>
<h4 class="tsd-type-parameters-title">Type parameters</h4>
Expand Down Expand Up @@ -1096,7 +1100,7 @@ <h3>assign</h3>
<aside class="tsd-sources">
<p>Overrides <a href="_maybe_.maybe.html">Maybe</a>.<a href="_maybe_.maybe.html#assign">assign</a></p>
<ul>
<li>Defined in <a href="https://github.com/kofno/maybeasy/blob/f30571a/src/Just.ts#L29">Just.ts:29</a></li>
<li>Defined in <a href="https://github.com/kofno/maybeasy/blob/a2bf999/src/Just.ts#L29">Just.ts:29</a></li>
</ul>
</aside>
<h4 class="tsd-type-parameters-title">Type parameters</h4>
Expand Down Expand Up @@ -1132,7 +1136,7 @@ <h3>cata</h3>
<aside class="tsd-sources">
<p>Overrides <a href="_maybe_.maybe.html">Maybe</a>.<a href="_maybe_.maybe.html#cata">cata</a></p>
<ul>
<li>Defined in <a href="https://github.com/kofno/maybeasy/blob/f30571a/src/Just.ts#L25">Just.ts:25</a></li>
<li>Defined in <a href="https://github.com/kofno/maybeasy/blob/a2bf999/src/Just.ts#L25">Just.ts:25</a></li>
</ul>
</aside>
<h4 class="tsd-type-parameters-title">Type parameters</h4>
Expand All @@ -1151,6 +1155,48 @@ <h4 class="tsd-returns-title">Returns <span class="tsd-signature-type">B</span><
</li>
</ul>
</section>
<section class="tsd-panel tsd-member tsd-kind-method tsd-parent-kind-class tsd-is-overwrite">
<a name="do" class="tsd-anchor"></a>
<h3>do</h3>
<ul class="tsd-signatures tsd-kind-method tsd-parent-kind-class tsd-is-overwrite">
<li class="tsd-signature tsd-kind-icon">do<span class="tsd-signature-symbol">(</span>fn<span class="tsd-signature-symbol">: </span><span class="tsd-signature-type">function</span><span class="tsd-signature-symbol">)</span><span class="tsd-signature-symbol">: </span><a href="_maybe_.maybe.html" class="tsd-signature-type">Maybe</a><span class="tsd-signature-symbol">&lt;</span><span class="tsd-signature-type">A</span><span class="tsd-signature-symbol">&gt;</span></li>
</ul>
<ul class="tsd-descriptions">
<li class="tsd-description">
<aside class="tsd-sources">
<p>Overrides <a href="_maybe_.maybe.html">Maybe</a>.<a href="_maybe_.maybe.html#do">do</a></p>
<ul>
<li>Defined in <a href="https://github.com/kofno/maybeasy/blob/a2bf999/src/Just.ts#L40">Just.ts:40</a></li>
</ul>
</aside>
<h4 class="tsd-parameters-title">Parameters</h4>
<ul class="tsd-parameters">
<li>
<h5>fn: <span class="tsd-signature-type">function</span></h5>
<ul class="tsd-parameters">
<li class="tsd-parameter-siganture">
<ul class="tsd-signatures tsd-kind-type-literal">
<li class="tsd-signature tsd-kind-icon"><span class="tsd-signature-symbol">(</span>a<span class="tsd-signature-symbol">: </span><span class="tsd-signature-type">A</span><span class="tsd-signature-symbol">)</span><span class="tsd-signature-symbol">: </span><span class="tsd-signature-type">void</span></li>
</ul>
<ul class="tsd-descriptions">
<li class="tsd-description">
<h4 class="tsd-parameters-title">Parameters</h4>
<ul class="tsd-parameters">
<li>
<h5>a: <span class="tsd-signature-type">A</span></h5>
</li>
</ul>
<h4 class="tsd-returns-title">Returns <span class="tsd-signature-type">void</span></h4>
</li>
</ul>
</li>
</ul>
</li>
</ul>
<h4 class="tsd-returns-title">Returns <a href="_maybe_.maybe.html" class="tsd-signature-type">Maybe</a><span class="tsd-signature-symbol">&lt;</span><span class="tsd-signature-type">A</span><span class="tsd-signature-symbol">&gt;</span></h4>
</li>
</ul>
</section>
<section class="tsd-panel tsd-member tsd-kind-method tsd-parent-kind-class tsd-is-overwrite">
<a name="getorelse" class="tsd-anchor"></a>
<h3>get<wbr>OrElse</h3>
Expand All @@ -1162,7 +1208,7 @@ <h3>get<wbr>OrElse</h3>
<aside class="tsd-sources">
<p>Overrides <a href="_maybe_.maybe.html">Maybe</a>.<a href="_maybe_.maybe.html#getorelse">getOrElse</a></p>
<ul>
<li>Defined in <a href="https://github.com/kofno/maybeasy/blob/f30571a/src/Just.ts#L9">Just.ts:9</a></li>
<li>Defined in <a href="https://github.com/kofno/maybeasy/blob/a2bf999/src/Just.ts#L9">Just.ts:9</a></li>
</ul>
</aside>
<h4 class="tsd-parameters-title">Parameters</h4>
Expand Down Expand Up @@ -1198,7 +1244,7 @@ <h3>get<wbr>OrElse<wbr>Value</h3>
<aside class="tsd-sources">
<p>Overrides <a href="_maybe_.maybe.html">Maybe</a>.<a href="_maybe_.maybe.html#getorelsevalue">getOrElseValue</a></p>
<ul>
<li>Defined in <a href="https://github.com/kofno/maybeasy/blob/f30571a/src/Just.ts#L13">Just.ts:13</a></li>
<li>Defined in <a href="https://github.com/kofno/maybeasy/blob/a2bf999/src/Just.ts#L13">Just.ts:13</a></li>
</ul>
</aside>
<h4 class="tsd-parameters-title">Parameters</h4>
Expand All @@ -1222,7 +1268,7 @@ <h3>map</h3>
<aside class="tsd-sources">
<p>Overrides <a href="_maybe_.maybe.html">Maybe</a>.<a href="_maybe_.maybe.html#map">map</a></p>
<ul>
<li>Defined in <a href="https://github.com/kofno/maybeasy/blob/f30571a/src/Just.ts#L17">Just.ts:17</a></li>
<li>Defined in <a href="https://github.com/kofno/maybeasy/blob/a2bf999/src/Just.ts#L17">Just.ts:17</a></li>
</ul>
</aside>
<h4 class="tsd-type-parameters-title">Type parameters</h4>
Expand Down
71 changes: 65 additions & 6 deletions docs/classes/_maybe_.maybe.html
Original file line number Diff line number Diff line change
Expand Up @@ -921,6 +921,9 @@
<li class=" tsd-kind-method tsd-parent-kind-class tsd-has-type-parameter">
<a href="_maybe_.maybe.html#cata" class="tsd-kind-icon">cata</a>
</li>
<li class=" tsd-kind-method tsd-parent-kind-class">
<a href="_maybe_.maybe.html#do" class="tsd-kind-icon">do</a>
</li>
<li class=" tsd-kind-method tsd-parent-kind-class">
<a href="_maybe_.maybe.html#getorelse" class="tsd-kind-icon">get<wbr>OrElse</a>
</li>
Expand Down Expand Up @@ -978,6 +981,7 @@ <h3>Methods</h3>
<li class="tsd-kind-method tsd-parent-kind-class tsd-has-type-parameter"><a href="_maybe_.maybe.html#andthen" class="tsd-kind-icon">and<wbr>Then</a></li>
<li class="tsd-kind-method tsd-parent-kind-class tsd-has-type-parameter"><a href="_maybe_.maybe.html#assign" class="tsd-kind-icon">assign</a></li>
<li class="tsd-kind-method tsd-parent-kind-class tsd-has-type-parameter"><a href="_maybe_.maybe.html#cata" class="tsd-kind-icon">cata</a></li>
<li class="tsd-kind-method tsd-parent-kind-class"><a href="_maybe_.maybe.html#do" class="tsd-kind-icon">do</a></li>
<li class="tsd-kind-method tsd-parent-kind-class"><a href="_maybe_.maybe.html#getorelse" class="tsd-kind-icon">get<wbr>OrElse</a></li>
<li class="tsd-kind-method tsd-parent-kind-class"><a href="_maybe_.maybe.html#getorelsevalue" class="tsd-kind-icon">get<wbr>OrElse<wbr>Value</a></li>
<li class="tsd-kind-method tsd-parent-kind-class tsd-has-type-parameter"><a href="_maybe_.maybe.html#map" class="tsd-kind-icon">map</a></li>
Expand All @@ -998,7 +1002,7 @@ <h3><span class="tsd-flag ts-flagAbstract">Abstract</span> and<wbr>Then</h3>
<li class="tsd-description">
<aside class="tsd-sources">
<ul>
<li>Defined in <a href="https://github.com/kofno/maybeasy/blob/f30571a/src/Maybe.ts#L30">Maybe.ts:30</a></li>
<li>Defined in <a href="https://github.com/kofno/maybeasy/blob/a2bf999/src/Maybe.ts#L30">Maybe.ts:30</a></li>
</ul>
</aside>
<div class="tsd-comment tsd-typography">
Expand Down Expand Up @@ -1051,7 +1055,7 @@ <h3><span class="tsd-flag ts-flagAbstract">Abstract</span> assign</h3>
<li class="tsd-description">
<aside class="tsd-sources">
<ul>
<li>Defined in <a href="https://github.com/kofno/maybeasy/blob/f30571a/src/Maybe.ts#L49">Maybe.ts:49</a></li>
<li>Defined in <a href="https://github.com/kofno/maybeasy/blob/a2bf999/src/Maybe.ts#L49">Maybe.ts:49</a></li>
</ul>
</aside>
<div class="tsd-comment tsd-typography">
Expand Down Expand Up @@ -1098,7 +1102,7 @@ <h3><span class="tsd-flag ts-flagAbstract">Abstract</span> cata</h3>
<li class="tsd-description">
<aside class="tsd-sources">
<ul>
<li>Defined in <a href="https://github.com/kofno/maybeasy/blob/f30571a/src/Maybe.ts#L35">Maybe.ts:35</a></li>
<li>Defined in <a href="https://github.com/kofno/maybeasy/blob/a2bf999/src/Maybe.ts#L35">Maybe.ts:35</a></li>
</ul>
</aside>
<div class="tsd-comment tsd-typography">
Expand All @@ -1122,6 +1126,61 @@ <h4 class="tsd-returns-title">Returns <span class="tsd-signature-type">B</span><
</li>
</ul>
</section>
<section class="tsd-panel tsd-member tsd-kind-method tsd-parent-kind-class">
<a name="do" class="tsd-anchor"></a>
<h3><span class="tsd-flag ts-flagAbstract">Abstract</span> do</h3>
<ul class="tsd-signatures tsd-kind-method tsd-parent-kind-class">
<li class="tsd-signature tsd-kind-icon">do<span class="tsd-signature-symbol">(</span>fn<span class="tsd-signature-symbol">: </span><span class="tsd-signature-type">function</span><span class="tsd-signature-symbol">)</span><span class="tsd-signature-symbol">: </span><a href="_maybe_.maybe.html" class="tsd-signature-type">Maybe</a><span class="tsd-signature-symbol">&lt;</span><span class="tsd-signature-type">A</span><span class="tsd-signature-symbol">&gt;</span></li>
</ul>
<ul class="tsd-descriptions">
<li class="tsd-description">
<aside class="tsd-sources">
<ul>
<li>Defined in <a href="https://github.com/kofno/maybeasy/blob/a2bf999/src/Maybe.ts#L71">Maybe.ts:71</a></li>
</ul>
</aside>
<div class="tsd-comment tsd-typography">
<div class="lead">
<p>Inject a side-effectual operation into a chain of maybe operations.</p>
</div>
<p>The primary use case for <code>do</code> is to perform logging in the middle of a flow
of Maybe computations.</p>
<p>The side effect only runs when there is a value (Just).</p>
<p>The value will (should) remain unchanged during the <code>do</code> operation.</p>
<p> just({})
.assign(&#39;foo&#39;, just(42))
.assign(&#39;bar&#39;, just(&#39;hello&#39;))
.do(scope =&gt; console.log(&#39;Scope: &#39;, JSON.stringify(scope)))
.map(doSomethingElse)</p>
</div>
<h4 class="tsd-parameters-title">Parameters</h4>
<ul class="tsd-parameters">
<li>
<h5>fn: <span class="tsd-signature-type">function</span></h5>
<ul class="tsd-parameters">
<li class="tsd-parameter-siganture">
<ul class="tsd-signatures tsd-kind-type-literal">
<li class="tsd-signature tsd-kind-icon"><span class="tsd-signature-symbol">(</span>a<span class="tsd-signature-symbol">: </span><span class="tsd-signature-type">A</span><span class="tsd-signature-symbol">)</span><span class="tsd-signature-symbol">: </span><span class="tsd-signature-type">void</span></li>
</ul>
<ul class="tsd-descriptions">
<li class="tsd-description">
<h4 class="tsd-parameters-title">Parameters</h4>
<ul class="tsd-parameters">
<li>
<h5>a: <span class="tsd-signature-type">A</span></h5>
</li>
</ul>
<h4 class="tsd-returns-title">Returns <span class="tsd-signature-type">void</span></h4>
</li>
</ul>
</li>
</ul>
</li>
</ul>
<h4 class="tsd-returns-title">Returns <a href="_maybe_.maybe.html" class="tsd-signature-type">Maybe</a><span class="tsd-signature-symbol">&lt;</span><span class="tsd-signature-type">A</span><span class="tsd-signature-symbol">&gt;</span></h4>
</li>
</ul>
</section>
<section class="tsd-panel tsd-member tsd-kind-method tsd-parent-kind-class">
<a name="getorelse" class="tsd-anchor"></a>
<h3><span class="tsd-flag ts-flagAbstract">Abstract</span> get<wbr>OrElse</h3>
Expand All @@ -1132,7 +1191,7 @@ <h3><span class="tsd-flag ts-flagAbstract">Abstract</span> get<wbr>OrElse</h3>
<li class="tsd-description">
<aside class="tsd-sources">
<ul>
<li>Defined in <a href="https://github.com/kofno/maybeasy/blob/f30571a/src/Maybe.ts#L12">Maybe.ts:12</a></li>
<li>Defined in <a href="https://github.com/kofno/maybeasy/blob/a2bf999/src/Maybe.ts#L12">Maybe.ts:12</a></li>
</ul>
</aside>
<div class="tsd-comment tsd-typography">
Expand Down Expand Up @@ -1173,7 +1232,7 @@ <h3><span class="tsd-flag ts-flagAbstract">Abstract</span> get<wbr>OrElse<wbr>Va
<li class="tsd-description">
<aside class="tsd-sources">
<ul>
<li>Defined in <a href="https://github.com/kofno/maybeasy/blob/f30571a/src/Maybe.ts#L18">Maybe.ts:18</a></li>
<li>Defined in <a href="https://github.com/kofno/maybeasy/blob/a2bf999/src/Maybe.ts#L18">Maybe.ts:18</a></li>
</ul>
</aside>
<div class="tsd-comment tsd-typography">
Expand Down Expand Up @@ -1202,7 +1261,7 @@ <h3><span class="tsd-flag ts-flagAbstract">Abstract</span> map</h3>
<li class="tsd-description">
<aside class="tsd-sources">
<ul>
<li>Defined in <a href="https://github.com/kofno/maybeasy/blob/f30571a/src/Maybe.ts#L24">Maybe.ts:24</a></li>
<li>Defined in <a href="https://github.com/kofno/maybeasy/blob/a2bf999/src/Maybe.ts#L24">Maybe.ts:24</a></li>
</ul>
</aside>
<div class="tsd-comment tsd-typography">
Expand Down
Loading

0 comments on commit a5c61d8

Please sign in to comment.