<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>totalfunction &#8211; Software, Fitness, and Gaming &#8211; Jesse Warden</title>
	<atom:link href="https://jessewarden.com/tag/totalfunction/feed" rel="self" type="application/rss+xml" />
	<link>https://jessewarden.com</link>
	<description>Software &#124; Fitness &#124; Gaming</description>
	<lastBuildDate>Fri, 29 Jun 2018 15:35:44 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	

<image>
	<url>https://jessewarden.com/wp-content/uploads/2016/08/cropped-Lambda2-32x32.png</url>
	<title>totalfunction &#8211; Software, Fitness, and Gaming &#8211; Jesse Warden</title>
	<link>https://jessewarden.com</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>Pure Function vs. Total Function</title>
		<link>https://jessewarden.com/2018/06/pure-function-vs-total-function.html</link>
		
		<dc:creator><![CDATA[JesterXL]]></dc:creator>
		<pubDate>Fri, 29 Jun 2018 15:09:53 +0000</pubDate>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[fp]]></category>
		<category><![CDATA[functionalprogramming]]></category>
		<category><![CDATA[purefunction]]></category>
		<category><![CDATA[totalfunction]]></category>
		<guid isPermaLink="false">http://jessewarden.com/?p=5663</guid>

					<description><![CDATA[A pure function: const add = (a, b) =&#62; a + b vs. a total function: const addNumbers = (a, b) =&#62; ( isNumber(a) &#38;&#38; isNumber(b) ) ? {ok: true, data: a + b} : {ok: false, error: new Error(Either a or b aren't Numbers.)} While same input, same output, no side effects sounds like [&#8230;]]]></description>
										<content:encoded><![CDATA[<p>A pure function:</p>
<pre><code class="javascript">const add = (a, b) =&gt; a + b
</code></pre>
<p>vs. a total function:</p>
<pre><code class="javascript">const addNumbers = (a, b) =&gt;
  ( isNumber(a) &amp;&amp; isNumber(b) )
  ? {ok: true, data: a + b}
  : {ok: false, error: new Error(Either a or b aren't Numbers.)}
</code></pre>
<p>While same input, same output, no side effects sounds like the end all, be all&#8230; it&#8217;s not.</p>
<p><span id="more-5663"></span></p>
<p>A pure function like this will always return 3:</p>
<pre><code class="javascript">console.log(add(1, 2))
</code></pre>
<p>&#8230; but this will always return &#8220;1Error: moo&#8221;:</p>
<pre><code class="javascript">console.log(add(1, new Error('moo')))
</code></pre>
<p>Not helpful.</p>
<p>This is where types help, no doubt, but at runtime, validation of your your data becomes important when you&#8217;re veer away from what types can help you find (range of a number, shape of data from remote JSON, etc).</p>
<p>Using the total function:</p>
<pre><code>console.log(addNumbers(1, 2))
</code></pre>
<p>will always give return:</p>
<pre><code class="javascript:">{ok: true, data: 3}
</code></pre>
<p>however, if the types aren&#8217;t correct:</p>
<pre><code>console.log(addNumbers(1, new Error('moo')))
</code></pre>
<p>will always return:</p>
<pre><code class="javascript">{ok: false, error: {…}}
</code></pre>
<p>A much more useful result to the developer, and less burden on their part.</p>
<p>Not all functions need be total. Just make your privates pure, and your publics total, for example. That way the privates don&#8217;t get bad data.</p>
]]></content:encoded>
					
		
		
			</item>
	</channel>
</rss>
