<?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>DEV Community: Rafi Panoyan</title>
    <description>The latest articles on DEV Community by Rafi Panoyan (@rafipanoyan).</description>
    <link>https://dev.to/rafipanoyan</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%2F205536%2F58cd06d3-dc90-4e36-bcfe-05235c600765.jpg</url>
      <title>DEV Community: Rafi Panoyan</title>
      <link>https://dev.to/rafipanoyan</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/rafipanoyan"/>
    <language>en</language>
    <item>
      <title>Higher-order functions in Kotlin</title>
      <dc:creator>Rafi Panoyan</dc:creator>
      <pubDate>Thu, 04 Jun 2020 08:12:41 +0000</pubDate>
      <link>https://dev.to/rafipanoyan/higher-order-functions-in-kotlin-1o15</link>
      <guid>https://dev.to/rafipanoyan/higher-order-functions-in-kotlin-1o15</guid>
      <description>&lt;p&gt;Kotlin is a language where functions are first class citizens. This allow us to manipulate them as we please.  &lt;/p&gt;

&lt;p&gt;Today we will see what &lt;strong&gt;higher-order functions&lt;/strong&gt; are, why do they matter and how to create them in Kotlin.  &lt;/p&gt;

&lt;h2&gt;
  
  
  My functions are High
&lt;/h2&gt;

&lt;p&gt;In functional programming (FP for the rest of the post), as the name suggests, functions are the base building blocks of the code. &lt;/p&gt;

&lt;p&gt;Often in OOP, a function takes some data as an input, process them, and returns an object (representing some other data).&lt;/p&gt;

&lt;p&gt;In FP you can also find &lt;em&gt;higher-order functions&lt;/em&gt; : functions that returns another function &lt;strong&gt;or&lt;/strong&gt; functions that take one or more function as arguments.  &lt;/p&gt;

&lt;h3&gt;
  
  
  Show me the code
&lt;/h3&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;Step by step, here is what's happening :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;multiplyBy&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;multiplier&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Int&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;Int&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="n"&gt;value&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="p"&gt;*&lt;/span&gt; &lt;span class="n"&gt;multiplier&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;If we look at the &lt;code&gt;multiplyBy&lt;/code&gt; function signature and return type, we can tell that :&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;it takes an &lt;code&gt;Int&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;it returns a &lt;code&gt;function that takes an Int and returns an Int&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Returning a function is a typical signature of a higher-order function.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;multiplyBy&lt;/code&gt; body consist of returning a new function which takes a &lt;code&gt;value&lt;/code&gt; in parameter and returns that value multiplied by our &lt;code&gt;multiplier&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;by3&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;multiplyBy&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;/code&gt;&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;Here we call our higher-order function to create a new function named &lt;code&gt;by3&lt;/code&gt; which will multiply by 3 every parameter we give it.&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;result&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;by3&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;Clearly, we execute &lt;code&gt;by3&lt;/code&gt; with 5 as a parameter. The result is 15.&lt;/p&gt;
&lt;h3&gt;
  
  
  Ok but, why ?!
&lt;/h3&gt;

&lt;p&gt;If you would have asked me this question yesterday, I would have said... I don't know.&lt;/p&gt;

&lt;p&gt;I always liked this "function that creates other function" principle in FP, but never seemed to exceed the academic meaning of it.&lt;/p&gt;

&lt;p&gt;Yes, you can now write this :&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;by5&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;multiplyBy&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;but in a real application, what's the matter ?&lt;/p&gt;

&lt;p&gt;Unless...&lt;/p&gt;
&lt;h2&gt;
  
  
  My functions are Small
&lt;/h2&gt;

&lt;p&gt;After many years studying FP, writing higher-order functions (HOF for the rest of the post) in Clojure, Haskell or Kotlin, just for the sake of learning a new language, I finally came across a real use case where it saved me a good amount of duplicate code. &lt;/p&gt;

&lt;p&gt;I will now develop a concrete (and very simplified) example from an Android application I'm developping.&lt;/p&gt;
&lt;h3&gt;
  
  
  Context
&lt;/h3&gt;

&lt;p&gt;This application holds a list of dated objects (&lt;code&gt;Record&lt;/code&gt;). The user should be able to filter these records by year and by month independently.  &lt;/p&gt;

&lt;p&gt;Given this list : &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Record 1 : 1st january 2019&lt;/li&gt;
&lt;li&gt;Record 2 : 1st march 2020&lt;/li&gt;
&lt;li&gt;Record 3 : 1st may 2020&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I should get these filters : &lt;/p&gt;

&lt;p&gt;Years : 2019 2020&lt;br&gt;&lt;br&gt;
Months : january march may&lt;/p&gt;

