<?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 KKruups on Medium]]></title>
        <description><![CDATA[Stories by KKruups on Medium]]></description>
        <link>https://medium.com/@kkruups?source=rss-361270501b55------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*hTNNs7ykOjhXJMhx.</url>
            <title>Stories by KKruups on Medium</title>
            <link>https://medium.com/@kkruups?source=rss-361270501b55------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 24 Nov 2026 14:50:16 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@kkruups/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[elm Insights]]></title>
            <link>https://medium.com/@kkruups/elm-insights-c067c66161bb?source=rss-361270501b55------2</link>
            <guid isPermaLink="false">https://medium.com/p/c067c66161bb</guid>
            <category><![CDATA[elm-insights]]></category>
            <category><![CDATA[kkruups]]></category>
            <category><![CDATA[elm-lang]]></category>
            <category><![CDATA[elm]]></category>
            <category><![CDATA[andthen]]></category>
            <dc:creator><![CDATA[KKruups]]></dc:creator>
            <pubDate>Tue, 22 Nov 2016 06:57:34 GMT</pubDate>
            <atom:updated>2016-11-23T11:46:05.253Z</atom:updated>
            <content:encoded><![CDATA[<h4>Farewell to Backticks, Hello Pipeline!</h4><blockquote>Articles about the elm programming language and other stuff</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/193/1*FjRiKHBHv8Y8hAf4YHim_Q.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/171/1*m8nyH9j1chfyMCGNIdSRHw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/193/1*FjRiKHBHv8Y8hAf4YHim_Q.png" /><figcaption>elm Insights</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*1M4Wg2EPCyNBN0XIw5tCpA.jpeg" /></figure><p>Well, folks the new release of <strong><em>elm version 0.18</em></strong> is out!</p><p>I thought I would keep this edition short and sweet, given the epic novel length of my <a href="https://medium.com/@kkruups/elm-insights-c74047637206#.5hykvoitj"><em>last writeup</em></a>!</p><p><strong><em>Backticks</em></strong> are out and the <strong><em>Pipeline Pattern</em></strong> (<em>Pipe-lining</em>) is fast becoming the defacto method to chain functions together to form easy to read, easy to refactor and easy to maintain code. I first learned the <a href="https://www.r-statistics.com/2014/08/simpler-r-coding-with-pipes-the-present-and-future-of-the-magrittr-package/">Pipeline Pattern</a> using R (introduced by <a href="http://hadley.nz/">Hadley Wickham</a>) which made code extremely easy to read. I look forward to seeing this pattern adopted across the elm and elm community packages.</p><p>The <a href="https://github.com/NoRedInk/elm-decode-pipeline/tree/master"><strong><em>Json Decode Pipeline API</em></strong></a> as you may remember from my <a href="https://medium.com/@kkruups/elm-insights-c74047637206#.5hykvoitj">previous article</a> had Pipe-lining baked into it right from the start. This API was initially released earlier this year by <a href="https://www.noredink.com/"><strong><em>NoRedInk</em></strong></a>. This library was created to make it easy to build decoders using the forward Pipe Operator, <a href="http://package.elm-lang.org/packages/elm-lang/core/5.0.0/Basics#|&gt;">(|&gt;)</a>.</p><p><strong><em>elm version 0.18</em></strong> extends this pattern to the <a href="http://klaftertief.github.io/elm-search/?q=andThen"><em>andThen</em></a>function found in each of the <strong><em>Elm Core Library modules</em></strong> <em>Decoder</em>, <em>Generator</em>, <em>Maybe</em>, <em>Result</em>, and <em>Task</em>. Many of the Elm-Community Packages and other 3rd Party Packages have also followed suit. We will definitely, see this Pattern permeate throughout.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/163/1*OI57bSJATnUqnHEuV7Ncmw.png" /><figcaption>Farewell Backticks</figcaption></figure><p>Folks might be wondering, why this change?</p><p>Well, for <strong><em>elm version 0.18</em></strong>, the consensus appears to have been <a href="https://github.com/elm-lang/elm-platform/blob/master/upgrade-docs/0.18.md#backticks-and-andthen">removing the the Backticks as an infix operator,</a> in favor of Pipe-lining via the Pipe Operator because Backticks were redundant with the Pipe Operator, easily confused with single quotes depending on specific font used , and not as user friendly or popular as Pipe-lining (many languages nowadays support Pipe-lining).</p><h4>andThen &amp; Task.onError</h4><p>The functions affected are the <em>andThen</em>functions and the Task.onError function, but in order to do so, the function’s argument order from <strong><em>elm version 0.17</em></strong> had to be changed in <strong><em>elm version 0.18</em></strong>, since the <em>andThen</em> function and most functions supporting Backticks, `f`, were written with the <strong><em>first operation</em></strong> to be initiated in the<strong><em> first argument position </em></strong>and the <strong><em>callback</em></strong> in the <strong><em>second position</em></strong> in line with the natural order of their use and specifically, with <em>andThen</em>, in keeping with its namesake.</p><p>Below we compare a generic <strong><em>Backtick Operator supported function </em></strong>in elm version 0.17 to a generic <strong><em>Pipeline-friendly function</em></strong> in elm version 0.18 to understand what has exactly changed. Then we show how to roll our own for functions that do not support Pipe-lining.</p><pre><strong>Legend</strong></pre><pre>f: a -&gt; ( a  -&gt;  b ) -&gt; c         -- elm version 0.17</pre><pre>first_fx = result<br>second_fx = callback</pre><blockquote><strong>elm version 0.17</strong></blockquote><pre><strong><br>f</strong> first_fx second_fx     -- normal function order</pre><pre>first_fx <strong>`f`</strong> second_fx   -- Backticks infix Operator </pre><p>You could do the following in <strong><em>elm version 0.17</em></strong>, but this would not make much logical sense nor would it be easy to read if there were more than one pipe in the Pipeline.</p><pre>second_fx <br>      |&gt; andThen first_fx           --elm 0.17 (makes no sense)</pre><p>We can easily see that for functions which have their callback, or second operation as the second argument prevents the Pipe Operator from being used practically or sensibly.</p><p>Therefore, for<strong><em> elm version 0.18,</em></strong> <em>andThen</em>had its arguments switched accordingly.</p><blockquote><strong><em>elm version 0.18</em></strong></blockquote><pre><strong><em>f</em></strong>:( a -&gt; b) -&gt; a -&gt; c<br> <br><strong><em>f</em></strong> second_fx first_fx     -- arguments order switched</pre><pre>first_fx<br>        <strong>|&gt; <em>f</em> </strong>second_fx      -- Pipeline Pattern (no Backticks)<br>        </pre><p>It looks like there will be more functions which will support Pipe-lining.</p><h4>Rolling Your Own Pipeline Friendly Function</h4><p>In the meantime, if there are any functions that do not support Pipe-lining and you want to provide this feature you can always use the <em>flip</em> function to do so.</p><p>For example, in <strong><em>elm version 0.17</em></strong>, <em>Result.andThen</em> does not support <strong><em>Pipe-lining</em></strong> as discussed above. However if flip is used as shown below:</p><pre><strong><em>piped_andThen</em></strong> x y = <br>                 <em>flip</em> Result.andThen x y</pre><p>this new wrapped <strong><em>andThen </em></strong>for elm version 0.17, <em>piped_andThen</em>, now supports Pipe-lining as shown below:</p><pre>toFloatnCheckBetweenZeroAnd100 str =<br>              |&gt; String.toInt str <br>              |&gt; <strong><em>piped_andThen</em></strong> (\x -&gt; String.toFloat (toString x)) <br>              |&gt; <strong><em>piped_andThen</em></strong> isFloatBetweenZeroAnd100</pre><pre><br>-- elm-repl Output:<br>--               Ok 240 : Result String Float</pre><pre><br>-- <strong><em>Note:</em></strong> This code snippet supports <strong><em>elm v0.17</em></strong> not v0.18</pre><p>where <em>isFloatBetweenZeroAnd100 </em>is</p><pre><strong><em>isFloatBetweenZeroAnd100</em></strong>: Float -&gt; Result String Float<br><strong><em>isFloatBetweenZeroAnd100</em></strong> num =<br>    if num &gt;= 0.0 &amp;&amp; num &lt;= 100.0 then<br>         Ok (num * 10) <br>    else <br>         Err “Not a number between 0.0 and 100.0”</pre><p>We can see what <a href="http://package.elm-lang.org/packages/elm-lang/core/5.0.0/Basics#flip"><strong><em>flip</em></strong></a> does by just looking at its function annotation.</p><pre><strong><em>flip:</em></strong> (a -&gt; b -&gt; c) -&gt; (b -&gt; a -&gt; c)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*1M4Wg2EPCyNBN0XIw5tCpA.jpeg" /><figcaption>Hello Pipeline!</figcaption></figure><h4>Recap</h4><p>All that changed was the order of arguments the actual body of the function definitions did not change nor need not change to support Pipe-lining.</p><p>So, we now have Pipe-lining straight out of the box for <em>andThen</em> and <em>Task.onError,</em> expect to see more functions support Pipe-lining down the road, in the meantime roll your own. If you want to kick the tires, with a <strong><em>Result.andThen</em></strong> example using <strong><em>elm version 0.18</em></strong>, you can grab the source from<a href="https://gist.github.com/kkruups/472e5ba095eddaacfab72d3f0f9ba2d9"> here</a>.</p><p>Let me know what you think about Pipe-lining.</p><p>Do you like Pipe-lining? Or absolutely hate it?</p><p><em>If you liked this, click the💚 below so other people will see this here on Medium.</em></p><h4>Links of Interest</h4><blockquote><a href="https://www.r-statistics.com/2014/08/simpler-r-coding-with-pipes-the-present-and-future-of-the-magrittr-package/">Pipe-lining in R</a></blockquote><blockquote><a href="https://mail.python.org/pipermail/python-ideas/2007-January/000054.html">Case for Removing Backtick in Python</a></blockquote><blockquote><a href="https://github.com/elm-lang/elm-platform/blob/master/upgrade-docs/0.18.md#backticks-and-andthen">elm version 0.18 upgrade docs: Backticks and andThen</a></blockquote><blockquote><a href="http://klaftertief.github.io/elm-search/?q=andThen">andThen functions affected</a></blockquote><blockquote>Previous article: <a href="http://A Road to a Better Understanding: How the elm JSON API, Elm-Decode-Pipeline and Json.Decode API Work"><em>A Road to a Better Understanding: How the elm JSON API, </em><strong><em>Elm-Decode-Pipeline</em></strong><em>and </em><strong>Json.Decode API </strong><em>Work</em></a></blockquote><blockquote><a href="https://gist.github.com/kkruups/472e5ba095eddaacfab72d3f0f9ba2d9">example source code</a></blockquote><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c067c66161bb" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[elm Insights]]></title>
            <link>https://medium.com/@kkruups/elm-insights-c74047637206?source=rss-361270501b55------2</link>
            <guid isPermaLink="false">https://medium.com/p/c74047637206</guid>
            <category><![CDATA[elm]]></category>
            <category><![CDATA[elm-lang]]></category>
            <category><![CDATA[api]]></category>
            <category><![CDATA[json]]></category>
            <category><![CDATA[kkruups]]></category>
            <dc:creator><![CDATA[KKruups]]></dc:creator>
            <pubDate>Thu, 17 Nov 2016 02:00:18 GMT</pubDate>
            <atom:updated>2016-11-20T04:35:37.558Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/193/1*FjRiKHBHv8Y8hAf4YHim_Q.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/171/1*m8nyH9j1chfyMCGNIdSRHw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/193/1*CaIHm4Mx8NclFbfceE-cLg.png" /><figcaption>elm Insights</figcaption></figure><blockquote>A Road to a Better Understanding: How the elm JSON API, <strong>Elm-Decode-Pipeline</strong><em> </em>and <strong><em>Json.Decode API </em></strong>Work</blockquote><blockquote>Articles about the elm programming language</blockquote><p>This article was inspired by two excellent blog posts by <strong><em>Brian Hicks</em></strong>, <a href="https://www.brianthicks.com/post/2016/10/17/composing-decoders-like-lego/">“<strong>Composing Decoders Like Lego</strong>”</a> and <a href="https://www.brianthicks.com/post/2016/08/22/decoding-large-json-objects-a-summary/">“<strong>Decoding Large JSON Objects: A Summary</strong>”</a>. I found these two articles very good in explaining how to use the various elm JSON API. However, I wanted to know more than just how to use these API. I wanted to understand the why, so I could better understand the Design choices and the structure of these API.</p><p>This article is an exploration to that end. I hope you find it helpful. I would love your feedback and if there are some dicey or dodgey areas please let me know.</p><p>Before you begin I would recommend you get yourself a nice tall drink of your favorite elixir, sit back, and relax because its going to be a very long read! Cheers, Enjoy!</p><p>—<strong><em> I am new to medium, this is my first article, so I will be the first to say the formatting is not best it could be.</em></strong></p><p>Btw, while I was working on this article I was using elm release v 0.17, however there has been a new elm release version 0.18 since then, so, I have also indicated the needed changes. The source code for this article can be found <a href="https://gist.github.com/kkruups/89ae8e58f54bb9b15357406abc914caf"><strong><em>here</em></strong></a><strong><em> </em></strong><em>(copy-n-paste the source code into </em><a href="http://elm-lang.org/try"><strong><em>elmO</em></strong></a><em> (Elm Online Editor))</em>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*hEYn7BLbOSIWBBRwqbWQ6w.jpeg" /></figure><h4>Now Let’s Begin!</h4><p>This article will make use of both the <a href="http://package.elm-lang.org/packages/elm-lang/core/4.0.5"><strong><em>Json.Decode API</em></strong></a>, part of the the standard elm Core Library version 4.0.5 (elm version 0.17), and the<a href="https://github.com/NoRedInk/elm-decode-pipeline/tree/2.0.0"> <strong><em>Elm-Decode-Pipeline</em></strong> <strong><em>API</em></strong></a><strong><em>, </em></strong>an elm 3rd Party Package Library created by <a href="https://www.noredink.com/"><strong><em>NoRedInk</em></strong></a>,<strong><em> </em></strong>to illustrate<strong><em> </em></strong>how to use learning techniques such as <strong><em>decomposition</em></strong> and <strong><em>substitution</em></strong> to quickly learn “<strong><em>how to use</em></strong>” an elm API and gain insights into “H<strong><em>ow an elm API works</em></strong>”.</p><p>The<em> </em><strong><em>Json Decode API </em></strong>simplify decoding JSON Objects into elm data types. For a concrete example, we have <strong>Exhibit A </strong>(see below) which we want to transform into <strong>Exhibit B</strong> using the elm programming language. In order to deepen our understanding we will look at two ways of decoding <em>Exhibit A </em>into <em>Exhibit B</em>, first,we will use only the <strong><em>Json.Decode AP</em></strong>I and then the second time around we will use <strong><em>Json.Decode.Pipeline API (Pipeline API) </em></strong>to demonstrate how much easier it is to extract/decode multiple JSON Fields from a JSON Object without the limitations of the first way which we will discuss here.</p><p><strong>Exhibit A:</strong> JSON Object representation</p><pre>{</pre><pre>   “id”: 123,</pre><pre>   “name”: “samiam”,</pre><pre>   “email”: “samiam@exzamp.com”</pre><pre>}</pre><p><strong>Exhibit B:</strong> elm Record data type (represented by <strong>User , </strong>an elm <strong><em>type alias </em></strong>for a elm record containing these 3 items, defined in<strong><em> Exhibit C</em></strong> below.)</p><pre>{</pre><pre>  id = 123</pre><pre>  name = “samiam”</pre><pre>  email= “samiam@exzamp.com”</pre><pre>}</pre><p>For this specific case, the <strong><em>Json.Decode API</em></strong> would typically entail using the <a href="http://package.elm-lang.org/packages/elm-lang/core/3.0.0/Json-Decode#object3"><strong><em>Json.Decode.object3</em></strong></a><em> </em>function, since there are <strong><em>3 items</em></strong> we are interested in extracting from the JSON object. This function creates a decoder which extracts and transforms the fields from a JSON Object into a user defined elm Data Type, in this case an elm Record data type.</p><p>This is is just one of the many <strong><em>Json.Decode</em></strong> (Json.Decode.objectN) functions used to decode JSON objects into elm data types, depending on the number N fields one may wish to decode — the suffix N represents the number of fields in the JSON object to decode. The Json.Decode API provides several functions ranging from N=1, object1, to N=8, object8, for decoding from 1 to up to 8 desired fields in a JSON Object. <em>We can see that this could quickly lead to a limitation if we wanted to decode more than eight (8) fields in a JSON object. There are many clever ways to work around these limitation just using the </em><strong><em>Json.Decode API</em></strong><em>, however, we will not discuss them here, instead we will use the </em><strong><em>Pipeline API</em></strong><em> to show how it overcomes this limitation.</em></p><p>The <strong><em>Pipeline API</em></strong>, which I will discuss shortly, provides an alternate way to easily extend this limit. But before I begin discussing this API which is complementary to the Json.Decode API, I would like to provide the background we will need to understand both its use and its inner workings.</p><p>Let’s begin by decoding a JSON Object by limiting our use to only the <strong><em>Json.Decode API</em></strong>. Decoding a JSON Object would probably proceed by first testing a potential decoder before wiring it to some live JSON data (from internet or other source) as illustrated below:</p><p>We will rely heavily on <strong><em>elm function</em></strong> <strong><em>annotations</em></strong> to help us <strong><em>decompose</em></strong> functions into their component parts and use <strong><em>substitution</em></strong> to map/trace out the relationships between each function, its arguments, inputs and outputs to help better our understanding of<strong><em> elm Json Decode API.</em></strong></p><p>Let’s start by identifying the elm code elements we will be need to transform Exhibit A to Exhibit B.</p><h4><strong>Inputs:</strong></h4><ol><li><strong><em>import</em></strong> statement —to provide access to functions and elm data types in Json.Decode API</li><li><strong><em>tagger </em></strong>— function which both defines the data type and generates User Records</li><li><strong><em>Decoders</em></strong>— multiple decoders, a Tagger (User Record) decoder, converts a tagger into a tagger decoder, and corresponding decoders (Value Decoders) for each item in the tagger decoder corresponding to the data types in the JSON Object</li><li><strong><em>JSON Object</em></strong> — A String containing the items (fields) to decode into elm data type(s)</li></ol><h4><strong>Outputs:</strong></h4><ol><li><strong><em>User Record Decoder</em></strong> — decoder (the function returned by Json.Decode.object3 ) which converts JSON into elm data type (User Record)</li><li><strong><em>Result: (Ok User)</em></strong> — desired User Record wrapped in a<strong><em> Result</em></strong> elm data type (the data type returned by <strong><em>Json.Decode.decodeString</em></strong>)</li></ol><p><strong>Exhibit C:</strong></p><pre><br>import Json.Decode as Decode</pre><pre><br>type alias User = { <br>                    id : Int<br>                   ,name : String<br>                   ,email : String<br>            }</pre><pre>Decode.decodeString (</pre><pre>  Decode.object3</pre><pre>    User</pre><pre>     (“id” := Decode.int)         -- or ((:=) “id” Decode.int)</pre><pre>     (“name” := Decode.string)    -- or((:=) “name” Decode.string)</pre><pre>     (“email”:=Decode.string)     -- or((:=) “email” Decode.string)</pre><pre>)</pre><pre>“““{“id”: 123, “name”: “samiam”, “email”: “<a href="/service/mailto:sam@example.com">samiam@exzamp.com</a>”}”””<br></pre><p>The <strong><em>Decode.decodeString</em></strong> function returns a</p><pre>(<strong><em>Result (Err error) (OK payload)</em></strong>)</pre><p>a <a href="http://package.elm-lang.org/packages/elm-lang/core/3.0.0/Result#Result"><strong><em>Result</em></strong></a><strong><em> </em></strong>elm data type, where the <strong><em>payload</em></strong> in this specific case is a User record, an<strong><em> elm Record </em></strong>data type.</p><p><strong>Compare <em>decodeString</em> Output below to the Json String</strong></p><h4><strong>elm-repl Output:</strong></h4><pre><strong><em>Ok { id = 123, name = “samiam”, email = “</em></strong><a href="/service/mailto:sam@example.com"><strong><em>samiam@exzamp.com</em></strong></a><strong><em>” }</em></strong> <br>: Result.Result String Repl.User  </pre><h4><strong>Json String:</strong></h4><pre><strong><em>“““{“id”: 123, “name”: “samiam”, “email”: “</em></strong><a href="/service/mailto:sam@example.com"><strong><em>samiam@exzamp.com</em></strong></a><strong><em>”}”””</em></strong></pre><h4>How to represent Json Strings for testing your decoder</h4><blockquote>— Multi-line strings are convenient for avoiding the need to escape Json Strings as required for Single-line JSON Strings when exploring or testing code, since elm Multi-line strings automatically escape the in-lined strings in the JSON Object. Multi-line strings are denoted by a string preceded with 3 open quotes followed by 3 closing quotes as illustrated above and below.</blockquote><pre>“““{“id”: 123, “email”: “<a href="/service/mailto:sam@example.com">sam@example.com</a>”}”””</pre><blockquote>or equivalent Single-line Json String:</blockquote><pre>“{\“id\“: 123, , \“name\“: \“<a href="/service/mailto:sam@example.com">sam</a>iam\“, \“email\“: \“<a href="/service/mailto:sam@example.com">sam@exzamp.com</a>\“}”</pre><h4>How to retrieve the data payload from the Result data type</h4><p><strong>Note:</strong> To retrieve record data we would use either use <a href="http://package.elm-lang.org/packages/elm-lang/core/3.0.0/Result#withDefault"><strong><em>Result.withDefault</em></strong></a><strong><em> </em></strong>function<strong><em> </em></strong>or the <a href="https://github.com/izdi/elm-cheat-sheet#case-of"><strong><em>case..of </em></strong></a>expression<strong><em> </em></strong>pattern from the returned Result data type. For example:</p><pre><strong><em>Result.withDefault <br>   {id = 0, name = “none”, email = “none” } <br>   (decodeString user_decoder json_string )</em></strong></pre><p>or alternatively using <strong><em>&lt;|</em></strong> <strong><em>elm pipe operator </em></strong>instead of <strong><em>parenthesis ( )</em></strong></p><pre><strong><em>Result.withDefault  <br>   {id = 0, name = “none”, email = “none” } <br>   &lt;| decodeString user_decoder json_string</em></strong></pre><p>or make use of <strong><em>case..of </em></strong>pattern to retrieve the payload</p><pre><strong><em>someJsonHandler</em></strong> json_string =</pre><pre>      result = <strong><em>Decode.decodeString</em></strong> user_decoder json_string</pre><pre>     <strong><em> case </em>result<em> of</em></strong></pre><pre>      <strong> Ok</strong> <em>payload</em> -&gt;</pre><pre><strong><em>              payload</em></strong></pre><pre>        <strong>Err</strong> error_string-&gt;</pre><pre>              <strong>error_string</strong></pre><p>where <strong>result = (Ok payload)</strong>, if decoding is <strong><em>successful </em></strong>or Err error_string, if decoding fails.</p><p>Lets <strong>decompose</strong> the <strong><em>Decode.decodeString</em></strong> function and make some <strong>substitutions</strong> for a better understanding of how this function works.</p><p>The <strong><em>Decode.decodeString</em></strong> function takes two arguments a <strong><em>Decoder </em></strong>and a <strong><em>JSON Object</em></strong>. It is used to extract and decode the selected fields from the <strong><em>JSON Object </em></strong>into an<strong><em> Elm Object</em></strong>, a <strong><em>User Record</em></strong> in this specific case. This function returns a <strong><em>User Record</em></strong> wrapped in an elm <strong><em>Result</em></strong> data type. The <strong><em>User Record</em></strong> is the<strong><em> payload </em></strong>of a successful decoding or an<strong><em> Error Message</em></strong>, a <strong><em>String</em></strong>, if a failure occurs during the decoding.</p><p>— — — — — — — — — — — — — — — — — — — — — — — — —</p><p><strong><em>Legend</em></strong></p><pre><strong>Where </strong></pre><pre><strong><em>A</em></strong> (<strong>Generic:</strong> function annotation) </pre><pre><strong>== <em>B</em></strong> (<strong>Specific:</strong> with substitutions)</pre><pre><strong>== <em>C</em></strong> (<strong>Code Execution</strong>: function passed corresponding args)<br></pre><pre><strong><em>B</em></strong> here is only a device to aid understanding not actual code.</pre><p>— — — — — — — — — — — — — — — — — — — — — — — — — -</p><pre><strong>A:</strong> <strong><em>decodeString</em></strong>: Decoder a <br>                     -&gt; <a href="http://package.elm-lang.org/packages/elm-lang/core/3.0.0/Json-Decode#Value">Value</a> <br>                     -&gt; <a href="http://package.elm-lang.org/packages/elm-lang/core/3.0.0/Result#Result">Result</a> String a</pre><pre><strong>B:</strong> <strong><em>decodeString: </em>User_Decoder</strong> <br>                    -&gt; <strong>Json_String</strong> <br>                    -&gt; <strong><em>Result R_Err R_User</em></strong> </pre><pre>-- where (R_Err: <strong>Err (error:String) </strong>or R_User: <strong>Ok (Record:User)</strong> )</pre><pre><strong>C:</strong> <strong><em>decodeString user_decoder json_string</em></strong> </pre><pre>-- (user_decoder and json_strings variables representing the decoder and the Json String to decode)</pre><h4>Creating the User Decoder (user_decoder)</h4><p>— — — — — — — — — — — — — — — — — — — — — — — — — — — — — — —</p><p><strong>Note:</strong> As of <strong>elm version 0.18 </strong>the<strong> Json.Decode.object3 </strong>function has been removed and replaced with <strong><em>Json.Decode.map3</em></strong>. All the <strong><em>objectN</em></strong> functions have been replace with <strong><em>mapN</em></strong> functions. However, there input/output annotations have not changed. This tutorial is written assuming elm version 0.17, however updating changes to 0.18 are trivial and are provided.</p><p>— — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — -</p><p>However, before we can apply the<strong><em> decodeString</em></strong> function we must first construct the <strong><em>User Decoder</em></strong>, consisting of the<strong><em> tagger</em></strong> and the <strong><em>JSON Field Decoders</em></strong>, fieldname := valueDecoder , the fields we want to extract from <strong><em>Json Object (Note: Field Decoder </em></strong>means<strong><em> </em></strong>this entire expression <strong><em>(fieldname := valueDecoder))</em></strong>. In order to construct the User decoder we will make use of the Json.Decode.object3 function<strong><em> </em></strong>. The User Decoder, user_decoder , will consists of the following elm elements:</p><p>— — — — — — — — — — — — — — — — — — — — — — — — — -</p><pre><strong><em>user_decoder =<br>          object3 User <br>                  (“id”:=int) <br>                  (“name”:=string) <br>                  (“email”:=string)</em></strong></pre><p>— — — — — — — — — — — — — — — — — — — — — — — — — -</p><h4>object3 + 4 args: (tagger) and (3 decoders):</h4><pre><strong><em>object3 (tagger) <br>        (field_decoder a) <br>        (field_decoder b) <br>        (field _decoder c)</em></strong></pre><pre><strong><em><br>{--   </em></strong></pre><pre><strong><em>    == object3 User (“id”:=int) (“name”:=string) (“email”:=string)  </em></strong></pre><pre><strong><em>--}</em></strong></pre><p>Let’s decompose user_decoder a bit further, by looking more closely at the <strong><em>tagger</em></strong> function, <em>User</em>.</p><pre><strong><em>Json.Decode.object3</em></strong></pre><pre><strong><em>User: </em></strong>(a -&gt; b -&gt; c -&gt; value) --(generic annotation)</pre><pre>{--<br>    where, </pre><pre><strong><em>         User</em></strong> <br>         == (Int -&gt; String -&gt; String -&gt; User)</pre><pre><em>         == User Int String String  -- Tagger (User Constructor)</em></pre><pre><em>--} </em></pre><p><em>(here we do the type substitution, again this is not actual code, but a device to aid understanding, below is the actual code)</em></p><p><em>where the </em><em>User is constructor function, typically called a tagger, can be used to construct a </em><strong><em>User</em></strong><em> </em><strong><em>Record</em></strong><em>:</em></p><pre><strong><em>User 234 “julia” “julia@me.com”</em></strong></pre><p><em>creates the record,</em></p><pre><strong><em>{id = 234, name = “julia”, email = “julia@me.com”}</em></strong></pre><pre>(“id” := Json.Decode.int)         --  == (maps to <a href="http://package.elm-lang.org/packages/elm-lang/core/3.0.0/Json-Decode#Decoder">Decoder</a> a )</pre><pre>(“name” := Json.Decode.string)    --  == (maps to <a href="http://package.elm-lang.org/packages/elm-lang/core/3.0.0/Json-Decode#Decoder">Decoder</a> b)</pre><pre>(“email” := Json.Decode.string)   --  == (maps to <a href="http://package.elm-lang.org/packages/elm-lang/core/3.0.0/Json-Decode#Decoder">Decoder</a> c )</pre><p>because<strong><em> </em></strong><em>Json.Decode.object3</em> requires these arguments, first arg is a <strong><em>tagger </em></strong>(<strong><em>or </em></strong><em>a</em><strong><em> </em></strong><em>function which takes 3 elm data types inputs and produces a desired </em><strong><em>value </em></strong><em>which will be used to create the user defined </em><strong><em>value decoder</em></strong><em> to decode the JSON Object)</em><strong><em> </em></strong>and <strong><em>3 decoders</em></strong> one each for the corresponding fields in the JSON Object.</p><p>The <strong><em>tagger/function </em></strong>along with the<strong><em> 3 decoders </em></strong>are used to generate the desired <strong><em>User Defined Value Decoder</em></strong> (the Decoder for the JSON Object), in our case the <strong><em>User Decoder</em></strong>, user_decoder :</p><pre><strong>object3</strong> : (a -&gt; b -&gt; c -&gt; value)</pre><pre>                 -&gt; <a href="http://package.elm-lang.org/packages/elm-lang/core/3.0.0/Json-Decode#Decoder">Decoder</a> a</pre><pre>                 -&gt; <a href="http://package.elm-lang.org/packages/elm-lang/core/3.0.0/Json-Decode#Decoder">Decoder</a> b</pre><pre>                 -&gt; <a href="http://package.elm-lang.org/packages/elm-lang/core/3.0.0/Json-Decode#Decoder">Decoder</a> c</pre><pre>                 -&gt; <a href="http://package.elm-lang.org/packages/elm-lang/core/3.0.0/Json-Decode#Decoder">Decoder</a> value</pre><h4>The 3 Decoders (Decoder a, Decoder b, Decoder c)</h4><p>— — — — — — — — — — — — — — — — — — — — — — — — — — — — — — —</p><p><strong>Note:</strong> As of <strong>elm version 0.18 </strong>the <strong><em>(:=)</em></strong>operator has been removed and replaced it with <strong>field</strong> function which is not an infix function, therefore it will only accept its two (2) arguments in the post position,</p><pre><strong><em>field </em></strong><em>string_arg decoder_arg</em></pre><p>This tutorial is written assuming elm version 0.17, but to update to 0.18 the code changes are trivial and are provided.</p><p>— — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — -</p><p>All 3 decoders make use of <strong><em>(:=)</em></strong> <strong><em>JSON Field Decoder Operator</em></strong>. This operator is a function which decodes the field with the given name in the <strong><em>JSON Object</em></strong> or fails if it does not exist. It takes two arguments, a <strong><em>String</em></strong> and <strong><em>Decoder (value Decoder)</em></strong>. These three individual decoders (<strong><em>Field Decoders</em></strong>) are required to create the user defined value decoder using <strong><em>Json.Decode.object3 </em></strong><em>(or Json.Decode.map3 in elm v0.18)</em><strong><em> </em></strong>necessary to decode the JSON Object into a User Record.</p><p><strong><em>Note</em></strong> the distinction between <strong><em>Field Decoders </em></strong>(made up of a string and a value decoder, i.e. fieldDecoder == (&quot;id&quot; := Json.Decode.int) , <strong><em>Value Decoders </em></strong><em>valueDecoder == Json.Decode.int </em>and <strong><em>User Defined Decoder</em></strong> (our desired end product user_decoder, the result of applying the <strong><em>Json.Decode.objectN</em></strong> function)</p><p><strong>Json Field Decoder Operator Annotation:</strong></p><pre><strong><em>(:=): String -&gt; Decoder a -&gt; Decoder a</em></strong></pre><p>It can be used as an infix operator where its arguments position relative to the operator are the following:</p><p><em>(arg1:String) </em><strong><em>operator</em></strong><em> (arg2: Decoder)</em></p><pre><em>“int” </em><strong><em>:=</em></strong><em> Json.Decode.int</em></pre><p>or</p><p><strong><em>operator</em></strong> <em>(arg1:String) (arg2:Decoder)</em></p><pre><strong><em>(:=)</em></strong> <em>“int” Json.Decode.int</em></pre><p>— — — — — — — — — — — — — — — — — — — — — — — — — — — — — —</p><p><strong><em>Note:</em></strong> As of elm version 0.18,<strong><em> Json.Decode.field</em></strong> function constrains its arguments to this second format :</p><pre><strong> field</strong> <em>arg1 arg2</em> </pre><pre><br>{--<br>      <br>       == field string decoder <br>       == <strong>field <em>“id” Json.Decode.int</em></strong> </pre><pre>--}</pre><p>— — — — — — — — — — — — — — — — — — — — — — — — — — — — — —</p><h3>Recap</h3><blockquote>Up to this point we decomposed functions into there component parts and made several substitutions to trace out all the inputs and outputs necessary to create a <strong><em>User Decoder</em></strong> and return a<strong><em> User Record</em></strong> from the<strong><em> JSON Object</em></strong> using the Decoder we created from applying the<strong><em> Json.Decode.object3 </em></strong>function. We learned that if we want to extract more than 8 items from a JSON object we can’t use objectN without some clever workarounds which were not discussed here. However, now that we have sufficient background we now can introduce the <strong><em>Pipeline API</em></strong> to see how easy it is to get around this limitation.</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*hEYn7BLbOSIWBBRwqbWQ6w.jpeg" /><figcaption>Pipeline API just like a cascading waterfall</figcaption></figure><p>The <strong><em>Pipeline API </em></strong>allows us to use the elm <strong><em>Forward Pipe Operator,</em></strong> |&gt;, to build our <strong><em>User Decoder</em></strong> by passing a <strong><em>User Constructor Decoder</em></strong> (tagger Decoder) to a chain of <strong><em>Value Decoders. </em></strong>Again we will use decomposition and substitution to map/trace inputs and outputs for an understanding of how<strong><em> Pipeline API</em></strong> works. Let’s see how we would execute the same behavior using the <strong><em>Pipeline API</em></strong>:</p><pre>user_decoder = <br><strong> <em>Json.Decode.Pipeline.decode</em></strong> User <br> |&gt;<strong><em>Json.Decode.Pipeline.required</em></strong>  “login” Json.string<br> |&gt;<strong><em>Json.Decode.Pipeline.required</em></strong>  “id” Json.int <br> |&gt;<strong><em>Json.Decode.Pipeline.required</em></strong>  “name” Json.string <br> |&gt;<strong><em>Json.Decode.Pipeline.required  </em></strong>“company” Json.string <br> |&gt;<strong><em>Json.Decode.Pipeline.required  </em></strong>“blog” Json.string<br> |&gt;<strong><em>Json.Decode.Pipeline.required  </em></strong>“followers” Json.int</pre><pre>Json.decodeString user_decoder json_string</pre><h4><strong>or alternatively</strong></h4><pre>import Json.Decode (decodeString, int, string)</pre><pre>import Json.Decode.Pipeline exposing (decode, required)</pre><pre>user_decoder = <br><strong><em> decode</em></strong> User <br> |&gt;<strong><em>required</em></strong> “login” Json.string<br> |&gt;<strong><em>required</em></strong> “id” Json.int <br> |&gt;<strong><em>required</em></strong> “name” Json.string <br> |&gt;<strong><em>required </em></strong>“company” Json.string <br> |&gt;<strong><em>required</em></strong> “blog” Json.string<br> |&gt;<strong><em>required</em></strong> “followers” Json.int</pre><pre>decodeString user_decoder json_string</pre><pre>decodeString user_decoder json_string </pre><blockquote><strong>Outputs</strong> an <strong>elm User Record</strong> with 6 member values:</blockquote><pre>{<br>    <strong>login </strong>= ”julia3&quot;<br>   ,<strong>id </strong>= 234<br>   ,<strong>name </strong>= “julia”<br>   ,<strong>company </strong>= ”MusicBus”<br>   ,<strong>blog </strong>= ”https://musicbus.com/blog”<br>   ,<strong>followers </strong>= 1300<br>}</pre><p>Note: Our new User Record has been updated to accept 6 values we could go well over the 8 item without limitation of <em>Json.Decode.objectN</em> functions using this new Decoder <strong><em>Pipeline Pattern</em></strong>.</p><pre><strong>type alias</strong> User =</pre><pre>          {</pre><pre>              login: <strong>String</strong></pre><pre>              ,id : <strong>Int</strong></pre><pre>              ,name : <strong>String</strong></pre><pre>              ,company : <strong>String</strong></pre><pre>              ,blog : <strong>String</strong></pre><pre>              ,followers : <strong>Int</strong></pre><pre>}</pre><blockquote><strong>User Constructor Function(tagger):</strong></blockquote><pre><strong>User</strong> “julia3<strong><em>” </em></strong><em>234 “julia” “MusicBus” “https://musicbus.com/blog”  1300</em></pre><pre>{-- <br>   mapping to tagger&#39;s annotation represents:<br>        User String Int String String String Int<br>--}</pre><blockquote><strong>Creates a User Record:</strong></blockquote><pre>{<br>     login = ”julia3&quot;<br>     ,id = 234<br>     ,name = “julia”<br>     ,company = ”MusicBus”<br>     ,blog = ”https://musicbus.com/blog”<br>     ,followers = 1300<br>}</pre><p>The<strong><em> Pipeline API</em></strong> introduces two new helper functions, decode and required which allow us to conveniently use the <strong><em>Pipe Operator</em></strong> to chain decoders together to create a<strong><em> User Defined</em></strong> <strong><em>Value Decoder </em></strong>, or in our case, the<strong><em> User Decoder</em></strong>, user_decoder, needed to decode our JSON Object.</p><p>But first let’s take a slight detour, let’s build our own Pipeline functionality by just using <strong><em>Json.Decode API</em></strong>; we will not use the<strong><em> Pipeline API</em></strong>. This will give us a better understanding of how this new <strong><em>Decoder Pipeline Pattern</em></strong> code works under the covers.</p><p>In order to create our own Pipeline functionality will need to understand how these new functions,<strong><em> </em></strong><strong><em>decode</em></strong> and <strong><em>required</em></strong> work. The best place to look is in the <strong><em>NoRedInk’s Pipeline AP</em></strong>I <a href="https://github.com/NoRedInk/elm-decode-pipeline/blob/2.0.0/src/Json/Decode/Pipeline.elm">source code</a>. Let’s start with <strong><em>decode:</em></strong></p><pre>decode: a -&gt; Decoder a</pre><pre>decode a =</pre><pre>        succeed a</pre><p>The decode function just wraps Json.Decode.succeed<strong><em> </em></strong>function to allow it to be given a more user friendly name, appropriate to the job it is doing. It basically creates a Decoder out of anything that’s passed to it. In our case we pass our tagger, <em>User</em>,<strong><em> </em></strong>by doing so, we<strong><em> </em></strong>create the <strong><em>tagger Decoder</em></strong>, the <strong><em>User tagger Decoder</em></strong>. However, this is not yet our <strong><em>User Defined Value Decoder (User Decoder)</em></strong>. We will need to transform the <strong><em>User tagger Decoder</em></strong> into a <strong><em>User Decoder by </em></strong>adding the <strong><em>Pipe Operator</em></strong> and the other <strong><em>Pipeline</em></strong> function required into the mix. Let’s take a closer look to see how these all three work together.</p><h4>Pipe Operator</h4><p>The <strong><em>Forward Pipe Operator</em></strong> , |&gt; , is a function, found in the <strong><em>Basics</em></strong> <strong><em>module</em></strong> of the <strong>elm Core Package Library </strong>(there is also a <strong><em>Backward Pipe Operator</em></strong>, &lt;|, not discussed here). Let’s look at it’s elm annotation and function definition. This translates to,</p><p>Take any <strong><em>a </em></strong>and pass it to a function, <strong><em>f (a)= b </em></strong>,<strong><em> </em></strong>defined to<strong><em> </em></strong>take an <strong><em>a </em></strong>and return a <strong><em>b, then </em></strong>return that<strong><em> b</em></strong></p><p><strong>Decomposition of (|&gt;)</strong></p><pre><strong><em>(|&gt;) </em></strong>: <strong>a</strong> -&gt; <strong><em>(</em>a<em> -&gt; b)</em></strong> -&gt; <strong>b</strong></pre><pre>x |&gt; f = <br>        f x</pre><p><strong>Substitution :</strong></p><pre>-- i.e., x=1, f= x+1,</pre><pre>1 |&gt; f </pre><pre>{--<br>          == f 1 <br>          ==( <strong>1)</strong>+ 1 <br>          == 2<br>--}</pre><h4>Json.Decode.Pipeline.required</h4><p>Finally, let’s look at <strong><em>required.</em></strong></p><p><strong>required Function Annotation</strong></p><pre>required : String</pre><pre>            -&gt; Decoder a</pre><pre>            -&gt; Decoder (a -&gt; b)</pre><pre>            -&gt; Decoder b</pre><p><strong>required Function Definition</strong></p><pre><strong><em>required </em>key valDecoder decoder<em> = custom (</em>key<em> := </em>valDecoder<em>) </em>decoder</strong></pre><p>— — — — — — — — — — — — — — — — — — — — — — — — — — — — — — -</p><p><strong>Note:</strong> As of <strong><em>elm version 0.18 </em></strong>the function definition for <strong><em>required </em></strong>would probably be updated to replace the (:=) operator with field function in the following manner:</p><pre><strong><em>required </em>key valDecoder decoder<em> = <br>                              custom (</em>field key<em> </em>valDecoder<em>) </em>decoder</strong></pre><p>— — — — — — — — — — — — — — — — — — — — — — — — — — — — — — -</p><p>Let’s drill down deeper by decomposing <strong><em>required</em></strong> a bit further and then use substitution to understand the <strong><em>required</em></strong> function a bit better. Clearly, we can see that <strong><em>required</em></strong> accepts three (3) arguments,<em>key</em> , <em>valueDecoder</em><strong><em> </em></strong>and <em>decoder</em><strong><em> </em></strong>.</p><p><strong><em>What are these 3 arguments?</em></strong></p><p>They are,</p><h4>key,</h4><pre><strong>key</strong> <br>     {--  <br>         ==<strong><em> String</em></strong> </pre><pre>      --}   </pre><pre>{--            </pre><pre>  (name of a field in the JSON Object we want to extract)</pre><pre>--}</pre><h4>valDecoder,</h4><pre><strong>valDecoder</strong> </pre><pre>     {--   <br>         == D<strong><em>ecoder a</em></strong> </pre><pre>      --}     </pre><pre>{--         </pre><pre>(a value type decoder corresponding to the value type of              the field in JSON Object we want to extract, i.e. Json.Decode.int, etc…)<br>      --}</pre><h4>and decoder,</h4><pre>decoder </pre><pre>     {---   <br>         == <strong><em>tagger Decoder</em></strong> <br>      --}</pre><pre><br>{--<br>  (the decoder that cascades across the pipeline and is transformed from the <strong><em>tagger decoder</em></strong> step by step to the specific Value Decoder which turns a JSON Object to an elm data type, in our case, the User Decoder, user_decoder).<br>--}</pre><p>We will be able to understand <strong><em>required</em></strong> a bit better by creating our own <strong><em>Pipeline</em></strong> analogues for both <strong>decode</strong> &amp; <strong>required<em>, </em></strong>and chaining them via the<strong><em> pipe operator, </em></strong><strong><em>|&gt;</em></strong>.</p><h3><strong><em>Let’s begin</em></strong></h3><h4>Substitution &amp; Decomposition of <strong><em>decode </em></strong>&amp;<strong><em> |&gt;</em></strong></h4><p>I will create an analogue to <strong>decode</strong> function and rename it<strong><em> </em></strong><strong><em>toDecoder </em></strong>using more user friendly names for the function itself and its argument, since we are actually converting our tagger into a <strong><em>tagger Decoder</em></strong>. The <strong><em>toDecoder</em></strong> is equivalent to <strong><em>Pipeline API’s </em></strong><strong><em>decode.</em></strong></p><h4><strong><em>toDecoder Annotation &amp; Definition</em></strong></h4><pre><strong><em>toDecoder: a -&gt; Json.Decoder a</em></strong><br><strong><em>toDecoder</em></strong> tagger = <br>        Json.succeed tagger</pre><p>Now we will look at the <strong><em>User</em></strong> constructor function, in this case, our tagger function</p><p><strong><em>User Constructor Function’s Annotation (tagger):</em></strong></p><pre><strong><em>User: String -&gt; Int -&gt; String -&gt; String -&gt; String -&gt; Int -&gt; User</em></strong></pre><p>Now let’s see the transformation of the<strong><em> tagger </em></strong>into a <strong><em>tagger Decoder </em></strong>by applying <strong><em>toDecoder</em></strong> function to our <strong><em>tagger</em></strong>, <strong><em>User</em></strong>.</p><p><strong><em>(tagger Decoder):</em></strong></p><pre><strong><em>toDecoder User</em></strong></pre><pre>{--<br>    == <strong><em>Decoder (String-&gt;Int-&gt;String-&gt;String-&gt;String-&gt;Int-&gt;User)<br></em></strong>--}</pre><p>Now let’s create a analogue for <strong><em>required</em></strong> function. I don’t like the name <strong><em>required</em></strong>, since it really doesn’t explain what the function does, so I renamed it to <strong><em>pipeDecoderBldr</em></strong>, yes it verbose, but the name makes its purpose clear to me.</p><pre>import Json.Decode</pre><pre><strong>pipeDecoderBldr</strong>: String <br>                 -&gt; Decoder a <br>                 -&gt; Decoder (a -&gt; b) <br>                 -&gt; Decoder b</pre><pre><strong>pipeDecoderBldr</strong> key valueDecoder taggerDecoder <strong><em>=</em></strong></pre><pre>                   <strong>object2</strong> (|&gt;) (key := valueDecoder) taggerDecoder</pre><p><strong>Note: </strong>for elm v0.18 change <strong><em>pipeDecoderBldr </em></strong>to the following:</p><pre><strong><em>pipeDecoderBldr</em></strong> key valueDecoder taggerDecoder <strong>=</strong></pre><pre>                  map2 (|&gt;) ( field key valueDecoder) taggerDecoder</pre><p>Now if we begin the pipeline by using the <strong><em>Forward Pipe Operator </em></strong>and the <strong><em>first</em></strong> <strong><em>value</em></strong> <strong><em>Decoder </em></strong>,<strong><em> </em></strong>corresponding to the first argument of the <strong><em>tagger Decoder, </em></strong>we will begin to see the transformation of the the <strong><em>tagger Decoder</em></strong><em>.</em> The <strong><em>tagger Decoder </em></strong>is being reduced down by a chain of <strong><em>value Decoders </em></strong>with<strong><em> </em></strong>each step (while internally creating <strong><em>field Decoders </em></strong>at each step) it gets closer to becoming our <strong><em>User Defined Value Decoder (our User Decoder), </em></strong>as it does so, in the final step of the chain.</p><p>But in order to see this we must take another look at the <em>required</em>function<strong><em>.</em></strong></p><p>If we take a closer look at <em>required</em>, we see the function <em>custom</em><strong><em> </em></strong>in its function definition. This function is what helps make all the pipeline magic happen, it wraps the <em>Json.Decode.object2</em> ( or now <em>Json.Decode.map2</em> for elm v0.18) function followed by a <strong><em>Forward Pipe Operator </em></strong>function, the first argument to<strong><em> </em></strong><em>object2</em>.</p><pre><strong><em>custom</em></strong>: Decoder a -&gt; Decoder (a -&gt; b) -&gt; Decoder b</pre><pre><strong><em>custom</em></strong> = <br>          Json.Decode.object2 (|&gt;)</pre><h4><strong><em>We can see custom’s function annotation, but what’s actually going on?</em></strong></h4><p>We need to take a look at object2 and (|&gt;) function annotations side by side and do some substitution to clearly understand what is going on.</p><h4>object2 (map2 , elm v.0.18)</h4><pre><strong><em>object2</em></strong>: (a -&gt; b -&gt; value) <br>          -&gt; <a href="http://package.elm-lang.org/packages/elm-lang/core/5.0.0/Json-Decode#Decoder">Decoder</a> a <br>          -&gt; <a href="http://package.elm-lang.org/packages/elm-lang/core/5.0.0/Json-Decode#Decoder">Decoder</a> b <br>          -&gt; <a href="http://package.elm-lang.org/packages/elm-lang/core/5.0.0/Json-Decode#Decoder">Decoder</a> value</pre><h4>Forward Pipeline Operator</h4><pre><strong><em>(|&gt;)</em></strong>: a -&gt; (a-&gt;b) -&gt; b</pre><p><strong><em>object2</em></strong> takes <strong><em>3 arguments</em></strong>, a <strong><em>function</em></strong> and <strong><em>two decoders</em></strong> and returns a <strong><em>value decoder, </em></strong>a decoder for the last value of the function.</p><p><strong><em>custom </em></strong>has already curried the first argument of object2, the <strong><em>Forward Pipe Operator</em></strong> function, so in fact, via the required function we are passing the 2nd and 3rd arguments,<strong><em> </em></strong>the <strong><em>field Decoder</em></strong> and the <strong><em>tagger Decoder </em></strong>respectively<strong><em>, </em></strong>via the wrapped<strong><em> </em></strong>object2 in the customfunction.</p><p><strong><em>Let’s do the substitution</em></strong>,</p><pre>object2         (|&gt;)</pre><pre> a         ==&gt;    a</pre><pre> b         ==&gt;   (a -&gt; b)</pre><pre> value     ==&gt;   b</pre><p><strong><em>Transformation,</em></strong></p><pre>custom </pre><pre>{--<br>     ==  object2 (|&gt;)</pre><pre>     ==  object2 (a -&gt; (a-&gt;b) -&gt; b )</pre><pre>     ==  Decoder a -&gt; Decoder (a-&gt;b) -&gt; Decoder b</pre><pre>--}</pre><p>The <strong><em>custom</em></strong> function builds the <strong><em>field Decoder</em></strong> from the arguments passed into it by<strong><em> required </em></strong>and takes a <strong><em>tagger Decoder,</em></strong> then reduces the <strong><em>field Decoder</em></strong>,<strong><em> Decoder a</em></strong>, from the <strong><em>tagger Decoder</em></strong>,<strong><em> Decoder(a-&gt;b) </em></strong>, and then returns the balance of<strong><em> tagger Decoder</em></strong>,<strong><em> Decoder b</em></strong>,by using the properties of the<strong><em> Forward Operator function </em></strong>which in the final step of a pipeline chain results in the desired <strong><em>User Defined Value Decoder, </em></strong>in our case, the User Decoder (user_decoder == <strong><em>Decoder User)</em></strong>.</p><p>In our case we have not written a <strong><em>custom</em></strong> function, we have just inlined the <strong><em>object2</em></strong> function in our analogue to the <strong><em>required</em></strong> function , <strong><em>pipeDecoderBldr.</em></strong></p><pre><strong>pipeDecoderBldr</strong> key valueDecoder taggerDecoder <strong>=</strong></pre><pre>                     object2 (|&gt;) ( key := valueDecoder) taggerDecoder</pre><p>Our version of the Pipeline Decoder which creates a <strong><em>User Decoder</em></strong>, user_decoder, using only the Json.Decode API looks like this,</p><pre>user_decoder =</pre><pre><strong><em>       toDecoder</em></strong> User</pre><pre><strong>           |&gt; pipeDecoderBldr</strong> &quot;login”     Json.Decode.string</pre><pre><strong>           |&gt; pipeDecoderBldr</strong> “id”        Json.Decode.int</pre><pre><strong>           |&gt; pipeDecoderBldr</strong> “name”      Json.Decode.string</pre><pre><strong>           |&gt; pipeDecoderBldr</strong> “company”   Json.Decode.string</pre><pre><strong><em>           |&gt; pipeDecoderBldr</em></strong> “blog”      Json.Decode.string</pre><pre><strong><em>           |&gt; pipeDecoderBldr</em></strong> “followers” Json.Decode.int</pre><p>We would then finally use our pipeline <strong><em>User Decoder</em></strong> to decode our <strong><em>JSON Object</em></strong> in the same manner as before:</p><pre><strong><em>Json.Decode.decodeString user_decoder json_string</em></strong></pre><p>Let’s do a final substitution to fully understand how this all finally comes together.</p><h4>Legend:</h4><pre><strong><em>Decoder a = D_a</em></strong></pre><pre><strong><em>Decoder (a-&gt;b) = D_ab</em></strong></pre><pre><strong><em>Decoder b = D_b</em></strong></pre><pre><strong><em>pipeDecoderBldr:</em></strong> String-&gt; D_a -&gt; D_ab-&gt; D_b</pre><p>In our case, we have <strong><em>7 steps</em></strong> in our pipeline. Let’s start decomposing our <strong><em>Decoder pipeline</em></strong>. Your pipeline may have more or less steps depending on the number of arguments in your tagger function (assuming your tagger is based on number of fields you want to extract from the JSON Object).</p><h4>Pipeline: Step 1</h4><pre>user_decoder =</pre><pre><strong><em>  toDecoder User</em></strong></pre><pre><br> {--               <br>      == (a -&gt; Decoder a)</pre><pre><strong><em>     == Decoder (String-&gt;Int-&gt;String-&gt;String-&gt; String-&gt;Int-&gt; User)<br>--}</em></strong></pre><p>returns <strong><em>D_a</em></strong>, but when passed to next step in chain, it becomes <strong><em>D_ab</em></strong>, 3rd argument to<strong><em> pipeDecoderBldr, </em></strong>the<strong><em> tagger Decoder </em></strong>or balance<strong><em> of a tagger Decoder.</em></strong></p><pre>D_a</pre><pre>== <strong><em>Decoder (String-&gt;Int-&gt;String-&gt;String-&gt;String-&gt;Int-&gt;User)</em></strong></pre><pre>== D_ab</pre><h4>Pipeline: Step 2</h4><pre><strong>toDecoder User</strong></pre><pre><strong>|&gt;pipeDecoderBldr <em>“login” Json.Decode.string</em></strong></pre><p>passes</p><pre> <strong><em>D_ab</em></strong></pre><pre>==<strong><em>Decoder (String -&gt; Int -&gt; String -&gt; String -&gt; String -&gt; Int -&gt; User)</em></strong></pre><p>(remember <strong><em>String </em></strong>&amp;<strong><em> D_a</em></strong> represent the <strong><em>1st</em></strong> and<strong><em> 2nd args</em></strong> in each call to <strong><em>pipeDecoderBldr, </em></strong>these two args<strong><em> </em></strong>are<strong><em> </em></strong>used to subsequently build each <strong><em>field Decoder</em></strong> for our <strong><em>User Decoder</em></strong>)</p><p>returns</p><pre><strong><em>D_b</em></strong></pre><pre>== <strong><em>Decoder (Int -&gt; String -&gt; String -&gt; String -&gt; Int -&gt; User)</em></strong></pre><h4>Pipeline: Step 3</h4><pre><strong>|&gt;pipeDecoderBlr </strong>“login” Json.Decode.string</pre><pre><strong>|&gt; pipeDecoderBldr</strong> “id” Json.Decode.int</pre><p>passes</p><pre><strong><em>D_ab</em></strong></pre><pre><strong><em>== Decoder (Int -&gt; String -&gt; String -&gt; String -&gt; Int -&gt; User)</em></strong></pre><p>returns</p><pre><strong><em>D_b</em></strong></pre><pre><strong><em>== Decoder (String -&gt; String -&gt; String -&gt; Int -&gt; User)</em></strong></pre><h4>Pipeline: Step 4</h4><pre><strong>|&gt; pipeDecoderBldr</strong> “id” Json.Decode.int</pre><pre><strong>|&gt; pipeDecoderBldr </strong>“name” Json.Decode.string</pre><p>passes</p><pre><strong><em>D_ab</em></strong></pre><pre><strong><em>== Decoder (String -&gt; String -&gt; String -&gt; Int -&gt; User)</em></strong></pre><p>returns</p><pre><strong><em>D_b</em></strong></pre><pre><strong><em>== Decoder (String -&gt; String -&gt; Int -&gt; User)</em></strong></pre><h4>Pipeline: Step 5</h4><pre><strong>|&gt; pipeDecoderBldr </strong>“name” Json.Decode.string</pre><pre><strong>|&gt; pipeDecoderBldr </strong>“company” Json.Decode.string</pre><p>passes</p><pre><strong><em>D_ab</em></strong></pre><pre><strong><em>== Decoder (String -&gt; String -&gt; Int -&gt; User)</em></strong></pre><p>returns</p><pre><strong><em>D_b</em></strong></pre><pre>== <strong><em>Decoder (String -&gt; Int -&gt; User)</em></strong></pre><h4>Pipeline: Step 6</h4><pre><strong>|&gt; pipeDecoderBldr </strong>“company” Json.Decode.string</pre><pre><strong>|&gt; pipeDecoderBldr </strong>“blog” Json.Decode.string</pre><p>passes</p><pre><strong><em>D_ab</em></strong></pre><pre>== <strong><em>Decoder (String -&gt; Int -&gt; User)</em></strong></pre><p>returns</p><pre><strong><em>D_b</em></strong></pre><pre>== <strong><em>Decoder (Int -&gt; User)</em></strong></pre><h4>Pipeline: Step 7 (Final Step)</h4><pre><strong>|&gt; pipeDecoderBldr </strong>“blog” Json.Decode.string</pre><pre><strong>|&gt; pipeDecoderBldr </strong>“followers” Json.Decode.int</pre><p>passes</p><pre><strong><em>D_ab</em></strong></pre><pre>== <strong><em>Decoder (Int -&gt; User)</em></strong></pre><p>this final step returns our <strong><em>User Decoder</em></strong></p><pre><strong><em>D_b</em></strong></pre><pre>== <strong><em>Decoder User (Our User Decoder)</em></strong></pre><p>Let’s look at the reduction our<strong><em> tagger</em></strong> <strong><em>Decoder</em></strong> to<strong><em> User Decoder</em></strong>, as an output of the <strong><em>returned value</em></strong> for each <strong><em>chained function</em></strong> in the pipeline. This way we can clearly see the reduction.</p><pre><strong><em>Decoder (String -&gt; Int -&gt; String -&gt; String -&gt; String -&gt; Int -&gt; User)</em></strong></pre><pre>Decoder (Int -&gt; String -&gt; String -&gt; String -&gt; Int -&gt; User)</pre><pre>Decoder (String -&gt; String -&gt; String -&gt; Int -&gt; User)</pre><pre>Decoder (String -&gt; String -&gt; Int -&gt; User)</pre><pre>Decoder (String -&gt; Int -&gt; User)</pre><pre>Decoder (Int -&gt; User)</pre><pre>Decoder User</pre><p>Let’s do a recap by comparing once again the two ways to create a User defined Decoder for a JSON Object, using Json.Decode API’s <strong><em>objectN</em></strong> <strong><em>functions</em></strong> vs the <strong><em>Pipeline Pattern</em></strong>.</p><p>In our case, we wanted to decode 6 fields, so we would use the following for each method:</p><h4><strong>objectN Method</strong></h4><pre><strong><em>user_decoder</em></strong> =</pre><pre><strong><em>      object6</em></strong> tagger <br>                fieldDecoder1 <br>                fieldDecoder2 <br>                fieldDecoder3 <br>                fieldDecoder4 <br>                fieldDecoder5 <br>                fieldDecoder6</pre><p><strong>Note: </strong>where fieldDecoderN == (key := valueDecoder) ==&gt; i.e.(“login” := Json.Decode.string)</p><p>key is a String (a JSON field name, i.e., &quot;login”)</p><p><strong><em>valueDecoder</em></strong> = Json.Decode.some_data_type_name (i.e., Json.Decode.string)</p><p>, assuming use of elm v0.17, (replace object6 with map6 for elm v0.18)</p><h4>Pipeline Pattern</h4><pre><strong><em>user_decoder</em></strong> =</pre><pre><strong><em>         decode</em></strong> User</pre><pre>                     |&gt; <strong><em>required</em></strong> fieldName1 valueDecoder1</pre><pre>                     |&gt; <strong><em>required</em></strong> fieldName2 valueDecoder2</pre><pre>                     |&gt;<strong><em>required </em></strong>fieldName3 valueDecoder3</pre><pre>                     |&gt;<strong><em>required</em></strong> fieldName4 valueDecoder4</pre><pre>                     |&gt;<strong><em>required</em></strong> fieldName5 valueDecoder5</pre><pre>                     |&gt;<strong><em>required</em></strong> fieldName 6 valueDecoder6</pre><p>where <strong><em>f</em></strong><em>ieldNameN</em> is a String (a JSON field name i.e. &quot;login”) , and <strong><em>valueDecoder</em></strong> = Json.Decode.datatype_name (i.e., Json.Decode.string)</p><p>The <strong><em>objectN (mapN) </em></strong>functions return the desired <strong><em>User Defined Decoder</em></strong> directly by making use of the tagger and field Decoders, but limits the number of fields we can decode at one time.</p><p>The <strong><em>Pipeline Pattern</em></strong> uses Decode helpers,<strong><em> </em></strong><em>decode</em> and required, to allows us to easily chain the tagger to value Decoders to arrive at the User Defined Decoder without having to worry about an arbitrary maximum limit defined by <em>objectN</em><strong><em> functions</em></strong> in the <strong><em>Json.Decode API</em></strong>.</p><h4>Summary</h4><p>I hope you now have a better appreciation for the <strong><em>Pipeline API</em></strong>. I hope this article has fulfilled its mission in using decomposition and substitution as a means to help you understand<strong><em> How an elm API works</em></strong>, in this specific instance, <strong><em>How the Pipeline API works</em></strong>, and <strong><em>How it overcomes the limitations of the </em></strong><em>objectN</em><strong><em> (</em></strong><em>mapN</em><strong><em>) functions</em> </strong><em>of the</em><strong> <em>Json.Decode API</em></strong>.</p><p><strong><em>The Pipeline API</em></strong> leverages the<strong><em> Forward Pipe Operator </em></strong>to extend elm’s concise and clear Pipeline Pattern to JSON Object Decoding. I encourage you to use this API when working with <strong><em>large JSON Objects</em></strong> as does <a href="https://www.brianthicks.com/post/2016/08/22/decoding-large-json-objects-a-summary/">Brian Hicks</a> in his Blog, since the <strong><em>Pipeline API</em></strong> easily overcomes the limitations of the number of fields which can be decoded when working with very large JSON Objects as we have shown here.</p><p>P.S. Thanks to <strong><em>@xilnocas </em></strong>from <a href="https://elmlang.slack.com/messages/beginners/whats_new/"><strong>elm slack group</strong></a> and <strong><em>Duane Johnson</em></strong> from <a href="https://groups.google.com/forum/#!forum/elm-discuss"><strong>elm discuss</strong></a><strong> google group </strong>for helpful tips on formatting I was able to clean up the code snippets.</p><p>Borrowing the phrase below from <a href="https://medium.com/@cscalfani/so-you-want-to-be-a-functional-programmer-part-1-1f15e387e536#.9ecd2og28"><strong><em>Charles Scalfani</em></strong></a>, btw see his great articles on <em>Functional Programming</em> and <em>elm</em>.</p><p><em>If you liked this, click the💚 below so other people will see this here on Medium.</em></p><h3>Links:</h3><p><strong><em>Brian Hicks articles:</em></strong></p><p><a href="https://www.brianthicks.com/post/2016/10/17/composing-decoders-like-lego/">Composing Decoders Like Lego</a></p><p><a href="https://www.brianthicks.com/post/2016/10/17/composing-decoders-like-lego/">Decoding Large JSON Objects: A Summary</a></p><p><strong><em>Updated reflecting elm v 0.18:</em></strong></p><p><a href="http://package.elm-lang.org/packages/elm-lang/core/5.0.0/Json-Decode">Json.Decode API</a></p><p><a href="https://github.com/NoRedInk/elm-decode-pipeline/blob/master/src/Json/Decode/Pipeline.elm">Pipeline API</a></p><p><a href="http://package.elm-lang.org/packages/elm-lang/core/latest/">Elm Core Libraries</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c74047637206" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>