<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:content="http://purl.org/rss/1.0/modules/content/"><channel><title>Programming on sysarcher/@ich_code</title><link>https://sysarcher.github.io/categories/programming/</link><description>Recent content in Programming on sysarcher/@ich_code</description><generator>Hugo</generator><language>en-us</language><lastBuildDate>Wed, 23 Jan 2019 21:22:37 +0200</lastBuildDate><atom:link href="https://sysarcher.github.io/categories/programming/index.xml" rel="self" type="application/rss+xml"/><item><title>Functional Programming</title><link>https://sysarcher.github.io/posts/functional-programming/</link><pubDate>Wed, 23 Jan 2019 21:22:37 +0200</pubDate><guid>https://sysarcher.github.io/posts/functional-programming/</guid><description>&lt;p&gt;So I was just reading about Functinal programming and this is a summary of what I understand about it as of today.. (might change in the future; to err after all, is human.)&lt;/p&gt;
&lt;h2 id="benefits"&gt;Benefits&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;The &amp;ldquo;environment&amp;rdquo; you&amp;rsquo;re programming against are the function arguments&lt;/li&gt;
&lt;li&gt;Reuse is imminent. Composition is what everyone calls it.&lt;/li&gt;
&lt;li&gt;Functions&amp;rsquo; results can be cached (memoization)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;There must be many more benefits.. These three are the ones I can think of and the ones that get the most marks from my side.&lt;/p&gt;</description><content:encoded><![CDATA[<p>So I was just reading about Functinal programming and this is a summary of what I understand about it as of today.. (might change in the future; to err after all, is human.)</p>
<h2 id="benefits">Benefits</h2>
<ul>
<li>The &ldquo;environment&rdquo; you&rsquo;re programming against are the function arguments</li>
<li>Reuse is imminent. Composition is what everyone calls it.</li>
<li>Functions&rsquo; results can be cached (memoization)</li>
</ul>
<p>There must be many more benefits.. These three are the ones I can think of and the ones that get the most marks from my side.</p>
<h2 id="main-concepts">Main Concepts</h2>
<p>Functional programming is a technique that depends on the programmer adhering to some principles (it helps if the programming language forces him as well):</p>
<h3 id="purity">Purity</h3>
<p>A pure function has no side effects i.e. it depends only on its arguments.</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"><code class="language-clj" data-lang="clj"><span style="display:flex;"><span>(<span style="color:#66d9ef">defn </span>pure-function [input]
</span></span><span style="display:flex;"><span>  (<span style="color:#66d9ef">if </span>input output-1 output-2))
</span></span></code></pre></div><p>In this example, if the <code>pure-function</code> is really pure, then depending on the input, the return value always remains the same. This <em>also</em> means that the <code>output-1</code> and <code>output-2</code> data are <em>immutable</em> i.e. they&rsquo;re unchangable!</p>
<h3 id="referential-transparency">Referential Transparency</h3>
<p>In the example above, we might just have replaced the output of <code>pure-funtion</code> with either <code>output-1</code> or <code>output-2</code> thus avoiding the call to a function. Many languages can optimize this behavior by caching the result. It&rsquo;s called <em>memoization.</em> This also means that you could parallelize stuff i.e. functions can be independently evaluated on different machines/cores/nodes whatevers.</p>
<h3 id="persistence">Persistence</h3>
<p>Immutability is key. You never mutate. An operation on data doesn&rsquo;t change the data, it just returns new data. This naive approach is memory hungry I suppose but platforms and languages find ways to optimize stuff.</p>
<h3 id="laziness">Laziness</h3>
<p>Since I&rsquo;m learning Clojure, there might be a bias because <code>seq</code> are everywhere and they&rsquo;re laaaaaaaaaaazy! That is, you only evaluate stuff when it&rsquo;s required and never before. This enables infinite length sequences. Also, we pay for operations only when required.</p>
<h2 id="conclusion">Conclusion</h2>
<p>I&rsquo;m still a Functional Programming newbie but Clojure is opening my eyes! I am intrigued.</p>
<p>Fin!</p>
]]></content:encoded></item></channel></rss>