&lt;p&gt;From a single source of data I must get 2 distinct lists based on 2 distinct fields of the record's date (the year and the month).&lt;/p&gt;
&lt;h3&gt;
  
  
  First try
&lt;/h3&gt;

&lt;p&gt;I expose these filters to my UI with two different &lt;code&gt;LiveData&lt;/code&gt;, one for the years and one for the months (if you don't know what a &lt;a href="https://developer.android.com/topic/libraries/architecture/livedata"&gt;LiveData&lt;/a&gt; is, just think about them as Observable data).  &lt;/p&gt;

&lt;p&gt;The general idea of the flow is as follows : &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;get the list of all records with &lt;code&gt;getRecords.getAll()&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;map this list from &lt;code&gt;List&amp;lt;Record&amp;gt;&lt;/code&gt; to &lt;code&gt;List&amp;lt;Int&amp;gt;&lt;/code&gt; representing the list of years or months of the filters

&lt;ul&gt;
&lt;li&gt;we call &lt;a href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.sequences/distinct-by.html"&gt;&lt;code&gt;distinctBy&lt;/code&gt;&lt;/a&gt; to obtain a list of distinct records based on their year or month&lt;/li&gt;
&lt;li&gt;we &lt;a href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.sequences/map.html"&gt;&lt;code&gt;map&lt;/code&gt;&lt;/a&gt; this new list to extract only the corresponding field of the date&lt;/li&gt;
&lt;li&gt;we sort the final list of years or months&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;



&lt;p&gt;This piece of code has several issues. One of them being that the two filters have almost exactly the same code, and only this part differs :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;distinctBy&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;record&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="nd"&gt;@distinctBy&lt;/span&gt; &lt;span class="nc"&gt;Calendar&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getInstance&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;apply&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;time&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;record&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;date&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;
    &lt;span class="p"&gt;}[&lt;/span&gt;&lt;span class="nc"&gt;Calendar&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;YEAR&lt;/span&gt;&lt;span class="p"&gt;]&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="n"&gt;record&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="nd"&gt;@map&lt;/span&gt; &lt;span class="nc"&gt;Calendar&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getInstance&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;apply&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;time&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;record&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;date&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;
    &lt;span class="p"&gt;}[&lt;/span&gt;&lt;span class="nc"&gt;Calendar&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;YEAR&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;distinctBy&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;record&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="nd"&gt;@distinctBy&lt;/span&gt; &lt;span class="nc"&gt;Calendar&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getInstance&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;apply&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;time&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;record&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;date&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;
    &lt;span class="p"&gt;}[&lt;/span&gt;&lt;span class="nc"&gt;Calendar&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;MONTH&lt;/span&gt;&lt;span class="p"&gt;]&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="n"&gt;record&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="nd"&gt;@map&lt;/span&gt; &lt;span class="nc"&gt;Calendar&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getInstance&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;apply&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;time&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;record&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;date&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;
    &lt;span class="p"&gt;}[&lt;/span&gt;&lt;span class="nc"&gt;Calendar&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;MONTH&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;This seems to be a good candidate for a refact. &lt;/p&gt;
&lt;h4&gt;
  
  
  Quick note
&lt;/h4&gt;

&lt;p&gt;These functions (&lt;code&gt;map&lt;/code&gt;, &lt;code&gt;distinctBy&lt;/code&gt;, &lt;code&gt;filter&lt;/code&gt;, etc.) are all HOF as well, because they take another function as argument (here it's the lambdas we are passing).  They implement &lt;a href="https://en.wikipedia.org/wiki/Function_composition_(computer_science)"&gt;Function composition&lt;/a&gt; and it's one of the main use of HOF.&lt;/p&gt;
&lt;h3&gt;
  
  
  The OOP-way
&lt;/h3&gt;

&lt;p&gt;Instinctively in OOP, I would have created a function taking two parameters : a record and the calendar field to read from the &lt;code&gt;Calendar&lt;/code&gt; instance.&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;readCalendarField&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;record&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Record&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;calendarField&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Int&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="nc"&gt;Calendar&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getInstance&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;apply&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;time&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;record&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;date&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;
    &lt;span class="p"&gt;}[&lt;/span&gt;&lt;span class="n"&gt;calendarField&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;We would use it as follows :&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="c1"&gt;// years filter&lt;/span&gt;
&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;distinctBy&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;record&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="nd"&gt;@distinctBy&lt;/span&gt; &lt;span class="nf"&gt;readCalendarField&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;record&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;Calendar&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;YEAR&lt;/span&gt;&lt;span class="p"&gt;)&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="n"&gt;record&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="nd"&gt;@map&lt;/span&gt; &lt;span class="nf"&gt;readCalendarField&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;record&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;Calendar&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;YEAR&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// months filter&lt;/span&gt;
&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;distinctBy&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;record&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="nd"&gt;@distinctBy&lt;/span&gt; &lt;span class="nf"&gt;readCalendarField&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;record&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;Calendar&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;MONTH&lt;/span&gt;&lt;span class="p"&gt;)&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="n"&gt;record&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="nd"&gt;@map&lt;/span&gt; &lt;span class="nf"&gt;readCalendarField&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;record&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;Calendar&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;MONTH&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;Better. We avoided the duplicate implementation of reading the calendar field, but we still have duplication.  &lt;/p&gt;

