<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Sreedev Kodichath on Medium]]></title>
        <description><![CDATA[Stories by Sreedev Kodichath on Medium]]></description>
        <link>https://medium.com/@sreedevk?source=rss-9eba103f8123------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*p9SwPuz2X8pRSApW3jdlIA.jpeg</url>
            <title>Stories by Sreedev Kodichath on Medium</title>
            <link>https://medium.com/@sreedevk?source=rss-9eba103f8123------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 24 Nov 2026 08:22:55 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@sreedevk/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[An introductory guide to functional programming concepts]]></title>
            <link>https://blog.tarkalabs.com/an-introductory-guide-to-functional-programming-concepts-5d23abf0b03a?source=rss-9eba103f8123------2</link>
            <guid isPermaLink="false">https://medium.com/p/5d23abf0b03a</guid>
            <category><![CDATA[functional-programming]]></category>
            <category><![CDATA[composition]]></category>
            <category><![CDATA[lambda]]></category>
            <category><![CDATA[function]]></category>
            <category><![CDATA[recursion]]></category>
            <dc:creator><![CDATA[Sreedev Kodichath]]></dc:creator>
            <pubDate>Thu, 14 Apr 2022 12:45:53 GMT</pubDate>
            <atom:updated>2022-06-29T11:54:44.224Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="Introduction to Functional Concepts" src="https://cdn-images-1.medium.com/max/1024/1*UH6-uqtZ6-hPQTAmAXEueg.gif" /><figcaption>All images in this blog were created by <a href="https://medium.com/@singh.gaurav">Gaurav Singh</a>.</figcaption></figure><p>Functional Programming is a vast ocean of thoughts, ideas &amp; implementations. I have only begun to explore this exquisite programming paradigm. From an imperative world, I transitioned into a more organized process of Object-Oriented Design (OOD), and more recently, having experienced all the goodness and shortcomings of object-oriented thinking, I’ve stepped into the exotic sphere of “functional programming.”</p><p>My goal in writing this article is to share some of the concepts pertinent to functional programming &amp; how I altered my programming thought process to be more “functional.” If you’re looking for a more comprehensive guide to functional programming, I’d recommend reading one of many well-written &amp; organized books out there like “Category Theory For Programmers” by Bartosz Milewski or “An Introduction to Functional Programming Through Lambda Calculus” by Greg Michaelson.</p><h4>The Functional Approach</h4><p>Before we jump into the functional approach, I need to talk about the kind of functional programming I intend to discuss today. There is “<a href="https://en.wikipedia.org/wiki/Functional_programming">functional programming</a>”, then there is “<a href="https://en.wikipedia.org/wiki/Purely_functional_programming">purely functional programming</a>”.</p><p>Languages like Elixir do not implement Purely Functional Programming. For example, on one of the <a href="https://hexdocs.pm/elixir/main/IO.html">Elixir Hexdocs pages</a>, there is a line that states :</p><blockquote>”Note that an IO stream has side effects, and every time you go over the stream you may get different results.”</blockquote><p>In purely functional programming languages like Haskell, you cannot have side effects. Purely functional programming forbids side effects, which means purely functional programs cannot affect any changes in the real world. But programs would be pretty much useless without side effects. To model side effects in a purely functional way, languages like Haskell use “Monads.” James Iry <a href="https://james-iry.blogspot.com/2009/05/brief-incomplete-and-mostly-wrong.html">famously said</a>:</p><blockquote>A monad is just a monoid in the category of endofunctors, what’s the problem?</blockquote><p>I am not going to talk about Monads, Functors, or The Category Theory in this article though the concepts discussed here have their roots in category theory. Instead, I will take you through some functional programming ideas that can serve as a stepping stone into the world of functional programming and help mitigate any misunderstandings you may have had regarding these concepts.</p><h4>1. Composition, Complexity &amp; Pipes</h4><figure><img alt="Composition, Complexity &amp; Pipes" src="https://cdn-images-1.medium.com/max/1024/1*tgUfmP9AhQXyVOfNuaFl-g.png" /></figure><p>Composition is the essence of functional programming.</p><p><em>The concept of composition in functional programming can minimally be defined as a combination of simple functions to build more complex functions.</em></p><p>“Accidental Complexity” is a term used to describe the complexity that creeps into your code which is unintentional, unnecessary &amp; incidental to the actual problem being solved. Composition in functional programming aims to keep the accidental complexity &amp; even logical complexity at a minimum.</p><p>To illustrate composition quite simply, Let’s look at this example (ruby).</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcarbon.now.sh%2Fembed%3Fbg%3Drgba%2528171%252C%2B184%252C%2B195%252C%2B1%2529%26t%3Dsynthwave-84%26wt%3Dnone%26l%3Druby%26width%3D320%26ds%3Dtrue%26dsyoff%3D20px%26dsblur%3D68px%26wc%3Dtrue%26wa%3Dfalse%26pv%3D0px%26ph%3D0px%26ln%3Dfalse%26fl%3D1%26fm%3DHack%26fs%3D14px%26lh%3D133%2525%26si%3Dfalse%26es%3D2x%26wm%3Dfalse%26code%3Dy%252520%25253D%252520f%2528x%2529%25250Az%252520%25253D%252520g%2528y%2529%25250Az%252520%25253D%25253D%252520g%2528f%2528x%2529%2529&amp;display_name=Carbon&amp;url=https%3A%2F%2Fcarbon.now.sh%2F%3Fbg%3Drgba%252528171%25252C%2B184%25252C%2B195%25252C%2B1%252529%26t%3Dsynthwave-84%26wt%3Dnone%26l%3Druby%26width%3D320%26ds%3Dtrue%26dsyoff%3D20px%26dsblur%3D68px%26wc%3Dtrue%26wa%3Dfalse%26pv%3D0px%26ph%3D0px%26ln%3Dfalse%26fl%3D1%26fm%3DHack%26fs%3D14px%26lh%3D133%252525%26si%3Dfalse%26es%3D2x%26wm%3Dfalse%26code%3Dy%25252520%2525253D%25252520f%252528x%252529%2525250Az%25252520%2525253D%25252520g%252528y%252529%2525250Az%25252520%2525253D%2525253D%25252520g%252528f%252528x%252529%252529&amp;image=https%3A%2F%2Fcarbon.now.sh%2Fstatic%2Fbrand%2Fbanner.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=carbon" width="1024" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/e0c1d15febac02aa3d09cd8514f4c196/href">https://medium.com/media/e0c1d15febac02aa3d09cd8514f4c196/href</a></iframe><p>Functional programming languages offer several tools &amp; techniques to make the composition work elegantly, Pipelines/Threads being an example.</p><p>If you’ve worked with lisp-family languages, you’d know how easy it is to lose track of your progress in terms of the core problem solution. Even in other functional languages like Elixir, you’d run into this problem more often than you think.</p><p>Solutions for this problem already exist within the “framework” of the functional programming paradigm. Functional programming languages implement these solutions in different ways. Here’s an example:</p><p>Languages like Clojure <em>(-&gt;/-&gt;&gt;)</em> &amp; Elixir <em>(|&gt;)</em> introduce thread/pipe operator to avoid this problem, and that’s nothing but a tool for composition. Let’s examine some code to understand how exactly this works. I’m going to be using Elixir to make it easily understandable to people unfamiliar with lisps too.</p><p>This is a snippet of code from my <a href="https://adventofcode.com/2021/day/1">Advent Of Code 2021, Day 1</a> <a href="https://github.com/sreedevk/advent-of-code/blob/main/elixir/lib/twenty21/day1.ex">solution</a>.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcarbon.now.sh%2Fembed%3Fbg%3Drgba%2528171%252C%2B184%252C%2B195%252C%2B1%2529%26t%3Dsynthwave-84%26wt%3Dnone%26l%3Druby%26width%3D458%26ds%3Dtrue%26dsyoff%3D20px%26dsblur%3D68px%26wc%3Dtrue%26wa%3Dfalse%26pv%3D0px%26ph%3D0px%26ln%3Dfalse%26fl%3D1%26fm%3DHack%26fs%3D14px%26lh%3D133%2525%26si%3Dfalse%26es%3D2x%26wm%3Dfalse%26code%3DEnum.map%2528%25250A%252520%252520String.split%2528%25250A%252520%252520%252520%252520File.read%2528%252522data.txt%252522%2529%25252C%252520%252522%25255Cn%252522%25252C%252520trim%25253A%252520true%25250A%252520%252520%2529%25252C%252520%252526String.to_integer%25252F1%25250A%2529&amp;display_name=Carbon&amp;url=https%3A%2F%2Fcarbon.now.sh%2F%3Fbg%3Drgba%252528171%25252C%2B184%25252C%2B195%25252C%2B1%252529%26t%3Dsynthwave-84%26wt%3Dnone%26l%3Druby%26width%3D458%26ds%3Dtrue%26dsyoff%3D20px%26dsblur%3D68px%26wc%3Dtrue%26wa%3Dfalse%26pv%3D0px%26ph%3D0px%26ln%3Dfalse%26fl%3D1%26fm%3DHack%26fs%3D14px%26lh%3D133%252525%26si%3Dfalse%26es%3D2x%26wm%3Dfalse%26code%3DEnum.map%252528%2525250A%25252520%25252520String.split%252528%2525250A%25252520%25252520%25252520%25252520File.read%252528%25252522data.txt%25252522%252529%2525252C%25252520%25252522%2525255Cn%25252522%2525252C%25252520trim%2525253A%25252520true%2525250A%25252520%25252520%252529%2525252C%25252520%25252526String.to_integer%2525252F1%2525250A%252529&amp;image=https%3A%2F%2Fcarbon.now.sh%2Fstatic%2Fbrand%2Fbanner.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=carbon" width="1024" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/95e367c516799df4232999cd2229d559/href">https://medium.com/media/95e367c516799df4232999cd2229d559/href</a></iframe><p>As you can see, readability has already taken a significant hit. All we’re trying to do here is, read a file, split the contents of the file into an array of lines &amp; parse each line into an integer.</p><p>If you notice, the logic flows outward. The order of operations are not linear in the code.</p><p>Now let’s try using Elixir’s pipe operator, which takes the value that the expression on the “left side” of the operator evaluates and passes it into the next function as the first parameter to the function.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcarbon.now.sh%2Fembed%3Fbg%3Drgba%2528171%252C%2B184%252C%2B195%252C%2B1%2529%26t%3Dsynthwave-84%26wt%3Dnone%26l%3Delixir%26width%3D458%26ds%3Dtrue%26dsyoff%3D20px%26dsblur%3D68px%26wc%3Dtrue%26wa%3Dfalse%26pv%3D0px%26ph%3D0px%26ln%3Dfalse%26fl%3D1%26fm%3DHack%26fs%3D14px%26lh%3D133%2525%26si%3Dfalse%26es%3D2x%26wm%3Dfalse%26code%3DFile.read%2528%252522data.txt%252522%2529%25250A%25257C%25253E%252520String.split%2528%252522%25255Cn%252522%25252C%252520trim%25253A%252520true%2529%25250A%25257C%25253E%252520Enum.map%2528%252526String.to_integer%25252F1%2529&amp;display_name=Carbon&amp;url=https%3A%2F%2Fcarbon.now.sh%2F%3Fbg%3Drgba%252528171%25252C%2B184%25252C%2B195%25252C%2B1%252529%26t%3Dsynthwave-84%26wt%3Dnone%26l%3Delixir%26width%3D458%26ds%3Dtrue%26dsyoff%3D20px%26dsblur%3D68px%26wc%3Dtrue%26wa%3Dfalse%26pv%3D0px%26ph%3D0px%26ln%3Dfalse%26fl%3D1%26fm%3DHack%26fs%3D14px%26lh%3D133%252525%26si%3Dfalse%26es%3D2x%26wm%3Dfalse%26code%3DFile.read%252528%25252522data.txt%25252522%252529%2525250A%2525257C%2525253E%25252520String.split%252528%25252522%2525255Cn%25252522%2525252C%25252520trim%2525253A%25252520true%252529%2525250A%2525257C%2525253E%25252520Enum.map%252528%25252526String.to_integer%2525252F1%252529&amp;image=https%3A%2F%2Fcarbon.now.sh%2Fstatic%2Fbrand%2Fbanner.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=carbon" width="1024" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/128ff86636e276fa86cb41d0eea34749/href">https://medium.com/media/128ff86636e276fa86cb41d0eea34749/href</a></iframe><p>That magically seems to provide a lot of structure to the code in addition to readability.</p><p>You can almost see the data flow through those pipes.</p><p>Clojure offers two function composition operators (thread macros as they are called in Clojure).</p><p><em>thread-first (-&gt;)</em>, which passes the result of the previous function into the next function as the first parameter</p><p><em>thread-last (-&gt;&gt;)</em>, which passes the result of the previous function into the next function as the last parameter.</p><p>Here’s an example from <a href="https://github.com/sreedevk/advent-of-code/blob/main/clojure/advent/src/advent/y2021/day1.clj">my advent of code 2021, day 1 solution</a>:</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcarbon.now.sh%2Fembed%3Fbg%3Drgba%2528171%252C%2B184%252C%2B195%252C%2B1%2529%26t%3Dsynthwave-84%26wt%3Dnone%26l%3Dclojure%26width%3D458%26ds%3Dtrue%26dsyoff%3D20px%26dsblur%3D68px%26wc%3Dtrue%26wa%3Dfalse%26pv%3D0px%26ph%3D0px%26ln%3Dfalse%26fl%3D1%26fm%3DHack%26fs%3D14px%26lh%3D133%2525%26si%3Dfalse%26es%3D2x%26wm%3Dfalse%26code%3D%2528defn%252520alpha%252520%25255B%25255D%25250A%252509%2528-%25253E%25253E%252520%2528data%2529%25250A%252509%252509%252520%2528partition%2525202%2525201%2529%25250A%252509%252509%252520%2528filter%252520%252523%2528apply%252520%25253C%252520%252525%2529%2529%25250A%252509%252509%252520%2528count%2529%2529%2529&amp;display_name=Carbon&amp;url=https%3A%2F%2Fcarbon.now.sh%2F%3Fbg%3Drgba%252528171%25252C%2B184%25252C%2B195%25252C%2B1%252529%26t%3Dsynthwave-84%26wt%3Dnone%26l%3Dclojure%26width%3D458%26ds%3Dtrue%26dsyoff%3D20px%26dsblur%3D68px%26wc%3Dtrue%26wa%3Dfalse%26pv%3D0px%26ph%3D0px%26ln%3Dfalse%26fl%3D1%26fm%3DHack%26fs%3D14px%26lh%3D133%252525%26si%3Dfalse%26es%3D2x%26wm%3Dfalse%26code%3D%252528defn%25252520alpha%25252520%2525255B%2525255D%2525250A%25252509%252528-%2525253E%2525253E%25252520%252528data%252529%2525250A%25252509%25252509%25252520%252528partition%252525202%252525201%252529%2525250A%25252509%25252509%25252520%252528filter%25252520%25252523%252528apply%25252520%2525253C%25252520%25252525%252529%252529%2525250A%25252509%25252509%25252520%252528count%252529%252529%252529&amp;image=https%3A%2F%2Fcarbon.now.sh%2Fstatic%2Fbrand%2Fbanner.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=carbon" width="1024" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/d6a9e0156abf49c1c9e155f383e066c6/href">https://medium.com/media/d6a9e0156abf49c1c9e155f383e066c6/href</a></iframe><p>Even <a href="https://medium.com/tarkalabs-til/function-composition-in-ruby-69e37cdc9b0">Ruby offers a “function/lambda composition operator” since version 2.6</a>.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcarbon.now.sh%2Fembed%3Fbg%3Drgba%2528171%252C%2B184%252C%2B195%252C%2B1%2529%26t%3Dsynthwave-84%26wt%3Dnone%26l%3Druby%26width%3D458%26ds%3Dtrue%26dsyoff%3D20px%26dsblur%3D68px%26wc%3Dtrue%26wa%3Dfalse%26pv%3D0px%26ph%3D0px%26ln%3Dfalse%26fl%3D1%26fm%3DHack%26fs%3D14px%26lh%3D133%2525%26si%3Dfalse%26es%3D2x%26wm%3Dfalse%26code%3Dx%252520%25253D%25252010%25250Af%252520%25253D%252520-%25253E%252520%2528w%2529%252520%25257B%252520w%252520%252A%2525202%252520%25257D%25250Ag%252520%25253D%252520-%25253E%252520%2528w%2529%252520%25257B%252520w%252520%252A%2525204%252520%25257D%25250A%2528f%252520%25253E%25253E%252520g%2529.call%2528x%2529%25250A%252523%252520or%25250A%2528g%252520%25253C%25253C%252520f%2529.call%2528x%2529&amp;display_name=Carbon&amp;url=https%3A%2F%2Fcarbon.now.sh%2F%3Fbg%3Drgba%252528171%25252C%2B184%25252C%2B195%25252C%2B1%252529%26t%3Dsynthwave-84%26wt%3Dnone%26l%3Druby%26width%3D458%26ds%3Dtrue%26dsyoff%3D20px%26dsblur%3D68px%26wc%3Dtrue%26wa%3Dfalse%26pv%3D0px%26ph%3D0px%26ln%3Dfalse%26fl%3D1%26fm%3DHack%26fs%3D14px%26lh%3D133%252525%26si%3Dfalse%26es%3D2x%26wm%3Dfalse%26code%3Dx%25252520%2525253D%2525252010%2525250Af%25252520%2525253D%25252520-%2525253E%25252520%252528w%252529%25252520%2525257B%25252520w%25252520%25252A%252525202%25252520%2525257D%2525250Ag%25252520%2525253D%25252520-%2525253E%25252520%252528w%252529%25252520%2525257B%25252520w%25252520%25252A%252525204%25252520%2525257D%2525250A%252528f%25252520%2525253E%2525253E%25252520g%252529.call%252528x%252529%2525250A%25252523%25252520or%2525250A%252528g%25252520%2525253C%2525253C%25252520f%252529.call%252528x%252529&amp;image=https%3A%2F%2Fcarbon.now.sh%2Fstatic%2Fbrand%2Fbanner.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=carbon" width="1024" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/2f755a972c10473e2f10f023ccbe81d3/href">https://medium.com/media/2f755a972c10473e2f10f023ccbe81d3/href</a></iframe><p>There is a more mathematical explanation of the nature of composition in <a href="https://bartoszmilewski.com/2014/11/04/category-the-essence-of-composition/">Bartosz Milewski’s Article on The Essence of composition</a>.</p><h4>2. Pure Functions &amp; Referential Transparency</h4><figure><img alt="Pure Functions &amp; Referential Transparency" src="https://cdn-images-1.medium.com/max/1024/1*36e67Sd_uaza_M5z_MULpQ.png" /></figure><p>To classify a function as a pure function, it must adhere to certain rules in functional programming.</p><ol><li><em>The function should return identical return values for identical arguments.</em></li><li><em>The function does not affect any changes outside the function’s scope. (i.e., it should not cause side effects).</em></li></ol><p>A side effect, in general, is any change that an impure function makes outside the scope of the function.</p><p>For example, an addition function is a pure function. Here’s an example in Ruby.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcarbon.now.sh%2Fembed%3Fbg%3Drgba%2528171%252C%2B184%252C%2B195%252C%2B1%2529%26t%3Dsynthwave-84%26wt%3Dnone%26l%3Druby%26width%3D458%26ds%3Dtrue%26dsyoff%3D20px%26dsblur%3D68px%26wc%3Dtrue%26wa%3Dfalse%26pv%3D0px%26ph%3D0px%26ln%3Dfalse%26fl%3D1%26fm%3DHack%26fs%3D14px%26lh%3D133%2525%26si%3Dfalse%26es%3D2x%26wm%3Dfalse%26code%3Dadd%252520%25253D%252520-%25253E%252520%2528x%25252C%252520y%2529%252520%25257B%252520x%252520%25252B%252520y%252520%25257D%25250Aadd.%25281%25252C%2525202%2529%252520%252523%252520%25253D%25253E%2525203&amp;display_name=Carbon&amp;url=https%3A%2F%2Fcarbon.now.sh%2F%3Fbg%3Drgba%252528171%25252C%2B184%25252C%2B195%25252C%2B1%252529%26t%3Dsynthwave-84%26wt%3Dnone%26l%3Druby%26width%3D458%26ds%3Dtrue%26dsyoff%3D20px%26dsblur%3D68px%26wc%3Dtrue%26wa%3Dfalse%26pv%3D0px%26ph%3D0px%26ln%3Dfalse%26fl%3D1%26fm%3DHack%26fs%3D14px%26lh%3D133%252525%26si%3Dfalse%26es%3D2x%26wm%3Dfalse%26code%3Dadd%25252520%2525253D%25252520-%2525253E%25252520%252528x%2525252C%25252520y%252529%25252520%2525257B%25252520x%25252520%2525252B%25252520y%25252520%2525257D%2525250Aadd.%2525281%2525252C%252525202%252529%25252520%25252523%25252520%2525253D%2525253E%252525203&amp;image=https%3A%2F%2Fcarbon.now.sh%2Fstatic%2Fbrand%2Fbanner.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=carbon" width="1024" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/bb91efd5963ac6ecb92cdb305596d1bd/href">https://medium.com/media/bb91efd5963ac6ecb92cdb305596d1bd/href</a></iframe><p>For the above function/lambda, no matter what the condition, for a given set of numbers x &amp; y, it’ll always return the sum of x &amp; y. If your addition function gave different results each time you try to add 1 &amp; 2, it wouldn’t be reliable, would it?</p><p>Even though this seems doable for many cases, there are some cases where you cannot create a pure function. For example, a function that returns the current time or a function that returns a random number (without a seed or an implied seed parameter). Purely functional programming languages use <em>Monads</em> to solve this problem. But languages that are not purely functional, like Elixir, allow side effects in these special cases.</p><p>While thinking functionally, we want our functions to keep the level of side effects to a minimum.</p><p>“Referential transparency,” according to Wikipedia, is the property of an expression by which its corresponding value can replace it without affecting the program’s behavior. This requires the expression to be pure. A pure function can be treated as a subset of referential transparency by this definition.</p><p>Another way I tend to look at the difference between referential transparency &amp; purity of a function is,</p><p>Purity is a restriction imposed on a function, and referential transparency is the effect of the function being pure.</p><h4>3. Immutability, Assignment &amp; Binding</h4><figure><img alt="Immutability, Assignment &amp; Binding" src="https://cdn-images-1.medium.com/max/1024/1*U1aN2jdBeVRVsAIzC_aiLA.gif" /></figure><p>Mutation, by definition, is a side-effect. So, as a result, most functional languages do not allow mutation. Coming from a different paradigm, this might seem strange or even impossible to work with, but the more you work with immutability, the more you’ll understand why this is a good thing.</p><p>Variable assignments that model memory locations are not considered to be “functional” in nature. There is no concept of assignment in purely functional languages as the purpose of an assignment statement is to modify a variable. In functional languages, values are bound to labels. A neat way to understand this is with a little bit of good ol’ C. When you do something like <em>int x = 20</em>, you are doing two things, declaring the variable and then assigning a value to it, which is essentially mutating the variable.</p><blockquote>`Binding is used to create a new variable within the current context, while assignment can only change the value of a given variable within the narrowest bound scope.`</blockquote><p><a href="https://ashtonkemerling.com/posts/binding-vs-assignment/">https://ashtonkemerling.com/posts/binding-vs-assignment/</a></p><p>For Example, In Elixir this is perfectly valid code:</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcarbon.now.sh%2Fembed%3Fbg%3Drgba%2528171%252C%2B184%252C%2B195%252C%2B1%2529%26t%3Dsynthwave-84%26wt%3Dnone%26l%3Delixir%26width%3D458%26ds%3Dtrue%26dsyoff%3D20px%26dsblur%3D68px%26wc%3Dtrue%26wa%3Dfalse%26pv%3D0px%26ph%3D0px%26ln%3Dfalse%26fl%3D1%26fm%3DHack%26fs%3D14px%26lh%3D133%2525%26si%3Dfalse%26es%3D2x%26wm%3Dfalse%26code%3Diex%25281%2529%25253E%252520x%252520%25253D%2525201%25250Aiex%25282%2529%25253E%2525201%252520%25253D%252520x&amp;display_name=Carbon&amp;url=https%3A%2F%2Fcarbon.now.sh%2F%3Fbg%3Drgba%252528171%25252C%2B184%25252C%2B195%25252C%2B1%252529%26t%3Dsynthwave-84%26wt%3Dnone%26l%3Delixir%26width%3D458%26ds%3Dtrue%26dsyoff%3D20px%26dsblur%3D68px%26wc%3Dtrue%26wa%3Dfalse%26pv%3D0px%26ph%3D0px%26ln%3Dfalse%26fl%3D1%26fm%3DHack%26fs%3D14px%26lh%3D133%252525%26si%3Dfalse%26es%3D2x%26wm%3Dfalse%26code%3Diex%2525281%252529%2525253E%25252520x%25252520%2525253D%252525201%2525250Aiex%2525282%252529%2525253E%252525201%25252520%2525253D%25252520x&amp;image=https%3A%2F%2Fcarbon.now.sh%2Fstatic%2Fbrand%2Fbanner.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=carbon" width="1024" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/8501c93fa81ee241752bb84327a5e78c/href">https://medium.com/media/8501c93fa81ee241752bb84327a5e78c/href</a></iframe><p>Another distinction we need to make is: rebinding is not the same as mutation. So, doing something like</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcarbon.now.sh%2Fembed%3Fbg%3Drgba%2528171%252C%2B184%252C%2B195%252C%2B1%2529%26t%3Dsynthwave-84%26wt%3Dnone%26l%3Delixir%26width%3D458%26ds%3Dtrue%26dsyoff%3D20px%26dsblur%3D68px%26wc%3Dtrue%26wa%3Dfalse%26pv%3D0px%26ph%3D0px%26ln%3Dfalse%26fl%3D1%26fm%3DHack%26fs%3D14px%26lh%3D133%2525%26si%3Dfalse%26es%3D2x%26wm%3Dfalse%26code%3Diex%25281%2529%25253E%252520x%252520%25253D%2525201%25250Aiex%25282%2529%25253E%252520x%252520%25253D%2525202&amp;display_name=Carbon&amp;url=https%3A%2F%2Fcarbon.now.sh%2F%3Fbg%3Drgba%252528171%25252C%2B184%25252C%2B195%25252C%2B1%252529%26t%3Dsynthwave-84%26wt%3Dnone%26l%3Delixir%26width%3D458%26ds%3Dtrue%26dsyoff%3D20px%26dsblur%3D68px%26wc%3Dtrue%26wa%3Dfalse%26pv%3D0px%26ph%3D0px%26ln%3Dfalse%26fl%3D1%26fm%3DHack%26fs%3D14px%26lh%3D133%252525%26si%3Dfalse%26es%3D2x%26wm%3Dfalse%26code%3Diex%2525281%252529%2525253E%25252520x%25252520%2525253D%252525201%2525250Aiex%2525282%252529%2525253E%25252520x%25252520%2525253D%252525202&amp;image=https%3A%2F%2Fcarbon.now.sh%2Fstatic%2Fbrand%2Fbanner.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=carbon" width="1024" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/fce8264018edeee6b44546e5e4d365c4/href">https://medium.com/media/fce8264018edeee6b44546e5e4d365c4/href</a></iframe><p>is not mutation. It is simply rebinding.</p><h4>4. First Class Functions, Higher Order Functions, Currying &amp; Partial Application</h4><figure><img alt="First Class Functions, Higher Order Functions, Currying &amp; Partial Application" src="https://cdn-images-1.medium.com/max/1024/1*FHYBLxpPd8sieNhypk5PZw.png" /></figure><p>That’s a lot of things to cover under a single sub-heading, but they’re all related &amp; go together one after the other very well.</p><p>A language is said to support first class functions if it treats functions like any other variable or, more formally, as <a href="https://en.wikipedia.org/wiki/First-class_citizen">first-class citizens</a>.</p><p>It simply means that you should be able to pass in functions as parameters to other functions, return functions from other functions &amp; store functions in data structures.</p><p>In some languages like Ruby, functions are not first-class citizens but support things like `UnboundMethod` &amp; `Method` objects (different from methods), lambdas, and procs. In the examples involving Ruby, I will use lambdas instead of functions as they’re closer to functions in functional programming languages.</p><p>A higher order function is a function that takes at least one function as a parameter and returns a function. This is only possible in languages that support first class functions. For example, in Ruby,</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcarbon.now.sh%2Fembed%3Fbg%3Drgba%2528171%252C%2B184%252C%2B195%252C%2B1%2529%26t%3Dsynthwave-84%26wt%3Dnone%26l%3Druby%26width%3D458%26ds%3Dtrue%26dsyoff%3D20px%26dsblur%3D68px%26wc%3Dtrue%26wa%3Dfalse%26pv%3D0px%26ph%3D0px%26ln%3Dfalse%26fl%3D1%26fm%3DHack%26fs%3D14px%26lh%3D133%2525%26si%3Dfalse%26es%3D2x%26wm%3Dfalse%26code%3Df%252520%25253D%252520-%25253E%252520%2528x%2529%252520do%25250A%252509-%25253E%252520%2528y%2529%252520%25257B%252520x.%2528y%2529%252520%25257D%25250Aend&amp;display_name=Carbon&amp;url=https%3A%2F%2Fcarbon.now.sh%2F%3Fbg%3Drgba%252528171%25252C%2B184%25252C%2B195%25252C%2B1%252529%26t%3Dsynthwave-84%26wt%3Dnone%26l%3Druby%26width%3D458%26ds%3Dtrue%26dsyoff%3D20px%26dsblur%3D68px%26wc%3Dtrue%26wa%3Dfalse%26pv%3D0px%26ph%3D0px%26ln%3Dfalse%26fl%3D1%26fm%3DHack%26fs%3D14px%26lh%3D133%252525%26si%3Dfalse%26es%3D2x%26wm%3Dfalse%26code%3Df%25252520%2525253D%25252520-%2525253E%25252520%252528x%252529%25252520do%2525250A%25252509-%2525253E%25252520%252528y%252529%25252520%2525257B%25252520x.%252528y%252529%25252520%2525257D%2525250Aend&amp;image=https%3A%2F%2Fcarbon.now.sh%2Fstatic%2Fbrand%2Fbanner.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=carbon" width="1024" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/d18c9f560b1e548f8f0deec35a5e302b/href">https://medium.com/media/d18c9f560b1e548f8f0deec35a5e302b/href</a></iframe><p>In the above example, f is a function (lambda) that accepts a function x as a parameter and returns a function (lambda) that accepts an argument y and returns a value by invoking the function (lambda) x with y as a parameter. This is possible because lambdas in Ruby are closures. A closure is any function that closes over the environment in which it was defined.</p><p>On a side note, if you think closures are impure because they are functions that depend on a variable/value outside their scope, they are not. You can think of the “influencing value(s)” (a.k.a capture(s)) as an “implied parameter”. Languages like c++ are more explicit when it comes to “captures.” The format for defining a lambda in c++ is <em>[ captures ] ( params ) { body }</em>. After the lambda’s declaration, changes to captures outside lambda do not affect changes within the lambda. This is not the case with Ruby, as it really doesn’t care about purity. Here’s an example:</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcarbon.now.sh%2Fembed%3Fbg%3Drgba%2528171%252C%2B184%252C%2B195%252C%2B1%2529%26t%3Dsynthwave-84%26wt%3Dnone%26l%3Dtext%252Fx-c%252B%252Bsrc%26width%3D458%26ds%3Dtrue%26dsyoff%3D20px%26dsblur%3D68px%26wc%3Dtrue%26wa%3Dfalse%26pv%3D0px%26ph%3D0px%26ln%3Dfalse%26fl%3D1%26fm%3DHack%26fs%3D14px%26lh%3D133%2525%26si%3Dfalse%26es%3D2x%26wm%3Dfalse%26code%3D%252523include%252520%25253Ciostream%25253E%25250Aint%252520main%2528%2529%252520%25257B%25250A%252520%252520int%252520x%252520%25253D%25252010%25253B%25250A%252520%252520auto%252520w%252520%25253D%252520%25255Bx%25255D%252520%2528int%252520y%2529%252520%25257B%252520return%252520x%252520%25252B%252520y%25253B%252520%25257D%25253B%25250A%252520%252520x%252520%25253D%25252040%25253B%25250A%252520%252520printf%2528%252522%252525d%252522%25252C%252520w%252820%2529%2529%25253B%25250A%252520%252520return%2525200%25253B%25250A%25257D&amp;display_name=Carbon&amp;url=https%3A%2F%2Fcarbon.now.sh%2F%3Fbg%3Drgba%252528171%25252C%2B184%25252C%2B195%25252C%2B1%252529%26t%3Dsynthwave-84%26wt%3Dnone%26l%3Dtext%25252Fx-c%25252B%25252Bsrc%26width%3D458%26ds%3Dtrue%26dsyoff%3D20px%26dsblur%3D68px%26wc%3Dtrue%26wa%3Dfalse%26pv%3D0px%26ph%3D0px%26ln%3Dfalse%26fl%3D1%26fm%3DHack%26fs%3D14px%26lh%3D133%252525%26si%3Dfalse%26es%3D2x%26wm%3Dfalse%26code%3D%25252523include%25252520%2525253Ciostream%2525253E%2525250Aint%25252520main%252528%252529%25252520%2525257B%2525250A%25252520%25252520int%25252520x%25252520%2525253D%2525252010%2525253B%2525250A%25252520%25252520auto%25252520w%25252520%2525253D%25252520%2525255Bx%2525255D%25252520%252528int%25252520y%252529%25252520%2525257B%25252520return%25252520x%25252520%2525252B%25252520y%2525253B%25252520%2525257D%2525253B%2525250A%25252520%25252520x%25252520%2525253D%2525252040%2525253B%2525250A%25252520%25252520printf%252528%25252522%25252525d%25252522%2525252C%25252520w%25252820%252529%252529%2525253B%2525250A%25252520%25252520return%252525200%2525253B%2525250A%2525257D&amp;image=https%3A%2F%2Fcarbon.now.sh%2Fstatic%2Fbrand%2Fbanner.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=carbon" width="1024" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/04348e5f4fb5360211d07dd356759bf3/href">https://medium.com/media/04348e5f4fb5360211d07dd356759bf3/href</a></iframe><p>Currying is basically converting a function that takes multiple arguments into a series of functions that take a single argument each. Here’s an example of currying in Javascript.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcarbon.now.sh%2Fembed%3Fbg%3Drgba%2528171%252C%2B184%252C%2B195%252C%2B1%2529%26t%3Dsynthwave-84%26wt%3Dnone%26l%3Djavascript%26width%3D458%26ds%3Dtrue%26dsyoff%3D20px%26dsblur%3D68px%26wc%3Dtrue%26wa%3Dfalse%26pv%3D0px%26ph%3D0px%26ln%3Dfalse%26fl%3D1%26fm%3DHack%26fs%3D14px%26lh%3D133%2525%26si%3Dfalse%26es%3D2x%26wm%3Dfalse%26code%3Dconst%252520f%252520%25253D%252520x%252520%25253D%25253E%252520y%252520%25253D%25253E%252520x%252520%25252B%252520y%25250Aconsole.log%2528f%252810%2529%252820%2529%2529%252520%25252F%25252F%25252030&amp;display_name=Carbon&amp;url=https%3A%2F%2Fcarbon.now.sh%2F%3Fbg%3Drgba%252528171%25252C%2B184%25252C%2B195%25252C%2B1%252529%26t%3Dsynthwave-84%26wt%3Dnone%26l%3Djavascript%26width%3D458%26ds%3Dtrue%26dsyoff%3D20px%26dsblur%3D68px%26wc%3Dtrue%26wa%3Dfalse%26pv%3D0px%26ph%3D0px%26ln%3Dfalse%26fl%3D1%26fm%3DHack%26fs%3D14px%26lh%3D133%252525%26si%3Dfalse%26es%3D2x%26wm%3Dfalse%26code%3Dconst%25252520f%25252520%2525253D%25252520x%25252520%2525253D%2525253E%25252520y%25252520%2525253D%2525253E%25252520x%25252520%2525252B%25252520y%2525250Aconsole.log%252528f%25252810%252529%25252820%252529%252529%25252520%2525252F%2525252F%2525252030&amp;image=https%3A%2F%2Fcarbon.now.sh%2Fstatic%2Fbrand%2Fbanner.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=carbon" width="1024" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/05203ab8754c2ddb5b13211347cb94b9/href">https://medium.com/media/05203ab8754c2ddb5b13211347cb94b9/href</a></iframe><p>The above example illustrates the concept of currying. Instead of defining a single function that accepts parameters x &amp; y and returns x + y, we define a function that accepts x and returns a function that accepts y and returns x + y. There are many theoretical and practical uses to currying.</p><p>My purpose of introducing currying here is to explain the difference between currying &amp; partial application when talking about partial application, as they are often confused as being the same thing.</p><blockquote>A partial application function can be defined as a function (pf) that takes in a function (af) and some arguments and returns a function (bf) which acts like the function (af) that was passed in but accepts fewer arguments; the other functions being “bound in.”</blockquote><p><a href="https://web.archive.org/web/20161023205431/http://www.uncarved.com/articles/not_currying">https://web.archive.org/web/20161023205431/http://www.uncarved.com/articles/not_currying</a></p><p>Here’s an example of a partial application function in Ruby:</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcarbon.now.sh%2Fembed%3Fbg%3Drgba%2528171%252C%2B184%252C%2B195%252C%2B1%2529%26t%3Dsynthwave-84%26wt%3Dnone%26l%3Druby%26width%3D858%26ds%3Dtrue%26dsyoff%3D20px%26dsblur%3D68px%26wc%3Dtrue%26wa%3Dfalse%26pv%3D0px%26ph%3D0px%26ln%3Dfalse%26fl%3D1%26fm%3DHack%26fs%3D14px%26lh%3D133%2525%26si%3Dfalse%26es%3D2x%26wm%3Dfalse%26code%3Dadder%252520%25253D%252520-%25253E%252520%2528%252Aargs%2529%252520%25257B%252520args.sum%252520%25257D%25250Apartial_applier%252520%25253D%252520-%25253E%252520%2528func%25252C%252520%252Apf_args%2529%252520do%25250A%252520%252520processed_args%252520%25253D%252520func.%2528%252Apf_args%2529%25250A%252520%252520partially_applied_function%252520%25253D%252520-%25253E%252520%2528%252Areduced_args%2529%252520do%25250A%252520%252520%252520%252520func.%2528%252A%25255B%252Areduced_args%25252C%252520processed_args%25255D%2529%25250A%252520%252520end%25250Aend%25250Apartially_applied_adder%252520%25253D%252520partial_applier.%2528adder%25252C%25252020%25252C%25252030%2529%25250Apartially_applied_adder.%252850%2529%252520%252523%25253D%25253E%252520100&amp;display_name=Carbon&amp;url=https%3A%2F%2Fcarbon.now.sh%2F%3Fbg%3Drgba%252528171%25252C%2B184%25252C%2B195%25252C%2B1%252529%26t%3Dsynthwave-84%26wt%3Dnone%26l%3Druby%26width%3D858%26ds%3Dtrue%26dsyoff%3D20px%26dsblur%3D68px%26wc%3Dtrue%26wa%3Dfalse%26pv%3D0px%26ph%3D0px%26ln%3Dfalse%26fl%3D1%26fm%3DHack%26fs%3D14px%26lh%3D133%252525%26si%3Dfalse%26es%3D2x%26wm%3Dfalse%26code%3Dadder%25252520%2525253D%25252520-%2525253E%25252520%252528%25252Aargs%252529%25252520%2525257B%25252520args.sum%25252520%2525257D%2525250Apartial_applier%25252520%2525253D%25252520-%2525253E%25252520%252528func%2525252C%25252520%25252Apf_args%252529%25252520do%2525250A%25252520%25252520processed_args%25252520%2525253D%25252520func.%252528%25252Apf_args%252529%2525250A%25252520%25252520partially_applied_function%25252520%2525253D%25252520-%2525253E%25252520%252528%25252Areduced_args%252529%25252520do%2525250A%25252520%25252520%25252520%25252520func.%252528%25252A%2525255B%25252Areduced_args%2525252C%25252520processed_args%2525255D%252529%2525250A%25252520%25252520end%2525250Aend%2525250Apartially_applied_adder%25252520%2525253D%25252520partial_applier.%252528adder%2525252C%2525252020%2525252C%2525252030%252529%2525250Apartially_applied_adder.%25252850%252529%25252520%25252523%2525253D%2525253E%25252520100&amp;image=https%3A%2F%2Fcarbon.now.sh%2Fstatic%2Fbrand%2Fbanner.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=carbon" width="1024" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/ac520b3f47c421c4bcf3bcdb7f1d5a1c/href">https://medium.com/media/ac520b3f47c421c4bcf3bcdb7f1d5a1c/href</a></iframe><p>Since the above example touches some “Rubyisms”, here’s a similar example in javascript:</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcarbon.now.sh%2Fembed%3Fbg%3Drgba%2528171%252C%2B184%252C%2B195%252C%2B1%2529%26t%3Dsynthwave-84%26wt%3Dnone%26l%3Djavascript%26width%3D858%26ds%3Dtrue%26dsyoff%3D20px%26dsblur%3D68px%26wc%3Dtrue%26wa%3Dfalse%26pv%3D0px%26ph%3D0px%26ln%3Dfalse%26fl%3D1%26fm%3DHack%26fs%3D14px%26lh%3D133%2525%26si%3Dfalse%26es%3D2x%26wm%3Dfalse%26code%3Dconst%252520adder%252520%25253D%252520%2528%2525E2%252580%2525A6args%2529%252520%25253D%25253E%252520args.reduce%2528%2528partial_sum%25252C%252520i%2529%252520%25253D%25253E%252520partial_sum%252520%25252B%252520i%25252C%2525200%2529%25250Aconst%252520partial_applier%252520%25253D%252520%2528func%25252C%2525C2%2525A0%2525E2%252580%2525A6fargs%2529%252520%25253D%25253E%252520%25257B%25250A%252509const%252520processed_fargs%252520%25253D%252520adder%2528%2525E2%252580%2525A6fargs%2529%25250A%252509return%252520%2528%2525E2%252580%2525A6targs%2529%252520%25253D%25253E%252520%25257B%25250A%252509%252509return%252520func%2528%2525E2%252580%2525A6%25255Bprocessed_fargs%25252C%2525C2%2525A0%2525E2%252580%2525A6targs%25255D%2529%25250A%252509%25257D%25250A%25257D%25250Apartially_applied_adder%252520%25253D%252520partial_applier%2528adder%25252C%25252020%25252C%25252030%2529%25250Apartially_applied_adder%252850%2529&amp;display_name=Carbon&amp;url=https%3A%2F%2Fcarbon.now.sh%2F%3Fbg%3Drgba%252528171%25252C%2B184%25252C%2B195%25252C%2B1%252529%26t%3Dsynthwave-84%26wt%3Dnone%26l%3Djavascript%26width%3D858%26ds%3Dtrue%26dsyoff%3D20px%26dsblur%3D68px%26wc%3Dtrue%26wa%3Dfalse%26pv%3D0px%26ph%3D0px%26ln%3Dfalse%26fl%3D1%26fm%3DHack%26fs%3D14px%26lh%3D133%252525%26si%3Dfalse%26es%3D2x%26wm%3Dfalse%26code%3Dconst%25252520adder%25252520%2525253D%25252520%252528%252525E2%25252580%252525A6args%252529%25252520%2525253D%2525253E%25252520args.reduce%252528%252528partial_sum%2525252C%25252520i%252529%25252520%2525253D%2525253E%25252520partial_sum%25252520%2525252B%25252520i%2525252C%252525200%252529%2525250Aconst%25252520partial_applier%25252520%2525253D%25252520%252528func%2525252C%252525C2%252525A0%252525E2%25252580%252525A6fargs%252529%25252520%2525253D%2525253E%25252520%2525257B%2525250A%25252509const%25252520processed_fargs%25252520%2525253D%25252520adder%252528%252525E2%25252580%252525A6fargs%252529%2525250A%25252509return%25252520%252528%252525E2%25252580%252525A6targs%252529%25252520%2525253D%2525253E%25252520%2525257B%2525250A%25252509%25252509return%25252520func%252528%252525E2%25252580%252525A6%2525255Bprocessed_fargs%2525252C%252525C2%252525A0%252525E2%25252580%252525A6targs%2525255D%252529%2525250A%25252509%2525257D%2525250A%2525257D%2525250Apartially_applied_adder%25252520%2525253D%25252520partial_applier%252528adder%2525252C%2525252020%2525252C%2525252030%252529%2525250Apartially_applied_adder%25252850%252529&amp;image=https%3A%2F%2Fcarbon.now.sh%2Fstatic%2Fbrand%2Fbanner.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=carbon" width="1024" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/437ca7b2b32f3848303c8c5a8ec21a10/href">https://medium.com/media/437ca7b2b32f3848303c8c5a8ec21a10/href</a></iframe><p>Clojure has a partial application function called <em>partial</em>.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcarbon.now.sh%2Fembed%3Fbg%3Drgba%2528171%252C%2B184%252C%2B195%252C%2B1%2529%26t%3Dsynthwave-84%26wt%3Dnone%26l%3Dclojure%26width%3D858%26ds%3Dtrue%26dsyoff%3D20px%26dsblur%3D68px%26wc%3Dtrue%26wa%3Dfalse%26pv%3D0px%26ph%3D0px%26ln%3Dfalse%26fl%3D1%26fm%3DHack%26fs%3D14px%26lh%3D133%2525%26si%3Dfalse%26es%3D2x%26wm%3Dfalse%26code%3D%2528def%252520hundred-times%252520%2528partial%252520%252A%252520100%2529%2529%2525C2%2525A0%25253B%25253B%252520%25253D%25253E%252520%252523%2527user%25252Fhundred-times&amp;display_name=Carbon&amp;url=https%3A%2F%2Fcarbon.now.sh%2F%3Fbg%3Drgba%252528171%25252C%2B184%25252C%2B195%25252C%2B1%252529%26t%3Dsynthwave-84%26wt%3Dnone%26l%3Dclojure%26width%3D858%26ds%3Dtrue%26dsyoff%3D20px%26dsblur%3D68px%26wc%3Dtrue%26wa%3Dfalse%26pv%3D0px%26ph%3D0px%26ln%3Dfalse%26fl%3D1%26fm%3DHack%26fs%3D14px%26lh%3D133%252525%26si%3Dfalse%26es%3D2x%26wm%3Dfalse%26code%3D%252528def%25252520hundred-times%25252520%252528partial%25252520%25252A%25252520100%252529%252529%252525C2%252525A0%2525253B%2525253B%25252520%2525253D%2525253E%25252520%25252523%252527user%2525252Fhundred-times&amp;image=https%3A%2F%2Fcarbon.now.sh%2Fstatic%2Fbrand%2Fbanner.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=carbon" width="1024" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/046e429d8c6d169b40b4ef4291f5eb98/href">https://medium.com/media/046e429d8c6d169b40b4ef4291f5eb98/href</a></iframe><p>If you find the above examples hard to follow, <a href="https://web.archive.org/web/20161023205431/http://www.uncarved.com/articles/not_currying">this article here</a>, explains this difference more in detail with more examples.</p><p>Partial application can be useful in two contexts.</p><ol><li><em>It lets us fix a function’s arguments.</em></li><li><em>It let’s create specific behavior from more generic functions.</em></li></ol><h4>5. Recursion</h4><figure><img alt="Recursion" src="https://cdn-images-1.medium.com/max/1024/1*AIEtGIYc5OKd5i-52EHsoA.png" /></figure><p>Recursion does not need a special introduction as it is not specific to functional programming, though its more widely used in the functional paradigm. The reason for this heavy reliance on recursion is because, when immutability is a factor, recursion is a more expressive and powerful alternative to iteration.</p><p>The dynamic programming style of breaking down problems into recursive sub-problems is very relevant to the functional programming paradigm.</p><p>There is some really good content on <a href="https://www.youtube.com/watch?v=IJDJ0kBx2LM">recursion on the internet</a>. I’d recommend going through some of them to learn more about it.</p><h4>Reimagining Solutions in Functional Design</h4><p>As a statement of conclusion, I’d like to introduce you to some programs written in a few different languages that calculate factorials of a number (n), some written in a functional way and others in an imperative way.</p><p><strong>Imperative / Object Oriented</strong></p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcarbon.now.sh%2Fembed%3Fbg%3Drgba%2528171%252C%2B184%252C%2B195%252C%2B1%2529%26t%3Dsynthwave-84%26wt%3Dnone%26l%3Dtext%252Fx-csrc%26width%3D858%26ds%3Dtrue%26dsyoff%3D20px%26dsblur%3D68px%26wc%3Dtrue%26wa%3Dfalse%26pv%3D0px%26ph%3D0px%26ln%3Dfalse%26fl%3D1%26fm%3DHack%26fs%3D14px%26lh%3D133%2525%26si%3Dfalse%26es%3D2x%26wm%3Dfalse%26code%3Dint%252520fac%2528int%252520n%2529%252520%25257B%25250A%252520%252520%252520%252520int%252520result%252520%25253D%2525201%25252C%252520i%252520%25253D%2525201%25253B%25250A%252520%252520%252520%252520for%2528%25253Bi%252520%25253C%25253D%252520n%25253B%252520i%25252B%25252B%2529%252520result%252520%25253D%252520result%252520%252A%252520i%25253B%25250A%252520%252520%252520%252520return%252520result%25253B%25250A%252520%252520%25257D&amp;display_name=Carbon&amp;url=https%3A%2F%2Fcarbon.now.sh%2F%3Fbg%3Drgba%252528171%25252C%2B184%25252C%2B195%25252C%2B1%252529%26t%3Dsynthwave-84%26wt%3Dnone%26l%3Dtext%25252Fx-csrc%26width%3D858%26ds%3Dtrue%26dsyoff%3D20px%26dsblur%3D68px%26wc%3Dtrue%26wa%3Dfalse%26pv%3D0px%26ph%3D0px%26ln%3Dfalse%26fl%3D1%26fm%3DHack%26fs%3D14px%26lh%3D133%252525%26si%3Dfalse%26es%3D2x%26wm%3Dfalse%26code%3Dint%25252520fac%252528int%25252520n%252529%25252520%2525257B%2525250A%25252520%25252520%25252520%25252520int%25252520result%25252520%2525253D%252525201%2525252C%25252520i%25252520%2525253D%252525201%2525253B%2525250A%25252520%25252520%25252520%25252520for%252528%2525253Bi%25252520%2525253C%2525253D%25252520n%2525253B%25252520i%2525252B%2525252B%252529%25252520result%25252520%2525253D%25252520result%25252520%25252A%25252520i%2525253B%2525250A%25252520%25252520%25252520%25252520return%25252520result%2525253B%2525250A%25252520%25252520%2525257D&amp;image=https%3A%2F%2Fcarbon.now.sh%2Fstatic%2Fbrand%2Fbanner.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=carbon" width="1024" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/c603547829bcf9ab43a5036634c015aa/href">https://medium.com/media/c603547829bcf9ab43a5036634c015aa/href</a></iframe><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcarbon.now.sh%2Fembed%3Fbg%3Drgba%2528171%252C%2B184%252C%2B195%252C%2B1%2529%26t%3Dsynthwave-84%26wt%3Dnone%26l%3Djavascript%26width%3D858%26ds%3Dtrue%26dsyoff%3D20px%26dsblur%3D68px%26wc%3Dtrue%26wa%3Dfalse%26pv%3D0px%26ph%3D0px%26ln%3Dfalse%26fl%3D1%26fm%3DHack%26fs%3D14px%26lh%3D133%2525%26si%3Dfalse%26es%3D2x%26wm%3Dfalse%26code%3Dconst%252520fac%252520%25253D%252520n%252520%25253D%25253E%252520%25257B%25250A%252520%252520%252520%252520var%252520result%252520%25253D%2525201%25253B%25250A%252520%252520%252520%252520for%2528var%252520i%252520%25253D%2525201%25253B%252520i%252520%25253C%25253D%252520n%25253B%252520i%25252B%25252B%2529%252520%25257B%25250A%252520%252520%252520%252520%252520%252520result%252520%25253D%252520result%252520%252A%252520i%25253B%252520%25250A%252520%252520%252520%252520%25257D%25250A%252520%252520%252520%252520return%252520result%25253B%25250A%252520%252520%25257D&amp;display_name=Carbon&amp;url=https%3A%2F%2Fcarbon.now.sh%2F%3Fbg%3Drgba%252528171%25252C%2B184%25252C%2B195%25252C%2B1%252529%26t%3Dsynthwave-84%26wt%3Dnone%26l%3Djavascript%26width%3D858%26ds%3Dtrue%26dsyoff%3D20px%26dsblur%3D68px%26wc%3Dtrue%26wa%3Dfalse%26pv%3D0px%26ph%3D0px%26ln%3Dfalse%26fl%3D1%26fm%3DHack%26fs%3D14px%26lh%3D133%252525%26si%3Dfalse%26es%3D2x%26wm%3Dfalse%26code%3Dconst%25252520fac%25252520%2525253D%25252520n%25252520%2525253D%2525253E%25252520%2525257B%2525250A%25252520%25252520%25252520%25252520var%25252520result%25252520%2525253D%252525201%2525253B%2525250A%25252520%25252520%25252520%25252520for%252528var%25252520i%25252520%2525253D%252525201%2525253B%25252520i%25252520%2525253C%2525253D%25252520n%2525253B%25252520i%2525252B%2525252B%252529%25252520%2525257B%2525250A%25252520%25252520%25252520%25252520%25252520%25252520result%25252520%2525253D%25252520result%25252520%25252A%25252520i%2525253B%25252520%2525250A%25252520%25252520%25252520%25252520%2525257D%2525250A%25252520%25252520%25252520%25252520return%25252520result%2525253B%2525250A%25252520%25252520%2525257D&amp;image=https%3A%2F%2Fcarbon.now.sh%2Fstatic%2Fbrand%2Fbanner.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=carbon" width="1024" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/9d07f099f57d8a125d9b915b2860563e/href">https://medium.com/media/9d07f099f57d8a125d9b915b2860563e/href</a></iframe><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcarbon.now.sh%2Fembed%3Fbg%3Drgba%2528171%252C%2B184%252C%2B195%252C%2B1%2529%26t%3Dsynthwave-84%26wt%3Dnone%26l%3Dtext%252Fx-java%26width%3D858%26ds%3Dtrue%26dsyoff%3D20px%26dsblur%3D68px%26wc%3Dtrue%26wa%3Dfalse%26pv%3D0px%26ph%3D0px%26ln%3Dfalse%26fl%3D1%26fm%3DHack%26fs%3D14px%26lh%3D133%2525%26si%3Dfalse%26es%3D2x%26wm%3Dfalse%26code%3Dstatic%252520int%252520factorial%2528int%252520n%2529%252520%25257B%25250A%252520%252520%252520%252520int%252520res%252520%25253D%2525201%25252C%252520i%25253B%25250A%252520%252520%252520%252520for%252520%2528i%252520%25253D%2525202%25253B%252520i%252520%25253C%25253D%252520n%25253B%252520i%25252B%25252B%2529%25250A%252520%252520%252520%252520%252520%252520%252520%252520res%252520%252A%25253D%252520i%25253B%25250A%252520%252520%252520%252520return%252520res%25253B%25250A%25257D&amp;display_name=Carbon&amp;url=https%3A%2F%2Fcarbon.now.sh%2F%3Fbg%3Drgba%252528171%25252C%2B184%25252C%2B195%25252C%2B1%252529%26t%3Dsynthwave-84%26wt%3Dnone%26l%3Dtext%25252Fx-java%26width%3D858%26ds%3Dtrue%26dsyoff%3D20px%26dsblur%3D68px%26wc%3Dtrue%26wa%3Dfalse%26pv%3D0px%26ph%3D0px%26ln%3Dfalse%26fl%3D1%26fm%3DHack%26fs%3D14px%26lh%3D133%252525%26si%3Dfalse%26es%3D2x%26wm%3Dfalse%26code%3Dstatic%25252520int%25252520factorial%252528int%25252520n%252529%25252520%2525257B%2525250A%25252520%25252520%25252520%25252520int%25252520res%25252520%2525253D%252525201%2525252C%25252520i%2525253B%2525250A%25252520%25252520%25252520%25252520for%25252520%252528i%25252520%2525253D%252525202%2525253B%25252520i%25252520%2525253C%2525253D%25252520n%2525253B%25252520i%2525252B%2525252B%252529%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520res%25252520%25252A%2525253D%25252520i%2525253B%2525250A%25252520%25252520%25252520%25252520return%25252520res%2525253B%2525250A%2525257D&amp;image=https%3A%2F%2Fcarbon.now.sh%2Fstatic%2Fbrand%2Fbanner.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=carbon" width="1024" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/db75b02920a5b5112d34657e289e1e3f/href">https://medium.com/media/db75b02920a5b5112d34657e289e1e3f/href</a></iframe><h4>Functional</h4><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcarbon.now.sh%2Fembed%3Fbg%3Drgba%2528171%252C%2B184%252C%2B195%252C%2B1%2529%26t%3Dsynthwave-84%26wt%3Dnone%26l%3Dclojure%26width%3D858%26ds%3Dtrue%26dsyoff%3D20px%26dsblur%3D68px%26wc%3Dtrue%26wa%3Dfalse%26pv%3D0px%26ph%3D0px%26ln%3Dfalse%26fl%3D1%26fm%3DHack%26fs%3D14px%26lh%3D133%2525%26si%3Dfalse%26es%3D2x%26wm%3Dfalse%26code%3D%2528defn%252520factorial%252520%25255Bn%25255D%252520%2528reduce%252520%252A%252520%2528range%2525201%252520%2528inc%252520n%2529%2529%2529%2529&amp;display_name=Carbon&amp;url=https%3A%2F%2Fcarbon.now.sh%2F%3Fbg%3Drgba%252528171%25252C%2B184%25252C%2B195%25252C%2B1%252529%26t%3Dsynthwave-84%26wt%3Dnone%26l%3Dclojure%26width%3D858%26ds%3Dtrue%26dsyoff%3D20px%26dsblur%3D68px%26wc%3Dtrue%26wa%3Dfalse%26pv%3D0px%26ph%3D0px%26ln%3Dfalse%26fl%3D1%26fm%3DHack%26fs%3D14px%26lh%3D133%252525%26si%3Dfalse%26es%3D2x%26wm%3Dfalse%26code%3D%252528defn%25252520factorial%25252520%2525255Bn%2525255D%25252520%252528reduce%25252520%25252A%25252520%252528range%252525201%25252520%252528inc%25252520n%252529%252529%252529%252529&amp;image=https%3A%2F%2Fcarbon.now.sh%2Fstatic%2Fbrand%2Fbanner.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=carbon" width="1024" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/a61ca24262f4327cbf346678ec567b85/href">https://medium.com/media/a61ca24262f4327cbf346678ec567b85/href</a></iframe><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcarbon.now.sh%2Fembed%3Fbg%3Drgba%2528171%252C%2B184%252C%2B195%252C%2B1%2529%26t%3Dsynthwave-84%26wt%3Dnone%26l%3Druby%26width%3D858%26ds%3Dtrue%26dsyoff%3D20px%26dsblur%3D68px%26wc%3Dtrue%26wa%3Dfalse%26pv%3D0px%26ph%3D0px%26ln%3Dfalse%26fl%3D1%26fm%3DHack%26fs%3D14px%26lh%3D133%2525%26si%3Dfalse%26es%3D2x%26wm%3Dfalse%26code%3Dfac%252520%25253D%252520-%25253E%252520%2528n%2529%252520%25257B%252520%25281..n%2529.reduce%2528%252526%25253A%252A%2529%252520%25257D&amp;display_name=Carbon&amp;url=https%3A%2F%2Fcarbon.now.sh%2F%3Fbg%3Drgba%252528171%25252C%2B184%25252C%2B195%25252C%2B1%252529%26t%3Dsynthwave-84%26wt%3Dnone%26l%3Druby%26width%3D858%26ds%3Dtrue%26dsyoff%3D20px%26dsblur%3D68px%26wc%3Dtrue%26wa%3Dfalse%26pv%3D0px%26ph%3D0px%26ln%3Dfalse%26fl%3D1%26fm%3DHack%26fs%3D14px%26lh%3D133%252525%26si%3Dfalse%26es%3D2x%26wm%3Dfalse%26code%3Dfac%25252520%2525253D%25252520-%2525253E%25252520%252528n%252529%25252520%2525257B%25252520%2525281..n%252529.reduce%252528%25252526%2525253A%25252A%252529%25252520%2525257D&amp;image=https%3A%2F%2Fcarbon.now.sh%2Fstatic%2Fbrand%2Fbanner.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=carbon" width="1024" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/70b393de3d86d7235e35861f6c7a3b19/href">https://medium.com/media/70b393de3d86d7235e35861f6c7a3b19/href</a></iframe><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcarbon.now.sh%2Fembed%3Fbg%3Drgba%2528171%252C%2B184%252C%2B195%252C%2B1%2529%26t%3Dsynthwave-84%26wt%3Dnone%26l%3Delixir%26width%3D858%26ds%3Dtrue%26dsyoff%3D20px%26dsblur%3D68px%26wc%3Dtrue%26wa%3Dfalse%26pv%3D0px%26ph%3D0px%26ln%3Dfalse%26fl%3D1%26fm%3DHack%26fs%3D14px%26lh%3D133%2525%26si%3Dfalse%26es%3D2x%26wm%3Dfalse%26code%3Dfac%252520%25253D%252520fn%252520n%252520-%25253E%252520Enum.reduce%2528Range.new%25281%25252C%252520n%2529%25252C%252520%252526%252A%25252F2%2529%252520end&amp;display_name=Carbon&amp;url=https%3A%2F%2Fcarbon.now.sh%2F%3Fbg%3Drgba%252528171%25252C%2B184%25252C%2B195%25252C%2B1%252529%26t%3Dsynthwave-84%26wt%3Dnone%26l%3Delixir%26width%3D858%26ds%3Dtrue%26dsyoff%3D20px%26dsblur%3D68px%26wc%3Dtrue%26wa%3Dfalse%26pv%3D0px%26ph%3D0px%26ln%3Dfalse%26fl%3D1%26fm%3DHack%26fs%3D14px%26lh%3D133%252525%26si%3Dfalse%26es%3D2x%26wm%3Dfalse%26code%3Dfac%25252520%2525253D%25252520fn%25252520n%25252520-%2525253E%25252520Enum.reduce%252528Range.new%2525281%2525252C%25252520n%252529%2525252C%25252520%25252526%25252A%2525252F2%252529%25252520end&amp;image=https%3A%2F%2Fcarbon.now.sh%2Fstatic%2Fbrand%2Fbanner.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=carbon" width="1024" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/26b920e77dbee316eb2f92982394a7bd/href">https://medium.com/media/26b920e77dbee316eb2f92982394a7bd/href</a></iframe><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcarbon.now.sh%2Fembed%3Fbg%3Drgba%2528171%252C%2B184%252C%2B195%252C%2B1%2529%26t%3Dsynthwave-84%26wt%3Dnone%26l%3Djavascript%26width%3D858%26ds%3Dtrue%26dsyoff%3D20px%26dsblur%3D68px%26wc%3Dtrue%26wa%3Dfalse%26pv%3D0px%26ph%3D0px%26ln%3Dfalse%26fl%3D1%26fm%3DHack%26fs%3D14px%26lh%3D133%2525%26si%3Dfalse%26es%3D2x%26wm%3Dfalse%26code%3Dconst%252520fac%252520%25253D%252520n%252520%25253D%25253E%252520%25255B...Array%2528n%2529.keys%2528%2529%25255D.map%2528x%252520%25253D%25253E%252520x%252520%25252B%2525201%2529.reduce%2528%2528m%25252C%252520y%2529%252520%25253D%25253E%252520m%252520%252A%252520y%2529&amp;display_name=Carbon&amp;url=https%3A%2F%2Fcarbon.now.sh%2F%3Fbg%3Drgba%252528171%25252C%2B184%25252C%2B195%25252C%2B1%252529%26t%3Dsynthwave-84%26wt%3Dnone%26l%3Djavascript%26width%3D858%26ds%3Dtrue%26dsyoff%3D20px%26dsblur%3D68px%26wc%3Dtrue%26wa%3Dfalse%26pv%3D0px%26ph%3D0px%26ln%3Dfalse%26fl%3D1%26fm%3DHack%26fs%3D14px%26lh%3D133%252525%26si%3Dfalse%26es%3D2x%26wm%3Dfalse%26code%3Dconst%25252520fac%25252520%2525253D%25252520n%25252520%2525253D%2525253E%25252520%2525255B...Array%252528n%252529.keys%252528%252529%2525255D.map%252528x%25252520%2525253D%2525253E%25252520x%25252520%2525252B%252525201%252529.reduce%252528%252528m%2525252C%25252520y%252529%25252520%2525253D%2525253E%25252520m%25252520%25252A%25252520y%252529&amp;image=https%3A%2F%2Fcarbon.now.sh%2Fstatic%2Fbrand%2Fbanner.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=carbon" width="1024" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/6387b67cfabf302275776d28bb494198/href">https://medium.com/media/6387b67cfabf302275776d28bb494198/href</a></iframe><blockquote>Hello Montreal! We’ll be at the <a href="https://startupfestival.com/">Startupfest ’22</a> all three days, so drop by and say hi. We’d love to see what you’re building, and early birds get a free (limited) design audit or tech consultation. DM us on Twitter (<a href="https://twitter.com/tarkalabs">@tarkalabs</a>) and let’s talk.</blockquote><h4>References</h4><ol><li><a href="https://en.wikipedia.org/wiki/Functional_programming">Functional Programming</a></li><li><a href="https://en.wikipedia.org/wiki/Purely_functional_programming">Purely Functional Programming</a></li><li><a href="https://hexdocs.pm/elixir/main/IO.html">IO Side-Effects in Elixir</a></li><li><a href="https://james-iry.blogspot.com/2009/05/brief-incomplete-and-mostly-wrong.html">Brief Incomplete &amp; Mostly Wrong — James Iry</a></li><li><a href="https://adventofcode.com/2021/day/1">Advent Of Code 2021 — Day 1</a></li><li><a href="https://github.com/sreedevk/advent-of-code/blob/main/elixir/lib/twenty21/day1.ex">Advent Of Code 2021 — Day 1 Solution (Elixir)</a></li><li><a href="https://github.com/sreedevk/advent-of-code/blob/main/clojure/advent/src/advent/y2021/day1.clj">Advent Of Code 2021 — Day 1 Solution (Clojure)</a></li><li><a href="https://bartoszmilewski.com/2014/11/04/category-the-essence-of-composition/">The Essence of Composition</a></li><li><a href="https://ashtonkemerling.com/posts/binding-vs-assignment/">Binding vs Assignment</a></li><li><a href="https://en.wikipedia.org/wiki/First-class_citizen">First Class Citizen</a></li><li><a href="https://web.archive.org/web/20161023205431/http://www.uncarved.com/articles/not_currying">Partial Application vs Currying</a></li></ol><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=5d23abf0b03a" width="1" height="1" alt=""><hr><p><a href="https://blog.tarkalabs.com/an-introductory-guide-to-functional-programming-concepts-5d23abf0b03a">An introductory guide to functional programming concepts</a> was originally published in <a href="https://blog.tarkalabs.com/">Tarka Labs Blogs</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Function Composition in Ruby]]></title>
            <link>https://medium.com/tarkalabs-til/function-composition-in-ruby-69e37cdc9b0?source=rss-9eba103f8123------2</link>
            <guid isPermaLink="false">https://medium.com/p/69e37cdc9b0</guid>
            <category><![CDATA[ruby]]></category>
            <category><![CDATA[coding]]></category>
            <category><![CDATA[today-i-learned]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[functional-programming]]></category>
            <dc:creator><![CDATA[Sreedev Kodichath]]></dc:creator>
            <pubDate>Mon, 07 Feb 2022 05:20:46 GMT</pubDate>
            <atom:updated>2022-02-07T05:20:46.120Z</atom:updated>
            <content:encoded><![CDATA[<p>I was going through some functional programming content &amp; I came across a Wikipedia article on Function Composition. I was surprised to learn that Ruby has special operators for function composition. This is the recommended way to compose procs/functions in Ruby 2.6</p><pre>f = proc{|x| x + 2}<br>g = proc{|x| x * 3}<br>(f &lt;&lt; g).call(3) # -&gt; 11; identical to f(g(3))<br>(f &gt;&gt; g).call(3) # -&gt; 15; identical to g(f(3))</pre><p>I’ve been using Ruby for a long time and to this day, I run into such surprises now &amp; then.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=69e37cdc9b0" width="1" height="1" alt=""><hr><p><a href="https://medium.com/tarkalabs-til/function-composition-in-ruby-69e37cdc9b0">Function Composition in Ruby</a> was originally published in <a href="https://medium.com/tarkalabs-til">TarkaLabs TIL</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Digital Audio 101: Playing Audio From A Microcontroller]]></title>
            <link>https://blog.tarkalabs.com/digital-audio-101-playing-audio-from-a-microcontroller-5df1463616c?source=rss-9eba103f8123------2</link>
            <guid isPermaLink="false">https://medium.com/p/5df1463616c</guid>
            <category><![CDATA[digital-audio]]></category>
            <category><![CDATA[audio-programming]]></category>
            <category><![CDATA[microcontrollers]]></category>
            <category><![CDATA[audio]]></category>
            <dc:creator><![CDATA[Sreedev Kodichath]]></dc:creator>
            <pubDate>Thu, 15 Apr 2021 13:14:52 GMT</pubDate>
            <atom:updated>2022-06-29T11:49:45.139Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*PTBDxaYXrXoOxpuquPANlw.gif" /></figure><p>The world of audio programming is a vast and beautiful one. Adding audio to your Arduino projects can make them very interesting. In this article, we will delve into some basic theory of digital audio &amp; try to get a simple music output on your microcontroller.</p><p>The theory &amp; functionality described in this article is not specific to any microcontroller or development framework <em>(Although I’m working with a K210 development board with kendryte-standalone-sdk)</em> &amp; does not require any audio specific libraries.</p><h4>Things you will require to follow along</h4><ol><li>A microcontroller (&amp; programmer if required).</li><li>An <strong>active</strong> buzzer module / speaker.</li><li>Jumper wires.</li><li>A 220<strong>Ω</strong> resistor.</li><li>A breadboard.</li></ol><h3><strong>A Basic Introduction to Digital Audio</strong></h3><p>So what is audio/sound? In the simplest terms, <em>the sensation of sound is a result of vibrations in the air caused by pressure fluctuations.</em></p><p>A speaker is a simple device that can be used to play audio. A speaker has a voice coil, a permanent magnet &amp; a cone. When we pass electricity through the voice coil, it acts as an electromagnet &amp; the permanent magnet either repels or attracts the voice coil based on the polarity of the electromagnet. The voice coil is attached to the cone, so any movement of the voice coil itself causes the cone to be pulled inward or pushed outward. This motion of the cone creates pressure variations in the air around it causing it to vibrate creating sound. This is an oversimplified explanation, but good enough to get us going.</p><h4>Graphical Representation of Audio</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*UitybUJsfKPz4Ow7YuAKCA.png" /></figure><p>Like we just discussed, audio is a result of pressure variation of the air around us. These pressure variations can either be compressions or rarefactions of the air. A speaker produces these compressions and rarefactions by moving the cone rapidly. The magnitude and direction of the cone is varied by the changing the voltage (and in-turn current) supplied to the voice coil.</p><p>The pressure variations of the air produced or the audio signal passed into the voice coil of a speaker to create sound can be represented in a coordinate based graph as shown in the image above and is called an audio waveform.</p><p>The simplest audio waveforms are sinusoidal in nature, a curve representing periodic oscillations of constant amplitude as given by a sine function.</p><p>In fact, single musical notes are sinusoids of a constant frequency produced by simple harmonic motions. As an example, here is the waveform of an “A” note:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*b8zqZ5eWtKNeLki67nnSFA.png" /><figcaption>Waveform of an A note represented by the function <strong>f(x) = sin(2PI*440x)</strong></figcaption></figure><p>Many complex sounds like chords are created by adding the functions of the individual component notes.</p><p>Now lets take a look at some of the elements &amp; properties of the graph and see how they affect the sound produced by the waveform.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*FYa7Ggi1ro6AnuA3ask5tg.png" /></figure><p>As you can see in the above images, a sinusoidal audio waveform has the following properties:</p><ol><li><strong>Amplitude</strong> — The amplitude is the height of a point in the waveform measured from the reference line or origin. An increase in the amplitude results in an increase in the intensity of the sound. Sound with increased intensity is perceived as being louder.</li><li><strong>Frequency</strong> — Sinusoids are repetitive in nature. The number of times a cycle is repeated in one second is called the frequency of a waveform and the time taken to complete one cycle is the period of a waveform. Frequency is measured in Hertz (Hz). One cycle per second is one Hertz. Sounds of increased frequency have a higher pitch while sounds of lower frequency have a lower pitch. Human beings can only hear sounds ranging from 20Hz to 20,000Hz (or 20 KHz).</li><li><strong>Wavelength</strong> — Wavelength is the distance between two identical points in consecutive repeating waveforms. The wavelength is indirectly proportional to the frequency. So higher wavelengths results in lower pitched sounds.</li></ol><p>There are more properties &amp; measurements that can be made on an audio waveform represented in the time domain. But for the sake of simplicity, we will stop it here.</p><h3>Pulse Width Modulation (PWM)</h3><p>While working with electronics &amp; digital signals, we generally work with direct current. Direct current cannot switch polarity. Digital signals can only have two states either ON (1 — High Voltage) or OFF (0 — Low Voltage). Some microcontrollers have built in DAC (Digital to analog converter) which converts your digital signals to an analog output. We can also rely on external DACs to do the same but wiring DACs can take up multiple GPIOs on your microcontroller. We have a couple of other options too. It is possible to use a programmable arbitrary waveform generator like the AD9833BRMZ.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/300/1*AsCdLSk6IhmVlMpCEQOlbA.png" /></figure><p>This again requires additional wiring and plenty of additional code. To keep things as simple as possible, we will be using a technique called pulse width modulation. To understand how pulse width modulation works, let’s first look at a digital signal graph.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*SKNV1nPKo-7k6BlIzTNNNg.png" /></figure><p>In the above image, the amplitude represents the voltage of the digital signal and time is represented on the X axis. As discussed above, the change in voltage is almost instant and a digital signal can have only two states.</p><p>By repeatedly turning the signal on and off, we can control the average voltage that is outputted on the line over time. The amount of time that such a signal remains in the ON (High/1) state is called its <strong><em>duty cycle. </em></strong>By altering the duty cycle, we can alter the average voltage coming out of the line.</p><p>So if the signal remains ON (High/1) only 50% of the time, then the average voltage or the duty cycle is only 50% of the original supply voltage.</p><p>PWM has many use cases in digital signal processing. For our purposes, we will use PWM to vary the average voltage supplied to our Active Buzzer or Speaker.</p><h3>Wiring Up</h3><p>The circuit to connect a speaker or Active buzzer to your microcontroller is a fairly simple one. Please note that I am using a 0.5W speaker with a voltage requirement of 1V at 1KHz and an impedance of 8<strong>Ω</strong>. If your voltage requirements are higher, you can use a transistor with an external power source to amplify your signal. Both diagrams are given below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*k27WHYxd6sYSH53ViYdW-g.png" /></figure><h3>Playing Notes</h3><p>Like discussed before, notes are sinusoidal waves of simple harmonic motions. Here is a list of frequencies for notes on the 4th Octave.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/309/1*K7y56Rd1kolwV_GNOpmwEg.png" /></figure><p>In order to recreate the above frequencies in PWM, Let’s take a look at some sample code.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c2e6a678bfb70db2a3dc2b0251e3d10c/href">https://medium.com/media/c2e6a678bfb70db2a3dc2b0251e3d10c/href</a></iframe><p>The above code is mostly generic except for the usleep and gpiohs_set_pin function calls. You can replace these function calls with your microcontroller SDK specific functions for sleeping or delaying in microseconds(usleep)&amp; gpio digital control (gpiohs_set_pin). These functions in the above code are specific to K210 (kendryte-standalone-sdk).</p><p>In the above code, first, we are assigning the frequency of the note to a double variable (note_frequency). Then we calculate the period of each cycle for that frequency, convert the period from seconds to microseconds and store it in another double variable (note_period). The formula for calculating period is period (in sec) = 1/frequency (in Hz).</p><p>The total_duration is nothing but the duration for which a note is played. We add the periods of each cycle to the total duration and compare it with the desired duration value.</p><p>Inside the loop, we first set the output pin on the microcontroller to HIGH (1), then we wait for half the amount of calculated period &amp; then set the output pin to LOW (0) and again wait for half the amount of calculated period. This way, we are operating at 50% duty cycle.</p><p>Here is a simple output audio I created using the above method</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2Fx1XeU7YAbng%3Ffeature%3Doembed&amp;display_name=YouTube&amp;url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3Dx1XeU7YAbng&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2Fx1XeU7YAbng%2Fhqdefault.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=youtube" width="640" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/014e0b13497927ef224b456b6a7c5b29/href">https://medium.com/media/014e0b13497927ef224b456b6a7c5b29/href</a></iframe><p>That audio you just heard was generated by playing the following notes with appropriate pauses (denoted by -).</p><p>“GEG-GEG-AGFEDEF”</p><p>You can experiment further with chords and other complex audio structures. Make your Arduino projects more interesting with audio output! :) Cheers.</p><blockquote>Hello Montreal! We’ll be at the <a href="https://startupfestival.com/">Startupfest ’22</a> all three days, so drop by and say hi. We’d love to see what you’re building, and early birds get a free (limited) design audit or tech consultation. DM us on Twitter (<a href="https://twitter.com/tarkalabs">@tarkalabs</a>) and let’s talk.</blockquote><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=5df1463616c" width="1" height="1" alt=""><hr><p><a href="https://blog.tarkalabs.com/digital-audio-101-playing-audio-from-a-microcontroller-5df1463616c">Digital Audio 101: Playing Audio From A Microcontroller</a> was originally published in <a href="https://blog.tarkalabs.com/">Tarka Labs Blogs</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Real World Uses of Bitwise Operators]]></title>
            <link>https://blog.tarkalabs.com/real-world-uses-of-bitwise-operators-c41429df507f?source=rss-9eba103f8123------2</link>
            <guid isPermaLink="false">https://medium.com/p/c41429df507f</guid>
            <category><![CDATA[memories]]></category>
            <category><![CDATA[c-programming]]></category>
            <category><![CDATA[competitive-programming]]></category>
            <category><![CDATA[bitwise-operator]]></category>
            <category><![CDATA[coding]]></category>
            <dc:creator><![CDATA[Sreedev Kodichath]]></dc:creator>
            <pubDate>Thu, 03 Sep 2020 11:53:03 GMT</pubDate>
            <atom:updated>2020-09-03T11:53:03.131Z</atom:updated>
            <content:encoded><![CDATA[<p>As a developer, you definitely would’ve learned about bitwise operators at some point and would’ve thought to yourself. “huh, that’s neat. but where would I actually use it?”. That’s what we’ll be looking at today.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*KLRByQhD_Lldtaj1Rw_5dg.png" /></figure><p>The modern world of computing and software engineering has extracted the complexities away to an extent that you really don’t need to know about how the code you write is executed.</p><p>While this is okay for most uses where your code is executed on servers and powerful computers, you’ll run into major problems while working on limited resource devices like microcontrollers and FPGAs. You will need to know how your code is executed on these devices in order to optimize it to run on limited resources. (Kilobytes of RAM &amp; KHz of clock speed).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*u8ZPOdFS9mm-wxZQ4rCkUA.jpeg" /><figcaption>ESP32 Development Board</figcaption></figure><p>Understanding bitwise operators will bring you a step closer to optimization. Let’s first understand what bitwise operators are. Please note that this article will cover usage of bitwise operators in C, but the logic and syntax remains common across most languages. It’s also expected that you know and understand binary to decimal and decimal to binary conversions. If you don’t know that already, there are plenty resources available online for you to learn it.</p><h3>Table of Contents</h3><ol><li><a href="#5413">What are bitwise Operators?</a></li><li><a href="#73f2">The Different Bitwise Operators and Their Functions</a></li><li><a href="#42c6">Practical Use of Bitwise Operators</a> <br><em>(If you already know bitwise operators you can jump straight to this section)</em></li></ol><h3>What are bitwise Operators?</h3><p><em>Bitwise operators are operators that perform operations on data at a bit level.</em></p><p>What do I mean by that? They help you manipulate the bits that make up the piece of data which is represented by a datatype. This will start making more sense when we dive deeper into the topic. Let’s now take a look at what are the different bitwise operators available to us.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/40f2bce233f6bc452dfd6ea9282cd460/href">https://medium.com/media/40f2bce233f6bc452dfd6ea9282cd460/href</a></iframe><p>Using the sizeof() operator, we can find the number of bytes each datatype represents and since each byte it made up of 8 bits, we can find out the total number of bits taken up by each datatype with some multiplication magic.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/218/1*c3BiwEW0BnnanqSTYMf_dA.gif" /></figure><p>Let’s now take a look at each operator in detail.</p><h3>Different Bitwise Operators and Their Functions</h3><h4>&amp; Bitwise AND</h4><p>The bitwise AND operator (&amp;) takes two operands and compares the operands bit by bit and sets the corresponding output bit to 1 if and only if both input bits are 1. Here’s the truth table for the bitwise AND operator:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e21a31d73996f4918919d439a66d0a01/href">https://medium.com/media/e21a31d73996f4918919d439a66d0a01/href</a></iframe><p>Let’s take the following code for example</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d428bbf1224054dc86bbc8d3fb5bf3ea/href">https://medium.com/media/d428bbf1224054dc86bbc8d3fb5bf3ea/href</a></iframe><p>so the value of the variable x is 5 and the value of variable y is 6. The computer that I’m working on is a 64-bit machine so the sizeof(int) is 4 bytes so the binary representation of 5 world be:<br><em>00000000 00000000 00000000 00000101</em></p><p>and the binary representation of 6 would be:<br><em>00000000 00000000 00000000 00000110</em></p><p>For the sake of simplicity, let’s just take the last byte into consideration and let’s perform the bitwise AND operation on them.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5d0b5a446af25e759c2ecca12cb45130/href">https://medium.com/media/5d0b5a446af25e759c2ecca12cb45130/href</a></iframe><p>As you can see, the bits of 5 &amp; 6 were compared and the output byte was set to <em>00000100 </em>as a result which is the binary representation of the number 4. This is how we arrived at <em>“5 &amp; 6 = 4”.</em></p><h4>| Bitwise OR</h4><p>The bitwise OR operator take in two operands just like the bitwise AND and compares them bit by bit but instead of setting the output bit to 1 only when both input bits are 1, it follows this truth table:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0d97477489df5e869c7071f97731be66/href">https://medium.com/media/0d97477489df5e869c7071f97731be66/href</a></iframe><p>Let’s look at the below example:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/485d90b47cc1c88220181644a89234c4/href">https://medium.com/media/485d90b47cc1c88220181644a89234c4/href</a></iframe><p>The above program would print <em>“5 | 6 = 7”</em>. The following bit view of the operation would tell you how the program arrived at that output:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c5b92a10ce7c2a37dce6846794222e9d/href">https://medium.com/media/c5b92a10ce7c2a37dce6846794222e9d/href</a></iframe><h4>^ Bitwise XOR</h4><p>The bitwise XOR operator works similar to the bitwise OR operator. The only difference is that the output bit will be set to 1 when both input bits are different. The following is the logic table for bitwise XOR:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/bd8e547d974b13b30931b540e258e94a/href">https://medium.com/media/bd8e547d974b13b30931b540e258e94a/href</a></iframe><p>Here is an example program that would show how the bitwise XOR works:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d228d60163468158c954ba736529c625/href">https://medium.com/media/d228d60163468158c954ba736529c625/href</a></iframe><p>The above program would generate the following output:</p><pre>5^6 = 3</pre><p>How you ask? let’s take a look at the bits</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5602c01edd6d8ed7b9493d82a9a8e116/href">https://medium.com/media/5602c01edd6d8ed7b9493d82a9a8e116/href</a></iframe><p>Comparing the input bits according to the bitwise XOR truth table. We arrive at <em>00000011 </em>which is the binary representation of 3.</p><h4>~ Bitwise NOT</h4><p>The bitwise NOT operator is a little different from the other operators as it accepts only one operand. The bitwise NOT operator flips the bits of it’s operand. Let us take a look at the following example to understand it better,</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/74320470aed47b68cfadb58976ecd8f9/href">https://medium.com/media/74320470aed47b68cfadb58976ecd8f9/href</a></iframe><p>The above program would produce “~5=-6” as output. Let’s look at how the program arrived at the value of -6 when the bitwise NOT operator was applied to 5.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a00134e368804338d9a00a84ed8d23d2/href">https://medium.com/media/a00134e368804338d9a00a84ed8d23d2/href</a></iframe><p>Since negative numbers are stored as 2’s complement, “11111010” would be -6 in it’s decimal representation.</p><h4>&lt;&lt; Bitwise Left Shift Operator</h4><p>The left shift operator like the name suggests, moves the bits in it’s left operand to the right by the number of places specified in the right operand.</p><p>So, if the binary representation of 5 is 00000101 then applying the shift operator like so “5 &lt;&lt; 2” would move the bits to the left by two places striping the 2 most significant bits and adding 2 zeros as the least significant bits. The binary representation of the result would be 00010100 which is 20 in decimal representation. Let’s take a look this in code.</p><p>As long as your set bits don’t overflow and get dropped off, each left shift by 1 place has the effect of multiplying the left operand by 2.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c5c82fbf7e2194a1be0248c4daebec2e/href">https://medium.com/media/c5c82fbf7e2194a1be0248c4daebec2e/href</a></iframe><p>As expected, the program prints an output of “5 &lt;&lt; 2 = 20”.</p><h4>&gt;&gt; Bitwise Right Shift Operator</h4><p>The bitwise right shift operator is very similar to the left shift operator. Instead of shifting the bits to the left, believe it or not, it shifts the bits to the right. Yes, you didn’t see that one coming did you?</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/500/1*_MtviRcWAb6deKSE5qpoHQ.gif" /></figure><p>As the bits are shifted to the right, the least significant bits are dropped and an equivalent number of zeros are added as most significant bits.</p><p>Here’s some code.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ae5c731e17f25906240692bb40c7d552/href">https://medium.com/media/ae5c731e17f25906240692bb40c7d552/href</a></iframe><p>Simple enough, when we shift the bits of 5 (00000101) 2 places to the right, we get (00000001) which is basically 1 in decimal and binary which is why the above code would print “5 &gt;&gt; 2 = 1”.</p><p>As long as your set bits don’t overflow and get dropped off, each right shift by 1 place has the effect of dividing the left operand by 2.</p><p>Now that you’ve finally mastered bitwise operators, you’re probably thinking “What on earth would I do with this abundance of information?”.</p><p>I have got you covered. In the next section, we will discuss some of the practical uses of bitwise operators.</p><h3>Practical Uses of Bitwise Operators</h3><h4>1. Storing Multiple Boolean Flags</h4><p>When working on limited memory devices, you can’t really afford to have a thousand boolean flags assigned to variables. This is a really inefficient way to store your flags because Boolean values in C are basically a byte long. We don’t need an entire byte to store just a zero or a one. The rest of the bits are going to remain zero throughout. You cannot afford to throw away precious memory when you’re working with just KBs of RAM.</p><p>So what is the solution? Let’s look at the code.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6f53c24ab240bbaa51874df9645bcea4/href">https://medium.com/media/6f53c24ab240bbaa51874df9645bcea4/href</a></iframe><p>You must have definitely wondered, all of this just to save 1 byte? well that’s not entirely true. The increase in memory consumption per boolean flag is one byte, but while using the bitwise flags, you only need an additional byte every 8 flags you add to the program. To increase or decrease the number of flags you just need to change the datatype. if you need only less than 8 flags, you can use <em>“unsigned char ”</em>. If you need more than 8 flags, you can use <em>“unsigned long int”</em> or <em>“unsigned long long int”</em> depending on your requirement.</p><h4>2. Checking for Odd and Even Numbers</h4><p>Another trick you can achieve using bitwise operators is checking if an integer is even or odd. Let’s see how we can do it.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/04cc9a18e3989583bf6b94c1024029f1/href">https://medium.com/media/04cc9a18e3989583bf6b94c1024029f1/href</a></iframe><p>So the logic behind !(x&amp;1) is that the binary representation of 1 is 0001 (considering only 4 bits for simplicity) and the place value of the least significant bit is 1. All odd numbers can be represented as 2n+1. So the least significant bit is always 1 for odd numbers. So when compare any odd number with 0001 using the bitwise AND, the output bytes will be 0001. so by using the logical NOT operator, we are able to check for even numbers. For odd numbers you just need to remove the logical NOT and just check (x&amp;1).</p><h4>3. Swapping Variables Without Using a Third Variable</h4><p>So the swapping of two variables without involving a third variable can be achieved without the bitwise operators, but since you already know bitwise operators, why not?</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/71b54eb38f3df8c66db6a956d17e77f2/href">https://medium.com/media/71b54eb38f3df8c66db6a956d17e77f2/href</a></iframe><h4>4. Converting text casing (Lowercase &amp; Uppercase)</h4><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/30672e094be24743e9d06081b4a88ed6/href">https://medium.com/media/30672e094be24743e9d06081b4a88ed6/href</a></iframe><p>So in the above example, we are using the bitwise AND with ‘_’ char as the right operand to convert each character in the string to uppercase. And to convert to a lowercase character, we are using the bitwise OR operator with the space ASCII character as the right operand.</p><h4>5. Checking if a number is a power of 2</h4><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5282e997ab154a48974ce7361a043056/href">https://medium.com/media/5282e997ab154a48974ce7361a043056/href</a></iframe><p>To understand how the above condition works, we need to understand a property of powers of two. Powers of two when represented in binary have only one set bit i.e., only one bit has the value of 1. Any power of 2, minus 1 would have binary representation where all bits are set except the bit that represents the next number (which is a power of 2).</p><p>Using the bitwise AND with the number n and n-1, we can conclude that the number in question is a power of two if it returns 0.</p><p>To understand how the above condition works, we need to understand a property of powers of two. Powers of two when represented in binary have only one set bit i.e., only one bit has the value of 1. Any power of 2, minus 1 would have binary representation where all bits are set except the bit that represents the next number (which is a power of 2).</p><p>Using the bitwise AND with the number n and n-1, we can conclude that the number in question is a power of two if it returns 0.</p><p>These are just some of the many use cases of bitwise operators. The more you understand the properties of binary numbers, datatypes and encoding the better you’ll be able to utilize these operators. That’s all for now. Byte Byte.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/625/1*35oALYhkYyXB2aG0F-qrwA.jpeg" /></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c41429df507f" width="1" height="1" alt=""><hr><p><a href="https://blog.tarkalabs.com/real-world-uses-of-bitwise-operators-c41429df507f">Real World Uses of Bitwise Operators</a> was originally published in <a href="https://blog.tarkalabs.com/">Tarka Labs Blogs</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[All You Need To Know About Comments in Ruby]]></title>
            <link>https://blog.tarkalabs.com/all-you-need-to-know-about-comments-in-ruby-97d991714cf3?source=rss-9eba103f8123------2</link>
            <guid isPermaLink="false">https://medium.com/p/97d991714cf3</guid>
            <category><![CDATA[magic-comments]]></category>
            <category><![CDATA[frozen-string-literal]]></category>
            <category><![CDATA[ruby]]></category>
            <category><![CDATA[comment]]></category>
            <category><![CDATA[encoding]]></category>
            <dc:creator><![CDATA[Sreedev Kodichath]]></dc:creator>
            <pubDate>Fri, 10 Jul 2020 13:08:50 GMT</pubDate>
            <atom:updated>2020-07-15T06:36:00.549Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="Special Comments in Ruby" src="https://cdn-images-1.medium.com/max/1024/1*JqoME6Y6loVpW1IGmsFklQ.png" /></figure><p>It is well known that the ruby comments are used to leave notes or micro-docs within the code and is ignored by the Ruby Interpreter. But can we do other things with comments?</p><figure><img alt="Special Comments in Ruby" src="https://cdn-images-1.medium.com/max/478/1*d8TIcd9Wr57oMo9mToLP5Q.gif" /></figure><p>There are several other functions that comments can offer. In this article, we shall dive into some of the use cases of comments that can have certain desirable impacts on the Ruby interpreter. Each use case has been explained in detail making it easier for even beginners to understand.</p><h3><strong><em>Shebang Comments</em></strong></h3><p>Shebang is a “comment” that indicates the interpreter directive for the interpreter to be used when the file is run as an executable in *nix operating systems. This specific comment is not unique to Ruby. Many scripting languages make use of the shebang to make scripts executable in a simple manner.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/71e5af663d24c8f59e539210f8562733/href">https://medium.com/media/71e5af663d24c8f59e539210f8562733/href</a></iframe><p>in addition to specifying the interpreter directive, you can also specify the flags like ` — jit` or `-w` which will automatically be passed on to the interpreter when the executable is run. Let’s take a look at how this can be done:</p><p>create a new file “<strong><em>text_executable</em></strong>” with the following lines:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6090d7abe0875bbf97516cd4dc25fb40/href">https://medium.com/media/6090d7abe0875bbf97516cd4dc25fb40/href</a></iframe><p>now, to make this file an executable, we need to provide the execute permission to the file using the following command:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8fd5566a74c8ed5e9ffcd8271d7e6e8f/href">https://medium.com/media/8fd5566a74c8ed5e9ffcd8271d7e6e8f/href</a></iframe><p>voila! your file is now an executable that will be run using the ruby interpreter. You can run it by:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c6b2f08bbe60d11f9fef8bc06559cc29/href">https://medium.com/media/c6b2f08bbe60d11f9fef8bc06559cc29/href</a></iframe><figure><img alt="Special Comments in Ruby" src="https://cdn-images-1.medium.com/max/498/1*WyLkIhCknD5VnRo-kEFPvg.gif" /></figure><p>You’ll see the <em>“Running Executable”</em> output printed.</p><h3>Multi-line Comments</h3><p>Normally when we specify multi-line comments, we just stick to adding a ‘<strong><em>#</em></strong>’ sign in front of each line. This though not incorrect, can be replaced with a better way to write multi-line comments. Here is how you can achieve that:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7c886a7bf425d732b3c149b9947a3e6e/href">https://medium.com/media/7c886a7bf425d732b3c149b9947a3e6e/href</a></iframe><p>This makes the multi-line comments look much cleaner especially if you have more than 3–4 lines of comments to specify. Another way you can add multi-line comments <strong>AT THE END OF THE FILE </strong>is as follows:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/60e2fa448097ae13ed85dc871b781a89/href">https://medium.com/media/60e2fa448097ae13ed85dc871b781a89/href</a></iframe><p>The other option to add multi-line micro-doc in your code is by using a docstring. Docstrings are treated as string literals by ruby. Though this is not technically a comment, it is often used by developers to embed code documentation within the code.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4cebb9d49ad3ea11dbfa6b7c0f86bbfe/href">https://medium.com/media/4cebb9d49ad3ea11dbfa6b7c0f86bbfe/href</a></iframe><h3><strong><em>Indentation Warning Comment</em></strong></h3><p>You see how ruby interpreter ignores all your indentation errors? This is done to keep the error reporting clean. But if you wish to see all the indentation error warnings, you can add this little magic comment to the top of your ruby script:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/990479d91c35c64de3286bc7c428d85d/href">https://medium.com/media/990479d91c35c64de3286bc7c428d85d/href</a></iframe><p>Once you have this enabled, you will start seeing warnings when there are problematic indentations in your code.</p><p>Let’s take a look at the difference in the output of the following program before and after enabling the `<strong><em>#warn_indent: true</em></strong>` magic comment:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7c91aa6ab9213fddf9b1370815caac9d/href">https://medium.com/media/7c91aa6ab9213fddf9b1370815caac9d/href</a></iframe><p><strong>BEFORE ENABLING WARN_INDENT</strong></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/440e9d444a82fc4405fc54676b3643ab/href">https://medium.com/media/440e9d444a82fc4405fc54676b3643ab/href</a></iframe><p><strong>AFTER ENABLING WARN_INDENT</strong></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/15bfe0600451cca00a3c034b2d23654b/href">https://medium.com/media/15bfe0600451cca00a3c034b2d23654b/href</a></iframe><h3><strong><em>Frozen String Literal Comment</em></strong></h3><p>In ruby, by default, all string literals are treated as mutable and every time you create a new string literal with the same content/text, multiple objects are instantiated. This is one of the main reasons for increased memory consumption in ruby programs.</p><p>There is a comment that you can add to the top of your file that will make the ruby interpreter treat all string literals are frozen objects. This way, the same string is never instantiated twice.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f7af28427ecc752c8865d2e05a283d3b/href">https://medium.com/media/f7af28427ecc752c8865d2e05a283d3b/href</a></iframe><p>Keep in mind that if you contradicting frozen_string_literal values set in multiple places in your script, the last declaration is what is taken into consideration by the Ruby Interpreter.</p><p>Adding this comment has shown up to 20% decrease in memory consumption on most of the code that it was tested on.</p><p>Let’s take a closer look at the difference in the way ruby instantiates string literal before and after usage of the frozen_string_literal comment.</p><p><strong>Without frozen string literals</strong></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/47aba439ea776437fadf115d28cc919a/href">https://medium.com/media/47aba439ea776437fadf115d28cc919a/href</a></iframe><p><strong>With frozen string literals</strong></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e8074e541c98eb56e44a4a295850c404/href">https://medium.com/media/e8074e541c98eb56e44a4a295850c404/href</a></iframe><p>As you can see, new objects are not instantiated every time a string literal is declared. The previous instance of that variant of the string is utilized.</p><h3><strong><em>File Encoding Comment</em></strong></h3><p>As of ruby 2.0, the default encoding set by the ruby interpreter for the any string is UTF-8. Back when 1.9.x was a thing, ruby used US-ASCII by default. It is possible to change the encoding set by the interpreter using, yes you guessed it right. Yet another magic comment!</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e01cd5c121947bf8002f7157d8c4ea3b/href">https://medium.com/media/e01cd5c121947bf8002f7157d8c4ea3b/href</a></iframe><p>Depending on your requirement, you can set the encoding that’s appropriate for you.</p><p>Another format of the file encoding comment is as follows:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3f98ddd7b1440f93b60fe2bfb635c3d4/href">https://medium.com/media/3f98ddd7b1440f93b60fe2bfb635c3d4/href</a></iframe><p>Both of the above formats work in the same way, but if you want another developer reading the file to know that the encoding has been changed in a not-so-subtle way, the second format would be your best bet.</p><p>Please note that if you have multiple encoding magic comments in your file, only the first comment will be parsed by the interpreter. the rest of the encoding comments will be ignored.</p><p>consider the following piece of code:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8f3cf143e1ab10be2333ea6cd13dedcc/href">https://medium.com/media/8f3cf143e1ab10be2333ea6cd13dedcc/href</a></iframe><p>The above program will throw an error because <strong>é</strong> is an invalid ASCII character. The error will look like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4e69e11189c798a3b551978ef335ff71/href">https://medium.com/media/4e69e11189c798a3b551978ef335ff71/href</a></iframe><p>The same piece of code if run with the UTF-8 encoding, will output the following:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/cc15799d35db6450d833cde8702114ac/href">https://medium.com/media/cc15799d35db6450d833cde8702114ac/href</a></iframe><p>The following are the different encodings supported by the ruby interpreter:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/811/1*8EuzI9bn5UYRgbPnOajV4A.png" /><figcaption>String Encodings Supported By Ruby</figcaption></figure><p>These comments will help you achieve an additional level of control over how the ruby interpreter treats your code. Hope this was a informative read to you. Happy Rubying!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=97d991714cf3" width="1" height="1" alt=""><hr><p><a href="https://blog.tarkalabs.com/all-you-need-to-know-about-comments-in-ruby-97d991714cf3">All You Need To Know About Comments in Ruby</a> was originally published in <a href="https://blog.tarkalabs.com/">Tarka Labs Blogs</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Fetching a Single File From Git Stash]]></title>
            <link>https://medium.com/tarkalabs-til/fetching-a-single-file-from-git-stash-db632ed5b7fb?source=rss-9eba103f8123------2</link>
            <guid isPermaLink="false">https://medium.com/p/db632ed5b7fb</guid>
            <category><![CDATA[git]]></category>
            <category><![CDATA[vc]]></category>
            <category><![CDATA[stash]]></category>
            <category><![CDATA[checkout]]></category>
            <dc:creator><![CDATA[Sreedev Kodichath]]></dc:creator>
            <pubDate>Tue, 02 Jun 2020 19:50:14 GMT</pubDate>
            <atom:updated>2020-06-03T05:08:56.086Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*2oCm-9dd7qost2WsPvFyCA.jpeg" /></figure><p>A lot of times when jumping across branches on your Git repository, you’ll come across situations where you need to un-stash a single file from your stashes. Though there is no specific command to achieve this, there are workarounds that can be used to achieve this behavior in Git.</p><h3>Checking Out a Single File From The Stash</h3><p>This method will checkout the file from the stash into your working branch. Ensure that there are no local uncommitted changes on the existing file that you do not wish to lose before using this method as it will restore the version of the file as of the time when the stash was performed.</p><p>git checkout stash@{0} -- &lt;filename&gt;</p><h3>Creating a Copy of a Stashed File Under a Different Filename</h3><p>This is a non-destructive method that will let you recreate the file in the stash into the working branch under a new filename.</p><p>git show stash@{0}:stashed_file.rb &gt; copy_of_stashed_file.rb</p><p>The stash tool can be used in conjunction with other git features to achieve desired outcomes. I would recommend checking out Atlassian’s documentation on Git Stashing: <br><a href="https://www.atlassian.com/git/tutorials/saving-changes/git-stash">https://www.atlassian.com/git/tutorials/saving-changes/git-stash</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=db632ed5b7fb" width="1" height="1" alt=""><hr><p><a href="https://medium.com/tarkalabs-til/fetching-a-single-file-from-git-stash-db632ed5b7fb">Fetching a Single File From Git Stash</a> was originally published in <a href="https://medium.com/tarkalabs-til">TarkaLabs TIL</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Removing Accented Characters From Strings in Rails]]></title>
            <link>https://medium.com/tarkalabs-til/removing-accented-characters-from-strings-in-rails-1be55cd1a739?source=rss-9eba103f8123------2</link>
            <guid isPermaLink="false">https://medium.com/p/1be55cd1a739</guid>
            <category><![CDATA[diacritics]]></category>
            <category><![CDATA[ascii]]></category>
            <category><![CDATA[ruby-on-rails]]></category>
            <category><![CDATA[utf-8]]></category>
            <category><![CDATA[accented]]></category>
            <dc:creator><![CDATA[Sreedev Kodichath]]></dc:creator>
            <pubDate>Thu, 28 May 2020 14:28:59 GMT</pubDate>
            <atom:updated>2020-05-28T14:28:59.387Z</atom:updated>
            <content:encoded><![CDATA[<h3>Removing Accented (Diacritic) Characters From Strings in Rails</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/492/1*fjvGV_a9kt65AP23C1Uuvg.png" /></figure><p>If you have a huge list of text records with accented characters and want to get rid of the accented (diacritic) characters because you want to keep the data ASCII-only, Rails offers a simple method to get rid of accented characters without having to go through the pain of writing complex Regex matchers.</p><pre>I18n.transliterate &quot;São Paulo&quot;</pre><pre>=&gt; &quot;Sao Paulo&quot;</pre><p>As per the official documentation, the `transliterate` method replaces non-ASCII characters with an ASCII approximation.</p><pre>NOTE: In case, an ASCII Approximation for a non-ASCII character is not found, the character is replaced with a `?`.</pre><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1be55cd1a739" width="1" height="1" alt=""><hr><p><a href="https://medium.com/tarkalabs-til/removing-accented-characters-from-strings-in-rails-1be55cd1a739">Removing Accented Characters From Strings in Rails</a> was originally published in <a href="https://medium.com/tarkalabs-til">TarkaLabs TIL</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>