<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Ian Hofmann-Hicks</title>
    <description>The latest articles on Forem by Ian Hofmann-Hicks (@evilsoft).</description>
    <link>https://forem.com/evilsoft</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F110892%2F526913d1-2ed1-463f-9b54-70d1f55df0c9.png</url>
      <title>Forem: Ian Hofmann-Hicks</title>
      <link>https://forem.com/evilsoft</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/evilsoft"/>
    <language>en</language>
    <item>
      <title>Monad Say What? (Part 1)</title>
      <dc:creator>Ian Hofmann-Hicks</dc:creator>
      <pubDate>Fri, 09 Nov 2018 19:08:10 +0000</pubDate>
      <link>https://forem.com/evilsoft/monad-say-what-part-1-4a61</link>
      <guid>https://forem.com/evilsoft/monad-say-what-part-1-4a61</guid>
      <description>&lt;p&gt;With all the buzz of late, Functional Programming this and Composition that, terms like &lt;code&gt;Functor&lt;/code&gt; and &lt;code&gt;Monad&lt;/code&gt; may have crossed your feeds and left you wondering, "what the heck are these people even talking about?". With such strange names and possibly even more foreign explanations that require a deep understanding of Algebra (the abstract kind) and Category Theory, it may be hard to see how these types of constructs fit in our comfy, day-to-day JavaScript code.&lt;/p&gt;

&lt;p&gt;In this series we'll explore, from a JavaScript programmers point of view, what a &lt;code&gt;Monad&lt;/code&gt; is and how they can be used to great effect in our everyday code. We will be focusing mostly on the usage of these types and not the theory behind them.&lt;/p&gt;

&lt;p&gt;So for instance, instead of working to understand the following definition:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;A &lt;code&gt;Monad&lt;/code&gt; is a &lt;code&gt;Monoid&lt;/code&gt; in the &lt;code&gt;Category&lt;/code&gt; of &lt;code&gt;Endofunctors&lt;/code&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;We will work toward understanding a more practical definition:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;A &lt;code&gt;Monad&lt;/code&gt; is a data type that allows for sequential application of its "effects" or "embellishments" while mapping its underlying data.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Now, while the second definition still may not be clear right now, I think we can agree that working to understanding those words and the meaning derived from how they all fit together seems a bit more approachable.&lt;/p&gt;

&lt;p&gt;Understanding the first definition is critical when we venture out and create our own types. Although if you are anything like me, I like to get my hands dirty and build an understanding by first having a play with things and applying the theory when I have a good intuition of how use them. There are a slew of types already implemented in the wild that we can blissfully play with...without understanding the Maths behind them.&lt;/p&gt;

&lt;p&gt;These posts assume an understanding of not only the JavaScript language, but how "currying", "partial application" and "function composition" is accomplished in Javascript. If you feel a bit fuzzy on these topics, there are many resources available on the webs to get you sorted out.&lt;/p&gt;

&lt;p&gt;So without further ado, lets get cracking.&lt;/p&gt;

&lt;h2&gt;
  
  
  Part 1: The Algebraic Data Type (ADT)
&lt;/h2&gt;

&lt;p&gt;Many times when people say "I used this &lt;code&gt;Monad&lt;/code&gt; for this, or that &lt;code&gt;Monad&lt;/code&gt; for that", what they really mean is: "I used this Algebraic Data Type &lt;em&gt;(ADT)&lt;/em&gt; for this and that ADT for that". When looking at the code they are presenting, you find that they never touch the &lt;code&gt;Monad&lt;/code&gt; part of the type, or in some cases the type is not even a &lt;code&gt;Monad&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;I would like to start things off by clearing up this point of contention with me. It seems like a minor thing, but I have found calling things a &lt;code&gt;Monad&lt;/code&gt; when we really mean some ADT tends to lead to confusion when we are starting to build our initial intuitions around &lt;code&gt;Monad&lt;/code&gt;s and other aspects of a data type.&lt;/p&gt;

&lt;p&gt;Before we can begin to understand what makes an Algebraic Data Type a &lt;code&gt;Monad&lt;/code&gt; or not, we need to first get a feel around what an ADT is. The best way I can think of to broach the topic is provide a simple definition as to what an ADT is. Then demonstrate how an ADT in JavaScript is used in contrast to a (hopefully) more familiar imperative implementation.&lt;/p&gt;

&lt;p&gt;Let's take a look at the data we will be processing with our examples:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// data :: [ * ]&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;9CYolEKK&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;learner&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Molly&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;gbdCC8Ui&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;learner&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Thomas&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="kc"&gt;undefined&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;1AceDkK_&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;learner&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Lisa&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;learner&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Chad&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;gid&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;11232&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;learner&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Mitch&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The data is a mixed &lt;code&gt;Array&lt;/code&gt; that could contain values of any type. In this specific instance we have three types in play: &lt;code&gt;POJO&lt;/code&gt;s (Plain ol' JavaScript Object) of varying shape, a &lt;code&gt;Null&lt;/code&gt; instance and an &lt;code&gt;Undefined&lt;/code&gt; instance.&lt;/p&gt;