&lt;p&gt;For each filter, the exact same lambda has been created twice : one for &lt;code&gt;distinctBy&lt;/code&gt; and another for &lt;code&gt;map&lt;/code&gt;. How can we avoid that ?&lt;/p&gt;
&lt;h3&gt;
  
  
  The FP-way
&lt;/h3&gt;

&lt;p&gt;You saw it coming, let's introduce our higher-order function !  &lt;/p&gt;

&lt;p&gt;We need to write a function that creates a function, very similar to the lambda we were creating twice, but with an additional parameter : the calendar field.&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;newCalendarFieldGetter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;calendarField&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Int&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Record&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;Int&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; 
    &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;record&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt;
        &lt;span class="nc"&gt;Calendar&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getInstance&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;apply&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;time&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;record&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;date&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;
        &lt;span class="p"&gt;}[&lt;/span&gt;&lt;span class="n"&gt;calendarField&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 return type of our function is key here : &lt;code&gt;(Record) -&amp;gt; Int&lt;/code&gt;. It complies to both &lt;code&gt;distinctBy&lt;/code&gt; and &lt;code&gt;map&lt;/code&gt; input type which is &lt;code&gt;(T) -&amp;gt; R&lt;/code&gt;.&lt;br&gt;&lt;br&gt;
Additionnaly, our HOF asks for a calendar field to parameterize the new function that it creates.&lt;/p&gt;

&lt;p&gt;We can now write this :&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;



&lt;p&gt;This starts to look pretty good ! We take advantage of the fact that &lt;code&gt;newCalendarFieldGetter&lt;/code&gt; is a &lt;a href="https://en.wikipedia.org/wiki/Pure_function"&gt;pure function&lt;/a&gt; (thus has no side effects) to share the same instance for &lt;code&gt;dinstinctBy&lt;/code&gt; and &lt;code&gt;map&lt;/code&gt;.  &lt;/p&gt;

&lt;p&gt;Now let's tackle the last duplication of this snippet : the two filters are exactly identical, only differing by the calendar field parameter. &lt;/p&gt;

&lt;p&gt;Following the exact same logic as previously, we can create a parameterized function to fill the &lt;code&gt;getRecords.getAll().map()&lt;/code&gt; input.&lt;/p&gt;

&lt;h3&gt;
  
  
  Perfection
&lt;/h3&gt;

&lt;p&gt;The &lt;code&gt;map&lt;/code&gt; function we are trying to call needs one parameter of type &lt;code&gt;suspend (T) -&amp;gt; R&lt;/code&gt;.&lt;br&gt;&lt;br&gt;
Our new higher-order function will comply to this by returning a &lt;code&gt;suspend (List&amp;lt;Record&amp;gt;) -&amp;gt; List&amp;lt;Int&amp;gt;&lt;/code&gt;, and it will take one more parameter, the calendar field.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;getDistinctDateFields&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;calendarField&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;suspend&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;List&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Record&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;)&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;List&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Int&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;records&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt;
        &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;fieldGetter&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;newCalendarFieldGetter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;calendarField&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="n"&gt;records&lt;/span&gt;
            &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;asSequence&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
            &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;distinctBy&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;fieldGetter&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="n"&gt;fieldGetter&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sorted&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
            &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;toList&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 previously duplicated block is now generic and can be used to extract a list of any disctinct field of the &lt;code&gt;Record&lt;/code&gt; date !  &lt;/p&gt;

&lt;p&gt;Here is how the entire code looks like.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;



&lt;p&gt;With this version, adding a third filter on another field of the date, let's say DAY_OF_MONTH, is quit easy !&lt;/p&gt;

&lt;h2&gt;
  
  
  Final thoughts
&lt;/h2&gt;

&lt;p&gt;This kind of refact is not the most impressive thing you can do with HOF, but it was simple enough to understand how it works in Kotlin and how to use them with common collection manipulation functions as &lt;code&gt;map&lt;/code&gt;, &lt;code&gt;filter&lt;/code&gt; etc, which also are higher-order functions.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://en.wikipedia.org/wiki/Function_composition_(computer_science)"&gt;Function composition&lt;/a&gt; is a frequent use of HOF, I will certainly post an article about this in the future.&lt;/p&gt;