&lt;p&gt;Our examples will be defined with the following list of requirements:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Accept any value of any type at its input.&lt;/li&gt;
&lt;li&gt;Unless the data is an &lt;code&gt;Array&lt;/code&gt; with at least one valid record, an empty &lt;code&gt;Object&lt;/code&gt; will be returned.&lt;/li&gt;
&lt;li&gt;Return an &lt;code&gt;Object&lt;/code&gt; of valid records keyed with a valid &lt;code&gt;id&lt;/code&gt; from the included record, effectively filtering out any invalid records.&lt;/li&gt;
&lt;li&gt;We define a valid record as an &lt;code&gt;Object&lt;/code&gt; with a &lt;code&gt;String&lt;/code&gt; keyed with &lt;code&gt;id&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;This function does not throw, no matter the input, and provides a reasonable default in the empty &lt;code&gt;Object&lt;/code&gt; it returns.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;From these requirements, we can implement an imperative function that does the following:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Verify input is an &lt;code&gt;Array&lt;/code&gt;, return an empty &lt;code&gt;Object&lt;/code&gt; if it isn't.&lt;/li&gt;
&lt;li&gt;Declare a &lt;code&gt;result&lt;/code&gt; accumulator for building our final result, defaulting it to an empty &lt;code&gt;Object&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Iterate over the provided &lt;code&gt;Array&lt;/code&gt; and do the following for each item:

&lt;ol&gt;
&lt;li&gt;Validate the item against our record criteria&lt;/li&gt;
&lt;li&gt;If passed, add the record to the result, keyed by the &lt;code&gt;id&lt;/code&gt; value on the record. Otherwise do nothing.&lt;/li&gt;
&lt;/ol&gt;


&lt;/li&gt;

&lt;li&gt;Return the &lt;code&gt;result&lt;/code&gt;.&lt;/li&gt;

&lt;/ol&gt;

&lt;p&gt;With a few helpers to help us with some type checking, we can provide an implementation like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// isArray :: a -&amp;gt; Boolean&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;isArray&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;
  &lt;span class="nb"&gt;Array&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;isArray&lt;/span&gt;

&lt;span class="c1"&gt;// isString :: a -&amp;gt; Boolean&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;isString&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;
  &lt;span class="k"&gt;typeof&lt;/span&gt; &lt;span class="nx"&gt;x&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;string&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;

&lt;span class="c1"&gt;// isObject :: a -&amp;gt; Boolean&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;isObject&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;
  &lt;span class="o"&gt;!!&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nb"&gt;Object&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;prototype&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;toString&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;call&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;[object Object]&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;

&lt;span class="c1"&gt;// indexById :: [ * ] -&amp;gt; Object&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;indexById&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;records&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nf"&gt;isArray&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;records&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;

  &lt;span class="k"&gt;for &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;records&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;rec&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;records&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;

    &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;isObject&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;rec&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nf"&gt;isString&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;rec&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;result&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;rec&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;rec&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;result&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nf"&gt;indexById&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;//=&amp;gt; {}&lt;/span&gt;

&lt;span class="nf"&gt;indexById&lt;/span&gt;&lt;span class="p"&gt;([])&lt;/span&gt;
&lt;span class="c1"&gt;//=&amp;gt; {}&lt;/span&gt;

&lt;span class="nf"&gt;indexById&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt; &lt;span class="p"&gt;])&lt;/span&gt;
&lt;span class="c1"&gt;//=&amp;gt; {}&lt;/span&gt;

&lt;span class="nf"&gt;indexById&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;//=&amp;gt; {&lt;/span&gt;
&lt;span class="c1"&gt;//   9CYolEKK: { id: '9CYolEKK', learner: 'Molly' },&lt;/span&gt;
&lt;span class="c1"&gt;//   gbdCC8Ui: { id: 'gbdCC8Ui', learner: 'Thomas' },&lt;/span&gt;
&lt;span class="c1"&gt;//   1AceDkK_: { id: '1AceDkK_', learner: 'Lisa' }&lt;/span&gt;
&lt;span class="c1"&gt;// }&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As we see, we have a strong implementation that meets our requirements and responds to any input we give it as expected.&lt;/p&gt;