&lt;p&gt;The goal is not to use them everywhere. A good balance must be found between readability, maintainability and optimization (as every aspect of programming). However it's always interesting to learn more new tools to helps us choose the right one for every task.&lt;/p&gt;

&lt;h4&gt;
  
  
  P.S.
&lt;/h4&gt;

&lt;p&gt;A more optimized version of the &lt;code&gt;getDistinctDateFields&lt;/code&gt; function consists of mapping first all records to the date field, and call &lt;code&gt;distinct()&lt;/code&gt; right after. It eliminates the need to use the same &lt;code&gt;fieldGetter&lt;/code&gt; instance twice :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;getDistinctDateFields&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;calendarField&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;suspend&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;List&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Record&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;)&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;List&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Int&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;records&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt;
        &lt;span class="n"&gt;records&lt;/span&gt;
            &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;asSequence&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;newCalendarFieldGetter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;calendarField&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
            &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;distinct&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
            &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sorted&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
            &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;toList&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;That being said, I won't modify the post to include this new version because it allows us to see the possibility of this kind of "function instance" use.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Code is beautiful</title>
      <dc:creator>Rafi Panoyan</dc:creator>
      <pubDate>Fri, 15 May 2020 06:58:14 +0000</pubDate>
      <link>https://dev.to/rafipanoyan/code-is-beautiful-105p</link>
      <guid>https://dev.to/rafipanoyan/code-is-beautiful-105p</guid>
      <description>&lt;p&gt;Today's post will be a bit different. First of all because, at the end of the article, we will have the opportunity to share some code. Then because we won't talk about technical details of a specific programming language, instead we will take a step back and look at the &lt;em&gt;shape&lt;/em&gt; of what we write everyday. &lt;/p&gt;

&lt;h2&gt;
  
  
  Why do we write code ?
&lt;/h2&gt;

&lt;p&gt;We often look at languages as tools that enable us to build software, and at the end of the day, it's exactly what they are. We solve real-world problems with software. Sometimes people's lifes are at stake. Programming is a serious task.&lt;/p&gt;

&lt;p&gt;Our code must be robust, maintanable, concise, performant. To achieve that, we define clean code guidelines, clean architectures, development methodology like TDD, CI platform to control the entire pipelines, etc...&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Programming is a serious task&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;But there is more to that.&lt;/p&gt;

&lt;h2&gt;
  
  
  Take a breath
&lt;/h2&gt;

&lt;p&gt;Every day, in a file, characters become keywords at the tips of our fingers.&lt;br&gt;&lt;br&gt;
We associate these keywords in a very specific way, particular to each programming language, to define the behavior of our software.&lt;/p&gt;

&lt;p&gt;And we look at these files, we look at these functions, properties, classes throughout the day.&lt;br&gt;&lt;br&gt;
Of course we do that to understand what real-life rules they are trying to express, we look at them because we might want to refact these pieces of code, or we want to test them. &lt;u&gt;But we definitely look at them&lt;/u&gt;, &lt;strong&gt;A LOT&lt;/strong&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  I love your shapes
&lt;/h2&gt;

&lt;p&gt;Consequently, the looks of our code matters. And the appareance of a piece of code is ruled by the programming language we are using.&lt;/p&gt;

&lt;p&gt;This is one of the reason I really like learning new programming languages, they don't look the same as the one I'm used to write every day. Different keywords, syntax, special characters... &lt;em&gt;Different shapes&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;I will now show you some snippets of a very basic function with different languages, just to admire the shape of the syntax.  &lt;/p&gt;

&lt;h2&gt;
  
  
  The code
&lt;/h2&gt;

&lt;p&gt;The code you will read is certainly not optimized, we could surely write these functions with less lines, with more advanced concepts, but we don't care here. &lt;/p&gt;

&lt;p&gt;FizzBuzz is a trivial function that takes an integer &lt;code&gt;x&lt;/code&gt; and returns either "fizzbuzz" if it's divisible by 15, "fizz" if it is by 5, "buzz" if it is by 3 and &lt;code&gt;x&lt;/code&gt; otherwise. &lt;/p&gt;

&lt;h3&gt;
  
  
  Kotlin
&lt;/h3&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;fizzbuzz&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Int&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt;
    &lt;span class="k"&gt;when&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="p"&gt;%&lt;/span&gt; &lt;span class="mi"&gt;15&lt;/span&gt; &lt;span class="p"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="s"&gt;"fizzbuzz"&lt;/span&gt;
        &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="p"&gt;%&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt; &lt;span class="p"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="s"&gt;"fizz"&lt;/span&gt;
        &lt;span class="n"&gt;x&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="mi"&gt;0&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="s"&gt;"buzz"&lt;/span&gt;
        &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="s"&gt;"$x"&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The first language is the one I use the most as an Android Developer. Kotlin introduced a new syntax to develop on the JVM and it has been some fresh air from Java !  &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Clean, readable&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Haskell
&lt;/h3&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight haskell"&gt;&lt;code&gt;&lt;span class="n"&gt;fizzbuzz&lt;/span&gt; &lt;span class="o"&gt;::&lt;/span&gt; &lt;span class="kt"&gt;Int&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;
&lt;span class="n"&gt;fizzbuzz&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;
    &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="p"&gt;`&lt;/span&gt;&lt;span class="n"&gt;mod&lt;/span&gt;&lt;span class="p"&gt;`&lt;/span&gt; &lt;span class="mi"&gt;15&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;   &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"fizzbuzz"&lt;/span&gt;
    &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="p"&gt;`&lt;/span&gt;&lt;span class="n"&gt;mod&lt;/span&gt;&lt;span class="p"&gt;`&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;    &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"fizz"&lt;/span&gt;
    &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="p"&gt;`&lt;/span&gt;&lt;span class="n"&gt;mod&lt;/span&gt;&lt;span class="p"&gt;`&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;    &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"buzz"&lt;/span&gt;
    &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="n"&gt;otherwise&lt;/span&gt;         &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;This purely functional language is very comparable to pure mathematic in its syntax. I really love this facet, you can tell it by the way the signature of the function is declared.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Pure, rational&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Clojure
&lt;/h3&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight clojure"&gt;&lt;code&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;defn&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;fizzbuzz&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;let&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;d&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt; 
        &lt;/span&gt;&lt;span class="no"&gt;:by15&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;mod&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;15&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="no"&gt;:by5&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;mod&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;&lt;span class="w"&gt; 
        &lt;/span&gt;&lt;span class="no"&gt;:by3&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;mod&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;))}]&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;cond&lt;/span&gt;&lt;span class="w"&gt; 
        &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="no"&gt;:by15&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;d&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s"&gt;"fizzbuzz"&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="no"&gt;:by5&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;d&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s"&gt;"fizz"&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="no"&gt;:by3&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;d&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s"&gt;"buzz"&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="no"&gt;:else&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;)))&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Controversial choice here but... what a beauty ! A &lt;a href="https://en.wikipedia.org/wiki/Lisp_(programming_language)"&gt;LISP language&lt;/a&gt;, where absolutely &lt;strong&gt;everything&lt;/strong&gt; is a list.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;(element1 element2 element3 ...)&lt;/code&gt; &lt;/p&gt;

&lt;p&gt;That's it. Each and every piece of code you write is exactly this, &lt;strong&gt;a list&lt;/strong&gt;. Either you declare or call a function, compare two values, create a conditional branch... &lt;em&gt;Consistency&lt;/em&gt;.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;CONSISTENT&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Now what ?
&lt;/h2&gt;

&lt;p&gt;You might not have learnt anything new reading this post, but I really hope you stopped for a moment to think about the shape of what you write everyday. &lt;/p&gt;

&lt;p&gt;That being said, I'm sure that caring about the "physical" beauty of our code can help us write it better overall. &lt;br&gt;
More concise, readable, enjoyable to read... you certainly don't want this 30+ lines' method in your class !&lt;/p&gt;

&lt;p&gt;Our job can seem like art sometimes, and syntax is just one the many facet of it. Architecture, algorithm, even project structures, are aspects we should look at with art in mind.&lt;/p&gt;

&lt;h2&gt;
  
  
  Share !
&lt;/h2&gt;

&lt;p&gt;I would really appreciate if you could share a piece of art with your own FizzBuzz function, written in whatever language you find &lt;strong&gt;good-looking&lt;/strong&gt; !&lt;/p&gt;

&lt;p&gt;If you are curious about the languages above, here are some links : &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://kotlinlang.org/docs/reference/"&gt;Learn Kotlin&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://learnyouahaskell.com/"&gt;Learn Haskell&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.braveclojure.com/"&gt;Learn Clojure&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>codequality</category>
      <category>haskell</category>
      <category>kotlin</category>
      <category>clojure</category>
    </item>
    <item>
      <title>Basic key behaviors of Kotlin coroutines</title>
      <dc:creator>Rafi Panoyan</dc:creator>
      <pubDate>Thu, 30 Apr 2020 20:25:09 +0000</pubDate>
      <link>https://dev.to/rafipanoyan/basic-key-behaviors-of-kotlin-coroutines-3pin</link>
      <guid>https://dev.to/rafipanoyan/basic-key-behaviors-of-kotlin-coroutines-3pin</guid>
      <description>&lt;p&gt;When I first learned about Kotlin coroutines, I was having a hard time identifying the specific behavior of all keywords: &lt;code&gt;launch&lt;/code&gt;, &lt;code&gt;withContext&lt;/code&gt;, &lt;code&gt;async&lt;/code&gt;, etc...  &lt;/p&gt;