&lt;p&gt;As for our ADT implementation, we will be leaning heavily on the &lt;a href="https://evilsoft.github.io/crocks/" rel="noopener noreferrer"&gt;&lt;code&gt;crocks&lt;/code&gt;&lt;/a&gt; library. Even though JavaScript is a fully functional programming language, it lacks some structures that appear in other languages that are not general purpose languages, but are strictly functional. As a result, libraries like &lt;a href="https://evilsoft.github.io/crocks/" rel="noopener noreferrer"&gt;&lt;code&gt;crocks&lt;/code&gt;&lt;/a&gt; are typically used for working with ADTs.&lt;/p&gt;

&lt;p&gt;Here is an implementation that implements the requirements using ADTs:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;Assign&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;Maybe&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;composeK&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;converge&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;isArray&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nx"&gt;isObject&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;isString&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;liftA2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;mreduceMap&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;objOf&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nx"&gt;prop&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;safe&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;crocks&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;// wrapRecord :: Object -&amp;gt; Maybe Object&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;wrapRecord&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;converge&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="nf"&gt;liftA2&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;objOf&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
  &lt;span class="nf"&gt;composeK&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;safe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;isString&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="nf"&gt;prop&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;id&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)),&lt;/span&gt;
  &lt;span class="nx"&gt;Maybe&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;of&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;// mapRecord :: a -&amp;gt; Object&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;mapRecord&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;record&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;
  &lt;span class="nf"&gt;safe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;isObject&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;record&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;chain&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;wrapRecord&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;option&lt;/span&gt;&lt;span class="p"&gt;({})&lt;/span&gt;

&lt;span class="c1"&gt;// indexById :: [ * ] -&amp;gt; Object&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;indexById&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;records&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;
  &lt;span class="nf"&gt;safe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;isArray&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;records&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;mreduceMap&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;Assign&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;mapRecord&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;option&lt;/span&gt;&lt;span class="p"&gt;({})&lt;/span&gt;

&lt;span class="nf"&gt;indexById&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;//=&amp;gt; {}&lt;/span&gt;

&lt;span class="nf"&gt;indexById&lt;/span&gt;&lt;span class="p"&gt;([])&lt;/span&gt;
&lt;span class="c1"&gt;//=&amp;gt; {}&lt;/span&gt;

&lt;span class="nf"&gt;indexById&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt; &lt;span class="p"&gt;])&lt;/span&gt;
&lt;span class="c1"&gt;//=&amp;gt; {}&lt;/span&gt;

&lt;span class="nf"&gt;indexById&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;//=&amp;gt; {&lt;/span&gt;
&lt;span class="c1"&gt;//   9CYolEKK: { id: '9CYolEKK', learner: 'Molly' },&lt;/span&gt;
&lt;span class="c1"&gt;//   gbdCC8Ui: { id: 'gbdCC8Ui', learner: 'Thomas' },&lt;/span&gt;
&lt;span class="c1"&gt;//   1AceDkK_: { id: '1AceDkK_', learner: 'Lisa' }&lt;/span&gt;
&lt;span class="c1"&gt;// }&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;One of the differences between the two implementation that I hope was noticed is the lack of familiar flow control and logic patterns in the ADT implementation. Things like &lt;code&gt;for&lt;/code&gt; loops and &lt;code&gt;if&lt;/code&gt; statements do not appear once in the second implementation. They are still there, of course they are still there, but when working with ADTs we encode these flows/logic in specific types.&lt;/p&gt;

&lt;p&gt;For instance, notice that &lt;code&gt;safe&lt;/code&gt; function that is used in a couple places? Take a look at the predicate functions passed to the first argument of those calls. Notice that the same checks are being done there, but instead of an &lt;code&gt;if&lt;/code&gt; we are using the &lt;code&gt;safe&lt;/code&gt; function that returns an ADT called &lt;code&gt;Maybe&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Another thing you may have noticed is the lack of state anywhere in the second implementation. Every variable declared was a function, not a single JavaScript value in sight. We used two bits of state in the original implementation, &lt;code&gt;result&lt;/code&gt; to put together our final result and a little helper called &lt;code&gt;rec&lt;/code&gt; which just cleans up the code and keeps us from having to reference the indexed value from the &lt;code&gt;Array&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;We were able to get rid of the need of the &lt;code&gt;for&lt;/code&gt; loop and the &lt;code&gt;result&lt;/code&gt; variable, by using the function &lt;code&gt;mreduceMap&lt;/code&gt; to fold each record over an &lt;code&gt;Assign&lt;/code&gt; type. &lt;code&gt;Assign&lt;/code&gt; lets us combine &lt;code&gt;Object&lt;/code&gt;s similar to the way &lt;code&gt;Object.assign&lt;/code&gt; does in vanilla JavaScript, removing the need to keep track of an accumulator like the &lt;code&gt;result&lt;/code&gt; &lt;code&gt;Object&lt;/code&gt;. So now that we have a means to accumulate, we can then remove the &lt;code&gt;for&lt;/code&gt; loop by leaning on &lt;code&gt;mreduceMap&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;Maybe&lt;/code&gt;, &lt;code&gt;Assign&lt;/code&gt;, fold, etc. stuff does not need to be understood right now. I only mention them because I want to communicate that every pattern in the original implementation is present in the ADT version, there is no magic going on here. When we code with ADTs, we remove a lot of the mechanical bits like accumulation, logic, control flow and state juggling by encoding them in ADTs and let the types take care of all the "plumbing" for us.&lt;/p&gt;