&lt;p&gt;Is my coroutine suspended ? How can I wait many of them at one point ? Where are they executing ?  &lt;/p&gt;

&lt;p&gt;Quickly, I decided to sum up some key basic behaviors to help me with that, and that's what I will share today ! This post is aimed at developers who are not using coroutines right now, or are using it but need a quick refresh on some basic behaviors.&lt;/p&gt;

&lt;h3&gt;
  
  
  What is a coroutine ?
&lt;/h3&gt;

&lt;p&gt;According to the &lt;a href="https://kotlinlang.org/docs/reference/coroutines/basics.html"&gt;offical documentation&lt;/a&gt;, a coroutine is essentially a (&lt;a href="https://youtu.be/_hfBv0a09Jc?t=1840"&gt;very&lt;/a&gt;) light-weight thread.&lt;br&gt;
It enables asynchronous operations without the need of callbacks, &lt;code&gt;Promise&lt;/code&gt;, or &lt;code&gt;Future&lt;/code&gt; concepts. Write imperative code, run concurrent operations.&lt;/p&gt;
&lt;h3&gt;
  
  
  Snippets setup
&lt;/h3&gt;

&lt;p&gt;Let's declare some context for our coroutines snippets:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;job&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Job&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;customScope&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;CoroutineScope&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;job&lt;/span&gt; &lt;span class="p"&gt;+&lt;/span&gt; &lt;span class="nc"&gt;Dispatchers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Default&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="k"&gt;suspend&lt;/span&gt; &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;fakeHardWork&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;tag&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;tag&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="nf"&gt;delay&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;4000&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s"&gt;"I'm exhausted"&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;A &lt;a href="https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-job/"&gt;&lt;code&gt;Job&lt;/code&gt;&lt;/a&gt; is responsible for holding a reference to a coroutine. With that reference you can &lt;code&gt;.start()&lt;/code&gt; the coroutine if it was not started, wait for its completion with &lt;code&gt;.join()&lt;/code&gt; or cancel it with &lt;code&gt;.cancel()&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;customScope&lt;/code&gt; is a &lt;a href="https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-coroutine-scope/index.html"&gt;&lt;code&gt;CoroutineScope&lt;/code&gt;&lt;/a&gt; composed of the &lt;code&gt;job&lt;/code&gt; and a &lt;a href="https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-coroutine-dispatcher/index.html"&gt;&lt;code&gt;CoroutineDispatcher&lt;/code&gt;&lt;/a&gt; (and it can be &lt;a href="https://medium.com/@elizarov/coroutine-context-and-scope-c8b255d59055"&gt;much more&lt;/a&gt;). Yes, I added a &lt;code&gt;CoroutineDispatcher&lt;/code&gt; to a &lt;code&gt;Job&lt;/code&gt;. That's because they both are a &lt;a href="https://proandroiddev.com/demystifying-coroutinecontext-1ce5b68407ad"&gt;&lt;code&gt;CoroutineContext&lt;/code&gt;&lt;/a&gt; and they can be merged together.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;fakeHardWork()&lt;/code&gt; is, as the name can suggest, a &lt;code&gt;suspend&lt;/code&gt; function faking a 4 seconds task&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I did not get into the details of these objects that are part of the coroutine's vocabulary. I encourage you to read the documentation about all of them, or  find some &lt;a href="https://dev.to/touchlab/kotlin-coroutines-cheat-sheet-5872"&gt;posts&lt;/a&gt; describing them.&lt;/p&gt;

&lt;h5&gt;
  
  
  Quick look at &lt;code&gt;CoroutineDispatcher&lt;/code&gt;
&lt;/h5&gt;