&lt;p&gt;The last thing I hoped was picked up on is how we are using what looks like a &lt;a href="https://martinfowler.com/bliki/FluentInterface.html" rel="noopener noreferrer"&gt;fluent api&lt;/a&gt; for chaining our operations together in the functions &lt;code&gt;mapRecord&lt;/code&gt; and &lt;code&gt;indexById&lt;/code&gt;. Seeing code like this may make us believe that we are working with traditional &lt;code&gt;Object&lt;/code&gt;s and classes like a typical Object Oriented Programmer might. It is even reinforced when you hear these operations called methods (all the &lt;a href="https://evilsoft.github.io/crocks/" rel="noopener noreferrer"&gt;crocks&lt;/a&gt; documentation does this). These intuitions and misleading characterizations can get in the way of how we understand the way that ADTs are used in our day to day code.&lt;/p&gt;

&lt;p&gt;Next time we will dig a little deeper on ADT usage by exploring how ADTs are not &lt;code&gt;Object&lt;/code&gt;s in the sense that an Object Oriented Programmer would view an &lt;code&gt;Object&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Exercises For Fun
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;Take the first POJ (Plain ol' JavaScript) function and remove the &lt;code&gt;for&lt;/code&gt; loop by using the &lt;code&gt;reduce&lt;/code&gt; method available on &lt;code&gt;Array.prototype&lt;/code&gt;. Take note on what happens to the &lt;code&gt;result&lt;/code&gt; variable and how the default value of &lt;code&gt;{}&lt;/code&gt; is applied.&lt;/li&gt;
&lt;li&gt;Take the first POJ function and, without using timers (&lt;code&gt;setTimeout&lt;/code&gt; or &lt;code&gt;setInterval&lt;/code&gt;), refactor it to be the &lt;em&gt;MOST INEFFICIENT&lt;/em&gt; implementation you can think of. As you refactor, think about what you picked it as the &lt;em&gt;MOST INEFFICIENT&lt;/em&gt;.&lt;/li&gt;
&lt;li&gt;Using either the first &lt;code&gt;POJ&lt;/code&gt; function or your refactor from Exercise 1, identify the discrete actions/transformations that could live in their own functions. Then create those functions and refactor the main function to use them.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Additional Exercises (Also For Fun)
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;We used a third-party library's type checking predicate functions for doing our type checks. Pick one of the predicates we used and implement your own version of it, throwing different values of different types at your implementation and see if it behaves as expected.&lt;/li&gt;
&lt;li&gt;If you happen to be versed in libraries like &lt;a href="https://ramdajs.com/" rel="noopener noreferrer"&gt;ramda&lt;/a&gt; or &lt;a href="https://github.com/lodash/lodash/wiki/FP-Guide" rel="noopener noreferrer"&gt;lodash-fp&lt;/a&gt;, implement the same behavior in a function using just the library you are familiar with. Compare the result of your function with the following pointfree version of the above ADT version:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// wrapRecord :: Object -&amp;gt; Maybe Object&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;wrapRecord&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;converge&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="nf"&gt;liftA2&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;objOf&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
  &lt;span class="nf"&gt;composeK&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;safe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;isString&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="nf"&gt;prop&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;id&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)),&lt;/span&gt;
  &lt;span class="nx"&gt;Maybe&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;of&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;// mapRecord :: a -&amp;gt; Object&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;mapRecord&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;compose&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="nf"&gt;option&lt;/span&gt;&lt;span class="p"&gt;({}),&lt;/span&gt;
  &lt;span class="nf"&gt;chain&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;wrapRecord&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
  &lt;span class="nf"&gt;safe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;isObject&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;// indexById :: [ * ] -&amp;gt; Object&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;indexById&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;records&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;compose&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="nf"&gt;option&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;error&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt; &lt;span class="p"&gt;}),&lt;/span&gt;
  &lt;span class="nf"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;mreduceMap&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;Assign&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;mapRecord&lt;/span&gt;&lt;span class="p"&gt;)),&lt;/span&gt;
  &lt;span class="nf"&gt;safe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;isArray&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>monad</category>
      <category>javascript</category>
      <category>functional</category>
    </item>
  </channel>
</rss>