&lt;p&gt;The &lt;code&gt;CoroutineDispatcher&lt;/code&gt; is responsible for describing &lt;em&gt;where&lt;/em&gt; the coroutine must be executed. Some dispatchers are provided by the &lt;code&gt;core&lt;/code&gt; library with these properties : &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;a href="https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-dispatchers/-default.html"&gt;&lt;code&gt;Dispatchers.Default&lt;/code&gt;&lt;/a&gt; which serves for basic background task.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-dispatchers/-i-o.html"&gt;&lt;code&gt;Dispatchers.IO&lt;/code&gt;&lt;/a&gt; offers a thread pool optimized for IO tasks (only available for the JVM).&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-dispatchers/-unconfined.html"&gt;&lt;code&gt;Dispatchers.Unconfined&lt;/code&gt;&lt;/a&gt; if the thread of execution does not matter.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Finally, &lt;a href="https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-dispatchers/-main.html"&gt;&lt;code&gt;Dispatchers.Main&lt;/code&gt;&lt;/a&gt; property is a special dispatcher that is allocated to &lt;strong&gt;main thread&lt;/strong&gt; operations. Such tasks as manipulating the UI are often required to be executed on this dispatcher (Android crashes the app if that's not the case for example).&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Using the &lt;code&gt;Dispatchers.Main&lt;/code&gt; property without providing a concrete implementation in the classpath of your application will throw an &lt;code&gt;IllegalStateException&lt;/code&gt;. &lt;code&gt;kotlinx-coroutines-android&lt;/code&gt; is the provider of the &lt;code&gt;Main&lt;/code&gt; dispatcher for Android.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Launching a coroutine
&lt;/h2&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;newCoroutine&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; 
    &lt;span class="n"&gt;customScope&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;launch&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="c1"&gt;// starts a new coroutine 'C1'&lt;/span&gt;

        &lt;span class="nf"&gt;fakeHardWork&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"working"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// this suspends C1 until the function completes&lt;/span&gt;
        &lt;span class="nf"&gt;println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"done"&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;Theoretical output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="mi"&gt;00&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="mi"&gt;0000&lt;/span&gt;&lt;span class="n"&gt;ms&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;working&lt;/span&gt;
&lt;span class="mi"&gt;04&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="mi"&gt;0000&lt;/span&gt;&lt;span class="n"&gt;ms&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;done&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;This piece of code is quit straighforward. We launch a new coroutine inside the &lt;code&gt;customScope&lt;/code&gt;. Since this scope has been initialized with &lt;code&gt;Dispatchers.Default&lt;/code&gt;, it's executing on the corresponding thread pool.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;a href="https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/launch.html"&gt;&lt;code&gt;launch&lt;/code&gt;&lt;/a&gt; creates and executes a new coroutine and must be called on an existing &lt;code&gt;CoroutineScope&lt;/code&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Switching Dispatchers
&lt;/h2&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;switchDispatchers&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; 
    &lt;span class="n"&gt;customScope&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;launch&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="c1"&gt;// starts a new coroutine 'C1'&lt;/span&gt;
        &lt;span class="nf"&gt;fakeHardWork&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"work from Dispatchers.Default"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// same as above, suspends C1&lt;/span&gt;

        &lt;span class="c1"&gt;// switch Dispatcher, still suspends C1&lt;/span&gt;
        &lt;span class="nf"&gt;withContext&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Dispatchers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;IO&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; 
            &lt;span class="nf"&gt;fakeHardWork&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"work from Dispatchers.IO"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; 
        &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="nf"&gt;println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"done on Dispatchers.Default"&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;Theoretical output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="mi"&gt;00&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="mi"&gt;0000&lt;/span&gt;&lt;span class="n"&gt;ms&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;work&lt;/span&gt; &lt;span class="n"&gt;from&lt;/span&gt; &lt;span class="nc"&gt;Dispatchers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Default&lt;/span&gt;
&lt;span class="mi"&gt;04&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="mi"&gt;0000&lt;/span&gt;&lt;span class="n"&gt;ms&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;work&lt;/span&gt; &lt;span class="n"&gt;from&lt;/span&gt; &lt;span class="nc"&gt;Dispatchers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;IO&lt;/span&gt;
&lt;span class="mi"&gt;08&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="mi"&gt;0000&lt;/span&gt;&lt;span class="n"&gt;ms&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;done&lt;/span&gt; &lt;span class="n"&gt;on&lt;/span&gt; &lt;span class="nc"&gt;Dispatchers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Default&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Here, &lt;a href="https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/with-context.html"&gt;&lt;code&gt;withContext&lt;/code&gt;&lt;/a&gt; changes the execution context of the block in parameter. The second call to &lt;code&gt;fakeHardWork()&lt;/code&gt; is executed on Dispatchers.IO, and &lt;code&gt;withContext&lt;/code&gt; is still suspending C1. &lt;/p&gt;

&lt;p&gt;&lt;code&gt;withContext&lt;/code&gt; merges the current &lt;a href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.coroutines/-coroutine-context/"&gt;&lt;code&gt;CoroutineContext&lt;/code&gt;&lt;/a&gt; it has been launched from (here &lt;code&gt;customScope&lt;/code&gt;'s context), with the context you passed in parameter.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;code&gt;withContext&lt;/code&gt; does not create a new coroutine and suspends its caller until its block is executed.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Parallel execution inside a coroutine
&lt;/h2&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;parallelWorkInCoroutine&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; 
    &lt;span class="n"&gt;customScope&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;launch&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="c1"&gt;// starts a new coroutine C1&lt;/span&gt;

        &lt;span class="c1"&gt;// fire a new coroutine C2. C1 is not suspended&lt;/span&gt;
        &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;work_1&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;async&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nf"&gt;fakeHardWork&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"w_1 from Dispatchers.Default"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="c1"&gt;// fire a new coroutine C3 on another Dispatcher. C1 is not suspended&lt;/span&gt;
        &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;work_2&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;async&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Dispatchers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;IO&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nf"&gt;fakeHardWork&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"w_2 from Dispatchers.IO"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;

        &lt;span class="c1"&gt;// await() calls suspend C1&lt;/span&gt;
        &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;result&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;work_1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;await&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;+&lt;/span&gt; &lt;span class="n"&gt;work_2&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;await&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
        &lt;span class="nf"&gt;println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;result&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;Theoretical output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="mi"&gt;00&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="mi"&gt;0000&lt;/span&gt;&lt;span class="n"&gt;ms&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;w_1&lt;/span&gt; &lt;span class="n"&gt;from&lt;/span&gt; &lt;span class="nc"&gt;Dispatchers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Default&lt;/span&gt;
&lt;span class="mi"&gt;00&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="mi"&gt;0010&lt;/span&gt;&lt;span class="n"&gt;ms&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;w_2&lt;/span&gt; &lt;span class="n"&gt;from&lt;/span&gt; &lt;span class="nc"&gt;Dispatchers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;IO&lt;/span&gt;
&lt;span class="mi"&gt;04&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="mi"&gt;0010&lt;/span&gt;&lt;span class="n"&gt;ms&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;I&lt;/span&gt;&lt;span class="err"&gt;'&lt;/span&gt;&lt;span class="n"&gt;m&lt;/span&gt; &lt;span class="n"&gt;exhaustedI&lt;/span&gt;&lt;span class="err"&gt;'&lt;/span&gt;&lt;span class="n"&gt;m&lt;/span&gt; &lt;span class="n"&gt;exhausted&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Now this is interesting. &lt;a href="https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/async.html"&gt;&lt;code&gt;async&lt;/code&gt;&lt;/a&gt; is a &lt;em&gt;coroutine builder&lt;/em&gt;. Unlike &lt;code&gt;withContext&lt;/code&gt;, it actually creates and starts a new coroutine while also &lt;em&gt;immediatly&lt;/em&gt; returning a &lt;a href="https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-deferred/index.html"&gt;&lt;code&gt;Deferred&amp;lt;T&amp;gt;&lt;/code&gt;&lt;/a&gt; (&lt;code&gt;T&lt;/code&gt; being the return type of the block passed to &lt;code&gt;async&lt;/code&gt;, a &lt;code&gt;String&lt;/code&gt; in our example).  &lt;/p&gt;

&lt;p&gt;We can then call &lt;a href="https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-deferred/await.html"&gt;&lt;code&gt;await()&lt;/code&gt;&lt;/a&gt; on this object to effectively suspend the caller coroutine (C1 in the snippet) and get back the &lt;code&gt;String&lt;/code&gt; produced by the blocks passed to &lt;code&gt;async&lt;/code&gt;.  &lt;/p&gt;

&lt;p&gt;Here &lt;code&gt;result&lt;/code&gt; equals to &lt;code&gt;"I'm exhaustedI'm exhausted"&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;With this, we could await many &lt;code&gt;Deffered&lt;/code&gt; and then execute something only when all of them complete.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;code&gt;async&lt;/code&gt; creates and starts a new coroutine. The returned &lt;code&gt;Deferred&lt;/code&gt; allows us to suspend the caller coroutine and get the produced value.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Wrapping it up
&lt;/h2&gt;

&lt;p&gt;I described very few behaviors about coroutines, there's so much more to discover and to learn about them ! But I find these points to be good tools to start manipulating coroutines and learn step by step. &lt;/p&gt;

&lt;p&gt;I'll probably write later about other more advanced concepts. For now, if you were asking yourself if coroutines are worth a try, go ahead !&lt;/p&gt;

&lt;h4&gt;
  
  
  More starting resources
&lt;/h4&gt;

&lt;p&gt;If you are curious, &lt;a href="https://www.youtube.com/watch?v=_hfBv0a09Jc"&gt;this great talk&lt;/a&gt; is a perfect introduction to Coroutines from the creator, Roman Elizarov.&lt;/p&gt;

</description>
      <category>kotlin</category>
      <category>coroutine</category>
    </item>
  </channel>
</rss>
