<?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" xmlns:media="http://search.yahoo.com/mrss/" version="2.0">
    <channel>
        
        <title>
            <![CDATA[ Scala - freeCodeCamp.org ]]>
        </title>
        <description>
            <![CDATA[ Browse thousands of programming tutorials written by experts. Learn Web Development, Data Science, DevOps, Security, and get developer career advice. ]]>
        </description>
        <link>https://www.freecodecamp.org/news/</link>
        <image>
            <url>https://cdn.freecodecamp.org/universal/favicons/favicon.png</url>
            <title>
                <![CDATA[ Scala - freeCodeCamp.org ]]>
            </title>
            <link>https://www.freecodecamp.org/news/</link>
        </image>
        <generator>Eleventy</generator>
        <lastBuildDate>Fri, 26 Jun 2026 22:47:20 +0000</lastBuildDate>
        <atom:link href="https://www.freecodecamp.org/news/tag/scala/rss.xml" rel="self" type="application/rss+xml" />
        <ttl>60</ttl>
        
            <item>
                <title>
                    <![CDATA[ How to Read and Write Deeply Partitioned Files Using Apache Spark ]]>
                </title>
                <description>
                    <![CDATA[ If you use Apache Spark to write your data pipeline, you might need to export or copy data from a source to destination while preserving the partition folders between the source and destination. When I researched online on how to do this in Spark, I ... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/how-to-read-and-write-deeply-partitioned-files-using-apache-spark/</link>
                <guid isPermaLink="false">68b4bd4bbcba09cb447c5a9f</guid>
                
                    <category>
                        <![CDATA[ #apache-spark ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Scala ]]>
                    </category>
                
                    <category>
                        <![CDATA[ big data ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ Arun Shanmugam Kumar ]]>
                </dc:creator>
                <pubDate>Sun, 31 Aug 2025 21:23:23 +0000</pubDate>
                <media:content url="https://cdn.hashnode.com/res/hashnode/image/upload/v1756671369152/1e620925-6fb6-47fa-8344-86b9d3c7cd02.png" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>If you use Apache Spark to write your data pipeline, you might need to export or copy data from a source to destination while preserving the partition folders between the source and destination.</p>
<p>When I researched online on how to do this in Spark, I found very few tutorials giving an end-to-end solution that worked – especially when the partitions are deeply nested and you don't know beforehand the values these folder names will take (for example <code>year=*/month=*/day=*/hour=*/*.csv</code>).</p>
<p>In this tutorial, I have provided one such implementation using Spark.</p>
<h3 id="heading-heres-what-well-cover">Here’s what we’ll cover:</h3>
<ul>
<li><p><a class="post-section-overview" href="#heading-prerequisite">Prerequisite</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-setup">Setup</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-false-starts">False Starts</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-my-solution">My Solution</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-conclusion">Conclusion</a></p>
</li>
</ul>
<h2 id="heading-prerequisite">Prerequisite</h2>
<p>To follow along in this tutorial, you need to have basic understanding of distributed computing using frameworks like Hadoop and Spark, as well as code that’s programmed in Object Oriented languages like Scala/Java. The code is tested using the below dependencies:</p>
<ul>
<li><p>Scala 2.12+</p>
</li>
<li><p>Java 17 (earlier versions might work)</p>
</li>
<li><p>Sbt</p>
</li>
</ul>
<h2 id="heading-setup">Setup</h2>
<p>I’m assuming you have partition folders that are created at the source with the below pattern (which is a standard partition column involving date-time):</p>
<p><code>year/month/day/hour</code></p>
<p>Crucially, as I mentioned above, I’m assuming that you don’t know the full name of the folders – except that they have some constant prefix pattern in them.</p>
<h2 id="heading-false-starts">False Starts</h2>
<ol>
<li><p>If you think of using <code>recursiveFileLookup</code> and <code>pathGlobFilter</code> option while both reading and writing, it doesn’t quite work, as the above functions are only available on read API.</p>
</li>
<li><p>If you think of parameterizing the reading and writing based on all the possible year/month/day/hour combination and skip export if the corresponding partition folder is not found, then it might work but won’t be very efficient.</p>
</li>
</ol>
<h2 id="heading-my-solution">My Solution</h2>
<p>After a few trials and errors and searching in Stack Overflow and the Spark documentation, I hit upon an idea to use a combination of <code>input_file_name()</code>, <code>regexp_extract()</code>, and <code>partitionBy()</code> API's on the write side to achieve the end goal. You can find a Scala-based sample code below:</p>
<pre><code class="lang-scala"><span class="hljs-keyword">package</span> main.scala.blog

<span class="hljs-comment">/**
*  Spark stream example code to read and write from a partitioned folder
*  to a partitioned folder without explicitly known datetime.
*/</span>

<span class="hljs-keyword">import</span> org.apache.spark.sql.<span class="hljs-type">SparkSession</span>
<span class="hljs-keyword">import</span> org.apache.spark.sql.types.<span class="hljs-type">StringType</span>
<span class="hljs-keyword">import</span> org.apache.spark.sql.functions.{udf, input_file_name, col, lit, regexp_extract}

<span class="hljs-class"><span class="hljs-keyword">object</span> <span class="hljs-title">PartitionedReaderWriter</span> </span>{

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">main</span></span>(args: <span class="hljs-type">Array</span>[<span class="hljs-type">String</span>]) {
        <span class="hljs-comment">// 1.</span>
        <span class="hljs-keyword">val</span> spark = <span class="hljs-type">SparkSession</span>
                    .builder
                    .appName(<span class="hljs-string">"PartitionedReaderWriterApp"</span>)
                    .getOrCreate()

        <span class="hljs-keyword">val</span> sourceBasePath = <span class="hljs-string">"data/partitioned_files_source/user"</span>
        <span class="hljs-comment">// 2.</span>
        <span class="hljs-keyword">val</span> sourceDf = spark.read
                            .format(<span class="hljs-string">"csv"</span>)
                            .schema(<span class="hljs-string">"State STRING, Color STRING, Count INT"</span>)
                            .option(<span class="hljs-string">"header"</span>, <span class="hljs-string">"true"</span>)
                            .option(<span class="hljs-string">"pathGlobFilter"</span>, <span class="hljs-string">"*.csv"</span>)
                            .option(<span class="hljs-string">"recursiveFileLookup"</span>, <span class="hljs-string">"true"</span>)
                            .load(sourceBasePath)

        <span class="hljs-keyword">val</span> destinationBasePath = <span class="hljs-string">"data/partitioned_files_destination/user"</span>
        <span class="hljs-comment">// 3.</span>
        <span class="hljs-keyword">val</span> writeDf = sourceDf
                        .withColumn(<span class="hljs-string">"year"</span>, regexp_extract(input_file_name(), <span class="hljs-string">"year=(\\d{4})"</span>, <span class="hljs-number">1</span>))
                        .withColumn(<span class="hljs-string">"month"</span>, regexp_extract(input_file_name(), <span class="hljs-string">"month=(\\d{2})"</span>, <span class="hljs-number">1</span>))
                        .withColumn(<span class="hljs-string">"day"</span>, regexp_extract(input_file_name(), <span class="hljs-string">"day=(\\d{2})"</span>, <span class="hljs-number">1</span>))
                        .withColumn(<span class="hljs-string">"hour"</span>, regexp_extract(input_file_name(), <span class="hljs-string">"hour=(\\d{2})"</span>, <span class="hljs-number">1</span>))

        <span class="hljs-comment">// 4.</span>
        writeDf.write
                .format(<span class="hljs-string">"csv"</span>)
                .option(<span class="hljs-string">"header"</span>, <span class="hljs-string">"true"</span>)
                .mode(<span class="hljs-string">"overwrite"</span>)
                .partitionBy(<span class="hljs-string">"year"</span>, <span class="hljs-string">"month"</span>, <span class="hljs-string">"day"</span>, <span class="hljs-string">"hour"</span>)
                .save(destinationBasePath)

        <span class="hljs-comment">// 5.</span>
        spark.stop()        
    }
}
</code></pre>
<p>Here’s what’s going on in the above code:</p>
<ol>
<li><p>Inside main method, you begin by adding Spark initialization setup code to create a Spark session.</p>
</li>
<li><p>You read the data from <code>sourceBasePath</code> using spark <code>read()</code> API with the format as <code>csv</code> (you can also optionally provide the schema). Options <code>recursiveFileLookup</code> and <code>pathGlobFilter</code> are needed to recursively read through nested folders and to specify any <code>csv</code> file, respectively.</p>
</li>
<li><p>Th next section contains the core logic where you can use <code>input_file_name()</code> to return the full path of the file and <code>regexp_extract()</code> to extract <code>year</code> , <code>month</code>, <code>day</code>, and <code>hour</code> from the corresponding subfolders in the path and store them as auxiliary columns on the dataframe.</p>
</li>
<li><p>Finally, you write the dataframe using the <code>csv</code> format again and crucially use <code>partitionBy</code> to specify the previously created auxiliary columns as partition columns. Then save the dataframe in the <code>destinationBasePath</code>.</p>
</li>
<li><p>After the copy is done, you stop the Spark session by calling the <code>stop()</code> API.</p>
</li>
</ol>
<h2 id="heading-conclusion">Conclusion</h2>
<p>In this article I have shown you how to export / copy a deeply nested data files from source to destination using Apache Spark in an efficient way. I hope you find it useful!</p>
<p>You can read my other articles at <a target="_blank" href="https://www.beyonddream.me/post-1/">https://www.beyonddream.me.</a></p>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ How to Use Thread.sleep Without Blocking on the JVM ]]>
                </title>
                <description>
                    <![CDATA[ By Daniel Sebban JVM Languages like Java and Scala have the ability to run concurrent code using the Thread  class. Threads are notoriously complex and very error prone, so having a solid understanding of how they work is essential. Let's start with ... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/non-blocking-thread-sleep-on-jvm/</link>
                <guid isPermaLink="false">66d45e40bc9760a197a10380</guid>
                
                    <category>
                        <![CDATA[ Java ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Scala ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ freeCodeCamp ]]>
                </dc:creator>
                <pubDate>Mon, 21 Dec 2020 17:39:16 +0000</pubDate>
                <media:content url="https://www.freecodecamp.org/news/content/images/2020/12/164b263b-0d10-452b-9bd0-b14c22adeb31-screen-shot-2019-11-15-at-122953-pm-4-1.png" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>By Daniel Sebban</p>
<p>JVM Languages like Java and Scala have the ability to run concurrent code using the <code>Thread</code>  class. Threads are notoriously complex and very error prone, so having a solid understanding of how they work is essential.</p>
<p>Let's start with the Javadoc for <code>Thread.sleep</code>:</p>
<blockquote>
<p>Causes the currently executing <strong>thread</strong> to sleep (temporarily <strong>cease execution)</strong> for the specified number of milliseconds</p>
</blockquote>
<p>What are the implications of <code>**cease execution**</code>, also known as <strong>blocking</strong>, and what does it mean? Is it bad? And if so can we achieve <strong>non-blocking sleep</strong>?</p>
<h3 id="heading-what-well-cover-in-this-article">What We'll Cover in This Article</h3>
<p>This post covers a lot of ground and hopefully you will learn a lot of cool things.</p>
<ul>
<li>What happens at the <strong>OS level when sleeping</strong>?</li>
<li>The <strong>problem with sleeping</strong></li>
<li><strong>Project Loom</strong> and virtual threads</li>
<li>Functional programming and design</li>
<li><strong>ZIO</strong> Scala library for concurrency</li>
</ul>
<p>Yes, all of this is coming up below.</p>
<p>But first, let’s start with this simple Scala snippet that we will change throughout the post to achieve what we want:</p>
<pre><code class="lang-scala">println(<span class="hljs-string">"a"</span>)
<span class="hljs-type">Thread</span>.sleep(<span class="hljs-number">1000</span>)
println(<span class="hljs-string">"b"</span>)
</code></pre>
<p>It's quite simple: it prints “a” and then 10 seconds later in prints “b”</p>
<p>Let’s focus on <code>Thread.sleep</code> and try to understand <strong>HOW</strong> it achieves sleeping. Once we understand the how, we will be able to see the problem and define it more concretely.</p>
<h2 id="heading-how-does-sleeping-work-at-the-os-level">How Does Sleeping Work at the OS Level?</h2>
<p>Here is what happens when you call <code>Thread.sleep</code> under the hood.</p>
<ul>
<li>It calls the thread API of the underlying OS</li>
<li>Because the JVM uses a one to one mapping between Java and kernel threads, it asks the OS to give up the thread’s “rights” to the CPU for the specified time</li>
<li>When the time has elapsed the OS scheduler will wake the thread via an interrupt (this is efficient) and assign it a CPU slice to allow it to resume running</li>
</ul>
<p>The critical point here is that the sleeping thread <strong>is completely taken out and is not reusable while sleeping</strong>.</p>
<h3 id="heading-limitations-of-threads">Limitations of Threads</h3>
<p>Here are few important limitations that come with threads:</p>
<ul>
<li>There is a limit to how many threads you can create. After around 30K, you will get this error:</li>
</ul>
<pre><code>java.lang.OutOfMemoryError : unable to create <span class="hljs-keyword">new</span> native Thread
</code></pre><ul>
<li>JVM Threads can be expensive memory-wise to create, as they come with a dedicated stack</li>
<li>Too many JVM threads will incur overhead because of expensive context switches and the way they share finite hardware resources</li>
</ul>
<p>Now that we understand more about what goes on behind the scenes let’s go back to the sleeping problem.</p>
<h2 id="heading-the-problem-with-sleeping">The Problem with Sleeping</h2>
<p>Let’s define the problem more concretely and run a snippet to show the issue we are facing. We will use this function to illustrate the point:</p>
<pre><code class="lang-scala"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">task</span></span>(id: <span class="hljs-type">Int</span>): <span class="hljs-type">Runnable</span> = () =&gt; 
{
  println(<span class="hljs-string">s"<span class="hljs-subst">${Thread.currentThread().getName()}</span> start-<span class="hljs-subst">$id</span>"</span>)
  <span class="hljs-type">Thread</span>.sleep(<span class="hljs-number">10000</span>)
  println(<span class="hljs-string">s"<span class="hljs-subst">${Thread.currentThread().getName()}</span> end-<span class="hljs-subst">$id</span>"</span>)
}
</code></pre>
<p>This simple function will</p>
<ul>
<li>print <code>**start**</code> followed by the thread id</li>
<li>sleeps for 10 seconds</li>
<li>print <code>**end**</code> followed by the thread id</li>
</ul>
<h3 id="heading-your-mission-if-you-accept-it-is-to-run-2-tasks-concurrently-with-1-thread">Your mission if you accept it is to run 2 tasks concurrently with 1 thread</h3>
<p>We want to run 2 tasks concurrently, meaning the whole program should take a total of 10 seconds. But we only have 1 thread available.</p>
<p>Are you up for this challenge?</p>
<p>Let’s play a little bit with the number of tasks and threads to get a sense of what the problem is exactly.</p>
<h3 id="heading-1-task-gt-1-thread"><code>1 task -&gt; 1 thread</code></h3>
<pre><code class="lang-scala"><span class="hljs-keyword">new</span> <span class="hljs-type">Thread</span>(task(<span class="hljs-number">1</span>)).start()
</code></pre>
<pre><code><span class="hljs-number">12</span>:<span class="hljs-number">11</span>:<span class="hljs-number">08</span> INFO  Thread<span class="hljs-number">-0</span> start<span class="hljs-number">-1</span>
<span class="hljs-number">12</span>:<span class="hljs-number">11</span>:<span class="hljs-number">18</span> INFO  Thread<span class="hljs-number">-0</span> end<span class="hljs-number">-1</span>
</code></pre><p>Let’s fire up <code>jvisualvm</code> to check out what the thread is doing:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2020/12/vissualm_vm_1.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p>You can see that the Thread-0 is in the purple <code>sleeping</code> state. </p>
<p>Hitting the thread dump button will print this:</p>
<pre><code><span class="hljs-string">"Thread-0"</span> #<span class="hljs-number">13</span> prio=<span class="hljs-number">5</span> os_prio=<span class="hljs-number">31</span> tid=<span class="hljs-number">0x00007f9a3e0e2000</span> nid=<span class="hljs-number">0x5b03</span> waiting on condition [<span class="hljs-number">0x0000700004ac8000</span>]  
  java.lang.Thread.State: TIMED_WAITING (sleeping)
  at java.lang.Thread.sleep(Native Method)
  at example.Blog$.$anonfun$task$<span class="hljs-number">1</span>(Blog.scala:<span class="hljs-number">7</span>)
  at example.Blog$$$Lambda$<span class="hljs-number">2</span>/<span class="hljs-number">1359484306.</span>run(Unknown Source)
  at java.lang.Thread.run(Thread.java:<span class="hljs-number">748</span>)
  Locked ownable synchronizers:        - None
</code></pre><p>Clearly, this thread is not usable anymore until it finishes to sleep.</p>
<h3 id="heading-2-tasks-gt-1-thread"><code>2 tasks -&gt; 1 thread</code></h3>
<p>Let’s illustrate the problem by running 2 such tasks with only one thread available:</p>
<pre><code class="lang-scala"><span class="hljs-keyword">import</span> java.util.concurrent.<span class="hljs-type">Executors</span>

<span class="hljs-comment">// an executor with only 1 thread available</span>
<span class="hljs-keyword">val</span> oneThreadExecutor = <span class="hljs-type">Executors</span>.newFixedThreadPool(<span class="hljs-number">1</span>)

<span class="hljs-comment">// send 2 tasks to the executor</span>
(<span class="hljs-number">1</span> to <span class="hljs-number">2</span>).foreach(id =&gt;
   oneThreadExecutor.execute(task(id)))
</code></pre>
<p>We get this output:</p>
<pre><code><span class="hljs-number">2020.09</span><span class="hljs-number">.28</span> <span class="hljs-number">21</span>:<span class="hljs-number">49</span>:<span class="hljs-number">56</span> INFO  pool<span class="hljs-number">-1</span>-thread<span class="hljs-number">-1</span> start<span class="hljs-number">-1</span>
<span class="hljs-number">2020.09</span><span class="hljs-number">.28</span> <span class="hljs-number">21</span>:<span class="hljs-number">50</span>:<span class="hljs-number">07</span> INFO  pool<span class="hljs-number">-1</span>-thread<span class="hljs-number">-1</span> end<span class="hljs-number">-1</span>
<span class="hljs-number">2020.09</span><span class="hljs-number">.28</span> <span class="hljs-number">21</span>:<span class="hljs-number">50</span>:<span class="hljs-number">07</span> INFO  pool<span class="hljs-number">-1</span>-thread<span class="hljs-number">-1</span> start<span class="hljs-number">-2</span>
<span class="hljs-number">2020.09</span><span class="hljs-number">.28</span> <span class="hljs-number">21</span>:<span class="hljs-number">50</span>:<span class="hljs-number">17</span> INFO  pool<span class="hljs-number">-1</span>-thread<span class="hljs-number">-1</span> end<span class="hljs-number">-2</span>
</code></pre><p><img src="https://www.freecodecamp.org/news/content/images/2020/12/visualvm_3.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p>You can see the purple color (sleeping state) for the <code>pool-1-thread-1</code>. The tasks have no choice but to run one after the other because the thread is taken out each time <code>Thread.sleep</code> is used.</p>
<h3 id="heading-2-tasks-gt-2-threads"><code>2 tasks -&gt; 2 threads</code></h3>
<p>Let’s run the same code with 2 threads available. We get this:</p>
<pre><code class="lang-scala"><span class="hljs-comment">// an executor with 2 threads available</span>
<span class="hljs-keyword">val</span> oneThreadExecutor = <span class="hljs-type">Executors</span>.newFixedThreadPool(<span class="hljs-number">2</span>)

<span class="hljs-comment">// send 2 tasks to the executor</span>
(<span class="hljs-number">1</span> to <span class="hljs-number">2</span>).foreach(id =&gt;
   oneThreadExecutor.execute(task(id)))
</code></pre>
<pre><code><span class="hljs-number">2020.09</span><span class="hljs-number">.28</span> <span class="hljs-number">22</span>:<span class="hljs-number">42</span>:<span class="hljs-number">04</span> INFO  pool<span class="hljs-number">-1</span>-thread<span class="hljs-number">-2</span> start<span class="hljs-number">-2</span>
<span class="hljs-number">2020.09</span><span class="hljs-number">.28</span> <span class="hljs-number">22</span>:<span class="hljs-number">42</span>:<span class="hljs-number">04</span> INFO  pool<span class="hljs-number">-1</span>-thread<span class="hljs-number">-1</span> start<span class="hljs-number">-1</span>
<span class="hljs-number">2020.09</span><span class="hljs-number">.28</span> <span class="hljs-number">22</span>:<span class="hljs-number">42</span>:<span class="hljs-number">14</span> INFO  pool<span class="hljs-number">-1</span>-thread<span class="hljs-number">-1</span> end<span class="hljs-number">-1</span>
<span class="hljs-number">2020.09</span><span class="hljs-number">.28</span> <span class="hljs-number">22</span>:<span class="hljs-number">42</span>:<span class="hljs-number">14</span> INFO  pool<span class="hljs-number">-1</span>-thread<span class="hljs-number">-2</span> end<span class="hljs-number">-2</span>
</code></pre><p>Each thread can run one task at a time. We finally accomplished what we wanted, running 2 tasks concurrently, and the whole program finished in 10 seconds.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2020/12/visulavm_4.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p>That was easy because we used 2 threads (pool-1-thread-1 and pool-1-thread-2), but we want to do the same with only 1 thread.</p>
<p>Let’s identify the problem and then find a solution.</p>
<h3 id="heading-the-problem-threadsleep-is-blocking">The problem : <code>Thread.sleep is blocking</code></h3>
<p>We now understand that we cannot use <code>Thread.sleep</code> – it blocks the thread. </p>
<p>This makes it unusable until it resumes, preventing us from running 2 tasks concurrently.</p>
<p>Fortunately, there are solutions, which we'll discuss next.</p>
<h2 id="heading-first-solution-upgrade-your-jvm-with-project-loom">First Solution: Upgrade your JVM with Project Loom</h2>
<p>I mentioned before that JVM threads map one to one to OS threads. And this fatal design mistake leads us here.</p>
<p><a target="_blank" href="https://wiki.openjdk.java.net/display/loom/Main">Project Loom</a> aims to correct that by adding virtual threads.</p>
<p>Here is our code rewritten using virtual threads from Loom:</p>
<pre><code class="lang-scala"><span class="hljs-type">Thread</span>.startVirtualThread(() -&gt; {
  <span class="hljs-type">System</span>.out.println(<span class="hljs-string">"a"</span>)  
  <span class="hljs-type">Thread</span>.sleep(<span class="hljs-number">1000</span>)  
  <span class="hljs-type">System</span>.out.println(<span class="hljs-string">"b"</span>)
});
</code></pre>
<p>The amazing thing is that the <code>Thread.sleep</code> will not block anymore! It's fully async. And on top of that, virtual threads are super cheap. You could create hundreds of thousands of them without overhead or limitations.</p>
<p>All our problems are solved now – well besides the fact that Project Loom will not be available until at least JDK 17 (as of now scheduled for September 2021).</p>
<p>Oh well, let’s go back and try to solve the sleeping problem with what the JVM currently gives us.</p>
<h3 id="heading-key-insight-you-can-express-sleeping-in-terms-of-scheduling-a-task-in-the-future">Key insight: You can express sleeping in terms of scheduling a task in the future</h3>
<p>If you tell your boss that you are busy and you will resume your work in 10 minutes, your boss does not know that you are about to take a nap. They only see that you started your work in the morning then paused for 10 minutes then resumed.</p>
<p>This:</p>
<pre><code>start
sleep(<span class="hljs-number">10</span>)
end
</code></pre><p>is equivalent from the outside to this:</p>
<pre><code>start
resumeIn(<span class="hljs-number">10</span>s, end)
</code></pre><p>What we did above is to <strong>SCHEDULE</strong> the task to end in 10 seconds.</p>
<p>That’s it, we don’t need to sleep anymore. We just need to be able to schedule things in the future instead.</p>
<p>We've reduced one problem with another, one that is easier and has a simpler solution.</p>
<h3 id="heading-the-scheduling-problem">The scheduling problem</h3>
<p>Luckily for us, scheduling tasks is very simple to do. We just have to switch out the executor as follows:</p>
<pre><code>val oneThreadScheduleExecutor = Executors.newScheduledThreadPool(<span class="hljs-number">1</span>)
</code></pre><p>We can now use the <code>schedule</code> function instead of <code>execute</code>:</p>
<pre><code class="lang-scala">oneThreadScheduleExecutor.schedule
(task(<span class="hljs-number">1</span>),<span class="hljs-number">10</span>, <span class="hljs-type">TimeUnit</span>.<span class="hljs-type">SECONDS</span>)
</code></pre>
<p>Well that’s not exactly what we want. We want to split the start and end printing by 10 seconds, so let’s change our task function as follows:</p>
<pre><code class="lang-scala"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">nonBlockingTask</span></span>(id: <span class="hljs-type">Int</span>): <span class="hljs-type">Runnable</span> = () =&gt; {
  println(<span class="hljs-string">s"<span class="hljs-subst">${Thread.currentThread().getName()}</span> start-<span class="hljs-subst">$id</span>"</span>)
  <span class="hljs-keyword">val</span> endTask: <span class="hljs-type">Runnable</span> = () =&gt; 
  {
    println(<span class="hljs-string">s"<span class="hljs-subst">${Thread.currentThread().getName()}</span> end-<span class="hljs-subst">$id</span>"</span>)
  }
  <span class="hljs-comment">//instead of Thread.sleep for 10s, we schedule it in the future, no     more blocking!</span>
  oneThreadScheduleExecutor.schedule(endTask, <span class="hljs-number">10</span>, <span class="hljs-type">TimeUnit</span>.<span class="hljs-type">SECONDS</span>)
  }
</code></pre>
<pre><code><span class="hljs-number">2020.09</span><span class="hljs-number">.28</span> <span class="hljs-number">23</span>:<span class="hljs-number">35</span>:<span class="hljs-number">45</span> INFO  pool<span class="hljs-number">-1</span>-thread<span class="hljs-number">-1</span> start<span class="hljs-number">-1</span>
<span class="hljs-number">2020.09</span><span class="hljs-number">.28</span> <span class="hljs-number">23</span>:<span class="hljs-number">35</span>:<span class="hljs-number">45</span> INFO  pool<span class="hljs-number">-1</span>-thread<span class="hljs-number">-1</span> start<span class="hljs-number">-2</span>
<span class="hljs-number">2020.09</span><span class="hljs-number">.28</span> <span class="hljs-number">23</span>:<span class="hljs-number">35</span>:<span class="hljs-number">56</span> INFO  pool<span class="hljs-number">-1</span>-thread<span class="hljs-number">-1</span> end<span class="hljs-number">-1</span>
<span class="hljs-number">2020.09</span><span class="hljs-number">.28</span> <span class="hljs-number">23</span>:<span class="hljs-number">35</span>:<span class="hljs-number">56</span> INFO  pool<span class="hljs-number">-1</span>-thread<span class="hljs-number">-1</span> end<span class="hljs-number">-2</span>
</code></pre><p>Yes! We did it! Only one thread and 2 concurrent tasks that “sleep” 10 seconds each.</p>
<p>Ok great, but you cannot really write code like this. What if you want another task in the middle as follows:</p>
<pre><code><span class="hljs-number">00</span>:<span class="hljs-number">00</span>:<span class="hljs-number">00</span> start
<span class="hljs-number">00</span>:<span class="hljs-number">00</span>:<span class="hljs-number">10</span> middle
<span class="hljs-number">00</span>:<span class="hljs-number">00</span>:<span class="hljs-number">20</span> end
</code></pre><p>You would need to change the implementation of the <code>nonBlockingTask</code> and add another call to <code>schedule</code> in there. And that will get pretty messy very quickly.</p>
<h2 id="heading-how-to-use-functional-programming-to-write-a-dsl-with-a-non-blocking-sleep">How to Use Functional Programming to Write a DSL with a Non-Blocking Sleep</h2>
<p>Functional Programming in Scala is a joy, and writing a DSL (domain-specific language) using FP principles is quite easy.</p>
<p>Let’s start at the end. We would like our final program to look something like this:</p>
<pre><code class="lang-scala"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">nonBlockingFunctionalTask</span></span>(id: <span class="hljs-type">Int</span>) = {
  <span class="hljs-type">Print</span>(id,<span class="hljs-string">"start"</span>) andThen 
  <span class="hljs-type">Print</span>(id,<span class="hljs-string">"middle"</span>).sleep(<span class="hljs-number">1000</span>) andThen
  <span class="hljs-type">Print</span>(id,<span class="hljs-string">"end"</span>).sleep(<span class="hljs-number">1000</span>)
}
</code></pre>
<p>This mini-language will achieve exactly the same behavior as our previous solution but without exposing all the nasty internals of the scheduled executor and threads.</p>
<h3 id="heading-the-model">The model</h3>
<p>Let’s define our data types:</p>
<pre><code class="lang-scala"><span class="hljs-class"><span class="hljs-keyword">object</span> <span class="hljs-title">Task</span> </span>{
<span class="hljs-keyword">sealed</span> <span class="hljs-class"><span class="hljs-keyword">trait</span> <span class="hljs-title">Task</span> </span>{ self =&gt;
  <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">andThen</span></span>(other: <span class="hljs-type">Task</span>) = <span class="hljs-type">AndThen</span>(self,other)
  <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">sleep</span></span>(millis: <span class="hljs-type">Long</span>) = <span class="hljs-type">Sleep</span>(self,millis)
}

<span class="hljs-keyword">case</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">AndThen</span>(<span class="hljs-params">t1: <span class="hljs-type">Task</span>, t2: <span class="hljs-type">Task</span></span>) <span class="hljs-keyword">extends</span> <span class="hljs-title">Task</span></span>
<span class="hljs-keyword">case</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Print</span>(<span class="hljs-params">id: <span class="hljs-type">Int</span>, value: <span class="hljs-type">String</span></span>) <span class="hljs-keyword">extends</span> <span class="hljs-title">Task</span> </span>
<span class="hljs-keyword">case</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Sleep</span>(<span class="hljs-params">t1: <span class="hljs-type">Task</span>, millis: <span class="hljs-type">Long</span></span>) <span class="hljs-keyword">extends</span> <span class="hljs-title">Task</span></span>
</code></pre>
<p>In FP the data types only hold data and no behavior. So this whole code does “nothing" –  it just captures the language structure and information we want.</p>
<p>We need 2 functions:</p>
<ul>
<li><code>sleep</code> to make a task sleep</li>
<li><code>andThen</code> to chain tasks</li>
</ul>
<p>Notice that their implementation does nothing. It just wraps it in the correct class and that’s it.</p>
<p>Let’s use our <code>nonBlockingFunctionalTask</code> function:</p>
<pre><code class="lang-scala"><span class="hljs-keyword">import</span> <span class="hljs-type">Task</span>._
<span class="hljs-comment">//create 2 tasks, this does not run them, no threads involved here</span>
(<span class="hljs-number">1</span> to <span class="hljs-number">2</span>).toList.map(nonBlockingFunctionalTask)
</code></pre>
<p>It’s a description of the problem. It does nothing, it just builds a list with 2 tasks, each one describing what to do.</p>
<p>If we print the result in the REPL we get this:</p>
<pre><code class="lang-scala">res3: <span class="hljs-type">List</span>[<span class="hljs-type">Task</span>] = <span class="hljs-type">List</span>(
<span class="hljs-comment">//first task  </span>
<span class="hljs-type">AndThen</span>(<span class="hljs-type">AndThen</span>(<span class="hljs-type">Print</span>(<span class="hljs-number">1</span>,start),<span class="hljs-type">Sleep</span>(<span class="hljs-type">Print</span>(<span class="hljs-number">1</span>,middle),<span class="hljs-number">10000</span>)),<span class="hljs-type">Sleep</span>(<span class="hljs-type">Print</span>(<span class="hljs-number">1</span>,end),<span class="hljs-number">10000</span>)), 
<span class="hljs-comment">//second task  </span>
<span class="hljs-type">AndThen</span>(<span class="hljs-type">AndThen</span>(<span class="hljs-type">Print</span>(<span class="hljs-number">2</span>,start),<span class="hljs-type">Sleep</span>(<span class="hljs-type">Print</span>(<span class="hljs-number">2</span>,middle),<span class="hljs-number">10000</span>)),<span class="hljs-type">Sleep</span>(<span class="hljs-type">Print</span>(<span class="hljs-number">2</span>,end),<span class="hljs-number">10000</span>))
)
</code></pre>
<p>Let’s write the <code>interpreter</code> that will turn this tree into one that's actually running the tasks.</p>
<h3 id="heading-the-interpreter">The interpreter</h3>
<p>In FP the function that turns a description into an executable program is called the <code>interpreter</code>. It takes the description of the program, the model, and interprets it into an executable form. Here it will execute and schedule the tasks directly.</p>
<p>We first need a <code>Stack</code> that will allow us to encode the dependencies between tasks. Think that <code>start &gt;&gt;= middle &gt;&gt;= end</code> will each be pushed to the stack and then popped in order of execution. This will be evident in the implementation.</p>
<p>And now the interpreter (don’t worry if you don't understand this code, it’s a bit complicated, there is a simpler solution coming up):</p>
<pre><code class="lang-scala"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">interpret</span></span>(task: <span class="hljs-type">Task</span>, executor: <span class="hljs-type">ScheduledExecutorService</span>): <span class="hljs-type">Unit</span> = {
  <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">loop</span></span>(current: <span class="hljs-type">Task</span>, stack: <span class="hljs-type">Stack</span>[<span class="hljs-type">Task</span>]): <span class="hljs-type">Unit</span> =
  current <span class="hljs-keyword">match</span> {
    <span class="hljs-keyword">case</span> <span class="hljs-type">AndThen</span>(t1, t2) =&gt;
      loop(t1,stack.push(t2))
    <span class="hljs-keyword">case</span> <span class="hljs-type">Print</span>(id, value) =&gt;  
      stack.pop <span class="hljs-keyword">match</span> {
        <span class="hljs-keyword">case</span> <span class="hljs-type">Some</span>((t2, b)) =&gt; 
          executor.execute(() =&gt; {
          println(<span class="hljs-string">s"<span class="hljs-subst">${Thread.currentThread().getName()}</span> <span class="hljs-subst">$value</span>-<span class="hljs-subst">$id</span>"</span>)
          })   
        loop(t2,b)
        <span class="hljs-keyword">case</span> <span class="hljs-type">None</span> =&gt; 
          executor.execute(() =&gt; {
          println(<span class="hljs-string">s"<span class="hljs-subst">${Thread.currentThread().getName()}</span> <span class="hljs-subst">$value</span>-<span class="hljs-subst">$id</span>"</span>)
          })
    <span class="hljs-keyword">case</span> <span class="hljs-type">Sleep</span>(t1,millis) =&gt; 
      <span class="hljs-keyword">val</span> r: <span class="hljs-type">Runnable</span> = () =&gt;{loop(t1,stack)}
      executor.schedule(r, millis, <span class="hljs-type">TimeUnit</span>.<span class="hljs-type">MILLISECONDS</span>)
}
loop(task,<span class="hljs-type">Nil</span>)
}
</code></pre>
<p>And the output is what we want:</p>
<pre><code><span class="hljs-number">2020.09</span><span class="hljs-number">.29</span> <span class="hljs-number">00</span>:<span class="hljs-number">06</span>:<span class="hljs-number">39</span> INFO  pool<span class="hljs-number">-1</span>-thread<span class="hljs-number">-1</span> start<span class="hljs-number">-1</span>
<span class="hljs-number">2020.09</span><span class="hljs-number">.29</span> <span class="hljs-number">00</span>:<span class="hljs-number">06</span>:<span class="hljs-number">39</span> INFO  pool<span class="hljs-number">-1</span>-thread<span class="hljs-number">-1</span> start<span class="hljs-number">-2</span>
<span class="hljs-number">2020.09</span><span class="hljs-number">.29</span> <span class="hljs-number">00</span>:<span class="hljs-number">06</span>:<span class="hljs-number">50</span> INFO  pool<span class="hljs-number">-1</span>-thread<span class="hljs-number">-1</span> middle<span class="hljs-number">-1</span>
<span class="hljs-number">2020.09</span><span class="hljs-number">.29</span> <span class="hljs-number">00</span>:<span class="hljs-number">06</span>:<span class="hljs-number">50</span> INFO  pool<span class="hljs-number">-1</span>-thread<span class="hljs-number">-1</span> middle<span class="hljs-number">-2</span>
<span class="hljs-number">2020.09</span><span class="hljs-number">.29</span> <span class="hljs-number">00</span>:<span class="hljs-number">07</span>:<span class="hljs-number">00</span> INFO  pool<span class="hljs-number">-1</span>-thread<span class="hljs-number">-1</span> end<span class="hljs-number">-1</span>
<span class="hljs-number">2020.09</span><span class="hljs-number">.29</span> <span class="hljs-number">00</span>:<span class="hljs-number">07</span>:<span class="hljs-number">00</span> INFO  pool<span class="hljs-number">-1</span>-thread<span class="hljs-number">-1</span> end<span class="hljs-number">-2</span>
</code></pre><p>One thread running 2 concurrent sleeping tasks. That’s a lot of code and a lot of work. As usual, you should always ask yourself whether there a library that already solves this problem. Turns out there is: ZIO.</p>
<h2 id="heading-non-blocking-sleep-in-zio">Non-Blocking Sleep in ZIO</h2>
<p><code>**[ZIO](https://zio.dev/)**</code> is a functional library for asynchronous and concurrent programming. It works in a similar fashion to our little DSL, because it gives you a few types you can mix and match to describe your program and nothing more.</p>
<p>And then it gives us an interpreter that lets you run a ZIO program.</p>
<p>As I said this interpreter pattern is pervasive in the world of FP. Once you get it, a new world opens up to you.</p>
<h3 id="heading-ziosleep-a-better-version-of-threadsleep"><code>ZIO.sleep</code> – a better version of <code>Thread.sleep</code></h3>
<p><code>ZIO</code> gives us the <code>ZIO.sleep</code> function, a non-blocking version of <code>Thread.sleep</code>. Here is our function written using <code>ZIO</code>:</p>
<pre><code class="lang-scala"><span class="hljs-keyword">import</span> zio._
<span class="hljs-keyword">import</span> zio.console._
<span class="hljs-keyword">import</span> zio.duration._
<span class="hljs-class"><span class="hljs-keyword">object</span> <span class="hljs-title">ZIOApp</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">zio</span>.<span class="hljs-title">App</span> </span>{
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">zioTask</span></span>(id: <span class="hljs-type">Int</span>) = 
  <span class="hljs-keyword">for</span> {
  _ &lt;- putStrLn(<span class="hljs-string">s"<span class="hljs-subst">${Thread.currentThread().getName()}</span> start-<span class="hljs-subst">$id</span>"</span>)
  _ &lt;- <span class="hljs-type">ZIO</span>.sleep(<span class="hljs-number">10.</span>seconds)
  _ &lt;- putStrLn(<span class="hljs-string">s"<span class="hljs-subst">${Thread.currentThread().getName()}</span> end-<span class="hljs-subst">$id</span>"</span>)
} <span class="hljs-keyword">yield</span> ()
</code></pre>
<p>It’s strikingly similar to the first snippet:</p>
<pre><code class="lang-scala"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">task</span></span>(id: <span class="hljs-type">Int</span>): <span class="hljs-type">Runnable</span> = () =&gt; 
{
  println(<span class="hljs-string">s"<span class="hljs-subst">${Thread.currentThread().getName()}</span> start-<span class="hljs-subst">$id</span>"</span>)
  <span class="hljs-type">Thread</span>.sleep(<span class="hljs-number">10000</span>)
  println(<span class="hljs-string">s"<span class="hljs-subst">${Thread.currentThread().getName()}</span> end-<span class="hljs-subst">$id</span>"</span>)
}
</code></pre>
<p>The clear difference is the <code>for</code> syntax that allows us to chain statements with the <code>ZIO</code> type. It's very similar to the <code>andThen</code> function from our previous mini-language.</p>
<p>As before with our mini-language, this program is just a description. It’s pure data, and it does nothing. To do something we need the interpreter.</p>
<h3 id="heading-the-zio-interpreter">The ZIO interpreter</h3>
<p>To interpret a ZIO program, you just have to extend the <code>ZIO.App</code> interface and put it in the <code>run</code> method and <code>ZIO</code> will take care of running it, like this:</p>
<pre><code class="lang-scala"><span class="hljs-class"><span class="hljs-keyword">object</span> <span class="hljs-title">ZIOApp</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">zio</span>.<span class="hljs-title">App</span> </span>
{ 
<span class="hljs-keyword">override</span> <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">run</span></span>(args: <span class="hljs-type">List</span>[<span class="hljs-type">String</span>]) = {
  <span class="hljs-type">ZIO</span>
  <span class="hljs-comment">//start 2 ZIO tasks in parallel</span>
  .foreachPar((<span class="hljs-number">1</span> to <span class="hljs-number">2</span>))(zioTasks)
  <span class="hljs-comment">//complete program when done</span>
  .as(<span class="hljs-type">ExitCode</span>.success) 
}
</code></pre>
<p>And we get this output – the tasks complete correctly in 10 seconds:</p>
<pre><code><span class="hljs-number">2020.09</span><span class="hljs-number">.29</span> <span class="hljs-number">00</span>:<span class="hljs-number">45</span>:<span class="hljs-number">12</span> INFO  zio-<span class="hljs-keyword">default</span>-<span class="hljs-keyword">async</span><span class="hljs-number">-3</span><span class="hljs-number">-1594199808</span> start<span class="hljs-number">-2</span>
<span class="hljs-number">2020.09</span><span class="hljs-number">.29</span> <span class="hljs-number">00</span>:<span class="hljs-number">45</span>:<span class="hljs-number">12</span> INFO  zio-<span class="hljs-keyword">default</span>-<span class="hljs-keyword">async</span><span class="hljs-number">-2</span><span class="hljs-number">-1594199808</span> start<span class="hljs-number">-1</span>
<span class="hljs-number">2020.09</span><span class="hljs-number">.29</span> <span class="hljs-number">00</span>:<span class="hljs-number">45</span>:<span class="hljs-number">33</span> INFO  zio-<span class="hljs-keyword">default</span>-<span class="hljs-keyword">async</span><span class="hljs-number">-7</span><span class="hljs-number">-1594199808</span> end<span class="hljs-number">-1</span>
<span class="hljs-number">2020.09</span><span class="hljs-number">.29</span> <span class="hljs-number">00</span>:<span class="hljs-number">45</span>:<span class="hljs-number">33</span> INFO  zio-<span class="hljs-keyword">default</span>-<span class="hljs-keyword">async</span><span class="hljs-number">-8</span><span class="hljs-number">-1594199808</span> end<span class="hljs-number">-2</span>
</code></pre><h2 id="heading-takeaways">Takeaways</h2>
<ul>
<li>Each JVM Thread maps to an OS thread, in a <strong>one to one fashion</strong>. And this is the root of a lot of problems.</li>
<li><code>Thread.sleep</code> is bad! It <strong>blocks the current thread</strong> and renders it unusable for further work.</li>
<li><strong>Project Loom</strong> (that will be available in JDK 17) will solve a lot of issues. <a target="_blank" href="https://www.youtube.com/watch?v=SJeAb-XEIe8">Here is a cool talk about it</a>.</li>
<li>You can use <code>ScheduledExecutorService</code> to achieve <strong>non-blocking sleep</strong>.</li>
<li>You can use <strong>Functional Programming to model a language</strong> where doing sleep is non-blocking.</li>
<li>The <strong>ZIO library</strong> provides a non-blocking sleep out of the box.</li>
</ul>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ How to implement Change Data Capture using Kafka Streams ]]>
                </title>
                <description>
                    <![CDATA[ By Luca Florio Change Data Capture (CDC) involves observing the changes happening in a database and making them available in a form that can be exploited by other systems.  One of the most interesting use-cases is to make them available as a stream o... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/how-to-implement-the-change-data-capture-pattern-using-kafka-streams/</link>
                <guid isPermaLink="false">66d45e444a7504b7409c3398</guid>
                
                    <category>
                        <![CDATA[ Apache Kafka ]]>
                    </category>
                
                    <category>
                        <![CDATA[ cdc ]]>
                    </category>
                
                    <category>
                        <![CDATA[ change data capture ]]>
                    </category>
                
                    <category>
                        <![CDATA[ elasticsearch ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Kafka streams ]]>
                    </category>
                
                    <category>
                        <![CDATA[ MongoDB ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Scala ]]>
                    </category>
                
                    <category>
                        <![CDATA[ stream processing ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ freeCodeCamp ]]>
                </dc:creator>
                <pubDate>Mon, 20 Jan 2020 21:10:30 +0000</pubDate>
                <media:content url="https://cdn-media-2.freecodecamp.org/w1280/5f9c9dac740569d1a4ca3900.jpg" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>By Luca Florio</p>
<p><strong>Change Data Capture</strong> (CDC) involves observing the changes happening in a database and making them available in a form that can be exploited by other systems. </p>
<p>One of the most interesting use-cases is to make them available as a stream of events. This means you can, for example, catch the events and update a search index as the data are written to the database.</p>
<p>Interesting right? Let's see how to implement a CDC system that can observe the changes made to a NoSQL database (<strong>MongoDB</strong>), stream them through a message broker (<strong>Kafka</strong>), process the messages of the stream (<strong>Kafka Streams</strong>), and update a search index (<strong>Elasticsearch</strong>)!?</p>
<h2 id="heading-tldr">TL;DR</h2>
<p>The full code of the project is available on GitHub in this <a target="_blank" href="https://github.com/elleFlorio/kafka-streams-playground">repository</a>. If you want to skip all my jibber jabber and just run the example, go straight to the 
<strong>How to run the project</strong> section near the end of the article!?</p>
<h1 id="heading-use-case-amp-infrastructure">Use case &amp; infrastructure</h1>
<p>We run a web application that stores photos uploaded by users. People can share their shots, let others download them, create albums, and so on. Users can also provide a description of their photos, as well as Exif metadata and other useful information. </p>
<p>We want to store such information and use it to improve our search engine. We will focus on this part of our system that is depicted in the following diagram.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2019/12/kafka-stream-playground.png" alt="Image" width="600" height="400" loading="lazy">
<em>Photo information storage architecture</em></p>
<p>The information is provided in <code>JSON</code> format. Since I like to post my shots on <a target="_blank" href="https://unsplash.com/">Unsplash</a>, and the website provides free access to its API, I used their model for the photo <code>JSON</code> document.</p>
<p>Once the <code>JSON</code> is sent through a <code>POST</code> request to our server, we store the document inside a <strong>MongoDB</strong> database. We will also store it in <strong>Elasticsearch</strong> for indexing and quick search. </p>
<p>However, we love <strong>long exposure shots</strong>, and we would like to store in a separate index a subset of information regarding this kind of photo. It can be the exposure time, as well as the location (latitude and longitude) where the photo has been taken. In this way, we can create a map of locations where photographers usually take long exposure photos.</p>
<p>Here comes the interesting part: instead of explicitly calling Elasticsearch in our code once the photo info is stored in MongoDB, we can implement a <strong>CDC</strong> exploiting Kafka and <strong>Kafka Streams</strong>. </p>
<p>We listen to modifications to MongoDB <strong>oplog</strong> using the interface provided by MongoDB itself. When the photo is stored we send it to a <code>photo</code> Kafka topic. Using <strong>Kafka Connect</strong>, an Elasticsearch sink is configured to save everything sent to that topic to a specific index. In this way, we can index all photos stored in MongoDB automatically.</p>
<p>We need to take care of the long exposure photos too. It requires some processing of the information to extract what we need. For this reason, we use Kafka Streams to create a <strong>processing topology</strong> to:</p>
<ol>
<li>Read from the <code>photo</code> topic</li>
<li>Extract Exif and location information</li>
<li>Filter long exposure photos (exposure time &gt; 1 sec.)</li>
<li>Write to a <code>long-exposure</code> topic.</li>
</ol>
<p>Then another Elasticsearch sink will read data from the <code>long-exposure</code> topic and write it to a specific index in Elasticsearch.</p>
<p>It is quite simple, but it's enough to have fun with CDC and Kafka Streams! ?</p>
<h1 id="heading-server-implementation">Server implementation</h1>
<p>Let's have a look at what we need to implement: our server exposing the <strong>REST API</strong>s!</p>
<h3 id="heading-models-and-dao">Models and DAO</h3>
<p>First things first, we need a model of our data and a <strong>Data Access Object</strong> (DAO) to talk to our MongoDB database. </p>
<p>As I said, the model for the photo <code>JSON</code> information is the one used by Unsplash. Check out the free API <a target="_blank" href="https://unsplash.com/documentation#get-a-photo">documentation</a> for an example of the <code>JSON</code> we will use. </p>
<p>I created the mapping for the serializaion/deserialization of the photo <code>JSON</code> using <a target="_blank" href="https://github.com/spray/spray-json">spray-json</a>. I'll skip the details about this, if you are curious just look at the <a target="_blank" href="https://github.com/elleFlorio/kafka-streams-playground/tree/master/src/main/scala/com/elleflorio/kafka/streams/playground/dao/model/unsplash">repo</a>!</p>
<p>Let's focus on the model for the long exposusure photo.</p>
<pre><code class="lang-scala"><span class="hljs-keyword">case</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">LongExposurePhoto</span>(<span class="hljs-params">id: <span class="hljs-type">String</span>, exposureTime: <span class="hljs-type">Float</span>, createdAt: <span class="hljs-type">Date</span>, location: <span class="hljs-type">Location</span></span>)</span>

<span class="hljs-class"><span class="hljs-keyword">object</span> <span class="hljs-title">LongExposurePhotoJsonProtocol</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">DefaultJsonProtocol</span> </span>{
  <span class="hljs-keyword">implicit</span> <span class="hljs-keyword">val</span> longExposurePhotoFormat:<span class="hljs-type">RootJsonFormat</span>[<span class="hljs-type">LongExposurePhoto</span>] = jsonFormat(<span class="hljs-type">LongExposurePhoto</span>, <span class="hljs-string">"id"</span>, <span class="hljs-string">"exposure_time"</span>, <span class="hljs-string">"created_at"</span>, <span class="hljs-string">"location"</span>)
}
</code></pre>
<p>This is quite simple: we keep from the photo <code>JSON</code> the information about the <code>id</code>, the exposure time (<code>exposureTime</code>), when the photo has been created (<code>createdAt</code>), and the <code>location</code> where it has been taken. The <code>location</code> comprehends the <code>city</code>, the <code>country</code>, and the <code>position</code> composed of <code>latitude</code> and <code>longitude</code>.</p>
<p>The DAO consists of just the <code>PhotoDao.scala</code> class. </p>
<pre><code class="lang-scala"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">PhotoDao</span>(<span class="hljs-params">database: <span class="hljs-type">MongoDatabase</span>, photoCollection: <span class="hljs-type">String</span></span>) </span>{

  <span class="hljs-keyword">val</span> collection: <span class="hljs-type">MongoCollection</span>[<span class="hljs-type">Document</span>] = database.getCollection(photoCollection)

  <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">createPhoto</span></span>(photo: <span class="hljs-type">Photo</span>): <span class="hljs-type">Future</span>[<span class="hljs-type">String</span>] = {
    <span class="hljs-keyword">val</span> doc = <span class="hljs-type">Document</span>(photo.toJson.toString())
    doc.put(<span class="hljs-string">"_id"</span>, photo.id)
    collection.insertOne(doc).toFuture()
      .map(_ =&gt; photo.id)
  }
}
</code></pre>
<p>Since I want to keep this example minimal and focused on the CDC implementation, the DAO has just one method to create a new photo document in MongoDB. </p>
<p>It is straightforward: create a document from the photo <code>JSON</code>, and insert it in mongo using <code>id</code> as the one of the photo itself. Then, we can return the <code>id</code> of the photo just inserted in a <code>Future</code> (the MongoDB API is async).</p>
<h3 id="heading-kafka-producer">Kafka Producer</h3>
<p>Once the photo is stored inside MongoDB, we have to send it to the <code>photo</code> Kafka topic. This means we need a producer to write the message in its topic. The <code>PhotoProducer.scala</code> class looks like this.</p>
<pre><code class="lang-scala"><span class="hljs-keyword">case</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">PhotoProducer</span>(<span class="hljs-params">props: <span class="hljs-type">Properties</span>, topic: <span class="hljs-type">String</span></span>) </span>{

  createKafkaTopic(props, topic)
  <span class="hljs-keyword">val</span> photoProducer = <span class="hljs-keyword">new</span> <span class="hljs-type">KafkaProducer</span>[<span class="hljs-type">String</span>, <span class="hljs-type">String</span>](props)

  <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">sendPhoto</span></span>(photo: <span class="hljs-type">Photo</span>): <span class="hljs-type">Future</span>[<span class="hljs-type">RecordMetadata</span>] = {
    <span class="hljs-keyword">val</span> record = <span class="hljs-keyword">new</span> <span class="hljs-type">ProducerRecord</span>[<span class="hljs-type">String</span>, <span class="hljs-type">String</span>](topic, photo.id, photo.toJson.compactPrint)
    photoProducer.send(record)
  }

  <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">closePhotoProducer</span></span>(): <span class="hljs-type">Unit</span> = photoProducer.close()
}
</code></pre>
<p>I would say that this is pretty self-explanatory. The most interesting part is probably the <code>createKafkaTopic</code> method that is implemented in the <code>utils</code> package.</p>
<pre><code class="lang-scala"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">createKafkaTopic</span></span>(props: <span class="hljs-type">Properties</span>, topic: <span class="hljs-type">String</span>): <span class="hljs-type">Unit</span> = {
    <span class="hljs-keyword">val</span> adminClient = <span class="hljs-type">AdminClient</span>.create(props)
    <span class="hljs-keyword">val</span> photoTopic = <span class="hljs-keyword">new</span> <span class="hljs-type">NewTopic</span>(topic, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>)
    adminClient.createTopics(<span class="hljs-type">List</span>(photoTopic).asJava)
  }
</code></pre>
<p>This method creates the topic in Kafka setting 1 as a partition and replication factor (it is enough for this example). It is not required, but creating the topic in advance lets Kafka balance partitions, select leaders, and so on. This will be useful to get our stream topology ready to process as we start our server.</p>
<h3 id="heading-event-listener">Event Listener</h3>
<p>We have the DAO that writes in MongoDB and the producer that sends the message in Kafka. We need to glue them together in some way so that when the document is stored in MongoDB the message is sent to the <code>photo</code> topic. This is the purpose of the <code>PhotoListener.scala</code> class.</p>
<pre><code class="lang-scala"><span class="hljs-keyword">case</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">PhotoListener</span>(<span class="hljs-params">collection: <span class="hljs-type">MongoCollection</span>[<span class="hljs-type">Document</span>], producer: <span class="hljs-type">PhotoProducer</span></span>) </span>{

  <span class="hljs-keyword">val</span> cursor: <span class="hljs-type">ChangeStreamObservable</span>[<span class="hljs-type">Document</span>] = collection.watch()

  cursor.subscribe(<span class="hljs-keyword">new</span> <span class="hljs-type">Observer</span>[<span class="hljs-type">ChangeStreamDocument</span>[<span class="hljs-type">Document</span>]] {
    <span class="hljs-keyword">override</span> <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">onNext</span></span>(result: <span class="hljs-type">ChangeStreamDocument</span>[<span class="hljs-type">Document</span>]): <span class="hljs-type">Unit</span> = {
      result.getOperationType <span class="hljs-keyword">match</span> {
        <span class="hljs-keyword">case</span> <span class="hljs-type">OperationType</span>.<span class="hljs-type">INSERT</span> =&gt; {
          <span class="hljs-keyword">val</span> photo = result.getFullDocument.toJson().parseJson.convertTo[<span class="hljs-type">Photo</span>]
          producer.sendPhoto(photo).get()
          println(<span class="hljs-string">s"Sent photo with Id <span class="hljs-subst">${photo.id}</span>"</span>)
        }
        <span class="hljs-keyword">case</span> _ =&gt; println(<span class="hljs-string">s"Operation <span class="hljs-subst">${result.getOperationType}</span> not supported"</span>)
      }
    }
    <span class="hljs-keyword">override</span> <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">onError</span></span>(e: <span class="hljs-type">Throwable</span>): <span class="hljs-type">Unit</span> = println(<span class="hljs-string">s"onError: <span class="hljs-subst">$e</span>"</span>)
    <span class="hljs-keyword">override</span> <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">onComplete</span></span>(): <span class="hljs-type">Unit</span> = println(<span class="hljs-string">"onComplete"</span>)})
}
</code></pre>
<p>We exploit the <a target="_blank" href="https://docs.mongodb.com/manual/changeStreams/">Chage Streams interface</a> provided by the MongoDB scala library. </p>
<p>Here is how it works: we <code>watch()</code> the collection where photos are stored. When there is a new event (<code>onNext</code>) we run our logic. </p>
<p>For this example we are interested only in the creation of new documents, so we explicitly check that the operation is of type <code>OperationType.INSERT</code>. If the operation is the one we are interested in, we get the document and convert it to a <code>Photo</code> object to be sent by our producer. </p>
<p>That's it! With few lines of code we connected the creation of documents in MongoDB to a stream of events in Kafka.?</p>
<p>As a side note, be aware that to use the Change Streams interface <strong>we have to setup a MongoDB replica set</strong>. This means we need to run 3 instances of MongoDB and configure them to act as a replica set using the following command in mongo client:</p>
<pre><code class="lang-shell">rs.initiate({_id : "r0", members: [{ _id : 0, host : "mongo1:27017", priority : 1 },{ _id : 1, host :"mongo2:27017", priority : 0 },{ _id : 2, host : "mongo3:27017", priority : 0, arbiterOnly: true }]})
</code></pre>
<p>Here our instances are the containers we will run in the docker-compose file, that is <code>mongo1</code>, <code>mongo2</code>, and <code>mongo3</code>.</p>
<h3 id="heading-processing-topology">Processing Topology</h3>
<p>Time to build our processing topology! It will be in charge of the creation of the <code>long-exposure</code> index in Elasticsearch. The topology is described by the following diagram:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2020/01/processing-topology.png" alt="Image" width="600" height="400" loading="lazy">
<em>Processing topology</em></p>
<p>and it is implemented in the <code>LongExposureTopology.scala</code> object class.
Let's analyse every step of our processing topology.</p>
<pre><code class="lang-scala"><span class="hljs-keyword">val</span> stringSerde = <span class="hljs-keyword">new</span> <span class="hljs-type">StringSerde</span>

<span class="hljs-keyword">val</span> streamsBuilder = <span class="hljs-keyword">new</span> <span class="hljs-type">StreamsBuilder</span>()

<span class="hljs-keyword">val</span> photoSource: <span class="hljs-type">KStream</span>[<span class="hljs-type">String</span>, <span class="hljs-type">String</span>] = streamsBuilder.stream(sourceTopic, <span class="hljs-type">Consumed</span>.`<span class="hljs-keyword">with</span>`(stringSerde, stringSerde))
</code></pre>
<p>The first step is to read from a source topic. We start a stream from the <code>sourceTopic</code> (that is <code>photo</code> topic) using the <code>StreamsBuilder()</code> object. The <code>stringSerde</code> object is used to serialise and deserialise the content of the topic as a <code>String</code>. </p>
<p>Please notice that at each step of the processing we create a new stream of data with a <code>KStream</code> object. When creating the stream, we specify the key and the value produced by the stream. In our topology the key will always be a <code>String</code>. In this step the value produced is still a <code>String</code>.</p>
<pre><code class="lang-scala"><span class="hljs-keyword">val</span> covertToPhotoObject: <span class="hljs-type">KStream</span>[<span class="hljs-type">String</span>, <span class="hljs-type">Photo</span>] =
      photoSource.mapValues((_, jsonString) =&gt; {
        <span class="hljs-keyword">val</span> photo = jsonString.parseJson.convertTo[<span class="hljs-type">Photo</span>]
        println(<span class="hljs-string">s"Processing photo <span class="hljs-subst">${photo.id}</span>"</span>)
        photo
      })
</code></pre>
<p>The next step is to convert the value extracted from the <code>photo</code> topic into a proper <code>Photo</code> object. </p>
<p>So we start from the <code>photoSource</code> stream and work on the values using the <code>mapValues</code> function. We simply parse the value as a <code>JSON</code> and create the <code>Photo</code> object that will be sent in the <code>convertToPhotoObject</code> stream.</p>
<pre><code class="lang-scala"><span class="hljs-keyword">val</span> filterWithLocation: <span class="hljs-type">KStream</span>[<span class="hljs-type">String</span>, <span class="hljs-type">Photo</span>] = covertToPhotoObject.filter((_, photo) =&gt; photo.location.exists(_.position.isDefined))
</code></pre>
<p>There is no guarantee that the photo we are processing will have the info about the location, but we want it in our long exposure object. This step of the topology filters out from the <code>covertToPhotoObject</code> stream the photos that have no info about the location, and creates the <code>filterWithLocation</code> stream.</p>
<pre><code class="lang-scala"><span class="hljs-keyword">val</span> filterWithExposureTime: <span class="hljs-type">KStream</span>[<span class="hljs-type">String</span>, <span class="hljs-type">Photo</span>] = filterWithLocation.filter((_, photo) =&gt; photo.exif.exists(_.exposureTime.isDefined))
</code></pre>
<p>Another important fact for our processing is the exposure time of the photo. For this reason, we filter out from the <code>filterWithLocation</code> stream the photos without exposure time info, creating the <code>filterWithExposureTime</code>.</p>
<pre><code class="lang-scala"><span class="hljs-keyword">val</span> dataExtractor: <span class="hljs-type">KStream</span>[<span class="hljs-type">String</span>, <span class="hljs-type">LongExposurePhoto</span>] =
      filterWithExposureTime.mapValues((_, photo) =&gt; <span class="hljs-type">LongExposurePhoto</span>(photo.id, parseExposureTime(photo.exif.get.exposureTime.get), photo.createdAt, photo.location.get))
</code></pre>
<p>We now have all we need to create a <code>LongExposurePhoto</code> object! That is the result of the <code>dataExtractor</code>: it takes the <code>Photo</code> coming from the <code>filterWithExposureTime</code> stream and produces a new stream containing <code>LongExposurePhoto</code>.</p>
<pre><code class="lang-scala"><span class="hljs-keyword">val</span> longExposureFilter: <span class="hljs-type">KStream</span>[<span class="hljs-type">String</span>, <span class="hljs-type">String</span>] =
      dataExtractor.filter((_, item) =&gt; item.exposureTime &gt; <span class="hljs-number">1.0</span>).mapValues((_, longExposurePhoto) =&gt; {
        <span class="hljs-keyword">val</span> jsonString = longExposurePhoto.toJson.compactPrint
        println(<span class="hljs-string">s"completed processing: <span class="hljs-subst">$jsonString</span>"</span>)
        jsonString
      })
</code></pre>
<p>We are almost there. We now have to keep the photos with a long exposure time (that we decided is more then 1 sec.). So we create a new <code>longExposureFilter</code> stream without the photos that are not long exposure. </p>
<p>This time we also serialise the <code>LongExposurePhotos</code> into the corresponding <code>JSON</code> string, which will be written to Elasticsearch in the next step.</p>
<pre><code class="lang-scala">longExposureFilter.to(sinkTopic, <span class="hljs-type">Produced</span>.`<span class="hljs-keyword">with</span>`(stringSerde, stringSerde))

streamsBuilder.build()
</code></pre>
<p>This is the last step of our topology. We write <code>to</code> our <code>sinkTopic</code> (that is <code>long-exposure</code> topic) using the string serialiser/deserialiser what is inside the <code>longExposureFilter</code> stream.
The last command simply <code>build</code>s the topology we just created.</p>
<p>Now that we have our topology, we can use it in our server. The <code>PhotoStreamProcessor.scala</code> class is what manages the processing.</p>
<pre><code class="lang-scala"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">PhotoStreamProcessor</span>(<span class="hljs-params">kafkaProps: <span class="hljs-type">Properties</span>, streamProps: <span class="hljs-type">Properties</span>, sourceTopic: <span class="hljs-type">String</span>, sinkTopic: <span class="hljs-type">String</span></span>) </span>{

  createKafkaTopic(kafkaProps, sinkTopic)
  <span class="hljs-keyword">val</span> topology: <span class="hljs-type">Topology</span> = <span class="hljs-type">LongExposureTopology</span>.build(sourceTopic, sinkTopic)
  <span class="hljs-keyword">val</span> streams: <span class="hljs-type">KafkaStreams</span> = <span class="hljs-keyword">new</span> <span class="hljs-type">KafkaStreams</span>(topology, streamProps)

  sys.<span class="hljs-type">ShutdownHookThread</span> {
    streams.close(java.time.<span class="hljs-type">Duration</span>.ofSeconds(<span class="hljs-number">10</span>))
  }

  <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">start</span></span>(): <span class="hljs-type">Unit</span> = <span class="hljs-keyword">new</span> <span class="hljs-type">Thread</span> {
    <span class="hljs-keyword">override</span> <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">run</span></span>(): <span class="hljs-type">Unit</span> = {
      streams.cleanUp()
      streams.start()
      println(<span class="hljs-string">"Started long exposure processor"</span>)
    }
  }.start()
}
</code></pre>
<p>First we create the <code>sinkTopic</code>, using the same utility method we saw before. Then we build the stream topology and initialize a <code>KafkaStreams</code> object with that topology.</p>
<p>To start the stream processing, we need to create a dedicated <code>Thread</code> that will run the streaming while the server is alive. According to the official documentation, it is always a good idea to <code>cleanUp()</code> the stream before starting it. </p>
<p>Our <code>PhotoStreamProcessor</code> is ready to go!?</p>
<h3 id="heading-rest-api">REST API</h3>
<p>The server exposes REST APIs to send it the photo information to store. We make use of <a target="_blank" href="https://doc.akka.io/docs/akka-http/current/index.html">Akka HTTP</a> for the API implementation.</p>
<pre><code class="lang-scala"><span class="hljs-class"><span class="hljs-keyword">trait</span> <span class="hljs-title">AppRoutes</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">SprayJsonSupport</span> </span>{

  <span class="hljs-keyword">implicit</span> <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">system</span></span>: <span class="hljs-type">ActorSystem</span>
  <span class="hljs-keyword">implicit</span> <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">photoDao</span></span>: <span class="hljs-type">PhotoDao</span>
  <span class="hljs-keyword">implicit</span> <span class="hljs-keyword">lazy</span> <span class="hljs-keyword">val</span> timeout = <span class="hljs-type">Timeout</span>(<span class="hljs-number">5.</span>seconds)

  <span class="hljs-keyword">lazy</span> <span class="hljs-keyword">val</span> healthRoute: <span class="hljs-type">Route</span> = pathPrefix(<span class="hljs-string">"health"</span>) {
    concat(
      pathEnd {
        concat(
          get {
            complete(<span class="hljs-type">StatusCodes</span>.<span class="hljs-type">OK</span>)
          }
        )
      }
    )
  }

  <span class="hljs-keyword">lazy</span> <span class="hljs-keyword">val</span> crudRoute: <span class="hljs-type">Route</span> = pathPrefix(<span class="hljs-string">"photo"</span>) {
    concat(
      pathEnd {
        concat(
          post {
            entity(as[<span class="hljs-type">Photo</span>]) { photo =&gt;
              <span class="hljs-keyword">val</span> photoCreated: <span class="hljs-type">Future</span>[<span class="hljs-type">String</span>] =
                photoDao.createPhoto(photo)
              onSuccess(photoCreated) { id =&gt;
              complete((<span class="hljs-type">StatusCodes</span>.<span class="hljs-type">Created</span>, id))
              }
            }
          }
        )
      }
    )
  }

}
</code></pre>
<p>To keep the example minimal, we have only two routes:</p>
<ul>
<li><code>GET /health</code> - to check if the server is up &amp; running</li>
<li><code>POST /photo</code> - to send to the system the <code>JSON</code> of the photo information we want to store. This endpoint uses the DAO to store the document in MongoDB and returns a <code>201</code> with the id of the stored photo if the operation succeeded.</li>
</ul>
<p>This is by no means a complete set of APIs, but it is enough to run our example.?</p>
<h3 id="heading-server-main-class">Server main class</h3>
<p>OK, we implemented all the components of our server, so it's time to wrap everything up. This is our <code>Server.scala</code> object class.</p>
<pre><code class="lang-scala"><span class="hljs-keyword">implicit</span> <span class="hljs-keyword">val</span> system: <span class="hljs-type">ActorSystem</span> = <span class="hljs-type">ActorSystem</span>(<span class="hljs-string">"kafka-stream-playground"</span>)
<span class="hljs-keyword">implicit</span> <span class="hljs-keyword">val</span> materializer: <span class="hljs-type">ActorMaterializer</span> = <span class="hljs-type">ActorMaterializer</span>()
</code></pre>
<p>First a couple of <strong>Akka</strong> utility values. Since we use <a target="_blank" href="https://doc.akka.io/docs/akka-http/current/index.html">Akka HTTP</a> to run our server and REST API, these implicit values are required.</p>
<pre><code class="lang-scala"><span class="hljs-keyword">val</span> config: <span class="hljs-type">Config</span> = <span class="hljs-type">ConfigFactory</span>.load()
<span class="hljs-keyword">val</span> address = config.getString(<span class="hljs-string">"http.ip"</span>)
<span class="hljs-keyword">val</span> port = config.getInt(<span class="hljs-string">"http.port"</span>)

<span class="hljs-keyword">val</span> mongoUri = config.getString(<span class="hljs-string">"mongo.uri"</span>)
<span class="hljs-keyword">val</span> mongoDb = config.getString(<span class="hljs-string">"mongo.db"</span>)
<span class="hljs-keyword">val</span> mongoUser = config.getString(<span class="hljs-string">"mongo.user"</span>)
<span class="hljs-keyword">val</span> mongoPwd = config.getString(<span class="hljs-string">"mongo.pwd"</span>)
<span class="hljs-keyword">val</span> photoCollection = config.getString(<span class="hljs-string">"mongo.photo_collection"</span>)

<span class="hljs-keyword">val</span> kafkaHosts = config.getString(<span class="hljs-string">"kafka.hosts"</span>).split(',').toList
<span class="hljs-keyword">val</span> photoTopic = config.getString(<span class="hljs-string">"kafka.photo_topic"</span>)
<span class="hljs-keyword">val</span> longExposureTopic = config.getString(<span class="hljs-string">"kafka.long_exposure_topic"</span>)
</code></pre>
<p>Then we read all the configuration properties. We will come back to the configuration file in a moment.</p>
<pre><code class="lang-scala"><span class="hljs-keyword">val</span> kafkaProps = <span class="hljs-keyword">new</span> <span class="hljs-type">Properties</span>()
kafkaProps.put(<span class="hljs-string">"bootstrap.servers"</span>, kafkaHosts.mkString(<span class="hljs-string">","</span>))
kafkaProps.put(<span class="hljs-string">"key.serializer"</span>, <span class="hljs-string">"org.apache.kafka.common.serialization.StringSerializer"</span>)
kafkaProps.put(<span class="hljs-string">"value.serializer"</span>, <span class="hljs-string">"org.apache.kafka.common.serialization.StringSerializer"</span>)

<span class="hljs-keyword">val</span> streamProps = <span class="hljs-keyword">new</span> <span class="hljs-type">Properties</span>()
streamProps.put(<span class="hljs-type">StreamsConfig</span>.<span class="hljs-type">APPLICATION_ID_CONFIG</span>, <span class="hljs-string">"long-exp-proc-app"</span>)
streamProps.put(<span class="hljs-type">StreamsConfig</span>.<span class="hljs-type">BOOTSTRAP_SERVERS_CONFIG</span>, kafkaHosts.mkString(<span class="hljs-string">","</span>))

<span class="hljs-keyword">val</span> photoProducer = <span class="hljs-type">PhotoProducer</span>(kafkaProps, photoTopic)
<span class="hljs-keyword">val</span> photoStreamProcessor = <span class="hljs-keyword">new</span> <span class="hljs-type">PhotoStreamProcessor</span>(kafkaProps, streamProps, photoTopic, <span class="hljs-string">"long-exposure"</span>)
photoStreamProcessor.start()
</code></pre>
<p>We have to configure both our Kafka producer and the stream processor. We also start the stream processor, so the server will be ready to process the documents sent to it.</p>
<pre><code class="lang-scala"><span class="hljs-keyword">val</span> client = <span class="hljs-type">MongoClient</span>(<span class="hljs-string">s"mongodb://<span class="hljs-subst">$mongoUri</span>/<span class="hljs-subst">$mongoUser</span>"</span>)
<span class="hljs-keyword">val</span> db = client.getDatabase(mongoDb)
<span class="hljs-keyword">val</span> photoDao: <span class="hljs-type">PhotoDao</span> = <span class="hljs-keyword">new</span> <span class="hljs-type">PhotoDao</span>(db, photoCollection)
<span class="hljs-keyword">val</span> photoListener = <span class="hljs-type">PhotoListener</span>(photoDao.collection, photoProducer)
</code></pre>
<p>Also MongoDB needs to be configured. We setup the connection and initialize the DAO as well as the listener.</p>
<pre><code class="lang-scala"><span class="hljs-keyword">lazy</span> <span class="hljs-keyword">val</span> routes: <span class="hljs-type">Route</span> = healthRoute ~ crudRoute

<span class="hljs-type">Http</span>().bindAndHandle(routes, address, port)
<span class="hljs-type">Await</span>.result(system.whenTerminated, <span class="hljs-type">Duration</span>.<span class="hljs-type">Inf</span>)
</code></pre>
<p>Everything has been initialized. We create the REST routes for the communication to the server, bind them to the handlers, and finally start the server!?</p>
<h4 id="heading-server-configuration">Server configuration</h4>
<p>This is the configuration file used to setup the server:</p>
<pre><code>http {
  ip = <span class="hljs-string">"127.0.0.1"</span>
  ip = ${?SERVER_IP}

  port = <span class="hljs-number">8000</span>
  port = ${?SERVER_PORT}
}
mongo {
  uri = <span class="hljs-string">"127.0.0.1:27017"</span>
  uri = ${?MONGO_URI}
  db = <span class="hljs-string">"kafka-stream-playground"</span>
  user = <span class="hljs-string">"admin"</span>
  pwd = <span class="hljs-string">"admin"</span>
  photo_collection = <span class="hljs-string">"photo"</span>
}
kafka {
  hosts = <span class="hljs-string">"127.0.0.1:9092"</span>
  hosts = ${?KAFKA_HOSTS}
  photo_topic = <span class="hljs-string">"photo"</span>
  long_exposure_topic = <span class="hljs-string">"long-exposure"</span>
}
</code></pre><p>I think that this one does not require much explanation, right??</p>
<h1 id="heading-connectors-configuration">Connectors configuration</h1>
<p>The server we implemented writes in two Kafka topics: <code>photo</code> and <code>long-exposure</code>. But how are messages written in Elasticsearch as documents? Using <strong>Kafka Connect</strong>!</p>
<p>We can setup two connectors, one per topic, and tell the connectors to write  every message going through that topic in Elasticsearch. </p>
<p>First we need <a target="_blank" href="https://docs.confluent.io/current/connect/index.html">Kafka Connect</a>. We can use the container provided by Confluence in the docker-compose file:</p>
<pre><code class="lang-yml"><span class="hljs-attr">connect:</span>
    <span class="hljs-attr">image:</span> <span class="hljs-string">confluentinc/cp-kafka-connect</span>
    <span class="hljs-attr">ports:</span>
      <span class="hljs-bullet">-</span> <span class="hljs-number">8083</span><span class="hljs-string">:8083</span>
    <span class="hljs-attr">networks:</span>
      <span class="hljs-bullet">-</span> <span class="hljs-string">kakfa_stream_playground</span>
    <span class="hljs-attr">depends_on:</span>
      <span class="hljs-bullet">-</span> <span class="hljs-string">zookeeper</span>
      <span class="hljs-bullet">-</span> <span class="hljs-string">kafka</span>
    <span class="hljs-attr">volumes:</span>
      <span class="hljs-bullet">-</span> <span class="hljs-string">$PWD/connect-plugins:/connect-plugins</span>
    <span class="hljs-attr">environment:</span>
      <span class="hljs-attr">CONNECT_BOOTSTRAP_SERVERS:</span> <span class="hljs-string">kafka:9092</span>
      <span class="hljs-attr">CONNECT_REST_ADVERTISED_HOST_NAME:</span> <span class="hljs-string">connect</span>
      <span class="hljs-attr">CONNECT_REST_PORT:</span> <span class="hljs-number">8083</span>
      <span class="hljs-attr">CONNECT_GROUP_ID:</span> <span class="hljs-string">compose-connect-group</span>
      <span class="hljs-attr">CONNECT_CONFIG_STORAGE_TOPIC:</span> <span class="hljs-string">docker-connect-configs</span>
      <span class="hljs-attr">CONNECT_CONFIG_STORAGE_REPLICATION_FACTOR:</span> <span class="hljs-number">1</span>
      <span class="hljs-attr">CONNECT_OFFSET_FLUSH_INTERVAL_MS:</span> <span class="hljs-number">10000</span>
      <span class="hljs-attr">CONNECT_OFFSET_STORAGE_TOPIC:</span> <span class="hljs-string">docker-connect-offsets</span>
      <span class="hljs-attr">CONNECT_OFFSET_STORAGE_REPLICATION_FACTOR:</span> <span class="hljs-number">1</span>
      <span class="hljs-attr">CONNECT_STATUS_STORAGE_TOPIC:</span> <span class="hljs-string">docker-connect-status</span>
      <span class="hljs-attr">CONNECT_STATUS_STORAGE_REPLICATION_FACTOR:</span> <span class="hljs-number">1</span>
      <span class="hljs-attr">CONNECT_KEY_CONVERTER:</span> <span class="hljs-string">"org.apache.kafka.connect.storage.StringConverter"</span>
      <span class="hljs-attr">CONNECT_VALUE_CONVERTER:</span> <span class="hljs-string">"org.apache.kafka.connect.json.JsonConverter"</span>
      <span class="hljs-attr">CONNECT_VALUE_CONVERTER_SCHEMAS_ENABLE:</span> <span class="hljs-string">"false"</span>
      <span class="hljs-attr">CONNECT_INTERNAL_KEY_CONVERTER:</span> <span class="hljs-string">"org.apache.kafka.connect.json.JsonConverter"</span>
      <span class="hljs-attr">CONNECT_INTERNAL_VALUE_CONVERTER:</span> <span class="hljs-string">"org.apache.kafka.connect.json.JsonConverter"</span>
      <span class="hljs-attr">CONNECT_ZOOKEEPER_CONNECT:</span> <span class="hljs-string">zookeeper:2181</span>
      <span class="hljs-attr">CONNECT_PLUGIN_PATH:</span> <span class="hljs-string">/connect-plugins</span>
      <span class="hljs-attr">CONNECT_LOG4J_ROOT_LOGLEVEL:</span> <span class="hljs-string">INFO</span>
</code></pre>
<p>I want to focus on some of the configuration values. </p>
<p>First of all, we need to expose the port <code>8083</code> - that will be our endpoint to configure the connectors (<code>CONNECT_REST_PORT</code>). </p>
<p>We also need to map a volume to the <code>/connect-plugins</code> path, where we will place the <a target="_blank" href="https://docs.confluent.io/current/connect/kafka-connect-elasticsearch/index.html">Elasticsearch Sink Connector</a> to write to Elasticsearch. This is reflected also in the <code>CONNECT_PLUGIN_PATH</code>. </p>
<p>The <code>connect</code> container should know how to find the Kafka servers, so we set <code>CONNECT_BOOTSTRAP_SERVERS</code> as <code>kafka:9092</code>.</p>
<p>Once Kafka Connect is ready, we can send the configurations of our connectors to the <code>http://localhost:8083/connectors</code> endpoint. We need 2 connectors, one for the <code>photo</code> topic and one for the <code>long-exposure</code> topic. We can send the configuration as a <code>JSON</code> with a <code>POST</code> request.</p>
<pre><code class="lang-json">{
  <span class="hljs-attr">"name"</span>: <span class="hljs-string">"photo-connector"</span>,
  <span class="hljs-attr">"config"</span>: {
    <span class="hljs-attr">"connector.class"</span>: <span class="hljs-string">"io.confluent.connect.elasticsearch.ElasticsearchSinkConnector"</span>,
    <span class="hljs-attr">"tasks.max"</span>: <span class="hljs-string">"1"</span>,
    <span class="hljs-attr">"topics"</span>: <span class="hljs-string">"photo"</span>,
    <span class="hljs-attr">"key.converter"</span>: <span class="hljs-string">"org.apache.kafka.connect.storage.StringConverter"</span>,
    <span class="hljs-attr">"value.converter"</span>: <span class="hljs-string">"org.apache.kafka.connect.json.JsonConverter"</span>,
    <span class="hljs-attr">"value.converter.schemas.enable"</span>: <span class="hljs-string">"false"</span>,
    <span class="hljs-attr">"schema.ignore"</span>: <span class="hljs-string">"true"</span>,
    <span class="hljs-attr">"connection.url"</span>: <span class="hljs-string">"http://elastic:9200"</span>,
    <span class="hljs-attr">"type.name"</span>: <span class="hljs-string">"kafka-connect"</span>,
    <span class="hljs-attr">"behavior.on.malformed.documents"</span>: <span class="hljs-string">"warn"</span>,
    <span class="hljs-attr">"name"</span>: <span class="hljs-string">"photo-connector"</span>
  }
}
</code></pre>
<p>We explicitly say we are gonna use the <code>ElasticsearchSinkConnector</code> as the <code>connector.class</code> , as well as the <code>topics</code> that we want to sink - in this case <code>photo</code>. </p>
<p>We don't want to use a schema for the <code>value.converter</code>, so we can disable it (<code>value.converter.schemas.enable</code>) and tell the connector to ignore the schema (<code>schema.ignore</code>).</p>
<p>The connector for the <code>long-exposure</code> topic is exactly like this one. The only difference is the <code>name</code> and of course the <code>topics</code>.</p>
<h1 id="heading-how-to-run-the-project">How to run the project</h1>
<p>We have all we need to test the CDC! How can we do it? It's quite easy: simply run the <code>setup.sh</code> script in the root folder of the repo!</p>
<p>What will the script do?</p>
<ol>
<li>Run the <code>docker-compose</code> file with all the services.</li>
<li>Configure MongoDB replica set. This is required to enable the <strong>Change Stream interface</strong> to capture data changes. More info about this <a target="_blank" href="https://docs.mongodb.com/manual/changeStreams/">here</a>.</li>
<li>Configure the Kafka connectors.</li>
<li>Connect to the logs of the server.</li>
</ol>
<p>The docker-compose will run the following services:</p>
<ul>
<li>Our Server</li>
<li>3 instances of MongoDB (required for the replica set)</li>
<li>Mongoku, a MongoDB client</li>
<li>Kafka (single node)</li>
<li>Kafka connect</li>
<li>Zookeeper (required by Kafka)</li>
<li>Elasticsearch</li>
<li>Kibana</li>
</ul>
<p>There are a lot of containers to run, so make sure you have enough resources to run everything properly. If you want, remove Mongoku and Kibana from the compose-file, since they are used just for a quick look inside the DBs.</p>
<p>Once everything is up and running, you just have to send data to the server. </p>
<p>I collected some <code>JSON</code> documents of photos from Unplash that you can use to test the system in the <code>photos.txt</code> file. </p>
<p>There are a total of 10 documents, with 5 of them containing info about long exposure photos. Send them to the server running the <code>send-photos.sh</code> script in the root of the repo. Check that everything is stored in MongoDB connecting to Mongoku at <code>http://localhost:3100</code>. Then connect to Kibana at <code>http://localhost:5601</code> and you will find two indexes in Elasticsearch: <code>photo</code>, containing the JSON of all the photos stored in MongoDB, and <code>long-exposure</code>, containing just the info of the long exposure photos.</p>
<p>Amazing, right? ?</p>
<h1 id="heading-conclusion">Conclusion</h1>
<p>We made it guys!? </p>
<p>Starting from the design of the use-case, we built our system that connected a MongoDB database to Elasticsearch using CDC. </p>
<p>Kafka Streams is the enabler, allowing us to convert database events to a stream that we can process. </p>
<p>Do you need to see the whole project? Just checkout the <a target="_blank" href="https://github.com/elleFlorio/kafka-streams-playground">repository</a> on GitHub!?</p>
<p>That's it, enjoy! ?</p>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ How to understand Scala variances by building restaurants ]]>
                </title>
                <description>
                    <![CDATA[ By Luca Florio I understand that type variance is not fundamental to writing Scala code. It's been more or less a year since I've been using Scala for my day-to-day job, and honestly, I've never had to worry much about it.  However, I think it is an ... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/understand-scala-variances-building-restaurants/</link>
                <guid isPermaLink="false">66d45e4e052ad259f07e4ab3</guid>
                
                    <category>
                        <![CDATA[ covariant ]]>
                    </category>
                
                    <category>
                        <![CDATA[ contravariant ]]>
                    </category>
                
                    <category>
                        <![CDATA[ invariant ]]>
                    </category>
                
                    <category>
                        <![CDATA[ General Programming ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Scala ]]>
                    </category>
                
                    <category>
                        <![CDATA[ #variance ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ freeCodeCamp ]]>
                </dc:creator>
                <pubDate>Wed, 24 Jul 2019 16:34:04 +0000</pubDate>
                <media:content url="https://cdn-media-2.freecodecamp.org/w1280/5f9ca13e740569d1a4ca4d7c.jpg" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>By Luca Florio</p>
<p>I understand that type variance is not fundamental to writing Scala code. It's been more or less a year since I've been using Scala for my day-to-day job, and honestly, I've never had to worry much about it. </p>
<p>However, I think it is an interesting "advanced" topic, so I started to study it. It is not easy to grasp it immediately, but with the right example, it might be a little bit easier to understand. Let me try using a food-based analogy...</p>
<h2 id="heading-what-is-type-variance">What is type variance?</h2>
<p>First of all, we have to define what type variance is. When you develop in an Object-Oriented language, you can define complex types. That means that a type may be parametrized using another type (component type). </p>
<p>Think of <code>List</code> for example. You cannot define a <code>List</code> without specifying which types will be inside the list. You do it by putting the type contained in the list inside square brackets: <code>List[String]</code>. When you define a complex type, you can specify how it will vary its subtype relationship according to the relation between the component type and its subtypes. </p>
<p>Ok, sounds like a mess... Let's get a little practical. </p>
<h2 id="heading-building-a-restaurant-empire">Building a restaurant empire</h2>
<p>Our goal is to build an empire of restaurants. We want generic and specialised restaurants. Every restaurant we will open needs a menu composed of different recipes, and a (possibly) starred chef. </p>
<p>The recipes can be composed of different kinds of food (fish, meat, white meat, vegetables, etc.), while the chef we hire has to be able to cook that kind of food. This is our model. Now it's coding time!</p>
<h2 id="heading-different-types-of-food">Different types of food</h2>
<p>For our food-based example, we start by defining the <code>Trait Food</code>, providing just the name of the food. </p>
<pre><code class="lang-scala"><span class="hljs-class"><span class="hljs-keyword">trait</span> <span class="hljs-title">Food</span> </span>{

  <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">name</span></span>: <span class="hljs-type">String</span>

}
</code></pre>
<p>Then we can create <code>Meat</code> and <code>Vegetable</code>, that are subclasses of <code>Food</code>. </p>
<pre><code class="lang-scala"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Meat</span>(<span class="hljs-params">val name: <span class="hljs-type">String</span></span>) <span class="hljs-keyword">extends</span> <span class="hljs-title">Food</span></span>
</code></pre>
<pre><code class="lang-scala"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Vegetable</span>(<span class="hljs-params">val name: <span class="hljs-type">String</span></span>) <span class="hljs-keyword">extends</span> <span class="hljs-title">Food</span></span>
</code></pre>
<p>In the end, we define a <code>WhiteMeat</code> class that is a subclass of <code>Meat</code>. </p>
<pre><code class="lang-scala"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">WhiteMeat</span>(<span class="hljs-params">override val name: <span class="hljs-type">String</span></span>) <span class="hljs-keyword">extends</span> <span class="hljs-title">Meat</span>(<span class="hljs-params">name</span>)</span>
</code></pre>
<p>Sounds reasonable right? So we have this hierarchy of types.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2019/07/food_type_rel.png" alt="food subtype relationship" width="600" height="400" loading="lazy"></p>
<p>We can create some food instances of various type. They will be the ingredients of the recipes we are going to serve in our restaurants.</p>
<pre><code class="lang-scala"><span class="hljs-comment">// Food &lt;- Meat</span>
<span class="hljs-keyword">val</span> beef = <span class="hljs-keyword">new</span> <span class="hljs-type">Meat</span>(<span class="hljs-string">"beef"</span>)

<span class="hljs-comment">// Food &lt;- Meat &lt;- WhiteMeat</span>
<span class="hljs-keyword">val</span> chicken = <span class="hljs-keyword">new</span> <span class="hljs-type">WhiteMeat</span>(<span class="hljs-string">"chicken"</span>)
<span class="hljs-keyword">val</span> turkey = <span class="hljs-keyword">new</span> <span class="hljs-type">WhiteMeat</span>(<span class="hljs-string">"turkey"</span>)

<span class="hljs-comment">// Food &lt;- Vegetable</span>
<span class="hljs-keyword">val</span> carrot = <span class="hljs-keyword">new</span> <span class="hljs-type">Vegetable</span>(<span class="hljs-string">"carrot"</span>)
<span class="hljs-keyword">val</span> pumpkin = <span class="hljs-keyword">new</span> <span class="hljs-type">Vegetable</span>(<span class="hljs-string">"pumpkin"</span>)
</code></pre>
<h2 id="heading-recipe-a-covariant-type">Recipe, a covariant type</h2>
<p>Let's define the covariant type <code>Recipe</code>. It takes a component type that expresses the base food for the recipe - that is, a recipe based on meat, vegetable, etc.</p>
<pre><code class="lang-scala"><span class="hljs-class"><span class="hljs-keyword">trait</span> <span class="hljs-title">Recipe</span>[+<span class="hljs-type">A</span>] </span>{

  <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">name</span></span>: <span class="hljs-type">String</span>

  <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">ingredients</span></span>: <span class="hljs-type">List</span>[<span class="hljs-type">A</span>]

}
</code></pre>
<p>The <code>Recipe</code> has a name and a list of ingredients. The list of ingredients has the same type of <code>Recipe</code>. To express that the <code>Recipe</code> is covariant in its type <code>A</code>, we write it as <code>Recipe[+A]</code>. The generic recipe is based on every kind of food, the meat recipe is based on meat, and a white meat recipe has just white meat in its list of ingredients.</p>
<pre><code class="lang-scala"><span class="hljs-keyword">case</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">GenericRecipe</span>(<span class="hljs-params">ingredients: <span class="hljs-type">List</span>[<span class="hljs-type">Food</span>]</span>) <span class="hljs-keyword">extends</span> <span class="hljs-title">Recipe</span>[<span class="hljs-type">Food</span>] </span>{

  <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">name</span></span>: <span class="hljs-type">String</span> = <span class="hljs-string">s"Generic recipe based on <span class="hljs-subst">${ingredients.map(_.name)}</span>"</span>

}
</code></pre>
<pre><code class="lang-scala"><span class="hljs-keyword">case</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">MeatRecipe</span>(<span class="hljs-params">ingredients: <span class="hljs-type">List</span>[<span class="hljs-type">Meat</span>]</span>) <span class="hljs-keyword">extends</span> <span class="hljs-title">Recipe</span>[<span class="hljs-type">Meat</span>] </span>{

  <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">name</span></span>: <span class="hljs-type">String</span> = <span class="hljs-string">s"Meat recipe based on <span class="hljs-subst">${ingredients.map(_.name)}</span>"</span>

}
</code></pre>
<pre><code class="lang-scala"><span class="hljs-keyword">case</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">WhiteMeatRecipe</span>(<span class="hljs-params">ingredients: <span class="hljs-type">List</span>[<span class="hljs-type">WhiteMeat</span>]</span>) <span class="hljs-keyword">extends</span> <span class="hljs-title">Recipe</span>[<span class="hljs-type">WhiteMeat</span>] </span>{

  <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">name</span></span>: <span class="hljs-type">String</span> = <span class="hljs-string">s"Meat recipe based on <span class="hljs-subst">${ingredients.map(_.name)}</span>"</span>

}
</code></pre>
<p>A type is covariant if it follows the same relationship of subtypes of its component type. This means that <code>Recipe</code> follows the same subtype relationship of its component Food.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2019/07/recipe_type_rel-1.png" alt="recipe subtype relationship" width="600" height="400" loading="lazy"></p>
<p>Let's define some recipes that will be part of different menus.</p>
<pre><code class="lang-scala"><span class="hljs-comment">// Recipe[Food]: Based on Meat or Vegetable</span>
<span class="hljs-keyword">val</span> mixRecipe = <span class="hljs-keyword">new</span> <span class="hljs-type">GenericRecipe</span>(<span class="hljs-type">List</span>(chicken, carrot, beef, pumpkin))
<span class="hljs-comment">// Recipe[Food] &lt;- Recipe[Meat]: Based on any kind of Meat</span>
<span class="hljs-keyword">val</span> meatRecipe = <span class="hljs-keyword">new</span> <span class="hljs-type">MeatRecipe</span>(<span class="hljs-type">List</span>(beef, turkey))
<span class="hljs-comment">// Recipe[Food] &lt;- Recipe[Meat] &lt;- Recipe[WhiteMeat]: Based only on WhiteMeat</span>
<span class="hljs-keyword">val</span> whiteMeatRecipe = <span class="hljs-keyword">new</span> <span class="hljs-type">WhiteMeatRecipe</span>(<span class="hljs-type">List</span>(chicken, turkey))
</code></pre>
<h2 id="heading-chef-a-contravariant-type">Chef, a contravariant type</h2>
<p>We defined some recipes, but we need a chef to cook them. This gives us the chance to talk about contravariance. A type is contravariant if it follows an inverse relationship of subtypes of its component type. Let's define our complex type <code>Chef</code>, that is contravariant in the component type. The component type will be the food that the chef can cook. </p>
<pre><code class="lang-scala"><span class="hljs-class"><span class="hljs-keyword">trait</span> <span class="hljs-title">Chef</span>[-<span class="hljs-type">A</span>] </span>{

  <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">specialization</span></span>: <span class="hljs-type">String</span>

  <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">cook</span></span>(recipe: <span class="hljs-type">Recipe</span>[<span class="hljs-type">A</span>]): <span class="hljs-type">String</span>
}
</code></pre>
<p>A <code>Chef</code> has a specialisation and a method to cook a recipe based on a specific food. We express that it is contravariant writing it as <code>Chef[-A]</code>. Now we can create a chef able to cook generic food, a chef able to cook meat and a chef specialised on white meat.</p>
<pre><code class="lang-scala"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">GenericChef</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Chef</span>[<span class="hljs-type">Food</span>] </span>{

  <span class="hljs-keyword">val</span> specialization = <span class="hljs-string">"All food"</span>

  <span class="hljs-keyword">override</span> <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">cook</span></span>(recipe: <span class="hljs-type">Recipe</span>[<span class="hljs-type">Food</span>]): <span class="hljs-type">String</span> = <span class="hljs-string">s"I made a <span class="hljs-subst">${recipe.name}</span>"</span>
}
</code></pre>
<pre><code class="lang-scala"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">MeatChef</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Chef</span>[<span class="hljs-type">Meat</span>] </span>{

  <span class="hljs-keyword">val</span> specialization = <span class="hljs-string">"Meat"</span>

  <span class="hljs-keyword">override</span> <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">cook</span></span>(recipe: <span class="hljs-type">Recipe</span>[<span class="hljs-type">Meat</span>]): <span class="hljs-type">String</span> = <span class="hljs-string">s"I made a <span class="hljs-subst">${recipe.name}</span>"</span>
}
</code></pre>
<pre><code class="lang-scala"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">WhiteMeatChef</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Chef</span>[<span class="hljs-type">WhiteMeat</span>] </span>{

  <span class="hljs-keyword">override</span> <span class="hljs-keyword">val</span> specialization = <span class="hljs-string">"White meat"</span>

  <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">cook</span></span>(recipe: <span class="hljs-type">Recipe</span>[<span class="hljs-type">WhiteMeat</span>]): <span class="hljs-type">String</span> = <span class="hljs-string">s"I made a <span class="hljs-subst">${recipe.name}</span>"</span>
}
</code></pre>
<p>Since <code>Chef</code> is contravariant, <code>Chef[Food]</code> is a subclass of <code>Chef[Meat]</code> that is a subclass of <code>Chef[WhiteMeat]</code>. This means that the relationship between subtypes is the inverse of its component type Food.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2019/07/chef_type_rel-1.png" alt="chef subtype relationship" width="600" height="400" loading="lazy"></p>
<p>Ok, we can now define different chef with various specialization to hire in our restaurants.</p>
<pre><code class="lang-scala"><span class="hljs-comment">// Chef[WhiteMeat]: Can cook only WhiteMeat</span>
<span class="hljs-keyword">val</span> giuseppe = <span class="hljs-keyword">new</span> <span class="hljs-type">WhiteMeatChef</span>
giuseppe.cook(whiteMeatRecipe)

<span class="hljs-comment">// Chef[WhiteMeat] &lt;- Chef[Meat]: Can cook only Meat</span>
<span class="hljs-keyword">val</span> alfredo = <span class="hljs-keyword">new</span> <span class="hljs-type">MeatChef</span>
alfredo.cook(meatRecipe)
alfredo.cook(whiteMeatRecipe)

<span class="hljs-comment">// Chef[WhiteMeat]&lt;- Chef[Meat] &lt;- Chef[Food]: Can cook any Food</span>
<span class="hljs-keyword">val</span> mario = <span class="hljs-keyword">new</span> <span class="hljs-type">GenericChef</span>
mario.cook(mixRecipe)
mario.cook(meatRecipe)
mario.cook(whiteMeatRecipe)
</code></pre>
<h2 id="heading-restaurant-where-things-come-together">Restaurant, where things come together</h2>
<p>We have recipes, we have chefs, now we need a restaurant where the chef can cook a menu of recipes.</p>
<pre><code class="lang-scala"><span class="hljs-class"><span class="hljs-keyword">trait</span> <span class="hljs-title">Restaurant</span>[<span class="hljs-type">A</span>] </span>{

  <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">menu</span></span>: <span class="hljs-type">List</span>[<span class="hljs-type">Recipe</span>[<span class="hljs-type">A</span>]]
  <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">chef</span></span>: <span class="hljs-type">Chef</span>[<span class="hljs-type">A</span>]

  <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">cookMenu</span></span>: <span class="hljs-type">List</span>[<span class="hljs-type">String</span>] = menu.map(chef.cook)
}
</code></pre>
<p>We are not interested in the subtype relationship between restaurants, so we can define it as invariant. An invariant type does not follow the relationship between the subtypes of the component type. In other words, <code>Restaurant[Food]</code> is not a subclass or superclass of <code>Restaurant[Meat]</code>. They are simply unrelated.
We will have a <code>GenericRestaurant</code>, where you can eat different type of food. The <code>MeatRestaurant</code> is specialised in meat-based dished and the <code>WhiteMeatRestaurant</code> is specialised only in dishes based on white meat. Every restaurant to be instantiated needs a menu, that is a list of recipes, and a chef able to cook the recipes in the menu. Here is where the subtype relationship of <code>Recipe</code> and <code>Chef</code> comes into play.</p>
<pre><code class="lang-scala"><span class="hljs-keyword">case</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">GenericRestaurant</span>(<span class="hljs-params">menu: <span class="hljs-type">List</span>[<span class="hljs-type">Recipe</span>[<span class="hljs-type">Food</span>]], chef: <span class="hljs-type">Chef</span>[<span class="hljs-type">Food</span>]</span>) <span class="hljs-keyword">extends</span> <span class="hljs-title">Restaurant</span>[<span class="hljs-type">Food</span>]</span>
</code></pre>
<pre><code class="lang-scala"><span class="hljs-keyword">case</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">MeatRestaurant</span>(<span class="hljs-params">menu: <span class="hljs-type">List</span>[<span class="hljs-type">Recipe</span>[<span class="hljs-type">Meat</span>]], chef: <span class="hljs-type">Chef</span>[<span class="hljs-type">Meat</span>]</span>) <span class="hljs-keyword">extends</span> <span class="hljs-title">Restaurant</span>[<span class="hljs-type">Meat</span>]</span>
</code></pre>
<pre><code class="lang-scala"><span class="hljs-keyword">case</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">WhiteMeatRestaurant</span>(<span class="hljs-params">menu: <span class="hljs-type">List</span>[<span class="hljs-type">Recipe</span>[<span class="hljs-type">WhiteMeat</span>]], chef: <span class="hljs-type">Chef</span>[<span class="hljs-type">WhiteMeat</span>]</span>) <span class="hljs-keyword">extends</span> <span class="hljs-title">Restaurant</span>[<span class="hljs-type">WhiteMeat</span>]</span>
</code></pre>
<p>Let's start defining some generic restaurants. In a generic restaurant, the menu is composed of recipes of various type of food. Since <code>Recipe</code> is covariant, a <code>GenericRecipe</code> is a superclass of <code>MeatRecipe</code> and <code>WhiteMeatRecipe</code>, so I can pass them to my <code>GenericRestaurant</code> instance. The thing is different for the chef. If the Restaurant requires a chef that can cook generic food, I cannot put in it a chef able to cook only a specific one. The class <code>Chef</code> is covariant, so <code>GenericChef</code> is a subclass of <code>MeatChef</code> that is a subclass of <code>WhiteMeatChef</code>. This implies that I cannot pass to my instance anything different from <code>GenericChef</code>.</p>
<pre><code class="lang-scala"><span class="hljs-keyword">val</span> allFood = <span class="hljs-keyword">new</span> <span class="hljs-type">GenericRestaurant</span>(<span class="hljs-type">List</span>(mixRecipe), mario)
<span class="hljs-keyword">val</span> foodParadise = <span class="hljs-keyword">new</span> <span class="hljs-type">GenericRestaurant</span>(<span class="hljs-type">List</span>(meatRecipe), mario)
<span class="hljs-keyword">val</span> superFood = <span class="hljs-keyword">new</span> <span class="hljs-type">GenericRestaurant</span>(<span class="hljs-type">List</span>(whiteMeatRecipe), mario)
</code></pre>
<p>The same goes for <code>MeatRestaurant</code> and <code>WhiteMeatRestaurant</code>. I can pass to the instance only a menu composed of more specific recipes then the required one, but chefs that can cook food more generic than the required one.</p>
<pre><code class="lang-scala"><span class="hljs-keyword">val</span> meat4All = <span class="hljs-keyword">new</span> <span class="hljs-type">MeatRestaurant</span>(<span class="hljs-type">List</span>(meatRecipe), alfredo)
<span class="hljs-keyword">val</span> meetMyMeat = <span class="hljs-keyword">new</span> <span class="hljs-type">MeatRestaurant</span>(<span class="hljs-type">List</span>(whiteMeatRecipe), mario)
</code></pre>
<pre><code class="lang-scala"><span class="hljs-keyword">val</span> notOnlyChicken = <span class="hljs-keyword">new</span> <span class="hljs-type">WhiteMeatRestaurant</span>(<span class="hljs-type">List</span>(whiteMeatRecipe), giuseppe)
<span class="hljs-keyword">val</span> whiteIsGood = <span class="hljs-keyword">new</span> <span class="hljs-type">WhiteMeatRestaurant</span>(<span class="hljs-type">List</span>(whiteMeatRecipe), alfredo)
<span class="hljs-keyword">val</span> wingsLovers = <span class="hljs-keyword">new</span> <span class="hljs-type">WhiteMeatRestaurant</span>(<span class="hljs-type">List</span>(whiteMeatRecipe), mario)
</code></pre>
<p>That's it, our empire of restaurants is ready to make tons of money!</p>
<h2 id="heading-conclusion">Conclusion</h2>
<p>Ok guys, in this story I did my best to explain type variances in Scala. It is an advanced topic, but it is worth to know just out of curiosity. I hope that the restaurant example can be of help to make it more understandable. If something is not clear, or if I wrote something wrong (I'm still learning!) don't hesitate to leave a comment!</p>
<p>See you! ?</p>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ Futures Made Easy with Scala ]]>
                </title>
                <description>
                    <![CDATA[ By Martin Budi Future is an abstraction to represent the completion of an asynchronous operation. Today it is commonly used in popular languages from Java to Dart. However, as modern applications are becoming more complex, composing them is also beco... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/futures-made-easy-with-scala-da1beb3bb281/</link>
                <guid isPermaLink="false">66c34b250f58901a62091788</guid>
                
                    <category>
                        <![CDATA[ Java ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Scala ]]>
                    </category>
                
                    <category>
                        <![CDATA[ software development ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Software Engineering ]]>
                    </category>
                
                    <category>
                        <![CDATA[ tech  ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ freeCodeCamp ]]>
                </dc:creator>
                <pubDate>Fri, 26 Apr 2019 22:06:18 +0000</pubDate>
                <media:content url="https://cdn-media-1.freecodecamp.org/images/1*DBWep0oc9Om-9DzWoasoZQ.jpeg" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>By Martin Budi</p>
<p>Future is an abstraction to represent the completion of an asynchronous operation. Today it is commonly used in popular languages from Java to Dart. However, as modern applications are becoming more complex, composing them is also becoming more difficult. Scala utilizes a functional approach that makes it easy to visualize and construct Future composition.</p>
<p>This article aims to explain the basics in a pragmatic way. No jargon, no foreign terminology. You don’t even have to be a Scala programmer (yet). All you need to have is some understanding of a couple of higher-order functions: map and foreach. So let’s get started.</p>
<p>In Scala, a future can be created as simple as this:</p>
<pre><code class="lang-scala"><span class="hljs-type">Future</span> {<span class="hljs-string">"Hi"</span>}
</code></pre>
<p>Now let’s run it and make a “Hi World”.</p>
<pre><code class="lang-scala"><span class="hljs-type">Future</span> {<span class="hljs-string">"Hi"</span>} .foreach (z =&gt; println(z + <span class="hljs-string">" World"</span>))
</code></pre>
<p>That’s all there is. We just ran a future using <code>foreach</code>, manipulated the result a bit, and printed it to the console.</p>
<p>But how is it possible? So we normally associate foreach and map with collections: we unwrap the content and tinker with it. If you look at it, it’s conceptually similar to a future in the way we want to unwrap the output from <code>Future{}</code>and manipulate it. To have this happen the future needs to be completed first, hence “running” it. This is the reasoning behind the functional composition of Scala Future.</p>
<p>In realistic applications, we want to coordinate not just one but several futures at once. A particular challenge is how to arrange them to run <strong>sequentially</strong> or <strong>simultaneously</strong>.</p>
<h4 id="heading-sequential-run"><strong>Sequential run</strong></h4>
<p>When several futures start one after another like a relay race we call it sequential run. A typical solution would simply be placing a task in the previous task’s callback, a technique known as chaining. The concept is correct but it doesn’t look pretty.</p>
<p>In Scala, we can use for-comprehension to help us abstract it. To see how it looks, let’s just go straight to an example.</p>
<pre><code class="lang-scala"><span class="hljs-keyword">import</span> scala.concurrent.<span class="hljs-type">ExecutionContext</span>.<span class="hljs-type">Implicits</span>.global

<span class="hljs-class"><span class="hljs-keyword">object</span> <span class="hljs-title">Main</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">App</span> </span>{

  <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">job</span></span>(n: <span class="hljs-type">Int</span>) = <span class="hljs-type">Future</span> {
    <span class="hljs-type">Thread</span>.sleep(<span class="hljs-number">1000</span>)
    println(n) <span class="hljs-comment">// for demo only as this is side-effecting </span>
    n + <span class="hljs-number">1</span>
  }

  <span class="hljs-keyword">val</span> f = <span class="hljs-keyword">for</span> {
    f1 &lt;- job(<span class="hljs-number">1</span>)
    f2 &lt;- job(f1)
    f3 &lt;- job(f2)
    f4 &lt;- job(f3)
    f5 &lt;- job(f4)
  } <span class="hljs-keyword">yield</span> <span class="hljs-type">List</span>(f1, f2, f3, f4, f5)
  f.map(z =&gt; println(<span class="hljs-string">s"Done. <span class="hljs-subst">${z.size}</span> jobs run"</span>))
  <span class="hljs-type">Thread</span>.sleep(<span class="hljs-number">6000</span>) <span class="hljs-comment">// needed to prevent main thread from quitting </span>
                     <span class="hljs-comment">// too early </span>
}
</code></pre>
<p>The first thing to do is importing <em>ExecutionContext</em> whose role is to manage thread pool. Without it, our future will not run.</p>
<p>Next, we define our “big job” which simply waits for a second and returns its input incremented by one.</p>
<p>Then we have our for-comprehension block. In this structure, each line inside assigns a job’s result to a value with <code>&amp;l</code>t;- which will then be available for any subsequent futures. We have arranged our jobs so that except for the first one, each one takes in the output of the previous job.</p>
<p>Also, note that the result of a for-comprehension is also a future with output determined by <strong>yield.</strong> After the execution, the result will be available inside <code>map</code>. For our purpose, we simply put all the jobs’ outputs in a list and take its size.</p>
<p>Let’s run it.</p>
<p><img src="https://cdn-media-1.freecodecamp.org/images/dzz5CXOB2TXLAzGnPcxQJpjjlgjaz-VomVBW" alt="Image" width="274" height="112" loading="lazy">
<em>Sequential run</em></p>
<p>We can see the five futures fired one-by-one. It is important to note that this arrangement should only be used when the future is dependent on the previous future.</p>
<h4 id="heading-simultaneous-or-parallel-run"><strong>Simultaneous or Parallel run</strong></h4>
<p>If the futures are independent of each other then they should be fired simultaneously. For this purpose, we’re going to use <em>Future.sequence</em>. The name is a bit confusing, but in principle it simply takes a list of futures and transforms it into a future of list. The evaluation, however, is done asynchronously.</p>
<p>Let’s create an example of mixed sequential and parallel futures.</p>
<pre><code class="lang-scala"><span class="hljs-keyword">val</span> f = <span class="hljs-keyword">for</span> {
  f1 &lt;- job(<span class="hljs-number">1</span>)
  f2 &lt;- <span class="hljs-type">Future</span>.sequence(<span class="hljs-type">List</span>(job(f1), job(f1)))
  f3 &lt;- job(f2.head)
  f4 &lt;- <span class="hljs-type">Future</span>.sequence(<span class="hljs-type">List</span>(job(f3), job(f3)))
  f5 &lt;- job(f4.head)
} <span class="hljs-keyword">yield</span> f2.size + f4.size
f.foreach(z =&gt; println(<span class="hljs-string">s"Done. <span class="hljs-subst">$z</span> jobs run in parallel"</span>))
</code></pre>
<p>Future.sequence takes a list of futures that we wish to run simultaneously. So here we have f2 and f4 containing two parallel jobs. As the argument fed into Future.sequence is a list, the result is also a list. In a realistic application, the results may be combined for further computation. Here we’ll take the first element from each list with <code>.head</code> then pass it to f3 and f5 respectively.</p>
<p>Let’s see it in action:</p>
<p><img src="https://cdn-media-1.freecodecamp.org/images/9yDEuET5UU-nSsW8VyNKMY4rkBP9igBs8w7s" alt="Image" width="278" height="142" loading="lazy">
<em>Parallel run</em></p>
<p>We can see the jobs in 2 and 4 fired simultaneously indicating successful parallelism. It is worth noting that parallel execution is not always guaranteed since it depends on available threads. If there are not enough threads then only some of the jobs will run in parallel. The others, however, will wait until some more threads are freed.</p>
<h4 id="heading-recovering-from-errors"><strong>Recovering from errors</strong></h4>
<p>Scala Future incorporates <strong>recover</strong> that acts as a back-up future when an error occurs<strong>.</strong> This allows the future composition to finish even with failures. To illustrate, consider this code:</p>
<pre><code class="lang-scala"><span class="hljs-type">Future</span> {<span class="hljs-string">"abc"</span>.toInt}
.map(z =&gt; z + <span class="hljs-number">1</span>)
</code></pre>
<p>Of course, this will not work, as “abc” is not an int. With <strong>recover,</strong> we can salvage it by passing a default value. Let’s try passing a zero:</p>
<pre><code class="lang-scala"><span class="hljs-type">Future</span> {<span class="hljs-string">"abc"</span>.toInt}
.recover {<span class="hljs-keyword">case</span> e =&gt; <span class="hljs-number">0</span>}
.map(z =&gt; z + <span class="hljs-number">1</span>)
</code></pre>
<p>Now the code will run and produce one as a result. In composition, we can fine-tune each future like this to make sure the process won’t fail.</p>
<p>However, there are also times when we want to reject errors explicitly. For this purpose, we can use <em>Future.succesful and Future.failed</em> to signal validation result. And if we don’t care about individual failure we can position recover to catch <em>any</em> error inside the composition.</p>
<p>Let’s work another bit of code using for-comprehension that checks if the input is a valid int and lower than 100. Future.failed and Future.successful are both futures so we don’t need to wrap it in one. Future.failed in particular requires a <em>Throwable</em> so we’re going to create a custom one for input larger than 100. After putting it all up together we would have as follows:</p>
<pre><code class="lang-scala"><span class="hljs-keyword">val</span> input = <span class="hljs-string">"5"</span> <span class="hljs-comment">// let's try "5", "200", and "abc"</span>
<span class="hljs-keyword">case</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">NumberTooLarge</span>(<span class="hljs-params"></span>) <span class="hljs-keyword">extends</span> <span class="hljs-title">Throwable</span>(<span class="hljs-params"></span>)</span>
<span class="hljs-keyword">val</span> f = <span class="hljs-keyword">for</span> {
   f1 &lt;- <span class="hljs-type">Future</span>{ input.toInt }
   f2 &lt;- <span class="hljs-keyword">if</span> (f1 &gt; <span class="hljs-number">100</span>) {
            <span class="hljs-type">Future</span>.failed(<span class="hljs-type">NumberTooLarge</span>())
          } <span class="hljs-keyword">else</span> {
            <span class="hljs-type">Future</span>.successful(f1)
          }
} <span class="hljs-keyword">yield</span> f2
f map(println) recover {<span class="hljs-keyword">case</span> e =&gt; e.printStackTrace()}
</code></pre>
<p>Notice the positioning of recover. With this configuration, it will simply intercept any error occurring inside the block. Let’s test it with several different inputs “5”, “200”, and “abc”:</p>
<pre><code><span class="hljs-string">"5"</span>   -&gt; <span class="hljs-number">5</span>
<span class="hljs-string">"200"</span> -&gt; NumberTooLarge stacktrace
<span class="hljs-string">"abc"</span> -&gt; NumberFormatException stacktrace
</code></pre><p>“5” reached the end no problem. “200” and “abc” arrived in recover. Now, what if we want to handle each error separately? This is where pattern matching comes into play. Expanding the recover block, we can have something like this:</p>
<pre><code class="lang-scala"><span class="hljs-keyword">case</span> e =&gt; 
  e <span class="hljs-keyword">match</span> {
    <span class="hljs-keyword">case</span> t: <span class="hljs-type">NumberTooLarge</span> =&gt; <span class="hljs-comment">// deal with number &gt; 100</span>
    <span class="hljs-keyword">case</span> t: <span class="hljs-type">NumberFormatException</span> =&gt; <span class="hljs-comment">// deal with not a number</span>
    <span class="hljs-keyword">case</span> _ =&gt; <span class="hljs-comment">// deal with any other errors</span>
  }
}
</code></pre>
<p>You might probably have guessed it but an all-or-nothing scenario like this is commonly used in public APIs. Such service wouldn’t process invalid input but needs to return a message to inform the client what they did wrong. By separating exceptions, we can pass a custom message for each error. If you like to build such service (with a very fast web framework), head over to my <a target="_blank" href="https://medium.freecodecamp.org/an-introduction-to-vert-x-the-fastest-java-framework-today-27d8661ceb14">Vert.x article</a>.</p>
<h4 id="heading-the-world-outside-scala"><strong>The world outside Scala</strong></h4>
<p>We have talked a lot about how easy Scala Future is. But is it really? To answer it we need to look at how it’s done in other languages. Arguably the closest language to Scala is Java as both operate on JVM. Furthermore, Java 8 has introduced Concurrency API with <em>CompletableFuture</em> which is also able to chain futures. Let’s rework the first sequence example with it.</p>
<p><img src="https://cdn-media-1.freecodecamp.org/images/LDS0WqYRsLYNTCV4gnEh0U6DsDn8HOUGi6lb" alt="Image" width="760" height="666" loading="lazy">
<em>Sequential run in Java</em></p>
<p>That’s sure a lot of stuff. And to code this I had to look up <em>supplyAsync</em> and <em>thenApply</em> among so many methods in the documentation. And even if I know all these methods, they can only be used within the context of the API.</p>
<p>On the other hand, Scala Future is not based on API or external libraries but a functional programming concept that is also used in other aspects of Scala. So with an initial investment in covering the fundamentals, you can reap the reward of less overhead and higher flexibility.</p>
<h4 id="heading-wrapping-up">Wrapping up</h4>
<p>That’s all for the basics. There’s more to Scala Future but what we have here has covered enough ground to build real-life applications. If you like to read more about Future or Scala, in general, I’d recommend <a target="_blank" href="https://alvinalexander.com/scala/how-use-multiple-scala-futures-in-for-comprehension-loop">Alvin Alexander tutorials</a>, <a target="_blank" href="http://allaboutscala.com/tutorials/chapter-9-beginner-tutorial-using-scala-futures/">AllAboutScala</a>, and <a target="_blank" href="https://medium.com/beingprofessional/understanding-functor-and-monad-with-a-bag-of-peanuts-8fa702b3f69e">Sujit Kamthe’s article</a> that offers easy to grasp explanations.</p>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ An introduction to Law Testing in Scala ]]>
                </title>
                <description>
                    <![CDATA[ By dor sever Property-based law testing is one of the most powerful tools in the scala ecosystem. In this post, I’ll explain how to use law testing and the value it’ll give you using in-depth code examples. This post is aimed for Scala developers who... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/an-introduction-to-law-testing-in-scala-4243d72272f9/</link>
                <guid isPermaLink="false">66d45e3d4a7504b7409c337a</guid>
                
                    <category>
                        <![CDATA[ Functional Programming ]]>
                    </category>
                
                    <category>
                        <![CDATA[ General Programming ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Scala ]]>
                    </category>
                
                    <category>
                        <![CDATA[ tech  ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Testing ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ freeCodeCamp ]]>
                </dc:creator>
                <pubDate>Mon, 22 Apr 2019 16:48:22 +0000</pubDate>
                <media:content url="https://cdn-media-2.freecodecamp.org/w1280/5f9ca2eb740569d1a4ca57e8.jpg" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>By dor sever</p>
<p>Property-based law testing is one of the most powerful tools in the scala ecosystem. In this post, I’ll explain how to use law testing and the value it’ll give you using in-depth code examples.</p>
<p>This post is aimed for Scala developers who want to improve their testing knowledge and skills. It assumes some knowledge of Scala, cats, and other functional libraries.</p>
<p><img src="https://cdn-media-1.freecodecamp.org/images/GpUIRfAe79BTz8JGTvk0oOD376oxjBxj4CiS" alt="Image" width="498" height="209" loading="lazy"></p>
<h4 id="heading-introduction"><strong>Introduction</strong></h4>
<ul>
<li>You might be familiar with types which are a set of values (for example Int values are : <code>1,2,3</code>… String values are : <code>“John Doe”</code> etc).</li>
<li>You also might be familiar with functions which is a mapping from Input type to Output type.</li>
<li>A property is defined on a type or a function and describes its desired behavior.</li>
</ul>
<p>So what is a Law? <strong>Keep on reading!</strong></p>
<h4 id="heading-a-concrete-example"><strong>A concrete example</strong></h4>
<p>Here is our beloved <code>_Person_</code> data type:</p>
<pre><code><span class="hljs-keyword">case</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Person</span>(<span class="hljs-title">name</span>: <span class="hljs-title">String</span>, <span class="hljs-title">age</span>: <span class="hljs-title">Int</span>)</span>
</code></pre><p>And serialization code using the <code>Play-Json</code> , a library that allows transforming your <code>Person</code> type into <code>JSON :</code></p>
<pre><code class="lang-scala"><span class="hljs-keyword">val</span> personFormat: <span class="hljs-type">OFormat</span>[<span class="hljs-type">Person</span>] = <span class="hljs-keyword">new</span> <span class="hljs-type">OFormat</span>[<span class="hljs-type">Person</span>] {
  <span class="hljs-keyword">override</span> <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">reads</span></span>(json: <span class="hljs-type">JsValue</span>): <span class="hljs-type">JsResult</span>[<span class="hljs-type">Person</span>] = {
    <span class="hljs-keyword">val</span> name = (json \ <span class="hljs-string">"name"</span>).as[<span class="hljs-type">String</span>]
    <span class="hljs-keyword">val</span> age = (json \ <span class="hljs-string">"age"</span>).as[<span class="hljs-type">Int</span>]
    <span class="hljs-type">JsSuccess</span>(<span class="hljs-type">Person</span>(name, age))
  }
<span class="hljs-keyword">override</span> <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">writes</span></span>(o: <span class="hljs-type">Person</span>): <span class="hljs-type">JsObject</span> =
    <span class="hljs-type">JsObject</span>(<span class="hljs-type">Seq</span>(<span class="hljs-string">"name"</span> -&gt; <span class="hljs-type">JsString</span>(o.name), 
                 <span class="hljs-string">"age"</span> -&gt; <span class="hljs-type">JsNumber</span>(o.age)))
}
</code></pre>
<p>We can now test this serialization function on a specific input like this:</p>
<pre><code class="lang-scala"><span class="hljs-keyword">import</span> org.scalatest._
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">PersonSerdeSpec</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">WordSpecLike</span> <span class="hljs-keyword">with</span> <span class="hljs-title">Matchers</span> </span>{
  <span class="hljs-string">"should serialize and deserialize a person"</span> in {
    <span class="hljs-keyword">val</span> person = <span class="hljs-type">Person</span>(<span class="hljs-string">"John Doe"</span>, <span class="hljs-number">32</span>)
    <span class="hljs-keyword">val</span> actualPerson =
      personFormat.reads(personFormat.writes(person))
    actualPerson.asOpt.shouldEqual(<span class="hljs-type">Some</span>(person))
  }
}
</code></pre>
<p>But, we now need to ask ourselves, whether all people will serialize successfully? What about a person with invalid data (such as negative age)? Will we want to repeat this thought process of finding edge-cases for all our test data?</p>
<p>And most importantly, will this code remain readable over time? (e.g.: changing the <code>person</code> data type [adding a <code>LastName</code> field], repeated tests for other data types, etc)</p>
<blockquote>
<p>“ We can solve any problem by introducing an extra level of indirection”.</p>
</blockquote>
<h3 id="heading-property-based-testing">Property-based testing</h3>
<p>The first weapon in our disposal is Property-based testing (PBT). PBT works by defining a property, which is a high-level specification of behavior that should hold for all values of the specific type.</p>
<p>In our example, the <strong>property</strong> will be:</p>
<ul>
<li>For every person p, if we serialize and deserialize them, we should get back the same person.</li>
</ul>
<p>Writing this property using scala check looks like this:</p>
<pre><code class="lang-scala"><span class="hljs-class"><span class="hljs-keyword">object</span> <span class="hljs-title">PersonSerdeSpec</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">org</span>.<span class="hljs-title">scalacheck</span>.<span class="hljs-title">Properties</span>(<span class="hljs-params">"<span class="hljs-type">PersonCodec</span>"</span>) </span>{
  property(<span class="hljs-string">"round trip consistency"</span>) = 
org.scalacheck.<span class="hljs-type">Prop</span>.forAll { a: <span class="hljs-type">Person</span> =&gt;
    personFormat.reads(personFormat.writes(a)).asOpt.contains(a)
  }
}
</code></pre>
<p>The property check requires a way to generate Persons. This is done by using an <code>Arbitrary[Person]</code> which can be defined like this:</p>
<pre><code class="lang-scala"><span class="hljs-keyword">implicit</span> <span class="hljs-keyword">val</span> personArb: <span class="hljs-type">Arbitrary</span>[<span class="hljs-type">Person</span>] = <span class="hljs-type">Arbitrary</span> {
  <span class="hljs-keyword">for</span> {
    name &lt;- <span class="hljs-type">Gen</span>.alphaStr
    age  &lt;- <span class="hljs-type">Gen</span>.chooseNum(<span class="hljs-number">0</span>, <span class="hljs-number">120</span>)
  } <span class="hljs-keyword">yield</span> <span class="hljs-type">Person</span>(name, age)
}
</code></pre>
<p>Furthermore, we can use <code>“scalacheck-shapeless”</code>- an amazing library which eliminates (almost) all needs for the verbose (quite messy and highly bug-prone) arbitrary type definition by generating it for us!</p>
<p><img src="https://cdn-media-1.freecodecamp.org/images/XgoIxileqrYUmSnMMkzuGFJJCkxvy6-Ryj-Q" alt="Image" width="275" height="252" loading="lazy"></p>
<p>This can be done by adding:</p>
<pre><code>libraryDependencies += <span class="hljs-string">"com.github.alexarchambault"</span> %% <span class="hljs-string">"scalacheck-shapeless_1.14"</span> % <span class="hljs-string">"1.2.0"</span>
</code></pre><p>and importing the following in our code:</p>
<pre><code><span class="hljs-keyword">import</span> org.scalacheck.ScalacheckShapeless._
</code></pre><p>And then we can remove the <code>_personArb_</code> instance we defined earlier.</p>
<h3 id="heading-the-codec-law">The Codec Law</h3>
<p>Let’s try to abstract further, by defining the <strong>laws</strong> of our data type:</p>
<pre><code class="lang-scala"><span class="hljs-class"><span class="hljs-keyword">trait</span> <span class="hljs-title">CodecLaws</span>[<span class="hljs-type">A</span>, <span class="hljs-type">B</span>] </span>{
  <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">serialize</span></span>: <span class="hljs-type">A</span> =&gt; <span class="hljs-type">B</span>
  <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">deserialize</span></span>: <span class="hljs-type">B</span> =&gt; <span class="hljs-type">A</span>
  <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">codecRoundTrip</span></span>(a: <span class="hljs-type">A</span>): <span class="hljs-type">Boolean</span> = serialize.
andThen(deserialize)(a) == a
}
</code></pre>
<p><img src="https://cdn-media-1.freecodecamp.org/images/De-BSLaDLDfZvMXrdBFzwvufzCyHVTONXTTg" alt="Image" width="416" height="193" loading="lazy"></p>
<p>This means That given</p>
<ul>
<li>The types <code>A, B</code></li>
<li>A function from <code>A to B</code></li>
<li>A function from <code>B to A</code></li>
</ul>
<p>We define a function called “<code>codecRoundTrip</code>” which takes an <code>“a: A”</code> and takes it through the functions and makes sure we get the same value of type A back.</p>
<p>This Law states (without giving away <strong>any implementation details</strong>), that the roundtrip we do on the given input does not “lose” any information.</p>
<blockquote>
<p>Another way of saying just that is by claiming that our types A and B are isomorphic.</p>
</blockquote>
<p>We can abstract even more, by using the cats-laws library with the <code>IsEq</code> case-class for defining an Equality description.</p>
<pre><code class="lang-scala"><span class="hljs-keyword">import</span> cats.laws._
<span class="hljs-class"><span class="hljs-keyword">trait</span> <span class="hljs-title">CodecLaws</span>[<span class="hljs-type">A</span>, <span class="hljs-type">B</span>] </span>{
  <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">serialize</span></span>: <span class="hljs-type">A</span> =&gt; <span class="hljs-type">B</span>
  <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">deserialize</span></span>: <span class="hljs-type">B</span> =&gt; <span class="hljs-type">A</span>
  <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">codecRoundTrip</span></span>(a: <span class="hljs-type">A</span>): cats.laws.<span class="hljs-type">IsEq</span>[<span class="hljs-type">A</span>] = serialize.andThen(deserialize)(a) &lt;-&gt; a
}
<span class="hljs-comment">/** Represents two values of the same type that are expected to be equal. */</span>
<span class="hljs-keyword">final</span> <span class="hljs-keyword">case</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">IsEq</span>[<span class="hljs-type">A</span>](<span class="hljs-params">lhs: <span class="hljs-type">A</span>, rhs: <span class="hljs-type">A</span></span>)</span>
</code></pre>
<p>What we get from this type and syntax is a <strong>description</strong> <strong>of equality</strong> between the two values instead of the equality result like before.</p>
<h3 id="heading-the-codec-test">The Codec Test</h3>
<p>It is time to test the laws we just defined. In order to do that, we will use the “<a target="_blank" href="https://github.com/typelevel/discipline">discipline</a>” library.</p>
<pre><code><span class="hljs-keyword">import</span> cats.laws.discipline._
<span class="hljs-keyword">import</span> org.scalacheck.{ Arbitrary, Prop }
trait CodecTests[A, B] <span class="hljs-keyword">extends</span> org.typelevel.discipline.Laws {
  def laws: CodecLaws[A, B]
  def tests(
    implicit
    <span class="hljs-attr">arbitrary</span>: Arbitrary[A],
    <span class="hljs-attr">eqA</span>: cats.Eq[A]
  ): RuleSet =
    <span class="hljs-keyword">new</span> DefaultRuleSet(
      name   = name,
      parent = None,
      <span class="hljs-string">"roundTrip"</span> -&gt; Prop.forAll { <span class="hljs-attr">a</span>: <span class="hljs-function"><span class="hljs-params">A</span> =&gt;</span>
        laws.codecRoundTrip(a)
      }
    )
}
</code></pre><p>We define a CodecTest trait that takes 2 type parameters <code>A and B</code>, which in our example will be <code>Person</code> and <code>JsResult</code>.</p>
<p>The trait holds an instance of the laws and defines a test method that takes an <code>Arbitrary[A]</code> and an equality checker (of type <code>Eq[A]</code>) and returns a <code>rule-set</code> for <code>scalacheck</code> to run.</p>
<p>Note that no tests actually run here. This gives us the power to run these tests which are defined just once for all the types we want</p>
<p>We can now commit to a specific type and implementation (like <code>Play-Json</code> serialization) by instantiating a <code>CodecTest</code> with the proper types.</p>
<pre><code class="lang-scala"><span class="hljs-class"><span class="hljs-keyword">object</span> <span class="hljs-title">JsonCodecTests</span> </span>{
  <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">apply</span></span>[<span class="hljs-type">A</span>: <span class="hljs-type">Arbitrary</span>](<span class="hljs-keyword">implicit</span> format: <span class="hljs-type">Format</span>[<span class="hljs-type">A</span>]): <span class="hljs-type">CodecTests</span>[<span class="hljs-type">A</span>, <span class="hljs-type">JsValue</span>] =
    <span class="hljs-keyword">new</span> <span class="hljs-type">CodecTests</span>[<span class="hljs-type">A</span>, <span class="hljs-type">JsValue</span>] {
      <span class="hljs-keyword">override</span> <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">laws</span></span>: <span class="hljs-type">CodecLaws</span>[<span class="hljs-type">A</span>, <span class="hljs-type">JsValue</span>] =
        <span class="hljs-type">CodecLaws</span>[<span class="hljs-type">A</span>, <span class="hljs-type">JsValue</span>](format.reads, format.writes)
    }
}
</code></pre>
<h4 id="heading-a-type-detour">A (type) detour</h4>
<p>But now we get the error:</p>
<pre><code class="lang-scala"><span class="hljs-type">Error</span>:(<span class="hljs-number">11</span>, <span class="hljs-number">38</span>) <span class="hljs-class"><span class="hljs-keyword">type</span> <span class="hljs-title">mismatch</span></span>;
 found   : play.api.libs.json.<span class="hljs-type">JsResult</span>[<span class="hljs-type">A</span>]
 required: <span class="hljs-type">A</span>
</code></pre>
<p>We expected the types to flow from:</p>
<pre><code>  A  =&gt;  <span class="hljs-function"><span class="hljs-params">B</span>  =&gt;</span>  A
</code></pre><p>But Play-Json types go from:</p>
<pre><code> A  =&gt;  <span class="hljs-function"><span class="hljs-params">JsValue</span>  =&gt;</span>  JsResult[A]
</code></pre><p>This means that our deserialize function can succeed or fail and will not always return an A, but rather a container of A.</p>
<p>In order to abstract over the types, we now need to use the <code>F[_]</code> type constructor syntax:</p>
<pre><code class="lang-scala"><span class="hljs-class"><span class="hljs-keyword">trait</span> <span class="hljs-title">CodecLaws</span>[<span class="hljs-type">F</span>[_],<span class="hljs-title">A</span>, <span class="hljs-title">B</span>] </span>{
  <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">serialize</span></span>: <span class="hljs-type">A</span> =&gt; <span class="hljs-type">B</span>
  <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">deserialize</span></span>: <span class="hljs-type">B</span> =&gt; <span class="hljs-type">F</span>[<span class="hljs-type">A</span>]
  <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">codecRoundTrip</span></span>(a: <span class="hljs-type">A</span>)(<span class="hljs-keyword">implicit</span> app:<span class="hljs-type">Applicative</span>[<span class="hljs-type">F</span>]): <span class="hljs-type">IsEq</span>[<span class="hljs-type">F</span>[<span class="hljs-type">A</span>]] =
    serialize.andThen(deserialize)(a) &lt;-&gt; app.pure(a)
}
</code></pre>
<p>The <code>Applicative</code> instance is used to take a simple value of type A and lift it into the <code>Applicative</code> context which returns a value of type <code>F[A]</code>.</p>
<blockquote>
<p>This process is similar to taking some value <code>x</code> and lifting it to an <code>Option</code> context using <code>Some(x)</code>, or in our concrete example taking a value <code>a:A</code> and lifting it to the <code>JsResult</code> type using <code>[JsSuccess](https://www.playframework.com/documentation/2.7.0/api/scala/play/api/libs/json/JsSuccess.html)(a)</code>.</p>
</blockquote>
<p>We can now finish the implementation for <code>CodecTests</code> and <code>JsonCodecTests</code> like this:</p>
<pre><code class="lang-scala"><span class="hljs-class"><span class="hljs-keyword">trait</span> <span class="hljs-title">CodecTests</span>[<span class="hljs-type">F</span>[_], <span class="hljs-title">A</span>, <span class="hljs-title">B</span>] <span class="hljs-keyword">extends</span> <span class="hljs-title">org</span>.<span class="hljs-title">typelevel</span>.<span class="hljs-title">discipline</span>.<span class="hljs-title">Laws</span> </span>{
  <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">laws</span></span>: <span class="hljs-type">CodecLaws</span>[<span class="hljs-type">F</span>, <span class="hljs-type">A</span>, <span class="hljs-type">B</span>]
  <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">tests</span></span>(
    <span class="hljs-keyword">implicit</span>
    arbitrary: <span class="hljs-type">Arbitrary</span>[<span class="hljs-type">A</span>],
    eqA: cats.<span class="hljs-type">Eq</span>[<span class="hljs-type">F</span>[<span class="hljs-type">A</span>]],
    applicative: <span class="hljs-type">Applicative</span>[<span class="hljs-type">F</span>]
  ): <span class="hljs-type">RuleSet</span> =
    <span class="hljs-keyword">new</span> <span class="hljs-type">DefaultRuleSet</span>(
      name   = name,
      parent = <span class="hljs-type">None</span>,
      <span class="hljs-string">"roundTrip"</span> -&gt; <span class="hljs-type">Prop</span>.forAll { a: <span class="hljs-type">A</span> =&gt;
        laws.codecRoundTrip(a)
      }
    )
}
<span class="hljs-class"><span class="hljs-keyword">object</span> <span class="hljs-title">JsonCodecTests</span> </span>{
  <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">apply</span></span>[<span class="hljs-type">A</span>: <span class="hljs-type">Arbitrary</span>](<span class="hljs-keyword">implicit</span> format: <span class="hljs-type">Format</span>[<span class="hljs-type">A</span>]): <span class="hljs-type">CodecTests</span>[<span class="hljs-type">JsResult</span>, <span class="hljs-type">A</span>, <span class="hljs-type">JsValue</span>] =
    <span class="hljs-keyword">new</span> <span class="hljs-type">CodecTests</span>[<span class="hljs-type">JsResult</span>, <span class="hljs-type">A</span>, <span class="hljs-type">JsValue</span>] {
      <span class="hljs-keyword">override</span> <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">laws</span></span>: <span class="hljs-type">CodecLaws</span>[<span class="hljs-type">JsResult</span>, <span class="hljs-type">A</span>, <span class="hljs-type">JsValue</span>] =
        <span class="hljs-type">CodecLaws</span>[<span class="hljs-type">JsResult</span>, <span class="hljs-type">A</span>, <span class="hljs-type">JsValue</span>](format.reads, format.writes)
    }
}
</code></pre>
<p>And to define a working <code>Person</code> serialization test in <strong>1 line of code:</strong></p>
<pre><code class="lang-scala"><span class="hljs-keyword">import</span> <span class="hljs-type">JsonCodecSpec</span>.<span class="hljs-type">Person</span>
<span class="hljs-keyword">import</span> play.api.libs.json._
<span class="hljs-keyword">import</span> org.scalacheck.<span class="hljs-type">ScalacheckShapeless</span>._
<span class="hljs-keyword">import</span> org.scalatest.<span class="hljs-type">FunSuiteLike</span>
<span class="hljs-keyword">import</span> org.scalatest.prop.<span class="hljs-type">Checkers</span>
<span class="hljs-keyword">import</span> org.typelevel.discipline.scalatest.<span class="hljs-type">Discipline</span>
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">JsonCodecSpec</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Checkers</span> <span class="hljs-keyword">with</span> <span class="hljs-title">FunSuiteLike</span> <span class="hljs-keyword">with</span> <span class="hljs-title">Discipline</span> </span>{ 
  checkAll(<span class="hljs-string">"PersonSerdeTests"</span>, <span class="hljs-type">JsonCodecTests</span>[<span class="hljs-type">Person</span>].tests)
}
</code></pre>
<p><img src="https://cdn-media-1.freecodecamp.org/images/imdSWbMKWX8xBSGWLXuMy-iB04TAGBkgqg5l" alt="Image" width="498" height="280" loading="lazy"></p>
<h3 id="heading-the-power-of-abstraction">The power of abstraction</h3>
<p>We were able to define our tests and laws without giving away any implementation details. This means we can switch to using a different library for serialization tomorrow and all our laws and tests will still hold.</p>
<h4 id="heading-another-example">Another example</h4>
<p>We can test this theory by adding support to BSON serialization using the <code>reactive-mongo</code> library:</p>
<pre><code class="lang-scala"><span class="hljs-keyword">import</span> cats.<span class="hljs-type">Id</span>
<span class="hljs-keyword">import</span> io.bigpanda.laws.serde.{ <span class="hljs-type">CodecLaws</span>, <span class="hljs-type">CodecTests</span> }
<span class="hljs-keyword">import</span> org.scalacheck.<span class="hljs-type">Arbitrary</span>
<span class="hljs-keyword">import</span> reactivemongo.bson.{ <span class="hljs-type">BSONDocument</span>, <span class="hljs-type">BSONReader</span>, <span class="hljs-type">BSONWriter</span> }
<span class="hljs-class"><span class="hljs-keyword">object</span> <span class="hljs-title">BsonCodecTests</span> </span>{
  <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">apply</span></span>[<span class="hljs-type">A</span>: <span class="hljs-type">Arbitrary</span>](
    <span class="hljs-keyword">implicit</span>
    reader: <span class="hljs-type">BSONReader</span>[<span class="hljs-type">BSONDocument</span>, <span class="hljs-type">A</span>],
    writer: <span class="hljs-type">BSONWriter</span>[<span class="hljs-type">A</span>, <span class="hljs-type">BSONDocument</span>]
  ): <span class="hljs-type">CodecTests</span>[<span class="hljs-type">Id</span>, <span class="hljs-type">A</span>, <span class="hljs-type">BSONDocument</span>] =
    <span class="hljs-keyword">new</span> <span class="hljs-type">CodecTests</span>[<span class="hljs-type">Id</span>, <span class="hljs-type">A</span>, <span class="hljs-type">BSONDocument</span>] {
      <span class="hljs-keyword">override</span> <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">laws</span></span>: <span class="hljs-type">CodecLaws</span>[<span class="hljs-type">Id</span>, <span class="hljs-type">A</span>, <span class="hljs-type">BSONDocument</span>] =
        <span class="hljs-type">CodecLaws</span>[<span class="hljs-type">Id</span>, <span class="hljs-type">A</span>, <span class="hljs-type">BSONDocument</span>](reader.read, writer.write)
<span class="hljs-keyword">override</span> <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">name</span></span>: <span class="hljs-type">String</span> = <span class="hljs-string">"BSON serde tests"</span>
    }
}
</code></pre>
<p>The types here flow from</p>
<pre><code>A =&gt; <span class="hljs-function"><span class="hljs-params">BsonDocument</span> =&gt;</span> A
</code></pre><p>and not <code>F[A]</code> as we had expected. Luckily for us, we have a solution and use the Id-type to represent just that.</p>
<p>And given the (very long) serializer definition:</p>
<pre><code class="lang-scala"><span class="hljs-keyword">implicit</span> <span class="hljs-keyword">val</span> personBsonFormat
  : <span class="hljs-type">BSONReader</span>[<span class="hljs-type">BSONDocument</span>, <span class="hljs-type">Person</span>] <span class="hljs-keyword">with</span> <span class="hljs-type">BSONWriter</span>[<span class="hljs-type">Person</span>, <span class="hljs-type">BSONDocument</span>] =
  <span class="hljs-keyword">new</span> <span class="hljs-type">BSONReader</span>[<span class="hljs-type">BSONDocument</span>, <span class="hljs-type">Person</span>] <span class="hljs-keyword">with</span> <span class="hljs-type">BSONWriter</span>[<span class="hljs-type">Person</span>, <span class="hljs-type">BSONDocument</span>] {
    <span class="hljs-keyword">override</span> <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">read</span></span>(bson: <span class="hljs-type">BSONDocument</span>): <span class="hljs-type">Person</span> =
      <span class="hljs-type">Person</span>(bson.getAs[<span class="hljs-type">String</span>](<span class="hljs-string">"name"</span>).get, bson.getAs[<span class="hljs-type">Int</span>](<span class="hljs-string">"age"</span>).get)
<span class="hljs-keyword">override</span> <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">write</span></span>(t: <span class="hljs-type">Person</span>): <span class="hljs-type">BSONDocument</span> =
      <span class="hljs-type">BSONDocument</span>(<span class="hljs-string">"name"</span> -&gt; t.name, <span class="hljs-string">"age"</span> -&gt; t.age)
  }
</code></pre>
<p>we can now define BsonCodecTests in all its <strong>1</strong> line of logic glory.</p>
<pre><code class="lang-scala"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">BsonCodecSpec</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Checkers</span> <span class="hljs-keyword">with</span> <span class="hljs-title">FunSuiteLike</span> <span class="hljs-keyword">with</span> <span class="hljs-title">Discipline</span> </span>{
    checkAll(<span class="hljs-string">"PersonSerdeTests"</span>, <span class="hljs-type">BsonCodecTests</span>[<span class="hljs-type">Person</span>].tests)
}
</code></pre>
<h3 id="heading-a-first-order-logic-perspective-on-our-code">A (First-order) logic perspective on our code</h3>
<p>Our first test attempt can be described as follows:</p>
<pre><code>∃p:Person,<span class="hljs-attr">s</span>:OFormat that holds : s.read(s.write(p)) &lt;-&gt; p
</code></pre><p>Meaning, <code>for the specific person p(“John Doe”,32)</code> and <code>for the format **s**</code>, the following statement is true: <code>decode(encode(p)) &lt;-</code>&gt; p.</p>
<p>The second attempt (using <code>PBT</code>) can be:</p>
<pre><code>∃s:OFormat, ∀p:Person the following should hold :  s.read(s.write(p)) &lt;-&gt; p
</code></pre><p>Which means, <strong>for all</strong> persons p and <code>for the specific format **s**</code>, the following is true: <code>decode(encode(p))&lt;</code>-&gt;p.</p>
<p>The third (and most powerful statement thus far) using <code>law testing</code>:</p>
<pre><code>∀s:Encoder, ∀p:Person the the following should hold :  s.read(s.write(p)) &lt;-&gt; p
</code></pre><p>Which means, <code>for all</code> <code>formats s</code>, and <code>**for all** persons p</code>, the following is true: <code>decode(encode(p))&lt;</code>-&gt;p.</p>
<h4 id="heading-summary"><strong>Summary</strong></h4>
<ul>
<li>Law testing allows you to reason about your data-types and functions in a mathematical and concise way and provides a totally new paradigm for testing your code!</li>
<li>Most of the type level libraries you use (like <code>cats</code>, <code>circe</code> and many more) use law testing internally to test their data-types.</li>
<li>Avoid writing specific test-cases for your data-types and functions and try to generalize them using property-based law tests.</li>
</ul>
<p>Thank you for reaching this far! I am super excited about finding more abstract and useful laws that I can use in my code! Please let me know about any you’ve used or can think of.</p>
<p>More inspiring and detailed content can be found in the <a target="_blank" href="https://typelevel.org/cats/typeclasses/lawtesting.html">cats-laws</a> site or <a target="_blank" href="https://github.com/circe/circe/blob/master/modules/testing/shared/src/main/scala/io/circe/testing/CodecTests.scala">circe</a>.</p>
<p>The complete code examples can be found <a target="_blank" href="https://gist.github.com/dorsev/0fdd8315228d7ef6914b27650f817ae6">here</a>.</p>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ Beyond unit tests: an intro to property and law testing in Scala ]]>
                </title>
                <description>
                    <![CDATA[ By Daniel Sebban Taking your unit tests to the next level. I have been using ScalaCheck testing library for at least 2 years now. It allows you to take your unit tests to the next level. You can do Property-based testing by generating a lot of tests... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/beyond-unit-tests-an-intro-to-property-and-law-testing-in-scala-dd6a15898a19/</link>
                <guid isPermaLink="false">66d45e3ebc9760a197a10378</guid>
                
                    <category>
                        <![CDATA[ coding ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Functional Programming ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Scala ]]>
                    </category>
                
                    <category>
                        <![CDATA[ technology ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Testing ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ freeCodeCamp ]]>
                </dc:creator>
                <pubDate>Fri, 05 Apr 2019 15:42:57 +0000</pubDate>
                <media:content url="https://cdn-media-1.freecodecamp.org/images/1*QcyFEloaC2fG_naIOEYXbQ.jpeg" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>By Daniel Sebban</p>
<h4 id="heading-taking-your-unit-tests-to-the-next-level">Taking your unit tests to the next level.</h4>
<p>I have been using ScalaCheck testing library for at least 2 years now. It allows you to take your unit tests to the next level.</p>
<ul>
<li>You can do <strong>Property-based testing</strong> by generating a lot of tests with random data and asserting properties on your functions. A simple code example is described below.</li>
<li>You can do <strong>Law testing</strong> that is even more powerful and allows you to check mathematical properties on your types.</li>
</ul>
<h1 id="heading-property-based-testing">Property-based testing</h1>
<p>Here is our beloved <code>User</code> data type:</p>
<pre><code class="lang-scala"><span class="hljs-keyword">case</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">User</span>(<span class="hljs-params">name: <span class="hljs-type">String</span>, age: <span class="hljs-type">Int</span></span>)</span>
</code></pre>
<p>And a random <code>User</code> generator:</p>
<pre><code class="lang-scala"><span class="hljs-keyword">import</span> org.scalacheck.{ <span class="hljs-type">Gen</span>, <span class="hljs-type">Arbitrary</span> }
<span class="hljs-keyword">import</span> <span class="hljs-type">Arbitrary</span>.arbitrary
<span class="hljs-keyword">implicit</span> <span class="hljs-keyword">val</span> randomUser: <span class="hljs-type">Arbitrary</span>[<span class="hljs-type">User</span>] = <span class="hljs-type">Arbitrary</span>(<span class="hljs-keyword">for</span> {
  randomName &lt;- <span class="hljs-type">Gen</span>.alphaStr
  randomAge  &lt;- <span class="hljs-type">Gen</span>.choose(<span class="hljs-number">0</span>,<span class="hljs-number">80</span>)
  } <span class="hljs-keyword">yield</span> <span class="hljs-type">User</span>(randomName, randomAge))
</code></pre>
<p>We can now generate a <code>User</code> like this:</p>
<pre><code class="lang-scala">scala&gt; randomUser.arbitrary.sample
res0: <span class="hljs-type">Option</span>[<span class="hljs-type">User</span>] = <span class="hljs-type">Some</span>(<span class="hljs-type">User</span>(<span class="hljs-type">OtwlaaxGbmdhuorlmgvXitbmGfbgetm</span>,<span class="hljs-number">22</span>))
</code></pre>
<p>Let’s define some functions on the <code>User</code>:</p>
<pre><code class="lang-scala"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">isAdult</span></span>: <span class="hljs-type">User</span> =&gt; <span class="hljs-type">Boolean</span> = _.age &gt;= <span class="hljs-number">18</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">isAllowedToDrink</span> </span>: <span class="hljs-type">User</span> =&gt; <span class="hljs-type">Boolean</span> = _.age &gt;= <span class="hljs-number">21</span>
</code></pre>
<p>Let’s claim that:</p>
<p><strong>All adults are allowed to drink.</strong></p>
<p><strong>Can we somehow prove this? Is this correct for all users?</strong></p>
<p>This is where property testing comes to the rescue. It allows us not to write specific unit-tests. Here they would be:</p>
<ul>
<li>18-year-olds are not allowed to drink</li>
<li>19-year-olds are not allowed to drink</li>
<li>20-year-olds are not allowed to drink</li>
</ul>
<p>All of these statements can be replaced by a single property check:</p>
<pre><code class="lang-scala"><span class="hljs-keyword">import</span> org.scalacheck.<span class="hljs-type">Prop</span>.forAll
<span class="hljs-keyword">val</span> allAdultsCanDrink = forAll { u: <span class="hljs-type">User</span> =&gt;
    <span class="hljs-keyword">if</span>(isAdult(u)) isAllowedToDrink(u) <span class="hljs-keyword">else</span> <span class="hljs-literal">true</span> }
</code></pre>
<p>Let’s run it:</p>
<pre><code class="lang-scala">scala&gt; allAdultsCanDrink.check()
! <span class="hljs-type">Falsified</span> after <span class="hljs-number">0</span> passed tests.
&gt; <span class="hljs-type">ARG_0</span>: <span class="hljs-type">User</span>(,<span class="hljs-number">19</span>)
</code></pre>
<p>It fails as expected for a 19-year-old.</p>
<p>Property testing is awesome for a few reasons:</p>
<ul>
<li>Saves time by writing less specific tests</li>
<li>Finds new use cases generated by Scala check that you forgot to handle</li>
<li>Forces you think in a more general way</li>
<li>Gives you more confidence for refactoring than conventional unit tests</li>
</ul>
<h1 id="heading-law-testing">Law testing</h1>
<p>It gets better: let’s take it to the next level and define an Ordering between Users:</p>
<pre><code class="lang-scala"><span class="hljs-keyword">import</span> scala.math.<span class="hljs-type">Orderingimplicit</span> <span class="hljs-keyword">val</span> userOrdering: <span class="hljs-type">Ordering</span>[<span class="hljs-type">User</span>] = <span class="hljs-type">Ordering</span>.by(_.age)
</code></pre>
<p>We want to make sure that we didn't <strong>forget any edge cases</strong> and that we defined our order properly. This property has a name, and it’s called a <strong>total order.</strong> It needs to holds for the following properties:</p>
<ul>
<li><strong>Totality</strong></li>
<li><strong>Antisymmetry</strong></li>
<li><strong>Transitivity</strong></li>
</ul>
<p><strong>Can we somehow prove this? Is this correct for all users?</strong></p>
<p>This is possible without writing a single test!</p>
<p>We use <code>cats-laws</code> library to define the laws we want to test on the ordering we defined:</p>
<pre><code class="lang-scala"><span class="hljs-keyword">import</span> cats.kernel.laws.discipline.<span class="hljs-type">OrderTests</span>
<span class="hljs-keyword">import</span> cats._
<span class="hljs-keyword">import</span> org.scalatest.<span class="hljs-type">FunSuite</span>
<span class="hljs-keyword">import</span> org.typelevel.discipline.scalatest.<span class="hljs-type">Discipline</span>
<span class="hljs-keyword">import</span> org.scalacheck.<span class="hljs-type">ScalacheckShapeless</span>._
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">UserOrderSpec</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">FunSuite</span> <span class="hljs-keyword">with</span> <span class="hljs-title">Discipline</span>  </span>{
<span class="hljs-comment">//needed boilerplate to satisfy the dependencies of the framework</span>
  <span class="hljs-keyword">implicit</span> <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">eqUser</span></span>[<span class="hljs-type">A</span>: <span class="hljs-type">Eq</span>]: <span class="hljs-type">Eq</span>[<span class="hljs-type">Option</span>[<span class="hljs-type">User</span>]] = <span class="hljs-type">Eq</span>.fromUniversalEquals
<span class="hljs-comment">//convert our standard ordering to a `cats` order </span>
  <span class="hljs-keyword">implicit</span> <span class="hljs-keyword">val</span> catsUserOrder: <span class="hljs-type">Order</span>[<span class="hljs-type">User</span>] = <span class="hljs-type">Order</span>.fromOrdering(userOrdering)

<span class="hljs-comment">//check all mathematical properties on our ordering</span>
  checkAll(<span class="hljs-string">"User"</span>, <span class="hljs-type">OrderTests</span>[<span class="hljs-type">User</span>].order)
}
</code></pre>
<p>Let’s run it:</p>
<pre><code class="lang-scala">scala&gt; <span class="hljs-keyword">new</span> <span class="hljs-type">UserOrderSpec</span>().execute()
<span class="hljs-type">UserOrderSpec</span>:
- <span class="hljs-type">User</span>.order.antisymmetry *** <span class="hljs-type">FAILED</span> ***
  <span class="hljs-type">GeneratorDrivenPropertyCheckFailedException</span> was thrown during property evaluation.
   (<span class="hljs-type">Discipline</span>.scala:<span class="hljs-number">14</span>)
    <span class="hljs-type">Falsified</span> after <span class="hljs-number">1</span> successful property evaluations.
    <span class="hljs-type">Location</span>: (<span class="hljs-type">Discipline</span>.scala:<span class="hljs-number">14</span>)
    <span class="hljs-type">Occurred</span> when passed generated values (
      arg0 = <span class="hljs-type">User</span>(h,<span class="hljs-number">17</span>),
      arg1 = <span class="hljs-type">User</span>(edsb,<span class="hljs-number">17</span>),
      arg2 = org.scalacheck.<span class="hljs-type">GenArities</span>$$<span class="hljs-type">Lambda</span>$<span class="hljs-number">2739</span>/<span class="hljs-number">1277317528</span>@<span class="hljs-number">41</span>d7b4cf
    )
    <span class="hljs-type">Label</span> of failing property:
      <span class="hljs-type">Expected</span>: <span class="hljs-literal">true</span>
  <span class="hljs-type">Received</span>: <span class="hljs-literal">false</span>
- <span class="hljs-type">User</span>.order.compare
- <span class="hljs-type">User</span>.order.gt
- <span class="hljs-type">User</span>.order.gteqv
- <span class="hljs-type">User</span>.order.lt
- <span class="hljs-type">User</span>.order.max
- <span class="hljs-type">User</span>.order.min
- <span class="hljs-type">User</span>.order.partialCompare
- <span class="hljs-type">User</span>.order.pmax
- <span class="hljs-type">User</span>.order.pmin
- <span class="hljs-type">User</span>.order.reflexitivity
- <span class="hljs-type">User</span>.order.reflexitivity gt
- <span class="hljs-type">User</span>.order.reflexitivity lt
- <span class="hljs-type">User</span>.order.symmetry
- <span class="hljs-type">User</span>.order.totality
- <span class="hljs-type">User</span>.order.transitivity
</code></pre>
<p>Sure enough, it fails on the antisymmetry law! Same age and different names are not supposed to be equals. We forgot to use the name in our original <code>Ordering</code>, so let's fix it and rerun the laws:</p>
<pre><code class="lang-scala"><span class="hljs-keyword">implicit</span> <span class="hljs-keyword">val</span> userOrdering: <span class="hljs-type">Ordering</span>[<span class="hljs-type">User</span>] = <span class="hljs-type">Ordering</span>.by( u =&gt; (u.age, u.name))
scala&gt; <span class="hljs-keyword">new</span> <span class="hljs-type">UserOrderSpec</span>().execute()
<span class="hljs-type">UserOrderSpec</span>:
- <span class="hljs-type">User</span>.order.antisymmetry
- <span class="hljs-type">User</span>.order.compare
- <span class="hljs-type">User</span>.order.gt
- <span class="hljs-type">User</span>.order.gteqv
- <span class="hljs-type">User</span>.order.lt
- <span class="hljs-type">User</span>.order.max
- <span class="hljs-type">User</span>.order.min
- <span class="hljs-type">User</span>.order.partialCompare
- <span class="hljs-type">User</span>.order.pmax
- <span class="hljs-type">User</span>.order.pmin
- <span class="hljs-type">User</span>.order.reflexitivity
- <span class="hljs-type">User</span>.order.reflexitivity gt
- <span class="hljs-type">User</span>.order.reflexitivity lt
- <span class="hljs-type">User</span>.order.symmetry
- <span class="hljs-type">User</span>.order.totality
- <span class="hljs-type">User</span>.order.transitivity
</code></pre>
<p>And now it passes :)</p>
<p>If you are wondering what can you test besides <code>Order</code>s, go check out the docs here: <a target="_blank" href="https://typelevel.org/cats/typeclasses/lawtesting.html">https://typelevel.org/cats/typeclasses/lawtesting.html</a></p>
<h1 id="heading-summary">Summary</h1>
<ul>
<li>Property tests are more powerful than unit tests. They allow us to define properties on functions and generate a large number of tests using random data generators.</li>
<li>Law testing takes it to the next level and uses the mathematical properties of structures like <code>Order</code> to generate the properties and the tests.</li>
<li>Next time you define an ordering and wonder if it’s well-defined, go ahead and run the laws on it!</li>
</ul>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ An introduction to Vert.x, the fastest Java framework today ]]>
                </title>
                <description>
                    <![CDATA[ By Martin Budi If you’ve recently googled “best web framework” you might have stumbled upon the Techempower benchmarks where more than three hundred frameworks are ranked. There you might have noticed that Vert.x is one of the top ranked, if not the ... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/an-introduction-to-vert-x-the-fastest-java-framework-today-27d8661ceb14/</link>
                <guid isPermaLink="false">66c344660fa3812cdd5ea99d</guid>
                
                    <category>
                        <![CDATA[ Java ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Scala ]]>
                    </category>
                
                    <category>
                        <![CDATA[ tech  ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Web Development ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ freeCodeCamp ]]>
                </dc:creator>
                <pubDate>Wed, 06 Mar 2019 16:52:04 +0000</pubDate>
                <media:content url="https://cdn-media-1.freecodecamp.org/images/1*RovqxSyUULpHDMxwYGXQPA.png" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>By Martin Budi</p>
<p>If you’ve recently googled “best web framework” you might have stumbled upon the Techempower benchmarks where more than three hundred frameworks are ranked. There you might have noticed that Vert.x is one of the top ranked, if not the <a target="_blank" href="https://www.techempower.com/benchmarks/#section=data-r17&amp;hw=cl&amp;test=fortune">first</a> by some measures.</p>
<p>So let’s talk about it.</p>
<p>Vert.x is a polyglot web framework that shares common functionalities among its supported languages Java, Kotlin, Scala, Ruby, and Javascript. Regardless of language, Vert.x operates on the Java Virtual Machine (JVM). Being modular and lightweight, it is geared toward microservices development.</p>
<p>Techempower benchmarks measure the performance of updating, fetching and delivering data from a database. The more requests served per second, the better. In such an IO scenario where little computing is involved, any non-blocking framework would have an edge. In recent years, such a paradigm is almost inseparable from Node.js which popularized it with its single-threaded event loop.</p>
<p>Vert.x, like Node, operates a single event loop. But Vert.x also takes advantage of the JVM. Whereas Node runs on a single core, Vert.x maintains a thread pool with a size that can match the number of available cores. With greater concurrency support, Vert.x is suitable for not only IO but also CPU-heavy processes that require parallel computing.</p>
<p>Event loops, however, are half of the story. The other half has little to do with Vert.x.</p>
<p>To connect to a database, a client requires a connector driver. In the Java realm, the most common driver for Sql is JDBC. The problem is, this driver is blocking. And it’s blocking at the socket level. A thread will always get stuck there until it returns with a response.</p>
<p>Needless to say, the driver has been a bottleneck in realizing a fully non-blocking application. Fortunately there has been progress (albeit unofficial) on <a target="_blank" href="https://github.com/mauricio/postgresql-async">an async driver</a> with several active forks, among them:</p>
<ul>
<li><a target="_blank" href="https://github.com/jasync-sql/jasync-sql">https://github.com/jasync-sql/jasync-sql</a> (for Postgres and MySql)</li>
<li><a target="_blank" href="https://github.com/reactiverse/reactive-pg-client">https://github.com/reactiverse/reactive-pg-client</a> (Postgres)</li>
</ul>
<h4 id="heading-the-golden-rule"><strong>The golden rule</strong></h4>
<p>Vert.x is pretty simple to work with, and an http server can be brought up with a few lines of code.</p>


<p>The method requestHandler is where the event loop delivers the request event. As Vert.x is un-opinionated, handling it is free style. But keep in mind the single important rule of non-blocking thread: don’t block it.</p>
<p>When working with concurrency we can draw from so many options available today such as Promise, Future, Rx, as well as Vert.x’s own idiomatic way. But as the complexity of an application grows, having async functionality alone is not enough. We also need the ease of coordinating and chaining calls while avoiding callback hell, as well as passing any error gracefully.</p>
<p>Scala Future satisfies all the conditions above with the additional advantage of being based on functional programming principles. Although this article doesn’t explore Scala Future in depth, we can try it with a simple app. Let’s say the app is an API service to find a user given their id:</p>


<p>There are three operations involved: checking request parameter, checking if the id is valid, and fetching the data. We will wrap each of these operations in a Future and coordinate the execution in a “for comprehension” structure.</p>
<ul>
<li>The first step is to match the request with a service. Scala has a powerful pattern matching feature that we can use for this purpose. Here we intercept any mention of “/user” and pass it into our service.</li>
<li>Next is the core of this service where our futures are arranged in a sequential for-comprehension. The first future <strong>f1</strong> wraps parameter check. We specifically want to retrieve the id from the get request and cast it into int. (Scala doesn’t require explicit return if the return value is the last line in the method.) As you see, this operation could potentially throw an exception as id might not be an int or not even available, but that is okay for now.</li>
<li>The second future <strong>f2</strong> checks the validity of id. We block any id lower than 100 by explicitly calling Future.failed with our own CustomException. Otherwise we pass an empty Future in the form of Future.unit as successful validation.</li>
<li>The last future <strong>f3</strong> retrieves the user with the id provided by <strong>f1.</strong> As this is just a sample, we don’t really connect to a database. We just return some mock string.</li>
<li><strong>map</strong> runs the arrangement that yields the user data from <strong>f3</strong> then prints it into the response.</li>
<li>Now if in any part of the sequence an error occurs, a Throwable is passed to <strong>recover</strong>. Here we can match its type to a suitable recovery strategy. Looking back in our code, we have anticipated several potential failures such as missing id, or id that was not int or not valid which would throw specific exceptions. We are handling each of them in handleException by passing an error message to client.</li>
</ul>
<p>This arrangement provides not only an asynchronous flow from the start to the end but also a clean approach to handling errors. And as it is streamlined across handlers we can focus on things that matter, like database query.</p>
<h4 id="heading-verticles-event-bus-and-other-gotchas"><strong>Verticles, Event Bus, and other gotchas</strong></h4>
<p>Vert.x also offers a concurrency model called verticle which resembles the Actor system. (If you’d like to learn more, head over to my <a target="_blank" href="https://medium.freecodecamp.org/still-using-synchronized-try-akka-actor-instead-ac2f2b22a9ed">Akka Actor guide</a>.) Verticle isolates its state and behavior to provide a thread-safe environment. The only way to communicate with it is through an event bus.</p>
<p>However, the Vert.x event bus requires its messages to be String or JSON. This makes it difficult to pass arbitrary non-POJO objects. And in a high performance system, dealing with JSON conversion is undesirable as it imposes some computing cost. If you are developing IO applications, you may be better off not using either verticle or event bus, as such applications have little need for local state.</p>
<p>Working with some Vert.x components can also be pretty challenging. You might find lack of documentation, unexpected behavior, and even failure to function. Vert.x might be suffering from its own ambition, as developing new components would require porting across many languages. This is a difficult undertaking. For that reason sticking to the core would be the best.</p>
<p>If you are developing a public API, then vertx-core should be enough. If it’s a web app, you may add vertx-web which provides http parameter handling and JWT/Session authentication. These two are the ones that dominated the benchmarks anyway. There is some decrease in performance in some tests for using vertx-web, but as it seems to have stemmed from optimization, it might get ironed out in the subsequent releases.</p>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ How to make a simple application with Akka Cluster ]]>
                </title>
                <description>
                    <![CDATA[ By Luca Florio If you read my previous story about Scalachain, you probably noticed that it is far from being a distributed system. It lacks all the features to properly work with other nodes. Add to it that a blockchain composed by a single node is ... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/how-to-make-a-simple-application-with-akka-cluster-506e20a725cf/</link>
                <guid isPermaLink="false">66d45e4873634435aafcef86</guid>
                
                    <category>
                        <![CDATA[ Akka ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Docker ]]>
                    </category>
                
                    <category>
                        <![CDATA[ General Programming ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Scala ]]>
                    </category>
                
                    <category>
                        <![CDATA[ tech  ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ freeCodeCamp ]]>
                </dc:creator>
                <pubDate>Tue, 26 Feb 2019 21:08:29 +0000</pubDate>
                <media:content url="https://cdn-media-1.freecodecamp.org/images/1*kbflVM9_tfKyfVOR1ldKAw.jpeg" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>By Luca Florio</p>
<p>If you read my previous story about <a target="_blank" href="https://github.com/elleFlorio/scalachain">Scalachain</a>, you probably noticed that it is far from being a distributed system. It lacks all the features to properly work with other nodes. Add to it that a blockchain composed by a single node is useless. For this reason I decided it is time to work on the issue.</p>
<p>Since Scalachain is powered by Akka, why not take the chance to play with Akka Cluster? I created a <a target="_blank" href="https://github.com/elleFlorio/akka-cluster-playground">simple project</a> to tinker a bit with <a target="_blank" href="https://doc.akka.io/docs/akka/2.5/index-cluster.html">Akka Cluster</a>, and in this story I’m going to share my learnings. We are going to create a cluster of three nodes, using <a target="_blank" href="https://doc.akka.io/docs/akka/2.5/cluster-routing.html#weakly-up">Cluster Aware Routers</a> to balance the load among them. Everything will run in a Docker container, and we will use docker-compose for an easy deployment.</p>
<p>Ok, Let’s roll! ?</p>
<h4 id="heading-quick-introduction-to-akka-cluster">Quick introduction to Akka Cluster</h4>
<p>Akka Cluster provides great support to the creation of distributed applications. The best use case is when you have a node that you want to replicate N times in a distributed environment. This means that all the N nodes are peers running the same code. Akka Cluster gives you out-of-the-box the discovery of members in the same cluster. Using Cluster Aware Routers it is possible to balance the messages between actors in different nodes. It is also possible to choose the balancing policy, making load-balancing a piece of cake!</p>
<p>Actually you can chose between two types of routers:</p>
<p><strong>Group Router</strong> — The actors to send the messages to — called routees — are specified using their actor path. The routers share the routees created in the cluster. We will use a Group Router in this example.</p>
<p><img src="https://cdn-media-1.freecodecamp.org/images/1*aRVBb-_v2dBpTV8m97Pd3w.png" alt="Image" width="541" height="491" loading="lazy">
<em>Group Router</em></p>
<p><strong>Pool Router</strong> — The routees are created and deployed by the router, so they are its children in the actor hierarchy. Routees are not shared between routers. This is ideal for a primary-replica scenario, where each router is the primary and its routees the replicas.</p>
<p><img src="https://cdn-media-1.freecodecamp.org/images/1*ofa_x3hkM_sMzH5Nzum_Gg.png" alt="Image" width="741" height="499" loading="lazy">
<em>Pool Router</em></p>
<p>This is just the tip of the iceberg, so I invite you to read the <a target="_blank" href="https://doc.akka.io/docs/akka/2.5/index-cluster.html">official documentation</a> for more insights.</p>
<h4 id="heading-a-cluster-for-mathematical-computations">A Cluster for mathematical computations</h4>
<p>Let’s picture a use-case scenario. Suppose to design a system to execute mathematical computations on request. The system is deployed online, so it needs a REST API to receive the computation requests. An internal processor handles these requests, executing the computation and returning the result.</p>
<p>Right now the processor can only compute the <a target="_blank" href="https://en.wikipedia.org/wiki/Fibonacci_number">Fibonacci number</a>. We decide to use a cluster of nodes to distribute the load among the nodes and improve performance. Akka Cluster will handle cluster dynamics and load-balancing between nodes. Ok, sounds good!</p>
<h4 id="heading-actor-hierarchy">Actor hierarchy</h4>
<p>First things first: we need to define our actor hierarchy. The system can be divided in three functional parts: the <strong>business logic</strong>, the <strong>cluster management</strong>, and the <strong>node</strong> itself. There is also the <strong>server</strong> but it is not an actor, and we will work on that later.</p>
<p><strong>Business logic</strong></p>
<p>The application should do mathematical computations. We can define a simple <code>Processor</code> actor to manage all the computational tasks. Every computation that we support can be implemented in a specific actor, that will be a child of the <code>Processor</code> one. In this way the application is modular and easier to extend and maintain. Right now the only child of <code>Processor</code> will be the <code>ProcessorFibonacci</code> actor. I suppose you can guess what its task is. This should be enough to start.</p>
<p><strong>Cluster management</strong></p>
<p>To manage the cluster we need a <code>ClusterManager</code>. Sounds simple, right? This actor handles everything related to the cluster, like returning its members when asked. It would be useful to log what happens inside the cluster, so we define a <code>ClusterListener</code> actor. This is a child of the <code>ClusterManager</code>, and subscribes to cluster events logging them.</p>
<p><strong>Node</strong></p>
<p>The <code>Node</code> actor is the root of our hierarchy. It is the entry point of our system that communicates with the API. The <code>Processor</code> and the <code>ClusterManager</code> are its children, along with the <code>ProcessorRouter</code> actor. This is the load balancer of the system, distributing the load among <code>Processor</code>s. We will configure it as a Cluster Aware Router, so every <code>ProcessorRouter</code> can send messages to <code>Processor</code>s on every node.</p>
<p><img src="https://cdn-media-1.freecodecamp.org/images/1*sblmA495LlrJLLBaIpbYYg.png" alt="Image" width="732" height="302" loading="lazy">
<em>Actor hierarchy</em></p>
<h4 id="heading-actor-implementation">Actor Implementation</h4>
<p>Time to implement our actors! Fist we implement the actors related to the business logic of the system. We move then on the actors for the cluster management and the root actor (<code>Node</code>) in the end.</p>
<p><strong>ProcessorFibonacci</strong></p>
<p>This actor executes the computation of the Fibonacci number. It receives a <code>Compute</code> message containing the number to compute and the reference of the actor to reply to. The reference is important, since there can be different requesting actors. Remember that we are working in a distributed environment!</p>
<p>Once the <code>Compute</code> message is received, the <code>fibonacci</code> function computes the result. We wrap it in a <code>ProcessorResponse</code> object to provide information on the node that executed the computation. This will be useful later to see the round-robin policy in action.</p>
<p>The result is then sent to the actor we should reply to. Easy-peasy.</p>
<pre><code class="lang-scala"><span class="hljs-class"><span class="hljs-keyword">object</span> <span class="hljs-title">ProcessorFibonacci</span> </span>{
  <span class="hljs-keyword">sealed</span> <span class="hljs-class"><span class="hljs-keyword">trait</span> <span class="hljs-title">ProcessorFibonacciMessage</span></span>
  <span class="hljs-keyword">case</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Compute</span>(<span class="hljs-params">n: <span class="hljs-type">Int</span>, replyTo: <span class="hljs-type">ActorRef</span></span>) <span class="hljs-keyword">extends</span> <span class="hljs-title">ProcessorFibonacciMessage</span></span>

  <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">props</span></span>(nodeId: <span class="hljs-type">String</span>) = <span class="hljs-type">Props</span>(<span class="hljs-keyword">new</span> <span class="hljs-type">ProcessorFibonacci</span>(nodeId))

  <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">fibonacci</span></span>(x: <span class="hljs-type">Int</span>): <span class="hljs-type">BigInt</span> = {
    <span class="hljs-meta">@tailrec</span> <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">fibHelper</span></span>(x: <span class="hljs-type">Int</span>, prev: <span class="hljs-type">BigInt</span> = <span class="hljs-number">0</span>, next: <span class="hljs-type">BigInt</span> = <span class="hljs-number">1</span>): <span class="hljs-type">BigInt</span> = x <span class="hljs-keyword">match</span> {
      <span class="hljs-keyword">case</span> <span class="hljs-number">0</span> =&gt; prev
      <span class="hljs-keyword">case</span> <span class="hljs-number">1</span> =&gt; next
      <span class="hljs-keyword">case</span> _ =&gt; fibHelper(x - <span class="hljs-number">1</span>, next, next + prev)
    }
    fibHelper(x)
  }
}

<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">ProcessorFibonacci</span>(<span class="hljs-params">nodeId: <span class="hljs-type">String</span></span>) <span class="hljs-keyword">extends</span> <span class="hljs-title">Actor</span> </span>{
  <span class="hljs-keyword">import</span> <span class="hljs-type">ProcessorFibonacci</span>._

  <span class="hljs-keyword">override</span> <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">receive</span></span>: <span class="hljs-type">Receive</span> = {
    <span class="hljs-keyword">case</span> <span class="hljs-type">Compute</span>(value, replyTo) =&gt; {
      replyTo ! <span class="hljs-type">ProcessorResponse</span>(nodeId, fibonacci(value))
    }
  }
}
</code></pre>
<p><strong>Processor</strong></p>
<p>The <code>Processor</code> actor manages the specific sub-processors, like the Fibonacci one. It should instantiate the sub-processors and forward the requests to them. Right now we only have one sub-processor, so the <code>Processor</code> receives one kind of message: <code>ComputeFibonacci</code>. This message contains the Fibonacci number to compute. Once received, the number to compute is sent to a <code>FibonacciProcessor</code>, along with the reference of the <code>sender()</code>.</p>
<pre><code class="lang-scala"><span class="hljs-class"><span class="hljs-keyword">object</span> <span class="hljs-title">Processor</span> </span>{

  <span class="hljs-keyword">sealed</span> <span class="hljs-class"><span class="hljs-keyword">trait</span> <span class="hljs-title">ProcessorMessage</span></span>

  <span class="hljs-keyword">case</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">ComputeFibonacci</span>(<span class="hljs-params">n: <span class="hljs-type">Int</span></span>) <span class="hljs-keyword">extends</span> <span class="hljs-title">ProcessorMessage</span></span>

  <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">props</span></span>(nodeId: <span class="hljs-type">String</span>) = <span class="hljs-type">Props</span>(<span class="hljs-keyword">new</span> <span class="hljs-type">Processor</span>(nodeId))
}

<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Processor</span>(<span class="hljs-params">nodeId: <span class="hljs-type">String</span></span>) <span class="hljs-keyword">extends</span> <span class="hljs-title">Actor</span> </span>{
  <span class="hljs-keyword">import</span> <span class="hljs-type">Processor</span>._

  <span class="hljs-keyword">val</span> fibonacciProcessor: <span class="hljs-type">ActorRef</span> = context.actorOf(<span class="hljs-type">ProcessorFibonacci</span>.props(nodeId), <span class="hljs-string">"fibonacci"</span>)

  <span class="hljs-keyword">override</span> <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">receive</span></span>: <span class="hljs-type">Receive</span> = {
    <span class="hljs-keyword">case</span> <span class="hljs-type">ComputeFibonacci</span>(value) =&gt; {
      <span class="hljs-keyword">val</span> replyTo = sender()
      fibonacciProcessor ! <span class="hljs-type">Compute</span>(value, replyTo)
    }
  }
}
</code></pre>
<p><strong>ClusterListener</strong></p>
<p>We would like to log useful information about what happens in the cluster. This could help us to debug the system if we need to. This is the purpose of the <code>ClusterListener</code> actor. Before starting, it subscribes itself to the event messages of the cluster. The actor reacts to messages like <code>MemberUp</code>, <code>UnreachableMember</code>, or <code>MemberRemoved</code>, logging the corresponding event. When <code>ClusterListener</code> is stopped, it unsubscribes itself from the cluster events.</p>
<pre><code class="lang-scala"><span class="hljs-class"><span class="hljs-keyword">object</span> <span class="hljs-title">ClusterListener</span> </span>{
  <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">props</span></span>(nodeId: <span class="hljs-type">String</span>, cluster: <span class="hljs-type">Cluster</span>) = <span class="hljs-type">Props</span>(<span class="hljs-keyword">new</span> <span class="hljs-type">ClusterListener</span>(nodeId, cluster))
}

<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">ClusterListener</span>(<span class="hljs-params">nodeId: <span class="hljs-type">String</span>, cluster: <span class="hljs-type">Cluster</span></span>) <span class="hljs-keyword">extends</span> <span class="hljs-title">Actor</span> <span class="hljs-keyword">with</span> <span class="hljs-title">ActorLogging</span> </span>{

  <span class="hljs-keyword">override</span> <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">preStart</span></span>(): <span class="hljs-type">Unit</span> = {
    cluster.subscribe(self, initialStateMode = <span class="hljs-type">InitialStateAsEvents</span>,
      classOf[<span class="hljs-type">MemberEvent</span>], classOf[<span class="hljs-type">UnreachableMember</span>])
  }

  <span class="hljs-keyword">override</span> <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">postStop</span></span>(): <span class="hljs-type">Unit</span> = cluster.unsubscribe(self)

  <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">receive</span> </span>= {
    <span class="hljs-keyword">case</span> <span class="hljs-type">MemberUp</span>(member) =&gt;
      log.info(<span class="hljs-string">"Node {} - Member is Up: {}"</span>, nodeId, member.address)
    <span class="hljs-keyword">case</span> <span class="hljs-type">UnreachableMember</span>(member) =&gt;
      log.info(<span class="hljs-string">s"Node {} - Member detected as unreachable: {}"</span>, nodeId, member)
    <span class="hljs-keyword">case</span> <span class="hljs-type">MemberRemoved</span>(member, previousStatus) =&gt;
      log.info(<span class="hljs-string">s"Node {} - Member is Removed: {} after {}"</span>,
        nodeId, member.address, previousStatus)
    <span class="hljs-keyword">case</span> _: <span class="hljs-type">MemberEvent</span> =&gt; <span class="hljs-comment">// ignore</span>
  }
}
</code></pre>
<p><strong>ClusterManager</strong></p>
<p>The actor responsible of the management of the cluster is <code>ClusterManager</code>. It creates the <code>ClusterListener</code> actor, and provides the list of cluster members upon request. It could be extended to add more functionalities, but right now this is enough.</p>
<pre><code class="lang-scala"><span class="hljs-class"><span class="hljs-keyword">object</span> <span class="hljs-title">ClusterManager</span> </span>{

  <span class="hljs-keyword">sealed</span> <span class="hljs-class"><span class="hljs-keyword">trait</span> <span class="hljs-title">ClusterMessage</span></span>
  <span class="hljs-keyword">case</span> <span class="hljs-class"><span class="hljs-keyword">object</span> <span class="hljs-title">GetMembers</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">ClusterMessage</span></span>

  <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">props</span></span>(nodeId: <span class="hljs-type">String</span>) = <span class="hljs-type">Props</span>(<span class="hljs-keyword">new</span> <span class="hljs-type">ClusterManager</span>(nodeId))
}

<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">ClusterManager</span>(<span class="hljs-params">nodeId: <span class="hljs-type">String</span></span>) <span class="hljs-keyword">extends</span> <span class="hljs-title">Actor</span> <span class="hljs-keyword">with</span> <span class="hljs-title">ActorLogging</span> </span>{

  <span class="hljs-keyword">val</span> cluster: <span class="hljs-type">Cluster</span> = <span class="hljs-type">Cluster</span>(context.system)
  <span class="hljs-keyword">val</span> listener: <span class="hljs-type">ActorRef</span> = context.actorOf(<span class="hljs-type">ClusterListener</span>.props(nodeId, cluster), <span class="hljs-string">"clusterListener"</span>)

  <span class="hljs-keyword">override</span> <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">receive</span></span>: <span class="hljs-type">Receive</span> = {
    <span class="hljs-keyword">case</span> <span class="hljs-type">GetMembers</span> =&gt; {
      sender() ! cluster.state.members.filter(_.status == <span class="hljs-type">MemberStatus</span>.up)
        .map(_.address.toString)
        .toList
    }
  }
}
</code></pre>
<p><strong>ProcessorRouter</strong></p>
<p>The load-balancing among processors is handled by the <code>ProcessorRouter</code>. It is created by the <code>Node</code> actor, but this time all the required information are provided in the configuration of the system.</p>
<pre><code class="lang-scala"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Node</span>(<span class="hljs-params">nodeId: <span class="hljs-type">String</span></span>) <span class="hljs-keyword">extends</span> <span class="hljs-title">Actor</span> </span>{

  <span class="hljs-comment">//...</span>

  <span class="hljs-keyword">val</span> processorRouter: <span class="hljs-type">ActorRef</span> = context.actorOf(<span class="hljs-type">FromConfig</span>.props(<span class="hljs-type">Props</span>.empty), <span class="hljs-string">"processorRouter"</span>)

  <span class="hljs-comment">//...</span>
}
</code></pre>
<p>Let’s analyse the relevant part in the <code>application.conf</code> file.</p>
<pre><code>akka {
  actor {
    ...
    deployment {
      <span class="hljs-regexp">/node/</span>processorRouter {
        router = round-robin-group
        routees.paths = [<span class="hljs-string">"/user/node/processor"</span>]
        cluster {
          enabled = on
          allow-local-routees = on
        }
      }
    }
  }
  ...
}
</code></pre><p>The first thing is to specify the path to the router actor, that is <code>/node/processorRouter</code>. Inside that property we can configure the behaviour of the router:</p>
<ul>
<li><code>router</code>: this is the policy for the load balancing of messages. I chose the <code>round-robin-group</code>, but there are many others.</li>
<li><code>routees.paths</code>: these are the paths to the actors that will receive the messages handled by the router. We are saying: <em>“When you receive a message, look for the actors corresponding to these paths. Choose one according to the policy and forward the message to it.”</em> Since we are using Cluster Aware Routers, the routees can be on any node of the cluster.</li>
<li><code>cluster.enabled</code>: are we operating in a cluster? The answer is <code>on</code>, of course!</li>
<li><code>cluster.allow-local-routees</code>: here we are allowing the router to choose a routee in its node.</li>
</ul>
<p>Using this configuration we can create a router to load balance the work among our processors.</p>
<p><strong>Node</strong></p>
<p>The root of our actor hierarchy is the <code>Node</code>. It creates the children actors — <code>ClusterManager</code>, <code>Processor</code>, and <code>ProcessorRouter</code> — and forwards the messages to the right one. Nothing complex here.</p>
<pre><code class="lang-scala"><span class="hljs-class"><span class="hljs-keyword">object</span> <span class="hljs-title">Node</span> </span>{

  <span class="hljs-keyword">sealed</span> <span class="hljs-class"><span class="hljs-keyword">trait</span> <span class="hljs-title">NodeMessage</span></span>

  <span class="hljs-keyword">case</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">GetFibonacci</span>(<span class="hljs-params">n: <span class="hljs-type">Int</span></span>)</span>

  <span class="hljs-keyword">case</span> <span class="hljs-class"><span class="hljs-keyword">object</span> <span class="hljs-title">GetClusterMembers</span></span>

  <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">props</span></span>(nodeId: <span class="hljs-type">String</span>) = <span class="hljs-type">Props</span>(<span class="hljs-keyword">new</span> <span class="hljs-type">Node</span>(nodeId))
}

<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Node</span>(<span class="hljs-params">nodeId: <span class="hljs-type">String</span></span>) <span class="hljs-keyword">extends</span> <span class="hljs-title">Actor</span> </span>{

  <span class="hljs-keyword">val</span> processor: <span class="hljs-type">ActorRef</span> = context.actorOf(<span class="hljs-type">Processor</span>.props(nodeId), <span class="hljs-string">"processor"</span>)
  <span class="hljs-keyword">val</span> processorRouter: <span class="hljs-type">ActorRef</span> = context.actorOf(<span class="hljs-type">FromConfig</span>.props(<span class="hljs-type">Props</span>.empty), <span class="hljs-string">"processorRouter"</span>)
  <span class="hljs-keyword">val</span> clusterManager: <span class="hljs-type">ActorRef</span> = context.actorOf(<span class="hljs-type">ClusterManager</span>.props(nodeId), <span class="hljs-string">"clusterManager"</span>)

  <span class="hljs-keyword">override</span> <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">receive</span></span>: <span class="hljs-type">Receive</span> = {
    <span class="hljs-keyword">case</span> <span class="hljs-type">GetClusterMembers</span> =&gt; clusterManager forward <span class="hljs-type">GetMembers</span>
    <span class="hljs-keyword">case</span> <span class="hljs-type">GetFibonacci</span>(value) =&gt; processorRouter forward <span class="hljs-type">ComputeFibonacci</span>(value)
  }
}
</code></pre>
<h4 id="heading-server-and-api">Server and API</h4>
<p>Every node of our cluster runs a server able to receive requests. The <code>Server</code> creates our actor system and is configured through the <code>application.conf</code> file.</p>
<pre><code class="lang-scala"><span class="hljs-class"><span class="hljs-keyword">object</span> <span class="hljs-title">Server</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">App</span> <span class="hljs-keyword">with</span> <span class="hljs-title">NodeRoutes</span> </span>{

  <span class="hljs-keyword">implicit</span> <span class="hljs-keyword">val</span> system: <span class="hljs-type">ActorSystem</span> = <span class="hljs-type">ActorSystem</span>(<span class="hljs-string">"cluster-playground"</span>)
  <span class="hljs-keyword">implicit</span> <span class="hljs-keyword">val</span> materializer: <span class="hljs-type">ActorMaterializer</span> = <span class="hljs-type">ActorMaterializer</span>()

  <span class="hljs-keyword">val</span> config: <span class="hljs-type">Config</span> = <span class="hljs-type">ConfigFactory</span>.load()
  <span class="hljs-keyword">val</span> address = config.getString(<span class="hljs-string">"http.ip"</span>)
  <span class="hljs-keyword">val</span> port = config.getInt(<span class="hljs-string">"http.port"</span>)
  <span class="hljs-keyword">val</span> nodeId = config.getString(<span class="hljs-string">"clustering.ip"</span>)

  <span class="hljs-keyword">val</span> node: <span class="hljs-type">ActorRef</span> = system.actorOf(<span class="hljs-type">Node</span>.props(nodeId), <span class="hljs-string">"node"</span>)

  <span class="hljs-keyword">lazy</span> <span class="hljs-keyword">val</span> routes: <span class="hljs-type">Route</span> = healthRoute ~ statusRoutes ~ processRoutes

  <span class="hljs-type">Http</span>().bindAndHandle(routes, address, port)
  println(<span class="hljs-string">s"Node <span class="hljs-subst">$nodeId</span> is listening at http://<span class="hljs-subst">$address</span>:<span class="hljs-subst">$port</span>"</span>)

  <span class="hljs-type">Await</span>.result(system.whenTerminated, <span class="hljs-type">Duration</span>.<span class="hljs-type">Inf</span>)

}
</code></pre>
<p><a target="_blank" href="https://doc.akka.io/docs/akka-http/current/index.html">Akka HTTP</a> powers the server itself and the REST API, exposing three simple endpoints. These endpoints are defined in the <code>NodeRoutes</code> trait.</p>
<p>The first one is <code>/health</code>, to check the health of a node. It responds with a <code>200 OK</code> if the node is up and running</p>
<pre><code class="lang-scala"><span class="hljs-keyword">lazy</span> <span class="hljs-keyword">val</span> healthRoute: <span class="hljs-type">Route</span> = pathPrefix(<span class="hljs-string">"health"</span>) {
    concat(
      pathEnd {
        concat(
          get {
            complete(<span class="hljs-type">StatusCodes</span>.<span class="hljs-type">OK</span>)
          }
        )
      }
    )
  }
</code></pre>
<p>The <code>/status/members</code> endpoint responds with the current active members of the cluster.</p>
<pre><code class="lang-scala"><span class="hljs-keyword">lazy</span> <span class="hljs-keyword">val</span> statusRoutes: <span class="hljs-type">Route</span> = pathPrefix(<span class="hljs-string">"status"</span>) {
    concat(
      pathPrefix(<span class="hljs-string">"members"</span>) {
        concat(
          pathEnd {
            concat(
              get {
                <span class="hljs-keyword">val</span> membersFuture: <span class="hljs-type">Future</span>[<span class="hljs-type">List</span>[<span class="hljs-type">String</span>]] = (node ? <span class="hljs-type">GetClusterMembers</span>).mapTo[<span class="hljs-type">List</span>[<span class="hljs-type">String</span>]]
                onSuccess(membersFuture) { members =&gt;
                  complete(<span class="hljs-type">StatusCodes</span>.<span class="hljs-type">OK</span>, members)
                }
              }
            )
          }
        )
      }
    )
  }
</code></pre>
<p>The last (but not the least) is the <code>/process/fibonacci/n</code> endpoint, used to request the Fibonacci number of <code>n</code>.</p>
<pre><code class="lang-scala"><span class="hljs-keyword">lazy</span> <span class="hljs-keyword">val</span> processRoutes: <span class="hljs-type">Route</span> = pathPrefix(<span class="hljs-string">"process"</span>) {
    concat(
      pathPrefix(<span class="hljs-string">"fibonacci"</span>) {
        concat(
          path(<span class="hljs-type">IntNumber</span>) { n =&gt;
            pathEnd {
              concat(
                get {
                  <span class="hljs-keyword">val</span> processFuture: <span class="hljs-type">Future</span>[<span class="hljs-type">ProcessorResponse</span>] = (node ? <span class="hljs-type">GetFibonacci</span>(n)).mapTo[<span class="hljs-type">ProcessorResponse</span>]
                  onSuccess(processFuture) { response =&gt;
                    complete(<span class="hljs-type">StatusCodes</span>.<span class="hljs-type">OK</span>, response)
                  }
                }
              )
            }
          }
        )
      }
    )
  }
</code></pre>
<p>It responds with a <code>ProcessorResponse</code> containing the result, along with the id of the node where the computation took place.</p>
<h4 id="heading-cluster-configuration">Cluster Configuration</h4>
<p>Once we have all our actors, we need to configure the system to run as a cluster! The <code>application.conf</code> file is where the magic takes place. I’m going to split it in pieces to present it better, but you can find the complete file <a target="_blank" href="https://github.com/elleFlorio/akka-cluster-playground/blob/master/src/main/resources/application.conf">here</a>.</p>
<p>Let’s start defining some useful variables.</p>
<pre><code>clustering {
  ip = <span class="hljs-string">"127.0.0.1"</span>
  ip = ${?CLUSTER_IP}

  port = <span class="hljs-number">2552</span>
  port = ${?CLUSTER_PORT}

  seed-ip = <span class="hljs-string">"127.0.0.1"</span>
  seed-ip = ${?CLUSTER_SEED_IP}

  seed-port = <span class="hljs-number">2552</span>
  seed-port = ${?CLUSTER_SEED_PORT}

  cluster.name = <span class="hljs-string">"cluster-playground"</span>
}
</code></pre><p>Here we are simply defining the ip and port of the nodes and the seed, as well as the cluster name. We set a default value, then we override it if a new one is specified. The configuration of the cluster is the following.</p>
<pre><code>akka {
  actor {
    provider = <span class="hljs-string">"cluster"</span>
    ...
    <span class="hljs-comment">/* router configuration */</span>
    ...
  }
  remote {
    log-remote-lifecycle-events = on
    netty.tcp {
      hostname = ${clustering.ip}
      port = ${clustering.port}
    }
  }
  cluster {
    seed-nodes = [
      <span class="hljs-string">"akka.tcp://"</span>${clustering.cluster.name}<span class="hljs-string">"@"</span>${clustering.seed-ip}<span class="hljs-string">":"</span>${clustering.seed-port}
    ]
    auto-down-unreachable-after = <span class="hljs-number">10</span>s
  }
}
...
<span class="hljs-comment">/* server vars */</span>
...
<span class="hljs-comment">/* cluster vars */</span>
}
</code></pre><p>Akka Cluster is build on top of Akka Remoting, so we need to configure it properly. First of all, we specify that we are going to use Akka Cluster saying that <code>provider = "cluster"</code>. Then we bind <code>cluster.ip</code> and <code>cluster.port</code> to the <code>hostname</code> and <code>port</code> of the <code>netty</code> web framework.</p>
<p>The cluster requires some seed nodes as its entry points. We set them in the <code>seed-nodes</code> array, in the format <code>akka.tcp://"{clustering.cluster.name}"@"{clustering.seed-ip}":”${clustering.seed-port}”</code>. Right now we have one seed node, but we may add more later.</p>
<p>The <code>auto-down-unreachable-after</code> property sets a member as down after it is unreachable for a period of time. This should be used only during development, as explained in the <a target="_blank" href="https://doc.akka.io/docs/akka/2.5/cluster-usage.html#auto-downing-do-not-use-">official documentation</a>.</p>
<p>Ok, the cluster is configured, we can move to the next step: Dockerization and deployment!</p>
<h4 id="heading-dockerization-and-deployment">Dockerization and deployment</h4>
<p>To create the Docker container of our node we can use <a target="_blank" href="https://www.scala-sbt.org/sbt-native-packager">sbt-native-packager</a>. Its installation is easy: add <code>addSbtPlugin("com.typesafe.sbt" % "sbt-native-packager" % "1.3.15")</code> to the <code>plugin.sbt</code> file in the <code>project/</code> folder. This amazing tool has a plugin for the creation of Docker containers. it allows us to configure the properties of our Dockerfile in the <code>build.sbt</code> file.</p>
<pre><code><span class="hljs-comment">// other build.sbt properties</span>

enablePlugins(JavaAppPackaging)
enablePlugins(DockerPlugin)
enablePlugins(AshScriptPlugin)

mainClass <span class="hljs-keyword">in</span> Compile := Some(<span class="hljs-string">"com.elleflorio.cluster.playground.Server"</span>)
<span class="hljs-attr">dockerBaseImage</span> := <span class="hljs-string">"java:8-jre-alpine"</span>
version <span class="hljs-keyword">in</span> Docker := <span class="hljs-string">"latest"</span>
<span class="hljs-attr">dockerExposedPorts</span> := Seq(<span class="hljs-number">8000</span>)
<span class="hljs-attr">dockerRepository</span> := Some(<span class="hljs-string">"elleflorio"</span>)
</code></pre><p>Once we have setup the plugin, we can create the docker image running the command <code>sbt docker:publishLocal</code>. Run the command and taste the magic… ?</p>
<p>We have the Docker image of our node, now we need to deploy it and check that everything works fine. The easiest way is to create a <code>docker-compose</code> file that will spawn a seed and a couple of other nodes.</p>
<pre><code class="lang-yml"><span class="hljs-attr">version:</span> <span class="hljs-string">'3.5'</span>

<span class="hljs-attr">networks:</span>
  <span class="hljs-attr">cluster-network:</span>

<span class="hljs-attr">services:</span>
  <span class="hljs-attr">seed:</span>
    <span class="hljs-attr">networks:</span>
      <span class="hljs-bullet">-</span> <span class="hljs-string">cluster-network</span>
    <span class="hljs-attr">image:</span> <span class="hljs-string">elleflorio/akka-cluster-playground</span>
    <span class="hljs-attr">ports:</span>
      <span class="hljs-bullet">-</span> <span class="hljs-string">'2552:2552'</span>
      <span class="hljs-bullet">-</span> <span class="hljs-string">'8000:8000'</span>
    <span class="hljs-attr">environment:</span>
      <span class="hljs-attr">SERVER_IP:</span> <span class="hljs-number">0.0</span><span class="hljs-number">.0</span><span class="hljs-number">.0</span>
      <span class="hljs-attr">CLUSTER_IP:</span> <span class="hljs-string">seed</span>
      <span class="hljs-attr">CLUSTER_SEED_IP:</span> <span class="hljs-string">seed</span>

  <span class="hljs-attr">node1:</span>
    <span class="hljs-attr">networks:</span>
      <span class="hljs-bullet">-</span> <span class="hljs-string">cluster-network</span>
    <span class="hljs-attr">image:</span> <span class="hljs-string">elleflorio/akka-cluster-playground</span>
    <span class="hljs-attr">ports:</span>
      <span class="hljs-bullet">-</span> <span class="hljs-string">'8001:8000'</span>
    <span class="hljs-attr">environment:</span>
      <span class="hljs-attr">SERVER_IP:</span> <span class="hljs-number">0.0</span><span class="hljs-number">.0</span><span class="hljs-number">.0</span>
      <span class="hljs-attr">CLUSTER_IP:</span> <span class="hljs-string">node1</span>
      <span class="hljs-attr">CLUSTER_PORT:</span> <span class="hljs-number">1600</span>
      <span class="hljs-attr">CLUSTER_SEED_IP:</span> <span class="hljs-string">seed</span>
      <span class="hljs-attr">CLUSTER_SEED_PORT:</span> <span class="hljs-number">2552</span>

  <span class="hljs-attr">node2:</span>
    <span class="hljs-attr">networks:</span>
      <span class="hljs-bullet">-</span> <span class="hljs-string">cluster-network</span>
    <span class="hljs-attr">image:</span> <span class="hljs-string">elleflorio/akka-cluster-playground</span>
    <span class="hljs-attr">ports:</span>
      <span class="hljs-bullet">-</span> <span class="hljs-string">'8002:8000'</span>
    <span class="hljs-attr">environment:</span>
      <span class="hljs-attr">SERVER_IP:</span> <span class="hljs-number">0.0</span><span class="hljs-number">.0</span><span class="hljs-number">.0</span>
      <span class="hljs-attr">CLUSTER_IP:</span> <span class="hljs-string">node2</span>
      <span class="hljs-attr">CLUSTER_PORT:</span> <span class="hljs-number">1600</span>
      <span class="hljs-attr">CLUSTER_SEED_IP:</span> <span class="hljs-string">seed</span>
      <span class="hljs-attr">CLUSTER_SEED_PORT:</span> <span class="hljs-number">2552</span>
</code></pre>
<p>I won’t spend time going through it, since it is quite simple.</p>
<h4 id="heading-lets-run-it">Let’s run it!</h4>
<p>Time to test our work! Once we run the <code>docker-compose up</code> command, we will have a cluster of three nodes up and running. The <code>seed</code> will respond to requests at port <code>:8000</code>, while <code>node1</code> and <code>node2</code> at port <code>:8001</code> and <code>:8002</code>. Play a bit with the various endpoints. You will see that the requests for a Fibonacci number will be computed by a different node each time, following a round-robin policy. That’s good, we are proud of our work and can get out for a beer to celebrate! ?</p>
<h4 id="heading-conclusion">Conclusion</h4>
<p>We are done here! We learned a lot of things in these ten minutes:</p>
<ul>
<li>What Akka Cluster is and what can do for us.</li>
<li>How to create a distributed application with it.</li>
<li>How to configure a Group Router for load-balancing in the cluster.</li>
<li>How to Dockerize everything and deploy it using docker-compose.</li>
</ul>
<p>You can find the complete application in my <a target="_blank" href="https://github.com/elleFlorio/akka-cluster-playground">GitHub repo</a>. Feel free to contribute or play with it as you like! ?</p>
<p>See you! ?</p>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ A survival guide to the Either monad in Scala ]]>
                </title>
                <description>
                    <![CDATA[ By Luca Florio I started to work with Scala few months ago. One of the concepts that I had the most difficulties to understand is the Either monad. So, I decided to play around with it and better understand its power. In this story I share what I’ve ... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/a-survival-guide-to-the-either-monad-in-scala-7293a680006/</link>
                <guid isPermaLink="false">66d45e3e8812486a37369c97</guid>
                
                    <category>
                        <![CDATA[ coding ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Functional Programming ]]>
                    </category>
                
                    <category>
                        <![CDATA[ General Programming ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Scala ]]>
                    </category>
                
                    <category>
                        <![CDATA[ tech  ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ freeCodeCamp ]]>
                </dc:creator>
                <pubDate>Fri, 21 Dec 2018 16:37:01 +0000</pubDate>
                <media:content url="https://www.freecodecamp.org/news/content/images/2019/06/either.jpeg" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>By Luca Florio</p>
<p>I started to work with Scala few months ago. One of the concepts that I had the most difficulties to understand is the <code>Either</code> monad. So, I decided to play around with it and better understand its power.</p>
<p>In this story I share what I’ve learned, hoping to help coders approaching this beautiful language.</p>
<h4 id="heading-the-either-monad">The Either monad</h4>
<p><code>Either</code> is one of the most useful monads in Scala. <a target="_blank" href="https://medium.com/@sinisalouc/demystifying-the-monad-in-scala-cc716bb6f534">If you are wondering what a monad is</a>, well… I cannot go into the details here, maybe in a future story!</p>
<p>Imagine <code>Either</code> like a box containing a computation. You work inside this box, until you decide to get the result out of it.</p>
<p>In this specific case, our <code>Either</code> box can have two “forms”. It can be (either) a <code>Left</code> or a <code>Right</code>, depending on the result of the computation inside it.</p>
<p>I can hear you asking: “OK, and what is it useful for?”</p>
<p>The usual answer is: error handling.</p>
<p>We can put a computation in the <code>Either</code>, and make it a <code>Left</code> in case of errors, or a <code>Right</code> containing a result in case of success. The use of <code>Left</code> for errors, and <code>Right</code> for success is a convention. Let’s understand this with some code!</p>
<div class="embed-wrapper"><div class="embed-loading"><div class="loadingRow"></div><div class="loadingRow"></div></div><a class="embed-card" href="https://scalafiddle.io/sf/sgAxvBI/0">https://scalafiddle.io/sf/sgAxvBI/0</a></div>
<p>In this snippet we are only defining an <code>Either</code> variable.</p>
<p>We can define it as a <code>Right</code> containing a valid value, or as <code>Left</code> containing an error. We also have a computation that return an <code>Either</code>, meaning it can be a <code>Left</code> or a <code>Right</code>. Simple, isn’t it?</p>
<h4 id="heading-right-and-left-projection">Right and left projection</h4>
<p>Once we have the computation in the box, we may want to get the value out of it. I’m sure you expect to call a <code>.get</code> on the <code>Either</code> and extract your result.</p>
<p>That’s not so simple.</p>
<p>Think about it: you put your computation in the <code>Either</code>, but you don’t know if it resulted in a <code>Left</code> or a <code>Right</code>. So what should a <code>.get</code> call return? The error, or the value?</p>
<p>This is why to get the result you should make an assumption about the outcome of the computation.</p>
<p>Here is where the <strong>projection</strong> comes into play.</p>
<p>Starting from an <code>Either</code>, you can get a <code>RightProjection</code> or a <code>LeftProjection</code>. The former means that you assume the computation resulted in a <code>Right</code>, the latter in a <code>Left</code>.</p>
<p>I know, I know… this may be a little confusing. It’s better to understand it with some code. After all, <strong>code always tells the truth</strong>.</p>
<div class="embed-wrapper"><div class="embed-loading"><div class="loadingRow"></div><div class="loadingRow"></div></div><a class="embed-card" href="https://scalafiddle.io/sf/8amYK4r">https://scalafiddle.io/sf/8amYK4r</a></div>
<p>That’s it. Note that when you try to get the result from a <code>RightProjection</code>, but it is a <code>Left</code>, you get an exception. The same goes for a <code>LeftProjection</code> and you have a <code>Right</code>.</p>
<p>The cool thing is that you can map on projections. This means you can say: “assume it is a Right: do this with it”, leaving the <code>Left</code> unchanged (and the other way around).</p>
<h4 id="heading-from-option-to-either">From Option to Either</h4>
<p><code>Option</code> is another common way to deal with invalid values.</p>
<p>An <code>Option</code> can have a value or be empty (it’s value is <code>Nothing</code>). I bet you noticed a similarity with <code>Either</code>… It’s even better, because we can actually transform an <code>Option</code> into an <code>Either</code>! Code time!</p>
<div class="embed-wrapper"><div class="embed-loading"><div class="loadingRow"></div><div class="loadingRow"></div></div><a class="embed-card" href="https://scalafiddle.io/sf/KCtNRkL">https://scalafiddle.io/sf/KCtNRkL</a></div>
<p>It is possible to transform an <code>Option</code> to a <code>Left</code> or a <code>Right</code>. The resulting side of the <code>Either</code> will contain the value of the <code>Option</code> if it is defined. Cool. Wait a minute… What if the <code>Option</code> is empty? We get the other side, but we need to specify what we expect to find in it.</p>
<h4 id="heading-inside-out">Inside out</h4>
<p><code>Either</code> is magic, we all agree on that. So we decide to use it for our uncertain computations. A typical scenario when doing functional programming is the mapping a function on a <code>List</code> of elements, or on a <code>Map</code>. Let’s do it with our fresh new <code>Either</code>-powered computation…</p>
<div class="embed-wrapper"><div class="embed-loading"><div class="loadingRow"></div><div class="loadingRow"></div></div><a class="embed-card" href="https://scalafiddle.io/sf/WozPbpV">https://scalafiddle.io/sf/WozPbpV</a></div>
<p>Huston, we have a “problem” (ok, it’s not a BIG problem, but it is a bit uncomfortable). It would be better to have the collection inside the <code>Either</code> than lots of <code>Either</code> inside the collection. We can work on that.</p>
<h4 id="heading-list"><strong>List</strong></h4>
<p>Let’s start with <code>List</code>. First we reason about it, then we can play with code.</p>
<p>We have to extract the value from the <code>Either</code>, put it in the <code>List</code>, and put the list inside an <code>Either</code>. Good, I like it.</p>
<p>The point is that we can have a <code>Left</code> or a <code>Right</code>, so we need to handle both cases. Until we find a <code>Right</code>, we can put its value inside a new <code>List</code>. We proceed this way accumulating every value in the new <code>List</code>.</p>
<p>Eventually we will reach the end of the <code>List</code> of <code>Either</code>, meaning we have a new <code>List</code> containing all the values. We can pack it in a <code>Right</code> and we are done. This was the case where our computation didn’t return an <code>Error</code> inside a <code>Left</code>.</p>
<p>If this happens, it means that something went wrong in our computation, so we can return the <code>Left</code> with the <code>Error</code>. We have the logic, now we need the code.</p>
<div class="embed-wrapper"><div class="embed-loading"><div class="loadingRow"></div><div class="loadingRow"></div></div><a class="embed-card" href="https://scalafiddle.io/sf/LjaXKPT">https://scalafiddle.io/sf/LjaXKPT</a></div>
<h4 id="heading-map"><strong>Map</strong></h4>
<p>The work on <code>Map</code> is quite simple once we have done the homework for the <code>List</code> (despite needing to make it generic):</p>
<ul>
<li>Step one: transform the <code>Map</code> in a <code>List</code> of <code>Either</code> containing the tuple (key, value).</li>
<li>Step two: pass the result to the function we defined on <code>List</code>.</li>
<li>Step three: transform the <code>List</code> of tuples inside the <code>Either</code> in a <code>Map</code>.</li>
</ul>
<p>Easy Peasy.</p>
<div class="embed-wrapper"><div class="embed-loading"><div class="loadingRow"></div><div class="loadingRow"></div></div><a class="embed-card" href="https://scalafiddle.io/sf/vlY7xV0">https://scalafiddle.io/sf/vlY7xV0</a></div>
<h4 id="heading-lets-get-classy-a-useful-implicit-converter">Let’s get classy: a useful implicit converter</h4>
<p>We introduced <code>Either</code> and understood it is useful for error handling. We played a bit with projections. We saw how to pass from an <code>Option</code> to an <code>Either</code>. We also implemented some useful functions to “extract” <code>Either</code> from <code>List</code> and <code>Map</code>. So far so good.</p>
<p>I would like to conclude our journey in the <code>Either</code> monad going a little bit further. The utility functions we defined do their jobs, but I feel like something is missing…</p>
<p>It would be amazing to do our conversion directly on the collection. We would have something like <code>myList.toEitherList</code> or <code>myMap.toEitherMap</code>. More or less like what we do with <code>Option.toRight</code> or <code>Option.toLeft</code>.</p>
<p>Good news: we can do it using <strong>implicit classes</strong>!</p>
<div class="embed-wrapper"><div class="embed-loading"><div class="loadingRow"></div><div class="loadingRow"></div></div><a class="embed-card" href="https://scalafiddle.io/sf/j8Ixqz5">https://scalafiddle.io/sf/j8Ixqz5</a></div>
<p>Using implicit classes in Scala lets us extend the capabilities of another class.</p>
<p>In our case, we extend the capability of <code>List</code> and <code>Map</code> to automagically “extract” the <code>Either</code>. The implementation of the conversion is the same we defined before. The only difference is that now we make it generic. Isn’t Scala awesome?</p>
<p>Since this can be a useful utility class, I prepared for you a gist you can copy and paste with ease.</p>
<pre><code class="lang-scala"><span class="hljs-class"><span class="hljs-keyword">object</span> <span class="hljs-title">EitherConverter</span> </span>{
  <span class="hljs-keyword">implicit</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">EitherList</span>[<span class="hljs-type">E</span>, <span class="hljs-type">A</span>](<span class="hljs-params">le: <span class="hljs-type">List</span>[<span class="hljs-type">Either</span>[<span class="hljs-type">E</span>, <span class="hljs-type">A</span>]]</span>)</span>{
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">toEitherList</span></span>: <span class="hljs-type">Either</span>[<span class="hljs-type">E</span>, <span class="hljs-type">List</span>[<span class="hljs-type">A</span>]] = {
      <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">helper</span></span>(list: <span class="hljs-type">List</span>[<span class="hljs-type">Either</span>[<span class="hljs-type">E</span>, <span class="hljs-type">A</span>]], acc: <span class="hljs-type">List</span>[<span class="hljs-type">A</span>]): <span class="hljs-type">Either</span>[<span class="hljs-type">E</span>, <span class="hljs-type">List</span>[<span class="hljs-type">A</span>]] = list <span class="hljs-keyword">match</span> {
        <span class="hljs-keyword">case</span> <span class="hljs-type">Nil</span> =&gt; <span class="hljs-type">Right</span>(acc)
        <span class="hljs-keyword">case</span> x::xs =&gt; x <span class="hljs-keyword">match</span> {
          <span class="hljs-keyword">case</span> <span class="hljs-type">Left</span>(e) =&gt; <span class="hljs-type">Left</span>(e)
          <span class="hljs-keyword">case</span> <span class="hljs-type">Right</span>(v) =&gt; helper(xs, acc :+ v)
        }
      }

      helper(le, <span class="hljs-type">Nil</span>)
    }
  }

  <span class="hljs-keyword">implicit</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">EitherMap</span>[<span class="hljs-type">K</span>, <span class="hljs-type">V</span>, <span class="hljs-type">E</span>](<span class="hljs-params">me: <span class="hljs-type">Map</span>[<span class="hljs-type">K</span>, <span class="hljs-type">Either</span>[<span class="hljs-type">E</span>, <span class="hljs-type">V</span>]]</span>) </span>{
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">toEitherMap</span></span>: <span class="hljs-type">Either</span>[<span class="hljs-type">E</span>, <span class="hljs-type">Map</span>[<span class="hljs-type">K</span>, <span class="hljs-type">V</span>]] = me.map{
        <span class="hljs-keyword">case</span> (k, <span class="hljs-type">Right</span>(v)) =&gt; <span class="hljs-type">Right</span>(k, v)
        <span class="hljs-keyword">case</span> (_, e) =&gt; e
      }.toList.toEitherList.map(l =&gt; l.asInstanceOf[<span class="hljs-type">List</span>[(<span class="hljs-type">K</span>, <span class="hljs-type">V</span>)]].toMap)
  }
}
</code></pre>
<h4 id="heading-conclusion">Conclusion</h4>
<p>That’s all folks. I hope this short story may help you to better understand the <code>Either</code> monad.</p>
<p>Please note that my implementation is quite simple. I bet there are more complex and elegant ways to do the same thing. I’m a newbie in Scala and I like to <a target="_blank" href="https://en.wikipedia.org/wiki/KISS_principle">KISS</a>, so I prefer readability over (elegant) complexity.</p>
<p>If you have a better solution, especially for the utility class, I will be happy to see it and learn something new! :-)</p>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ An introduction to Akka HTTP routing ]]>
                </title>
                <description>
                    <![CDATA[ By Miguel Lopez Akka HTTP’s routing DSL might seem complicated at first, but once you get the hang of it you’ll see how powerful it is. In this tutorial we will focus on creating routes and their structure. We won’t cover parsing to and from JSON, we... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/an-introduction-to-akka-http-routing-697b00399cad/</link>
                <guid isPermaLink="false">66c3440e4f1fc448a3678f9b</guid>
                
                    <category>
                        <![CDATA[ Akka ]]>
                    </category>
                
                    <category>
                        <![CDATA[ api ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Scala ]]>
                    </category>
                
                    <category>
                        <![CDATA[ tech  ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Web Development ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ freeCodeCamp ]]>
                </dc:creator>
                <pubDate>Wed, 19 Dec 2018 19:17:41 +0000</pubDate>
                <media:content url="https://cdn-media-1.freecodecamp.org/images/1*SLnwW0f177L3NOX0V88Z0g.png" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>By Miguel Lopez</p>
<p>Akka HTTP’s routing DSL might seem complicated at first, but once you get the hang of it you’ll see how powerful it is.</p>
<p>In this tutorial we will focus on creating routes and their structure. We won’t cover parsing to and from JSON, we have <a target="_blank" href="https://www.codemunity.io/tutorials/akka-http-json-circe/">other tutorials</a> that cover that topic.</p>
<h3 id="heading-what-are-directives">What are directives?</h3>
<p>One of the first concepts we’ll find when learning server-side Akka HTTP (there’s a client-side library as well) is <em>directives</em>.</p>
<p>So, what are they?</p>
<p>You can think of them as building blocks, Lego pieces if you will, that you can use to construct your routes. They are composable, which means we can create directives on top of other directives.</p>
<p>If you want a more in-depth reading, feel free to check out <a target="_blank" href="https://doc.akka.io/docs/akka-http/current/routing-dsl/directives/index.html">Akka HTTP’s official documentation</a>.</p>
<p>Before moving on, let’s discuss what we’ll build.</p>
<h3 id="heading-blog-like-api">Blog-like API</h3>
<p>We’ll create a sample of a public facing API for a blog, where we will allow users to:</p>
<ul>
<li>query a list of tutorials</li>
<li>query a single tutorial by ID</li>
<li>query the list of comments in a tutorial</li>
<li>add comments to a tutorial</li>
</ul>
<p>The endpoints will be:</p>
<pre><code>- List all tutorials GET /tutorials
</code></pre><pre><code>- Create a tutorial GET /tutorials/:id
</code></pre><pre><code>- Get all comments <span class="hljs-keyword">in</span> a tutorial GET /tutorials/:id/comments
</code></pre><pre><code>- Add a comment to a tutorial POST /tutorials/:id/comments
</code></pre><p>We will only implement the endpoints, no logic in them. This way we’ll learn how to create this structure and the common pitfalls when starting with Akka HTTP.</p>
<h3 id="heading-project-setup">Project Setup</h3>
<p>We’ve created a <a target="_blank" href="https://github.com/Codemunity/akka-http-routing-primer">repo</a> for this tutorial, in it you’ll find a branch per each section that requires coding. Feel free to clone it and use it as a base project or even just change between branches to look at the differences.</p>
<p>Otherwise, create a new SBT project, and then add the dependencies in the <code>build.sbt</code> file:</p>
<pre><code>name := <span class="hljs-string">"akkahttp-routing-dsl"</span>
</code></pre><pre><code>version := <span class="hljs-string">"0.1"</span>
</code></pre><pre><code>scalaVersion := <span class="hljs-string">"2.12.7"</span>
</code></pre><pre><code>val akkaVersion = <span class="hljs-string">"2.5.17"</span> val akkaHttpVersion = <span class="hljs-string">"10.1.5"</span>
</code></pre><pre><code>libraryDependencies ++= Seq(   <span class="hljs-string">"com.typesafe.akka"</span> %% <span class="hljs-string">"akka-actor"</span> % akkaVersion,   <span class="hljs-string">"com.typesafe.akka"</span> %% <span class="hljs-string">"akka-testkit"</span> % akkaVersion % Test,  <span class="hljs-string">"com.typesafe.akka"</span> %% <span class="hljs-string">"akka-stream"</span> % akkaVersion,   <span class="hljs-string">"com.typesafe.akka"</span> %% <span class="hljs-string">"akka-stream-testkit"</span> % akkaVersion % Test,   <span class="hljs-string">"com.typesafe.akka"</span> %% <span class="hljs-string">"akka-http"</span> % akkaHttpVersion,   <span class="hljs-string">"com.typesafe.akka"</span> %% <span class="hljs-string">"akka-http-testkit"</span> % akkaHttpVersion % Test,      <span class="hljs-string">"org.scalatest"</span> %% <span class="hljs-string">"scalatest"</span> % <span class="hljs-string">"3.0.5"</span> % Test )
</code></pre><p>We added Akka HTTP and its dependencies, Akka Actor and Streams. And we will also use Scalatest for testing.</p>
<h3 id="heading-listing-all-the-tutorials">Listing all the tutorials</h3>
<p>We’ll take a TDD approach to build our directive hierarchy, creating the tests first to make sure when don’t break our routes when adding others. Taking this approach is quite helpful when starting with Akka HTTP.</p>
<p>Let’s start with our route to listing all the tutorials. Create a new file under <code>src/test/scala</code> (if the folders don't exist, create them) named <code>RouterSpec</code>:</p>
<pre><code><span class="hljs-keyword">import</span> akka.http.scaladsl.testkit.ScalatestRouteTest <span class="hljs-keyword">import</span> org.scalatest.{Matchers, WordSpec}
</code></pre><pre><code><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">RouterSpec</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">WordSpec</span> <span class="hljs-title">with</span> <span class="hljs-title">Matchers</span> <span class="hljs-title">with</span> <span class="hljs-title">ScalatestRouteTest</span> </span>{
</code></pre><pre><code>}
</code></pre><p><code>WordSpec</code> and <code>Matchers</code> are provided by Scalatest, and we'll use them to structure our tests and assertions. <code>ScalatestRouteTest</code> is a trait provided by Akka HTTP's test kit, it will allow us to test our routes in a convenient way. Let's see how we can accomplish that.</p>
<p>Because we’re using <a target="_blank" href="http://www.scalatest.org/at_a_glance/WordSpec">Scalatest’s WordSpec</a>, we’ll start by creating a scope for our <code>Router</code> object that we will create soon and the first test:</p>
<pre><code><span class="hljs-string">"A Router"</span> should {   <span class="hljs-string">"list all tutorials"</span> <span class="hljs-keyword">in</span> {   } }
</code></pre><p>Next, we want to make sure can send a GET request to the path <code>/tutorials</code> and get the response we expect, let's see how we can accomplish that:</p>
<pre><code>Get(<span class="hljs-string">"/tutorials"</span>) ~&gt; Router.route ~&gt; check {   status shouldBe StatusCodes.OK   responseAs[<span class="hljs-built_in">String</span>] shouldBe <span class="hljs-string">"all tutorials"</span> }
</code></pre><p>It won’t even compile because we haven’t created our <code>Router</code> object. Let's do that now.</p>
<p>Create a new Scala object under <code>src/main/scala</code> named <code>Router</code>. In it we will create a method that will return a <code>Route</code>:</p>
<pre><code><span class="hljs-keyword">import</span> akka.http.scaladsl.server.Route
</code></pre><pre><code>object Router {
</code></pre><pre><code>  def route: Route = ???
</code></pre><pre><code>}
</code></pre><p>Don’t worry too much about the <code>???</code>, it's just a placeholder to avoid compilation errors temporarily. However, if that code is executed, it'll throw a <code>NotImplementedError</code> as we'll see soon.</p>
<p>Now that our tests and project are compiling, let’s run the tests (Right-click the spec and “Run ‘RouterSpec’”).</p>
<p>The test failed with the exception we were expecting, we haven’t implemented our routes. Let’s begin!</p>
<h3 id="heading-creating-the-listing-route">Creating the listing route</h3>
<p>By looking into the <a target="_blank" href="https://doc.akka.io/docs/akka-http/current/routing-dsl/directives/index.html#composing-directives">official documentation</a> we see that the route begins with the <code>path</code> directive. Let's mimic what they're doing and build our route:</p>
<pre><code><span class="hljs-keyword">import</span> akka.http.scaladsl.server.{Directives, Route}
</code></pre><pre><code>object Router <span class="hljs-keyword">extends</span> Directives {
</code></pre><pre><code>  def route: Route = path(<span class="hljs-string">"tutorials"</span>) {    get {      complete(<span class="hljs-string">"all tutorials"</span>)    }  }}
</code></pre><p>Seems reasonable, let’s run our spec. And it passes, great!</p>
<p>For reference, our entire <code>RouterSpec</code> now looks like:</p>
<pre><code><span class="hljs-keyword">import</span> akka.http.scaladsl.model.StatusCodesimport akka.http.scaladsl.testkit.ScalatestRouteTestimport org.scalatest.{Matchers, WordSpec}<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">RouterSpec</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">WordSpec</span> <span class="hljs-title">with</span> <span class="hljs-title">Matchers</span> <span class="hljs-title">with</span> <span class="hljs-title">ScalatestRouteTest</span> </span>{  <span class="hljs-string">"A Router"</span> should {    <span class="hljs-string">"list all tutorials"</span> <span class="hljs-keyword">in</span> {      Get(<span class="hljs-string">"/tutorials"</span>) ~&gt; Router.route ~&gt; check {        status shouldBe StatusCodes.OK        responseAs[<span class="hljs-built_in">String</span>] shouldBe <span class="hljs-string">"all tutorials"</span>      }    }  }}
</code></pre><h3 id="heading-getting-a-single-tutorial-by-id">Getting a single tutorial by ID</h3>
<p>Next, we will allow our users to retrieve a single tutorial.</p>
<p>Let’s add a test for our new route:</p>
<pre><code><span class="hljs-string">"return a single tutorial by id"</span> <span class="hljs-keyword">in</span> {  Get(<span class="hljs-string">"/tutorials/hello-world"</span>) ~&gt; Router.route ~&gt; check {    status shouldBe StatusCodes.OK    responseAs[<span class="hljs-built_in">String</span>] shouldBe <span class="hljs-string">"tutorial hello-world"</span>  }}
</code></pre><p>We expect to get back a message that includes the tutorial ID.</p>
<p>The test will fail because we haven’t created our route, let’s do that now.</p>
<p>From the same <a target="_blank" href="https://doc.akka.io/docs/akka-http/current/routing-dsl/directives/index.html#composing-directives">resource</a> we used earlier to base our route on, we can see how we can place multiple directives at the same level in the hierarchy using the <code>~</code> directive.</p>
<p>We will have to nest <code>path</code> directives because need another segment after the <code>/tutorials</code> route for the tutorial ID. In the documentation they use <code>IntNumber</code> to extract a number from the path, but we'll use a string and for that we use can <code>Segment</code> instead.</p>
<p>Our route looks like:</p>
<pre><code>def route: Route = path(<span class="hljs-string">"tutorials"</span>) {  get {    complete(<span class="hljs-string">"all tutorials"</span>)  } ~ path(Segment) { <span class="hljs-function"><span class="hljs-params">id</span> =&gt;</span>    get {      complete(s<span class="hljs-string">"tutorial $id"</span>)    }  }}
</code></pre><p>Let’s run the tests. And you should get a similar error:</p>
<pre><code>Request was rejectedScalaTestFailureLocation: RouterSpec at (RouterSpec.scala:<span class="hljs-number">17</span>)org.scalatest.exceptions.TestFailedException: Request was rejected
</code></pre><p>What’s going on?!</p>
<p>Well, a request is rejected when it doesn’t match our directive hierarchy. This is one of the things that got me when starting.</p>
<p>Now is probably a good time to look into how these directives match the incoming request as it goes through the hierarchy.</p>
<p>Different directives will match different aspects of an incoming request, we’ve seen <code>path</code> and <code>get</code>, one matches the URL of the request and the other the method. If a request matches a directive it will go inside it, if it doesn't it will continue to the next one. This also tells us that order matters. If it doesn't match any directive the request is rejected.</p>
<p>Now that we now that our request is not matching our directives, let’s start looking into why.</p>
<p>If we look the documentation for the <code>path</code> directive (Cmd + Click on Mac) we'll find:</p>
<pre><code><span class="hljs-comment">/** * Applies the given [[PathMatcher]] to the remaining unmatched path after consuming a leading slash. * The matcher has to match the remaining path completely. * If matched the value extracted by the [[PathMatcher]] is extracted on the directive level. * * <span class="hljs-doctag">@group </span>path */</span>
</code></pre><p>So, the <code>path</code> directive has to match exactly the path, meaning our first <code>path</code> directive will only match <code>/tutorials</code> and never <code>/tutorials/:id</code>.</p>
<p>In the same <code>PathDirectives</code> trait that contains the <code>path</code> directive we can see another directive named <code>pathPrefix</code>:</p>
<pre><code><span class="hljs-comment">/** * Applies the given [[PathMatcher]] to a prefix of the remaining unmatched path after consuming a leading slash. * The matcher has to match a prefix of the remaining path. * If matched the value extracted by the PathMatcher is extracted on the directive level. * * <span class="hljs-doctag">@group </span>path */</span>
</code></pre><p><code>pathPrefix</code> matches only a prefix and removes it. Sounds like this is what we're looking for, let's update our routes:</p>
<pre><code>def route: Route = pathPrefix(<span class="hljs-string">"tutorials"</span>) {  get {    complete(<span class="hljs-string">"all tutorials"</span>)  } ~ path(Segment) { <span class="hljs-function"><span class="hljs-params">id</span> =&gt;</span>    get {      complete(s<span class="hljs-string">"tutorial $id"</span>)    }  }}
</code></pre><p>Run the tests, and… we get another error. ?</p>
<pre><code><span class="hljs-string">"[all tutorials]"</span> was not equal to <span class="hljs-string">"[tutorial hello-world]"</span>ScalaTestFailureLocation: RouterSpec at (RouterSpec.scala:<span class="hljs-number">18</span>)Expected :<span class="hljs-string">"[tutorial hello-world]"</span>Actual   :<span class="hljs-string">"[all tutorials]"</span>
</code></pre><p>Looks like our request matched the first <code>get</code> directive. It now matches the <code>pathPrefix</code>, and because it also is a GET request it will match the first <code>get</code> directive. Order matters.</p>
<p>There are a couple of things we can do. The simplest solution would be to move the first <code>get</code> request to the end of the hierarchy, however, we would have to remember this or document it. Not ideal.</p>
<p>Personally, I prefer avoiding such solutions and instead make the intend clear through code. If we look in the <code>PathDirectives</code> trait from earlier, we'll find a directive called <code>pathEnd</code>:</p>
<pre><code><span class="hljs-comment">/** * Rejects the request if the unmatchedPath of the [[RequestContext]] is non-empty, * or said differently: only passes on the request to its inner route if the request path * has been matched completely. * * <span class="hljs-doctag">@group </span>path */</span>
</code></pre><p>That’s exactly what we want, so let’s wrap our first <code>get</code> directive with <code>pathEnd</code>:</p>
<pre><code>def route: Route = pathPrefix(<span class="hljs-string">"tutorials"</span>) {  pathEnd {    get {      complete(<span class="hljs-string">"all tutorials"</span>)    }  } ~ path(Segment) { <span class="hljs-function"><span class="hljs-params">id</span> =&gt;</span>    get {      complete(s<span class="hljs-string">"tutorial $id"</span>)    }  }}
</code></pre><p>Run the tests again, and… finally, the tests are passing! ?</p>
<h3 id="heading-listing-all-comments-in-a-tutorial">Listing all comments in a tutorial</h3>
<p>Let’s put into practice what we learned about nesting routes by taking it a bit further.</p>
<p>First the test:</p>
<pre><code><span class="hljs-string">"list all comments of a given tutorial"</span> <span class="hljs-keyword">in</span> {  Get(<span class="hljs-string">"/tutorials/hello-world/comments"</span>) ~&gt; Router.route ~&gt; check {    status shouldBe StatusCodes.OK    responseAs[<span class="hljs-built_in">String</span>] shouldBe <span class="hljs-string">"comments for the hello-world tutorial"</span>  }}
</code></pre><p>It’s a similar case as before: we know we’ll need to place a route next to another one, which means we need to:</p>
<ul>
<li>change the <code>path(Segmenter)</code> to <code>pathPrefix(Segmenter)</code></li>
<li>wrap the first <code>get</code> with the <code>pathEnd</code> directive</li>
<li>place the new route next to the <code>pathEnd</code></li>
</ul>
<p>Our routes end up looking like:</p>
<pre><code>def route: Route = pathPrefix(<span class="hljs-string">"tutorials"</span>) {  pathEnd {    get {      complete(<span class="hljs-string">"all tutorials"</span>)    }  } ~ pathPrefix(Segment) { <span class="hljs-function"><span class="hljs-params">id</span> =&gt;</span>    pathEnd {      get {        complete(s<span class="hljs-string">"tutorial $id"</span>)      }    } ~ path(<span class="hljs-string">"comments"</span>) {      get {        complete(s<span class="hljs-string">"comments for the $id tutorial"</span>)      }    }  }}
</code></pre><p>Run the tests, and they should pass! ?</p>
<h3 id="heading-adding-comments-to-a-tutorial">Adding comments to a tutorial</h3>
<p>Our last endpoint is similar to the previous, but it will match POST requests. We’ll use this example to see the difference between implementing and testing a GET request versus a POST request.</p>
<p>The test:</p>
<pre><code><span class="hljs-string">"add comments to a tutorial"</span> <span class="hljs-keyword">in</span> {  Post(<span class="hljs-string">"/tutorials/hello-world/comments"</span>, <span class="hljs-string">"new comment"</span>) ~&gt; Router.route ~&gt; check {    status shouldBe StatusCodes.OK    responseAs[<span class="hljs-built_in">String</span>] shouldBe <span class="hljs-string">"added the comment 'new comment' to the hello-world tutorial"</span>  }}
</code></pre><p>We’re using the <code>Post</code> method instead of the <code>Get</code> we've been using, and we're giving it an additional parameter which is the request body. The rest is familiar to us now.</p>
<p>To implement our last route, we can refer to the <a target="_blank" href="https://doc.akka.io/docs/akka-http/current/routing-dsl/index.html#longer-example">documentation</a> and look at how it’s usually done.</p>
<p>We have a <code>post</code> directive just as we have a <code>get</code> one. To extract the request body we need two directives, <code>entity</code> and <code>as</code>, to which we supply the type we expect. In our case it's a string.</p>
<p>Let’s give that a try:</p>
<pre><code>post {  entity(<span class="hljs-keyword">as</span>[<span class="hljs-built_in">String</span>]) { <span class="hljs-function"><span class="hljs-params">comment</span> =&gt;</span>    complete(s<span class="hljs-string">"added the comment '$comment' to the $id tutorial"</span>)  }}
</code></pre><p>Looks reasonable. We extract the request body as a string and use it in our response. Let’s add it to our <code>route</code> method next to the previous route we worked on:</p>
<pre><code>def route: Route = pathPrefix(<span class="hljs-string">"tutorials"</span>) {  pathEnd {    get {      complete(<span class="hljs-string">"all tutorials"</span>)    }  } ~ pathPrefix(Segment) { <span class="hljs-function"><span class="hljs-params">id</span> =&gt;</span>    pathEnd {      get {        complete(s<span class="hljs-string">"tutorial $id"</span>)      }    } ~ path(<span class="hljs-string">"comments"</span>) {      get {        complete(s<span class="hljs-string">"comments for the $id tutorial"</span>)      } ~ post {        entity(<span class="hljs-keyword">as</span>[<span class="hljs-built_in">String</span>]) { <span class="hljs-function"><span class="hljs-params">comment</span> =&gt;</span>          complete(s<span class="hljs-string">"added the comment '$comment' to the $id tutorial"</span>)        }      }    }  }}
</code></pre><p><em>If you’d like to learn how to parse Scala classes to and from JSON <a target="_blank" href="https://www.codemunity.io/tutorials/akka-http-json-circe/">we’ve got tutorials for that as well</a>.</em></p>
<p>Run the tests, and they should all pass.</p>
<h3 id="heading-conclusion">Conclusion</h3>
<p>Akka HTTP’s routing DSL might seem confusing at first, but after overcoming some bumps it just clicks. After a while it’ll come naturally and it can be very powerful.</p>
<p>We learned how to structure our routes, but more importantly, we learned how to create that structure guided by tests which will make sure we don’t break them at some point in the future.</p>
<p>Even though we only worked on four endpoints, we ended up with a somewhat complicated and deep structure. Stay tuned and we’ll explore different ways to simplify our routes and make them more manageable!</p>
<p><a target="_blank" href="http://link.codemunity.io/website-akka-http-quickstart">Learn how to build REST APIs with Scala and Akka HTTP with this step-by-step free course!</a></p>
<p><em>Originally published at <a target="_blank" href="https://www.codemunity.io/tutorials/akka-http-routing-primer/">www.codemunity.io</a>.</em></p>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ How to write composable functions and correct programs ]]>
                </title>
                <description>
                    <![CDATA[ By Pavel Zaytsev An overview with monads and Kleisli arrows A few words on the traditions ? If you have not written about monads are you even a programmer? It is a well-known fact that any respectable programmer who blogs about tech must at least ... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/monadic-composition-and-kleisli-arrows-1d96979bb32/</link>
                <guid isPermaLink="false">66c35b729de50ee9ca7fa709</guid>
                
                    <category>
                        <![CDATA[ Functional Programming ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Scala ]]>
                    </category>
                
                    <category>
                        <![CDATA[ software ]]>
                    </category>
                
                    <category>
                        <![CDATA[ software development ]]>
                    </category>
                
                    <category>
                        <![CDATA[ tech  ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ freeCodeCamp ]]>
                </dc:creator>
                <pubDate>Sun, 02 Dec 2018 11:52:18 +0000</pubDate>
                <media:content url="https://cdn-media-1.freecodecamp.org/images/1*CUnuyHcCX5A1KV1v3V8nTw.jpeg" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>By Pavel Zaytsev</p>
<h4 id="heading-an-overview-with-monads-and-kleisli-arrows">An overview with monads and Kleisli arrows</h4>
<p><img src="https://cdn-media-1.freecodecamp.org/images/1*CUnuyHcCX5A1KV1v3V8nTw.jpeg" alt="Image" width="800" height="533" loading="lazy"></p>
<h3 id="heading-a-few-words-on-the-traditions">A few words on the traditions ?</h3>
<blockquote>
<p>If you have not written about monads are you even a programmer?</p>
</blockquote>
<p>It is a well-known fact that any respectable programmer who blogs about tech must at least once in their life write a tutorial on <strong>monads</strong>. Most of the times it starts from <strong>functors</strong> and builds all the way up. The difficult comparisons are drawn here and there with the short snippets of code sprinkled around to demonstrate a couple of applications — all seem completely orthogonal, of course.</p>
<p>The problem is that there is a gigantic conceptual gap between “What is a monad?” and “How is a monad used in programming?”</p>
<p>The main reason for this gap is that people don’t usually think of their programs in such a way that they would need a monad in the first place. If you come from the imperative background — and most of us do — even when you finally go functional, it usually takes a while to see the entire picture and understand the correct way of doing things. We often preach referential transparency and purity. We often have a general idea of what these things mean separately, but it takes some time to piece them together in one coherent concept.</p>
<p>This article is not about monads but instead about writing composable functions and correct programs. The monads appear as the by-product of this goal. The following discussion also, hopefully, will facilitate bridging the aforementioned conceptual gap.</p>
<h3 id="heading-its-all-about-that-composition">It’s all about that composition ?‍</h3>
<p>In programming, the interesting and important problems are usually too complex too to deal with as a whole. Programmers break them up into smaller comprehensible pieces that they solve one-by-one and then bring the solutions to these subproblems into a clear picture. There are many ways of doing it, but there can be clearly defined two schools of thought — declarative and imperative approaches.</p>
<p>The imperative approach views a program as a sequence of computations in time, and the unit of a program is a statement. A statement does something. For example, it can print output to a console, assign a value to a variable, or compute a result.</p>
<p>The declarative approach is global. It cares only about the initial and final state of the system, and a unit of a program is an expression. The expression <strong>always</strong> evaluates to a value. In general, it’s harder to reason about a statement rather than about an expression because a statement is far too broad and unbounded, while an expression always deterministically evaluates to the same value for all the programs it can potentially appear in.</p>
<p>If you are a functional programmer and you follow a declarative paradigm, you see a program as a composition of functions, and functions are just the expressions. You can think of a composition as just a fusion of two or more functions into one.</p>
<p>If you have a function <em>f</em> that takes an argument of type A and it returns a result of type B and another function <em>g</em> that takes an argument of type B, and it returns a result of type C, you can compose them by passing a result of <em>f</em> to <em>g</em>. What you get is a new function that accepts an argument of type A and produces the result of type C. Do a couple of these, and you get a working program. This is similar to deductive reasoning, which is just a composition of logical steps, that follow from premises to the conclusions.</p>
<p>The problem is that in real programming it’s not so simple, and many functions do not evaluate to the same value all the time even if their arguments do. These functions do network calls, they read from files and databases, they write to them, they process a lot of different data, they print to a console, etc. Functions become <strong>context-dependent,</strong> and the more context-dependent they are, the more difficult it is to compose them. So if you have to deal with functions where each one of them introduces some of this complexity here and there, you better find a way to compose them.</p>
<h3 id="heading-the-ever-lasting-problem">The ever-lasting problem ?</h3>
<p>As I have already mentioned, writing useful code entails dealing with the chaos of the outside world — a problem of handling effects and state. By effect, I mean an effect on the outside world, like writing to a database or printing to a console. Even a simple beaten-to-death <code>HelloWorld</code> program produces an effect.</p>
<p>Many situations are traditionally solved by abandoning the purity of functions, including but not limited to:</p>
<ul>
<li>computations that access/modify a state outside of the context of a program.</li>
<li>computations that may fail or never terminate</li>
<li>computations whose result might be only known sometime in the future, aka asynchronous computations</li>
<li>console input and output</li>
</ul>
<p>As you can imagine, the functions that behave like this are not so easy to compose. Let’s say a function, given some username in the form of an input string, reads from a file, finds the given user, and then passes their last name to the next function for later processing. How do you represent a function like that? <code>String =&gt; Str</code>ing? But that would not be entirely correct, as you do get a string and you do return one, but you also do something else in between and it depends on the context — a file system and a file.</p>
<p>In functional programming, we usually encode this information in the function’s return type. We place the result of a computation in a box, so to speak, and our function turns from <code>A =&gt;</code>; B <code>to A =&gt;</code> Box[B].</p>
<p>Whatever happens inside this box is implementation-specific. But the definition of a function now describes what it does and allows to handle the function's behavior and compose it, much better.</p>
<p>Let’s look at the situations above and try to come up with a box for every one of them.</p>
<h3 id="heading-box-all-the-things">Box all the things ?</h3>
<p>Disclaimer: In this article, I will be using Scala to express my ideas, as it’s arguably one of the mainstream functional programming languages out there. But the same concepts can be easily translated into any functional programming language with a strong static type system and type constructors (like Haskell and OCaml).</p>
<p>A function that reads but does not modify the external state can be thought of as a function that also accepts this external state as an argument and, given some input parameter, produces a result. An example is reading from a database or config file.</p>
<p>We also supply a <code>run</code> function to materialize a reader and compute a result. The classic use case would be to:</p>
<ul>
<li>provide a configuration file</li>
<li>stack the readers to set up an application</li>
<li>execute the <code>run</code> function in the end on the instance of the config file.</li>
</ul>
<p>In the OOP world, a similar approach goes by <strong>Dependency Injection</strong>.</p>
<p>Functions that carry along the state of a computation accept two arguments — a regular one, that participates in the actual computation, and the state that will be propagated all the way through the composition.</p>
<p>We supply a <code>run</code> function that yields the result which a current writer holds. A typical use case of <code>Writer</code> is to propagate messages and errors during the program execution, and extracts the log alongside the final result. Another use case would be to record the sequences of steps in a multi-threaded environment. Since the result of a computation is tied to a particular log, the messages don’t get intertwined.</p>
<p>A function that never terminates (for example a long running process) is lifted to a <strong>bottom type</strong> of <code>Nothing</code>. If you expect that your computation might either return a value or run forever, you can model it as a disjoint union of a result type and <code>Nothing</code>. An example would be a stream consumer that processes data indefinitely until some event makes it yield.</p>
<p>A function that might fail can be modeled as a partial function — the one that’s not defined for some values. So we do just that — for some cases return nothing and, for others, an actual value. If you want the information regarding the failure and to interop with Java APIs that throw exceptions, you can carry the information of an error with you in the disjoint union.</p>
<p>Asynchronous computations do not execute on the current call-stack or a main flow of the program. They can be modeled as a function that accepts a handler, or callback. When this handler eventually is called — for example by some other thread or a web server — the result is produced.</p>
<p><code>ExecutionContext</code> manages the details of the asynchronous execution. On JVM it is a thread-pool, but it does not have to be threads that deal with asynchrony. Function <code>run</code> takes it implicitly because callbacks need to be called asynchronously. Every function that calls <code>run</code> needs to have an <strong>implicit</strong> <code>ExecutionContext</code> in its signature as well. <code>ExecutionContext</code> also makes recursive asynchronous calls stack-safe because you introduce an <strong>asynchronous boundary</strong> every time you call a function. As I mentioned earlier, asynchronous computations do not execute on the same call-stack. Pretty neat.</p>
<p>Console input can be modeled as a two-phase process. In the first phase we <strong>describe</strong> the computation and a result that a console input might produce. In the second phase, we run or <strong>interpret</strong> this computation. The data type that holds the outcome of this computation is called <code>IO</code>. So our function takes a singleton and produces the result inside of an <code>IO</code>.</p>
<p>Notice, since I mentioned that we describe the computation first, we pass call-by-name parameters <code>=&gt;</code>; A. They execute only when we need them to.<br>Console output can be modeled throu<code>gh</code> IO as well, with o<code>ur</code> run function producing a singleton type <code>of U</code>nit.</p>
<h3 id="heading-so-now-what">So, now what ? ?</h3>
<p>We, of course, implemented all the boxes with a type parameter to account for variability in types. We transform, or map, from some stuff to other stuff by supplying a function. A mapping between a category of some things to a category of other things is called a <strong>functor</strong>.</p>
<p>Functors are cool because they allow us to transform stuff inside, while preserving the structure of the original category, without messing things up. When using a functor, following the <a target="_blank" href="https://en.wikibooks.org/wiki/Haskell/The_Functor_class#The_functor_laws">functor laws</a> proves that things work as expected. It will get clear later in the article why <strong>these</strong> laws in particular.</p>
<p>Let’s define a map method that does transformation for each of the derived data types:</p>
<p>Notice that here we have defined a function <code>pure</code> that permits us to lift a pure computation into an asynchronous value.</p>
<p>Cool, now we can map things inside of the boxes. The problem is — it’s not useful because, although we can sequence computations within a functor, we can’t compose functor-producing functions:</p>
<pre><code>F1: A -&gt; Functor[B] ==&gt; F2: A -&gt; Functor[B] ==&gt; F3: A -&gt; Functor[B]
</code></pre><p>Each of <code>F1</code>, <code>F2</code>, and <code>F3</code> may do something completely different. We need to account for that, we need to compose them. Fortunately, there is an excellent way of doing this.</p>
<h3 id="heading-oh-no-its-that-guy-again">Oh no, it’s that guy again! ?</h3>
<p>Ok, I need to write a function that composes the functions for each of the functors in <code>A =&gt; Functor</code>[B]. The mathematical definition of composition is:</p>
<pre><code>If A =&gt;; B and B =&gt; C then A =&gt; C
</code></pre><p>So in our case:</p>
<pre><code>If A =&gt; Functor[B] and B =&gt; Functor[C] then A =&gt; Functor[C]
</code></pre><p>Let’s start with a reader. Again, just follow the types:</p>
<p>We defined composition as <code>andThen</code> . We also defined <code>pure</code> , which lifts a value into a functor.</p>
<p>Composition in any category follows <strong>two simple laws</strong>.</p>
<ol>
<li><p>It is associative:</p>
</li>
<li><p>There exists such a function, called <strong>identity,</strong> that, when composed with any function from left or right, produces that function again:</p>
</li>
</ol>
<p>You can take a pen and a piece of paper and try to draw these two laws. Substituting entities with circles and connections between them as arrows, you can visually prove to yourself that this is the case. Remember functor laws earlier? It’s the same thing. These laws establish the composability in every category.</p>
<p>As programmers, we mostly deal with a category of sets — objects are sets or types, and arrows are functions. Not all mathematical structures need to have an identity, but category certainly does.</p>
<p>Identity, for example, can be used to show whether two objects, “sets,” are isomorphic, or equal. It can also provide guarantees in certain instances as we will see later in the article.</p>
<p><img src="https://cdn-media-1.freecodecamp.org/images/1*TNDCu4w78aQktMU5aisvPg.png" alt="Image" width="544" height="338" loading="lazy">
<em>Category only has two properties — simple enough.</em></p>
<p>A functor that participates in <code>A =&gt; Functor</code>[B] and follows these composition laws, and hence composes for all the objects in the same catego<a target="_blank" href="https://blog.plover.com/prog/burritos.html">ry is not a burr</a>ito. It’s a monad.</p>
<p>Accordingly, the functions that we try to compose are actually <code>A =&gt; Monad</code>[B] . A wrapper around them, a category that is naturally associated with <code>a Monad</code>[B], is called a Kleisli categor<code>y. A =&gt; Mo</code>nad[<code>B] or K</code>leisli arrows is just a way to compose these sort of functions, nothing more.</p>
<p><img src="https://cdn-media-1.freecodecamp.org/images/1*pd47LtHoY8Hl4g_coz9SsQ.png" alt="Image" width="502" height="317" loading="lazy">
<em>Kleisli has the same objects as the original category but arrows between a and b in Kleisli correspond to the arrows between a and Fb, where F is a functor.</em></p>
<h3 id="heading-compose-all-the-things">Compose all the things! ?</h3>
<p>We replace Java-like <code>andThen</code> with an arrow <code>&gt;</code>==&gt; . This is called a fish operator because it looks like a fish ?. We also define a func<code>tion fl</code>atMap that, giv<code>en a Mon</code>ad[A] <code>and A =&gt;</code> Monad[B], p<code>roduces A =&amp;g</code>t; Monad[B]. This will make implementing arrows easier:</p>
<h4 id="heading-monoids">Monoids</h4>
<p>For a writer to be a monad, a second type should be composable too, and it’s composable only if it’s a <strong>monoid</strong>. Something is a monoid only if it can be associatively combined and has an empty element, which is an identity element.</p>
<p>For example, integers that sum up are a monoid with an identity element of 0. Strings that concat are a monoid with identity element of an empty string. Sets that union are a monoid with an identity element of an empty set. Sets that intersect form a semigroup and not a monoid. Intersecting a non-empty set with an empty one produces an empty set. You see why identity is useful?</p>
<p>Every other monad is easy to implement:</p>
<p>We will go into detail why <code>IO</code> looks like this and have a discussion about <code>Free</code> probably in another article.</p>
<h3 id="heading-when-to-use-which">When to use which? ?</h3>
<p><code>Kleisli</code> and <code>Monad</code> are two sides of the same coin. Many functional languages support them natively, but for Scala, at least in the current version 2.12.8, you can get them from the libraries like Cats and Scalaz. They have type classes for <code>Kleisli</code> and <code>Monad</code>: <code>Kleisli[F[_], A, B]</code> and <code>Monad[A]</code> respectively.</p>
<p>Monads are better at expressing the sequencing of the computations that happen within some context. In Scala, we do this contextual sequencing usually by utilizing for comprehensions.  </p>
<p>Fun fact: If you compose <code>Kleisli</code> arrows for <code>IO</code> monad, you will get a description of your computer program. Your computer program is essentially one gigantic <code>Kleisli</code> arrow, with some input and output of <code>Unit</code> that acts as a description, and a runtime environment that executes this program works as an interpreter.</p>
<p>So every program, by default, has an <code>IO</code> context. If you have a function that produces an error, it creates a context of <code>Option</code> or <code>Either.</code> So every subsequent function should have a signature of <code>A =&gt; Option</code>[A] <code>or A =&gt; Either[</code>A, B]. With<code>in</code> an IO program <code>it’s A =&gt; IO[O</code>ption<code>[A]] and A =&gt; IO[E</code>ither[A, B]]. You decide when to collapse this context or nest it even further. Monad transformers can help with the sequencing of the nested contexts, but that is material for another article.  </p>
<p>Asynchronous computations are only sequenced with the help of monads bec<strong>ause monad</strong>s explicitly solve the problems of synchronization and ordering in time. <code>If yo</code>u use Future combinators, for example, it is within a monadic context.</p>
<p><code>Kleisli</code> is better when the arrow combinator is more suitable. For example, we can have a bunch of effectful or impure functions, and we can fuse them without wrapping the result into <code>IO</code>. You can also create a bunch of individual programs and combine them into one <code>Kleisli</code> in the end and run if you want, of course.</p>
<p><code>Reader[A]</code> is better expressed with <code>Kleisli</code> than with monadic compositions because we can easily compose small local <code>Kleisli</code> arrows into a <code>Kleisli</code> arrow that represents our program’s global configuration. The large <code>Kleisli</code> arrow depends on some environment, let’s say a configuration file for production and development. Then we can run this at the very end, and configure the entire app.</p>
<p>This approach mimics the concept of <strong>dependency injection</strong>. Here we demonstrate <code>KleisliIO</code>, an effectful <code>Kleisli</code> from <a target="_blank" href="https://github.com/scalaz/scalaz-zio">ZIO</a> that performs a reader functionality to configure the application (error type omitted to save some space):</p>
<h3 id="heading-end">End ?</h3>
<p>Whew, that was a blast. I hope you enjoyed this article and now understand what’s going a little bit better :).</p>
<p>Here are some useful links to learn more about Kleisli and composition:</p>
<ul>
<li><a target="_blank" href="http://www.cse.chalmers.se/~rjmh/Papers/arrows.pdf">www.cse.chalmers.se/~rjmh/Papers/arrows.pdf</a></li>
<li><a target="_blank" href="https://www.youtube.com/watch?v=qL6Viix3npA">https://www.youtube.com/watch?v=qL6Viix3npA</a></li>
</ul>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ A streaming library with a superpower: FS2 and functional programming ]]>
                </title>
                <description>
                    <![CDATA[ By Daniel Sebban Scala has a very special streaming library called FS2 (Functional Streams for Scala). This library embodies all the advantages of functional programming (FP). By understanding its design goals you will get exposure to the core ideas ... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/a-streaming-library-with-a-superpower-fs2-and-functional-programming-6f602079f70a/</link>
                <guid isPermaLink="false">66d45e3c680e33282da25e67</guid>
                
                    <category>
                        <![CDATA[ Functional Programming ]]>
                    </category>
                
                    <category>
                        <![CDATA[ General Programming ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Scala ]]>
                    </category>
                
                    <category>
                        <![CDATA[ tech  ]]>
                    </category>
                
                    <category>
                        <![CDATA[ technology ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ freeCodeCamp ]]>
                </dc:creator>
                <pubDate>Mon, 19 Nov 2018 21:20:26 +0000</pubDate>
                <media:content url="https://cdn-media-1.freecodecamp.org/images/1*Mi3HukEz9_JHfv3Z5lp93g.jpeg" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>By Daniel Sebban</p>
<p>Scala has a very special streaming library called FS2 (Functional Streams for Scala). This library embodies all the advantages of functional programming (FP). By understanding its design goals you will get exposure to the core ideas that make FP so appealing.</p>
<p>FS2 has one central type: <code>**Stream[Effect,Output]**</code></p>
<p>You might get from this type that it’s a <code>Stream</code> and that it emits values of type <code>Output</code>.</p>
<p>The obvious question here is what is <code>Effect</code>? What is the link between <code>Effect</code> and <code>Output</code>? And what advantages does FS2 have over other streaming libraries?</p>
<h3 id="heading-overview">Overview</h3>
<p>I will start by reviewing what problems FS2 solves. Then I compare <code>List</code> and <code>Stream</code> with several code examples. After that, I will focus on how to use <code>Stream</code> with a DB or any other IO. That is where FS2 shines and where the <code>Effect</code>type is used. Once you will understand what <code>Effect</code> is, the advantages of Functional Programming should be evident to you.</p>
<p>At the end of this post you will get the answers to the following questions:</p>
<ul>
<li>What problems can I solve with FS2?</li>
<li>What can I do with <code>Stream</code> that <code>List</code> cannot ?</li>
<li>How can I feed data from an API/File/DB to <code>Stream</code> ?</li>
<li>What is this <code>Effect</code> type and how does it relate to functional programming?</li>
</ul>
<p>Note: The code is in Scala and should be understandable even without prior knowledge of the syntax.</p>
<h3 id="heading-what-problems-can-i-solve-with-fs2">What problems can I solve with FS2?</h3>
<ol>
<li>Streaming I/O: Loading incrementally big data sets that would not fit in memory and operating on them without blowing your heap.</li>
<li>Control Flow (not covered): Moving data from one/several DBs/files/APIs to others in a nice declarative way.</li>
<li>Concurrency (not covered): Run different streams in parallel and make them communicate together. For example loading data from multiple files and processing them concurrently as opposed to sequentially. You can do some advanced stuff here. Streams can communicate together <strong>during</strong> the processing stage and not only at the end.</li>
</ol>
<h3 id="heading-list-vs-stream"><code>List</code> vs <code>Stream</code></h3>
<p><code>List</code> is the most well known and used data structure. To get a feel for how it differs from an FS2 <code>Stream</code>, we will go through a few use cases. We will see how <code>Stream</code> can solve problems that <code>List</code> cannot.</p>
<h3 id="heading-your-data-is-too-big-and-does-not-fit-in-memory">Your data is too big and does not fit in memory</h3>
<p>Let’s say you have a very big file (40GB) <code>fahrenheit.txt</code>. The file has a temperature on each line and you want to convert it to <code>celsius.txt</code>.</p>
<h3 id="heading-loading-a-big-file-using-list">Loading a big file using <code>List</code></h3>
<pre><code class="lang-scala"><span class="hljs-keyword">import</span> scala.io.<span class="hljs-type">Source</span>
<span class="hljs-keyword">val</span> list = <span class="hljs-type">Source</span>.fromFile(<span class="hljs-string">"testdata/fahrenheit.txt"</span>).getLines.toList
java.lang.<span class="hljs-type">OutOfMemoryError</span>: <span class="hljs-type">Java</span> heap space
  java.util.<span class="hljs-type">Arrays</span>.copyOfRange(<span class="hljs-type">Arrays</span>.java:<span class="hljs-number">3664</span>)
  java.lang.<span class="hljs-type">String</span>.&lt;init&gt;(<span class="hljs-type">String</span>.java:<span class="hljs-number">207</span>)
  java.io.<span class="hljs-type">BufferedReader</span>.readLine(<span class="hljs-type">BufferedReader</span>.java:<span class="hljs-number">356</span>)
  java.io.<span class="hljs-type">BufferedReader</span>.readLine(<span class="hljs-type">BufferedReader</span>.java:<span class="hljs-number">389</span>)
</code></pre>
<p><code>List</code> fails miserably because of course, the file is too big to fit in memory. If you are curious, you can go check the full solution using <code>Stream</code> <a target="_blank" href="https://functional-streams-for-scala.github.io/fs2/#about">here</a> — but do that later, read on :)</p>
<h3 id="heading-when-list-wont-dostream-to-the-rescue">When List won’t do…Stream to the rescue!</h3>
<p>Let’s say I succeeded in reading my file and I want to write it back. I would like to preserve the line structure. I need to insert a newline character <code>\n</code> after each temperature.</p>
<p>I can use the <code>intersperse</code> combinator to do that</p>
<pre><code class="lang-scala"><span class="hljs-keyword">import</span> fs2._
<span class="hljs-type">Stream</span>(<span class="hljs-number">1</span>,<span class="hljs-number">2</span>,<span class="hljs-number">3</span>,<span class="hljs-number">4</span>).intersperse(<span class="hljs-string">"\n"</span>).toList
</code></pre>
<p>Another nice one is <code>zipWithNext</code></p>
<pre><code class="lang-scala">scala&gt; <span class="hljs-type">Stream</span>(<span class="hljs-number">1</span>,<span class="hljs-number">2</span>,<span class="hljs-number">3</span>,<span class="hljs-number">4</span>).zipWithNext.toList
res1: <span class="hljs-type">List</span>[(<span class="hljs-type">Int</span>, <span class="hljs-type">Option</span>[<span class="hljs-type">Int</span>])] = <span class="hljs-type">List</span>((<span class="hljs-number">1</span>,<span class="hljs-type">Some</span>(<span class="hljs-number">2</span>)), (<span class="hljs-number">2</span>,<span class="hljs-type">Some</span>(<span class="hljs-number">3</span>)), (<span class="hljs-number">3</span>,<span class="hljs-type">Some</span>(<span class="hljs-number">4</span>)), (<span class="hljs-number">4</span>,<span class="hljs-type">None</span>))
</code></pre>
<p>It bundles consecutive things together, very useful if you want to <a target="_blank" href="https://gist.github.com/dsebban/bb34ea4671bda8d52e2f083e2b160778">remove consecutive duplicates</a>.</p>
<p>These are only a few from a lot of very useful ones, here is the <a target="_blank" href="https://oss.sonatype.org/service/local/repositories/releases/archive/co/fs2/fs2-core_2.12/0.10.5/fs2-core_2.12-0.10.5-javadoc.jar/!/fs2/Stream.html">full list</a>.</p>
<p>Obviously <code>Stream</code> can do a lot of things that <code>List</code> cannot, but the best feature is coming in the next section, it's all about how to use <code>Stream</code> in the real world with DBs/Files/APIs ...</p>
<h3 id="heading-how-can-i-feed-data-from-an-apifiledb-to-stream">How can I feed data from an API/File/DB to <code>Stream</code>?</h3>
<p>Let’s just say for now that this our program</p>
<pre><code class="lang-scala">scala&gt; <span class="hljs-type">Stream</span>(<span class="hljs-number">1</span>,<span class="hljs-number">2</span>,<span class="hljs-number">3</span>)
res2: fs2.<span class="hljs-type">Stream</span>[fs2.<span class="hljs-type">Pure</span>,<span class="hljs-type">Int</span>] = <span class="hljs-type">Stream</span>(..)
</code></pre>
<p>What does this <code>Pure</code> mean? Here is the scaladoc from the source code:</p>
<pre><code class="lang-scala"><span class="hljs-comment">/**
    * Indicates that a stream evaluates no effects.
    *
    * A `Stream[Pure,O]` can be safely converted to a `Stream[F,O]` for all `F`.
*/</span>
<span class="hljs-class"><span class="hljs-keyword">type</span> <span class="hljs-title">Pure</span>[<span class="hljs-type">A</span>] <span class="hljs-title">&lt;</span></span>: <span class="hljs-type">Nothing</span>
</code></pre>
<p>It means no effects, ok …, but <strong>What is an effect?</strong> and more specifically what is the effect of our program <code>Stream(1,2,3)</code>?</p>
<p>This program has literally no <em>effect</em> on the world. Its only effect will be to make your CPU work and consumes some power!! It does not affect the world around you.</p>
<p>By affecting the world I mean it <strong>consumes</strong> any meaningful resource like a file, a database, or it <strong>produces</strong> anything like a file, uploading some data somewhere, writing to your terminal, and so on.</p>
<h3 id="heading-how-do-i-turn-a-pure-stream-to-something-useful">How do I turn a <code>Pure</code> stream to something useful?</h3>
<p>Let’s say I want to load user ids from a DB, I am given this function, it does a call to the DB and returns the userId as a <code>Long</code>.</p>
<pre><code class="lang-scala"><span class="hljs-keyword">import</span> scala.concurrent.<span class="hljs-type">Future</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">loadUserIdByName</span></span>(userName: <span class="hljs-type">String</span>): <span class="hljs-type">Future</span>[<span class="hljs-type">Long</span>] = ???
</code></pre>
<p>It returns a <code>[Future](https://www.scala-lang.org/api/2.12.3/scala/concurrent/Future.html)</code> which indicates that this call is asynchronous and the value will be available at some point in the future. It wraps the value returned by the DB.</p>
<p>I have this <code>Pure</code> stream.</p>
<pre><code class="lang-scala">scala&gt; <span class="hljs-keyword">val</span> names = <span class="hljs-type">Stream</span>(<span class="hljs-string">"bob"</span>, <span class="hljs-string">"alice"</span>, <span class="hljs-string">"joe"</span>)
names: fs2.<span class="hljs-type">Stream</span>[fs2.<span class="hljs-type">Pure</span>,<span class="hljs-type">String</span>] = <span class="hljs-type">Stream</span>(..)
</code></pre>
<p>How do I get a <code>Stream</code> of ids?</p>
<p>The naive approach would be to use the <code>map</code> function, it should run the function for each value in the <code>Stream</code>.</p>
<pre><code class="lang-scala">scala&gt; userIdsFromDB.compile
res5: fs2.<span class="hljs-type">Stream</span>.<span class="hljs-type">ToEffect</span>[scala.concurrent.<span class="hljs-type">Future</span>,<span class="hljs-type">Long</span>] = fs2.<span class="hljs-type">Stream</span>$<span class="hljs-type">ToEffect</span><span class="hljs-meta">@fc</span>0f18da
</code></pre>
<p>I still got back a <code>Pure</code>! I gave the <code>Stream</code> a function that <em>affects the world</em> and I still got a <code>Pure</code>, not cool ... It would have been neat if FS2 would have detected automatically that the <code>loadUserIdByName</code> function has an <em>effect</em> on the world and returned me something that is NOT <code>Pure</code> but it does not work like that. You have to use a special combinator instead of <code>map</code>: you have to use <code>evalMap</code>.</p>
<pre><code class="lang-scala">scala&gt; userIdsFromDB.toList
&lt;console&gt;:<span class="hljs-number">18</span>: error: value toList is not a member of fs2.<span class="hljs-type">Stream</span>[scala.concurrent.<span class="hljs-type">Future</span>,<span class="hljs-type">Long</span>]
       userIdsFromDB.toList
                     ^
</code></pre>
<p>No more <code>Pure</code>! we got <code>Future</code> instead, yay! What just happened?</p>
<p>It took:</p>
<ul>
<li><code>loadUserIdByName: Future[Long]</code></li>
<li><code>Stream[Pure, String]</code></li>
</ul>
<p>And switched the types of the stream to</p>
<ul>
<li><code>Stream[Future, Long]</code></li>
</ul>
<p>It separated the <code>Future</code> and isolated it! The left side that was the <code>Effect</code> type parameter is now the concrete <code>Future</code> type.</p>
<p>Neat trick, but how does it help me?</p>
<p>You just witnessed true <strong>separation of concerns.</strong> You can continue to operate on the stream with all the nice <code>List</code> like combinators and you don't have to worry about if the DB is down, slow or all the stuff that is related to the network (effect) concerns.</p>
<p>It all works until I want to use <code>toList</code> to get the values back</p>
<pre><code class="lang-scala">scala&gt; userIdsFromDB.toList
&lt;console&gt;:<span class="hljs-number">18</span>: error: value toList is not a member of fs2.<span class="hljs-type">Stream</span>[scala.concurrent.<span class="hljs-type">Future</span>,<span class="hljs-type">Long</span>]
       userIdsFromDB.toList
                     ^
</code></pre>
<p>What???!!! I could swear that I used <code>toList</code> before and it worked, how can it say that <code>toList</code> is not a member of <code>fs2.Stream[Future,String]</code> any more? It is as if this function was removed the moment I started using an effect-ful stream, that's impressive! But how do I get my values back?</p>
<pre><code class="lang-scala">scala&gt; userIdsFromDB.compile
res5: fs2.<span class="hljs-type">Stream</span>.<span class="hljs-type">ToEffect</span>[scala.concurrent.<span class="hljs-type">Future</span>,<span class="hljs-type">Long</span>] = fs2.<span class="hljs-type">Stream</span>$<span class="hljs-type">ToEffect</span><span class="hljs-meta">@fc</span>0f18da
</code></pre>
<p>First we use <code>compile</code> to tell the <code>Stream</code> to combine all the effects into one, effectively it folds all the calls to <code>loadUserIdByName</code> into one big <code>Future</code>. This is needed by the framework, and it will become apparent why this step is needed soon.</p>
<p>Now <code>toList</code> should work</p>
<pre><code class="lang-scala">scala&gt; userIdsFromDB.compile.toList
&lt;console&gt;:<span class="hljs-number">18</span>: error: could not find <span class="hljs-keyword">implicit</span> value <span class="hljs-keyword">for</span> parameter <span class="hljs-type">F</span>: cats.effect.<span class="hljs-type">Sync</span>[scala.concurrent.<span class="hljs-type">Future</span>]
       userIdsFromDB.compile.toList
                             ^
</code></pre>
<p>What?! the compiler is still complaining. That’s because <code>Future</code> is not a good <code>Effect</code> type — it breaks the philosophy of separation of concerns as explained in the next very important section.</p>
<h3 id="heading-important-the-one-thing-to-take-away-from-this-post">IMPORTANT: The ONE thing to take away from this post</h3>
<p>A key point here, is that the DB has not been called at this point. Nothing happened really, the full program does not produce anything.</p>
<pre><code class="lang-scala"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">loadUserIdByName</span></span>(userName: <span class="hljs-type">String</span>): <span class="hljs-type">Future</span>[<span class="hljs-type">Long</span>] = ???
<span class="hljs-type">Stream</span>(<span class="hljs-string">"bob"</span>, <span class="hljs-string">"alice"</span>, <span class="hljs-string">"joe"</span>).evalMap(loadUserIdByName).compile
</code></pre>
<h3 id="heading-separating-program-description-from-evaluation">Separating program description from evaluation</h3>
<p>Yes it might be surprising but the major theme in FP is separating the</p>
<ul>
<li><strong>Description</strong> of your program: a good example is the program we just wrote, it’s a pure description of the problem “I give you names and a DB, give me back IDs”</li>
</ul>
<p>And the</p>
<ul>
<li><strong>Execution</strong> of your program: running the actual code and asking it to go to the DB</li>
</ul>
<p>One more time our program has literally no <em>effect</em> on the world besides making your computer warm, exactly like our <code>Pure</code> stream.</p>
<p>Code that does not have an effect is called <strong>pure</strong> and that’s what all Functional Programming is about: writing programs with functions that are <strong>pure.</strong> Bravo, you now know what FP is all about.</p>
<p>Why would you want write code this way? Simple: to achieve separation of concerns between the IO parts and the rest of our code.</p>
<p>Now let’s fix our program and take care of this <code>Future</code> problem.</p>
<p>As we said <code>Future</code> is a bad <code>Effect</code> type, it goes against the separation of concerns principle. Indeed, <code>Future</code> is eager in Scala: the moment you create one it starts to executes on some thread, you don't have control of the execution and thus it breaks. FS2 is well aware of that and does not let you compile. To fix this we have to use a type called <code>IO</code> that wraps our bad <code>Future</code>.</p>
<p>That brings us to the last part, what is this <code>IO</code> type? and how do I finally get my list of <code>usedIds</code> back?</p>
<pre><code class="lang-scala">scala&gt; <span class="hljs-keyword">import</span> cats.effect.<span class="hljs-type">IO</span>
<span class="hljs-keyword">import</span> cats.effect.<span class="hljs-type">IO</span>
scala&gt; <span class="hljs-type">Stream</span>(<span class="hljs-string">"bob"</span>, <span class="hljs-string">"alice"</span>, <span class="hljs-string">"joe"</span>).evalMap(name =&gt; <span class="hljs-type">IO</span>.fromFuture(<span class="hljs-type">IO</span>(loadUserIdByName(name)))).compile.toList
res8: cats.effect.<span class="hljs-type">IO</span>[<span class="hljs-type">List</span>[<span class="hljs-type">Long</span>]] = <span class="hljs-type">IO</span>$<span class="hljs-number">2104439279</span>
</code></pre>
<p>It now gives us back a <code>List</code> but still, we didn't get our IDs back, so one last thing must be missing.</p>
<p><img src="https://cdn-media-1.freecodecamp.org/images/ivbIHjwyNlEmckvLT6thLcPRcVRUkRGgw-dH" alt="Image" width="738" height="388" loading="lazy"></p>
<h3 id="heading-what-does-io-really-mean">What does <code>IO</code> really mean?</h3>
<p><code>IO</code> comes from <a target="_blank" href="https://typelevel.org/cats-effect/datatypes/io.html">cats-effect library</a>. First let's finish our program and finally get out the ids back from the DB.</p>
<pre><code class="lang-scala">scala&gt; userIds.compile.toList.unsafeRunSync
&lt;console&gt;:<span class="hljs-number">18</span>: error: not found: value userIds
       userIds.compile.toList.unsafeRunSync
       ^
</code></pre>
<p>The proof that it’s doing something is the fact that it’s failing.</p>
<pre><code>loadUserIdByName(userName: <span class="hljs-built_in">String</span>): Future[Long] = ???
</code></pre><p>When <code>???</code> is called you will get this exception, it means the function was executed (as opposed to before when we made the point that nothing was really happening). When we implement this function it will go to the DB and load the ids, and it will have an <strong>effect</strong> on the world (network/files system).</p>
<p><code>IO[Long]</code> is a <strong>description</strong> of <strong>how</strong> to get a value of type <code>Long</code> and it most certainly involves doing some I/O i.e going to the network, loading a file,...</p>
<p>It’s the <strong>How</strong> and not the <strong>What.</strong> It describes how to get the value from the network. If you want to execute this description, you can use <code>unsafeRunSync</code> (or other functions prefixed <code>unsafe</code>). You can guess why they are called this way: indeed a call to a DB is inherently unsafe as it could fail if, for example, your Internet connection is out.</p>
<h3 id="heading-recap">Recap</h3>
<p>Let’s take a last look at <code>**Stream[Effect,Output]**</code><strong>.</strong></p>
<p><code>Output</code> is the type that the stream emits (could be a stream of <code>String</code>, <code>Long</code> or whatever type you defined).</p>
<p><code>Effect</code> is the way (the recipe) to produce the <code>Output</code> (i.e go to the DB and give me an <code>id</code> of type <code>Long</code>).</p>
<p>It’s important to understand that if these types are separated to make things easier, breaking down a problem in subproblems allows you to reason about the subproblems independently. You can then solve them and combine their solutions.</p>
<p>The link between these 2 types is the following :</p>
<p>In order for the <code>Stream</code> to emit an element of type</p>
<ul>
<li><code>Output</code></li>
</ul>
<p>It needs to evaluate a type</p>
<ul>
<li><code>Effect</code></li>
</ul>
<p>A special type that encodes an effective action as a value of type <code>IO</code>, this <code>IO</code> value allows the separation of 2 concerns:</p>
<ul>
<li><strong>Description</strong>:<code>IO</code> is a simple immutable value, it’s a recipe to get a type <code>A</code> by doing some kind of IO(network/filesystem/…)</li>
<li><strong>Execution</strong>: in order for<code>IO</code> to do something, you need to <em>execute/run it</em> using <code>io.unsafeRunSync</code></li>
</ul>
<h4 id="heading-putting-it-all-together">Putting it all together</h4>
<p><code>Stream[IO,Long]</code> says:</p>
<p>This is a <code>Stream</code> that emits values of type <code>Long</code> and in order to do so, it needs to run an <em>effective</em> function that produces<code>IO[Long]</code> for each value.</p>
<p>That’s a lot of details packed in this very short type. The more details you get about how things happen the fewer errors you make.</p>
<h3 id="heading-takeaways">Takeaways</h3>
<ul>
<li><code>Stream</code> is a <strong>super charged</strong> version of <code>List</code></li>
<li><code>Stream(1,2,3)</code> is of type <code>Stream[Pure, Int]</code> , the second type <code>Int</code> is the type of all values that this stream will emit</li>
<li><code>Pure</code> means no <em>effect</em> on the world. It just makes your CPU work and consumes some power, but besides that it does not affect the world around you.</li>
<li>Use <code>evalMap</code> instead of <code>map</code> when you want to apply a function that has an effect like <code>loadUserIdByName</code> to a <code>Stream</code>.</li>
<li><code>Stream[IO, Long]</code> separates the concerns of What and How by letting you work only with the values and not worrying about how to get them (loading from the db).</li>
<li>Separating program description from evaluation is a key aspect of FP.</li>
<li>All the programs you write with <code>Stream</code> will do nothing until you use <code>unsafeRunSync</code>. Before that your code is effectively <em>pure.</em></li>
<li><code>IO[Long]</code> is an effect type that tells you: you will get <code>Long</code> values from IO (could be a file, the network, the console ...). It's a description and not a wrapper!r</li>
<li><code>Future</code> does not abide by this philosophy and thus is not compatible with FS2, you have to use <code>IO</code> type instead.</li>
</ul>
<h3 id="heading-fs2-videos">FS2 videos</h3>
<ul>
<li>Hands on screencast by Michael Pilquist: <a target="_blank" href="https://www.youtube.com/watch?v=B1wb4fIdtn4">https://www.youtube.com/watch?v=B1wb4fIdtn4</a></li>
<li>Talk by Fabio Labella <a target="_blank" href="https://www.youtube.com/watch?v=x3GLwl1FxcA">https://www.youtube.com/watch?v=x3GLwl1FxcA</a></li>
</ul>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ How to build a simple actor-based blockchain ]]>
                </title>
                <description>
                    <![CDATA[ By Luca Florio Scalachain is a blockchain built using the Scala programming language and the actor model (Akka Framework). In this story I will show the development process to build this simple prototype of a blockchain. This means that the project i... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/how-to-build-a-simple-actor-based-blockchain-aac1e996c177/</link>
                <guid isPermaLink="false">66d45e428812486a37369ca3</guid>
                
                    <category>
                        <![CDATA[ Blockchain ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Functional Programming ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Scala ]]>
                    </category>
                
                    <category>
                        <![CDATA[ tech  ]]>
                    </category>
                
                    <category>
                        <![CDATA[ technology ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ freeCodeCamp ]]>
                </dc:creator>
                <pubDate>Thu, 15 Nov 2018 20:32:40 +0000</pubDate>
                <media:content url="https://cdn-media-1.freecodecamp.org/images/1*qrVfVF1nIjVO7ZvGs6q4UQ.jpeg" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>By Luca Florio</p>
<p>Scalachain is a blockchain built using the Scala programming language and the actor model (<a target="_blank" href="https://akka.io/">Akka Framework</a>).</p>
<p>In this story I will show the development process to build this simple prototype of a blockchain. This means that the project is not perfect, and there may be better implementations. For all these reasons any contribution — may it be a suggestion, or a PR on the GitHub <a target="_blank" href="https://github.com/elleFlorio/scalachain">repository</a> — is very welcome! :-)</p>
<p>Let’s start with a little introduction to the blockchain. After that we can define the simplified model that we will implement.</p>
<h4 id="heading-quick-introduction-to-the-blockchain"><strong>Quick Introduction to the blockchain</strong></h4>
<p>There a lot of good articles that explain how a blockchain works, so I will do a high level introduction just to provide some context to this project.</p>
<p>The blockchain is a <strong>distributed ledger</strong>: it registers some transaction of values (like coins) between a sender and a receiver. What makes a blockchain different from a traditional database is the decentralized nature of the blockchain: it is distributed among several communicating nodes that guarantee the validity of the transactions registered.</p>
<p>The blockchain stores transactions in blocks, that are created —we say <strong>mined</strong> — by nodes investing computational power. Every block is created by solving a cryptographic puzzle that is hard to solve, but easy to verify. In this way, every block represents the work needed to solve such puzzle. This is the reason why the cryptographic puzzle is called the <strong>Proof of Work</strong>: the solution of the puzzle is the proof that a node spent a certain amount of work to solve it and mine the block.</p>
<p>Why do nodes invest computational power to mine a block? Because the creation of a new block is rewarded by a predefined amount of coins. In this way nodes are encouraged to mine new blocks, contributing in the growth and strength of the blockchain.</p>
<p><img src="https://cdn-media-1.freecodecamp.org/images/1*IV05CUizMrIQCIXjqjaRKA.png" alt="Image" width="631" height="121" loading="lazy">
<em>Simple blockchain</em></p>
<p>The solution of the Proof Of Work depends on the values stored in the last mined block. In this way every block is chained to the previous one. This means that, to change a mined block, a node should mine again all the blocks above the modified one. Since every block represents an amount of work, this operation would be unfeasible once several blocks are mined upon the modified one. This is the foundation of the <strong>distributed</strong> <strong>consensus</strong>, The agreement of all the nodes on the validity of the blocks (that is the transactions) stored in the blockchain.</p>
<p>It may happen that different nodes mine a block at the same time, creating different “branches” from the same blockchain — this is called a <strong>fork</strong> in the blockchain. This situation is solved when a branch becomes longer than the others: the longest chain always wins, so the winning branch becomes the new blockchain.</p>
<h4 id="heading-the-blockchain-model"><strong>The blockchain model</strong></h4>
<p>Scalachain is based on a blockchain model that is a simplification of the Bitcoin one.</p>
<p>The main components of our blockchain model are the Transaction, the Chain, the Proof of Work (PoW) algorithm, and the Node. The transactions are stored inside the blocks of the chain, that are mined using the PoW. The node is the server that runs the blockchain.</p>
<p><img src="https://cdn-media-1.freecodecamp.org/images/1*jh1BNavGEHnHm0AWi2O-gA.png" alt="Image" width="281" height="471" loading="lazy">
<em>Scalachain model</em></p>
<p><strong>Transaction</strong></p>
<p>Transactions register the movement of coins between two entities. Every transaction is composed by a sender, a recipient, and an amount of coin. Transactions will be registered inside the blocks of our blockchain.</p>
<p><strong>Chain</strong></p>
<p>The chain is a linked list of blocks containing a list of transactions. Every block of the chain has an index, the proof that validates it (more on this later), the list of transactions, the hash of the previous block, the list of previous blocks, and a timestamp. Every block is chained to the previous one by its hash, that is computed converting the block to a <code>JSON</code> string and then hashing it through a <code>SHA-256</code> hashing function.</p>
<p><strong>PoW</strong></p>
<p>The PoW algorithm is required to mine the blocks composing the blockchain. The idea is to solve a cryptographic puzzle that is hard to solve, but easy to verify having the proof. The PoW algorithm that is implemented in Scalachain is similar to the Bitcoin one (based on <a target="_blank" href="https://en.wikipedia.org/wiki/Hashcash">Hashcash</a>). It consists in finding a hash with N leading zeros, that is computed starting from the hash of the last block and a number, that is the proof of our algorithm.</p>
<p>We can formalize it as:</p>
<pre><code>NzerosHash = SHA<span class="hljs-number">-256</span>(previousNodeHash + proof)
</code></pre><p>The higher is N, the harder is to find the proof. In Scalachain N=4 (It will be configurable eventually).</p>
<p><strong>Node</strong></p>
<p>The Node is the server running our blockchain. It provides some REST API to interact with it and perform basic operations such as send a new transaction, get the list of pending transactions, mine a block, and get the current status of the blockchain.</p>
<h4 id="heading-blockchain-implementation-in-scala">Blockchain implementation in Scala</h4>
<p>We are going to implement the defined model using the Scala Programming Language. From an high level view, the things we need to implement a blockchain are:</p>
<ul>
<li>transactions</li>
<li>the chain of blocks containing lists of transactions</li>
<li>the PoW algorithm to mine new blocks</li>
</ul>
<p>These components are the essential parts of a blockchain.</p>
<p><strong>Transaction</strong></p>
<p>The transaction is a very simple object: it has a sender, a recipient and a value. We can implement it as a simple <code>case class</code>.</p>
<pre><code class="lang-scala"><span class="hljs-keyword">case</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Transaction</span>(<span class="hljs-params">sender: <span class="hljs-type">String</span>, recipient: <span class="hljs-type">String</span>, value: <span class="hljs-type">Long</span></span>)</span>
</code></pre>
<p><strong>Chain</strong></p>
<p>The chain is the core of our blockchain: it is a linked list of blocks containing transactions.</p>
<pre><code class="lang-scala">
<span class="hljs-keyword">sealed</span> <span class="hljs-class"><span class="hljs-keyword">trait</span> <span class="hljs-title">Chain</span> </span>{

  <span class="hljs-keyword">val</span> index: <span class="hljs-type">Int</span>
  <span class="hljs-keyword">val</span> hash: <span class="hljs-type">String</span>
  <span class="hljs-keyword">val</span> values: <span class="hljs-type">List</span>[<span class="hljs-type">Transaction</span>]
  <span class="hljs-keyword">val</span> proof: <span class="hljs-type">Long</span>
  <span class="hljs-keyword">val</span> timestamp: <span class="hljs-type">Long</span>
}
</code></pre>
<p>We start by creating a <code>sealed trait</code> that represents the block of our chain. The <code>Chain</code> can have two types: it can be an <code>EmptyChain</code> or a <code>ChainLink</code>. The former is our block zero (the <em>genesis block</em>), and it is implemented as a singleton (it is a <code>case object</code>), while the latter is a regular mined block.</p>
<pre><code class="lang-scala"><span class="hljs-keyword">case</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">ChainLink</span>(<span class="hljs-params">index: <span class="hljs-type">Int</span>, proof: <span class="hljs-type">Long</span>, values: <span class="hljs-type">List</span>[<span class="hljs-type">Transaction</span>], previousHash: <span class="hljs-type">String</span> = "", tail: <span class="hljs-type">Chain</span> = <span class="hljs-type">EmptyChain</span>, timestamp: <span class="hljs-type">Long</span> = <span class="hljs-type">System</span>.currentTimeMillis(</span>)) <span class="hljs-keyword">extends</span> <span class="hljs-title">Chain</span> </span>{
  <span class="hljs-keyword">val</span> hash = <span class="hljs-type">Crypto</span>.sha256Hash(<span class="hljs-keyword">this</span>.toJson.toString)
}

<span class="hljs-keyword">case</span> <span class="hljs-class"><span class="hljs-keyword">object</span> <span class="hljs-title">EmptyChain</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Chain</span> </span>{
  <span class="hljs-keyword">val</span> index = <span class="hljs-number">0</span>
  <span class="hljs-keyword">val</span> hash = <span class="hljs-string">"1"</span>
  <span class="hljs-keyword">val</span> values = <span class="hljs-type">Nil</span>
  <span class="hljs-keyword">val</span> proof = <span class="hljs-number">100</span>L
  <span class="hljs-keyword">val</span> timestamp = <span class="hljs-type">System</span>.currentTimeMillis()
}
</code></pre>
<p>Let’s look more in detail at our chain. It provides an index, that is the current height of the blockchain. There is the list of <code>Transaction</code>, the proof that validated the block, and the timestamp of the block creation. The hash value is set to a default one in the <code>EmptyChain</code>, while in the <code>ChainLink</code> it is computed converting the object to its <code>JSON</code> representation and hashing it with an utility function (see the <code>crypto</code> package in the <a target="_blank" href="https://github.com/elleFlorio/scalachain">repository</a>). The <code>ChainLink</code> provides also the hash of the previous block in the chain (our link between blocks). The tail field is a reference to the previously mined blocks. This may not be the most efficient solution, but it is useful to see how the blockchain grows in our simplified implementation.</p>
<p>We can improve our <code>Chain</code> with some utilities. We can add it a <em>companion object</em> that defines an <code>apply</code> method to create a new chain passing it a list of blocks. A companion object is like a “set of static methods” — doing an analogy with Java — that has complete access rights on the fields and methods of the class/trait.</p>
<pre><code class="lang-scala"><span class="hljs-class"><span class="hljs-keyword">object</span> <span class="hljs-title">Chain</span> </span>{
  <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">apply</span></span>[<span class="hljs-type">T</span>](b: <span class="hljs-type">Chain</span>*): <span class="hljs-type">Chain</span> = {
    <span class="hljs-keyword">if</span> (b.isEmpty) <span class="hljs-type">EmptyChain</span>
    <span class="hljs-keyword">else</span> {
      <span class="hljs-keyword">val</span> link = b.head.asInstanceOf[<span class="hljs-type">ChainLink</span>]
      <span class="hljs-type">ChainLink</span>(link.index, link.proof, link.values, link.previousHash, apply(b.tail: _*))
    }
  }
}
</code></pre>
<p>If the list of blocks is empty, we simply initialize our blockchain with an <code>EmptyChain</code>. Otherwise we create a new <code>ChainLink</code> adding as a tail the result of the apply method on the remaining blocks of the list. In this way the list of blocks is added following the order of the list.</p>
<p>It would be nice to have the possibility to add a new block to our chain using a simple addition operator, like the one we have on <code>List</code>. We can define our own addition operator <code>::</code> inside the <code>Chain</code> trait.</p>
<pre><code class="lang-scala"><span class="hljs-keyword">sealed</span> <span class="hljs-class"><span class="hljs-keyword">trait</span> <span class="hljs-title">Chain</span> </span>{

  <span class="hljs-keyword">val</span> index: <span class="hljs-type">Int</span>
  <span class="hljs-keyword">val</span> hash: <span class="hljs-type">String</span>
  <span class="hljs-keyword">val</span> values: <span class="hljs-type">List</span>[<span class="hljs-type">Transaction</span>]
  <span class="hljs-keyword">val</span> proof: <span class="hljs-type">Long</span>
  <span class="hljs-keyword">val</span> timestamp: <span class="hljs-type">Long</span>

  <span class="hljs-function"><span class="hljs-keyword">def</span> </span>::(link: <span class="hljs-type">Chain</span>): <span class="hljs-type">Chain</span> = link <span class="hljs-keyword">match</span> {
    <span class="hljs-keyword">case</span> l:<span class="hljs-type">ChainLink</span> =&gt; <span class="hljs-type">ChainLink</span>(l.index, l.proof, l.values, <span class="hljs-keyword">this</span>.hash, <span class="hljs-keyword">this</span>)
    <span class="hljs-keyword">case</span> _ =&gt; <span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> <span class="hljs-type">InvalidParameterException</span>(<span class="hljs-string">"Cannot add invalid link to chain"</span>)
  }
}
</code></pre>
<p>We pattern match on the block that is passed as an argument: if it is a valid <code>ChainLink</code> object we add it as the head of our chain, putting the chain as the tail of the new block, otherwise we throw an exception.</p>
<p><strong>PoW</strong></p>
<p>The PoW algorithm is fundamental for the mining of new blocks. We implement it as a simple algorithm:</p>
<ol>
<li><p>Take the hash of the last block and a number representing the proof.</p>
</li>
<li><p>Concatenate the hash and the proof in a string.</p>
</li>
<li><p>hash the resulting string using the <code>SHA-256</code> algorithm.</p>
</li>
<li><p>check the 4 leading characters of the hash: if they are four zeros return the proof.</p>
</li>
<li><p>otherwise repeat the algorithm increasing the proof by one.</p>
</li>
</ol>
<p>This a simplification of the <a target="_blank" href="https://en.wikipedia.org/wiki/Hashcash">HashCash</a> algorithm used in the Bitcoin blockchain.</p>
<p>Since it is a recursive function, we can implement it as a tail recursive one to improve the usage of resources.</p>
<pre><code class="lang-scala"><span class="hljs-class"><span class="hljs-keyword">object</span> <span class="hljs-title">ProofOfWork</span> </span>{

  <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">proofOfWork</span></span>(lastHash: <span class="hljs-type">String</span>): <span class="hljs-type">Long</span> = {
    <span class="hljs-meta">@tailrec</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">powHelper</span></span>(lastHash: <span class="hljs-type">String</span>, proof: <span class="hljs-type">Long</span>): <span class="hljs-type">Long</span> = {
      <span class="hljs-keyword">if</span> (validProof(lastHash, proof))
        proof
      <span class="hljs-keyword">else</span>
        powHelper(lastHash, proof + <span class="hljs-number">1</span>)
    }

    <span class="hljs-keyword">val</span> proof = <span class="hljs-number">0</span>
    powHelper(lastHash, proof)
  }

  <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">validProof</span></span>(lastHash: <span class="hljs-type">String</span>, proof: <span class="hljs-type">Long</span>): <span class="hljs-type">Boolean</span> = {
    <span class="hljs-keyword">val</span> guess = (lastHash ++ proof.toString).toJson.toString
    <span class="hljs-keyword">val</span> guessHash = <span class="hljs-type">Crypto</span>.sha256Hash(guess)
    (guessHash take <span class="hljs-number">4</span>) == <span class="hljs-string">"0000"</span>
  }
}
</code></pre>
<p>The <code>validProof</code> function is used to check if the proof we are testing is the correct one. The <code>powHelper</code> function is a helper function that executes our loop using tail recursion, increasing the proof at each step. The <code>proofOfWork</code> function wrap all the things up, and is exposed by the <code>ProofOfWork</code> object.</p>
<h4 id="heading-the-actor-model">The actor model</h4>
<p>The actor model is a programming model designed for <strong>concurrent processing</strong>, <strong>scalability</strong>, and <strong>fault tolerance</strong>. The model defines the atomic elements that compose the software systems — the <strong>actors</strong> — and the way this elements interact between them. In this project we will use the actor model implemented in Scala by the Akka Framework.</p>
<p><img src="https://cdn-media-1.freecodecamp.org/images/1*6jDNPwSi80yxej_swJ5kfg.png" alt="Image" width="652" height="368" loading="lazy">
<em>Actor Model</em></p>
<p><strong>Actor</strong></p>
<p>The actor is the atomic unit of the actor model. it is a computational unit that can send and receive messages. Every actor has an internal <strong>private</strong> state and a mailbox. When an actor receives and compute a message, it can react in 3 ways:</p>
<ul>
<li>Send a message to another actor.</li>
<li>Change its internal state.</li>
<li>Create another actor.</li>
</ul>
<p>Communication is <strong>asynchronous</strong>, and messages are popped out from the mailbox and processed in series. To enable the parallel computation of messages you need to create several actors. Many actors together crate an <strong>actor system</strong>. The behavior of the application arises from the interaction between actors providing different functionalities.</p>
<p><strong>Actors are independent</strong></p>
<p>Actors are independent one to another, and they do not share their internal state. This fact has a couple of important consequences:</p>
<ol>
<li><p>Actors can process messages <strong>without side-effects</strong> one to another.</p>
</li>
<li><p>It’s not important where an actor is — be it your laptop, a sever, or in the cloud — once we know its address we can request its services sending it a message.</p>
</li>
</ol>
<p>The first point makes concurrent computation very easy. We can be sure that the processing of a message will not interfere with the processing of another one. To achieve <strong>concurrent processing</strong> we can deploy several actors able to process the same kind of message.</p>
<p>The second point is all about <strong>scalability</strong>: we need more computational power? No problem: we can start a new machine and deploy new actors that will join the existing actor system. Their mailbox addresses will be discoverable by existing actors, that will start communicate with them.</p>
<p><strong>Actors are supervised</strong></p>
<p>As we said in the description of the actor, one of the possible reaction to a message is the creation of other actors. When this happens, the father becomes the <em>supervisor</em> of its children. If a children fails, the supervisor can decide the action to take, may it be create a new actor, ignore the failure, or throw it up to its own supervisor. In this way the Actor System becomes a hierarchy tree, each node supervising its children. This is the way the actor model provides <strong>fault tolerance</strong>.</p>
<p><img src="https://cdn-media-1.freecodecamp.org/images/1*GMV9z5B_00Kwjx8b2Kolxg.png" alt="Image" width="671" height="281" loading="lazy">
<em>Actor hierarchy</em></p>
<h4 id="heading-broker-a-simple-actor">Broker, a simple actor</h4>
<p>The first actor we are going to implement is the Broker Actor: it is the manager of the transactions of our blockchain. Its responsibilities are the addition of new transactions, and the retrieval of pending ones.</p>
<p>The Broker Actor reacts to three kind of messages, defined in the <code>companion object</code> of the Broker class:</p>
<pre><code class="lang-scala">
<span class="hljs-class"><span class="hljs-keyword">object</span> <span class="hljs-title">Broker</span> </span>{
  <span class="hljs-keyword">sealed</span> <span class="hljs-class"><span class="hljs-keyword">trait</span> <span class="hljs-title">BrokerMessage</span></span>
  <span class="hljs-keyword">case</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">AddTransaction</span>(<span class="hljs-params">transaction: <span class="hljs-type">Transaction</span></span>) <span class="hljs-keyword">extends</span> <span class="hljs-title">BrokerMessage</span></span>
  <span class="hljs-keyword">case</span> <span class="hljs-class"><span class="hljs-keyword">object</span> <span class="hljs-title">GetTransactions</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">BrokerMessage</span></span>
  <span class="hljs-keyword">case</span> <span class="hljs-class"><span class="hljs-keyword">object</span> <span class="hljs-title">Clear</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">BrokerMessage</span></span>

  <span class="hljs-keyword">val</span> props: <span class="hljs-type">Props</span> = <span class="hljs-type">Props</span>(<span class="hljs-keyword">new</span> <span class="hljs-type">Broker</span>)
}
</code></pre>
<p>We create a trait <code>BrokerMessage</code> to identify the messages of the Broker Actor. Every other message will extend this trait. <code>AddTransaction</code> adds a new transaction to the list of pending ones. <code>GetTransaction</code> retrieve the pending transactions, and <code>Clear</code> empties the list. The <code>props</code> value is used to initialize the actor when it will be created.</p>
<pre><code class="lang-scala"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Broker</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Actor</span> <span class="hljs-keyword">with</span> <span class="hljs-title">ActorLogging</span> </span>{
  <span class="hljs-keyword">import</span> <span class="hljs-type">Broker</span>._

  <span class="hljs-keyword">var</span> pending: <span class="hljs-type">List</span>[<span class="hljs-type">Transaction</span>] = <span class="hljs-type">List</span>()

  <span class="hljs-keyword">override</span> <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">receive</span></span>: <span class="hljs-type">Receive</span> = {
    <span class="hljs-keyword">case</span> <span class="hljs-type">AddTransaction</span>(transaction) =&gt; {
      pending = transaction :: pending
      log.info(<span class="hljs-string">s"Added <span class="hljs-subst">$transaction</span> to pending Transaction"</span>)
    }
    <span class="hljs-keyword">case</span> <span class="hljs-type">GetTransactions</span> =&gt; {
      log.info(<span class="hljs-string">s"Getting pending transactions"</span>)
      sender() ! pending
    }
    <span class="hljs-keyword">case</span> <span class="hljs-type">Clear</span> =&gt; {
      pending = <span class="hljs-type">List</span>()
      log.info(<span class="hljs-string">"Clear pending transaction List"</span>)
    }
  }
}
</code></pre>
<p>The Broker <code>class</code> contains the business logic to react to the different messages. I won’t go into the details because it is trivial. The most interesting thing is how we respond to a request of the pending transactions. We send them to the <code>sender()</code> of the <code>GetTransaction</code> message using the <code>tell</code> (<code>!</code>) operator. This operator means “send the message and don’t wait for a response” — aka fire-and-forget.</p>
<h4 id="heading-miner-an-actor-with-different-states">Miner, an actor with different states</h4>
<p>The Miner Actor is the one mining new blocks for our blockchain. Since we don’t want mine a new block while we are mining another one, the Miner Actor will have two states: <code>ready</code>, when it is ready to mine a new block, and <code>busy</code>, when it is mining a block.</p>
<p>Let’s start by defining the <code>companion object</code> with the messages of the Miner Actor. The pattern is the same, with a sealed trait — <code>MinerMessage</code> — used to define the kind of messages this actor reacts to.</p>
<pre><code class="lang-scala"><span class="hljs-class"><span class="hljs-keyword">object</span> <span class="hljs-title">Miner</span> </span>{
  <span class="hljs-keyword">sealed</span> <span class="hljs-class"><span class="hljs-keyword">trait</span> <span class="hljs-title">MinerMessage</span></span>
  <span class="hljs-keyword">case</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Validate</span>(<span class="hljs-params">hash: <span class="hljs-type">String</span>, proof: <span class="hljs-type">Long</span></span>) <span class="hljs-keyword">extends</span> <span class="hljs-title">MinerMessage</span></span>
  <span class="hljs-keyword">case</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Mine</span>(<span class="hljs-params">hash: <span class="hljs-type">String</span></span>) <span class="hljs-keyword">extends</span> <span class="hljs-title">MinerMessage</span></span>
  <span class="hljs-keyword">case</span> <span class="hljs-class"><span class="hljs-keyword">object</span> <span class="hljs-title">Ready</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">MinerMessage</span></span>

  <span class="hljs-keyword">val</span> props: <span class="hljs-type">Props</span> = <span class="hljs-type">Props</span>(<span class="hljs-keyword">new</span> <span class="hljs-type">Miner</span>)
}
</code></pre>
<p>The <code>Validate</code> message asks for a validation of a proof, and pass to the Miner the hash and the proof to check. Since this component is the one interacting with the PoW algorithm, it is its duty to execute this check. The <code>Mine</code> message asks for the mining starting from a specified hash. The last message, <code>Ready</code>, triggers a state transition.</p>
<p><strong>Same actor, different states</strong></p>
<p>The peculiarity of this actor is that it reacts to the messages according to its state: <code>busy</code> or <code>ready</code>. Let’s analyze the difference in the behavior:</p>
<ul>
<li><strong>busy</strong>: the Miner is busy mining a block. If a new mining request comes, it should deny it. If it is requested to be ready, the Miner should change its state to the ready one.</li>
<li><strong>ready</strong>: the Miner is idle. If a mining request come, it should start mining a new block. If it is requested to be ready, it should say: “OK, I’m ready!”</li>
<li><strong>both</strong>: the Miner should be always available to verify the correctness of a proof, both in a ready or busy state.</li>
</ul>
<p>Time so see how we can implement this logic in our code. We start by defining the common behavior, the validation of a proof.</p>
<p>We define a function <code>validate</code> that reacts to the <code>Validate</code> message: if the proof is valid we respond to the sender with a success, otherwise with a failure. The <code>ready</code> and the <code>busy</code> states are defined as functions that “extends” the <code>validate</code> one, since that is a behavior we want in both states.</p>
<pre><code class="lang-scala"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">validate</span></span>: <span class="hljs-type">Receive</span> = {
    <span class="hljs-keyword">case</span> <span class="hljs-type">Validate</span>(hash, proof) =&gt; {
      log.info(<span class="hljs-string">s"Validating proof <span class="hljs-subst">$proof</span>"</span>)
      <span class="hljs-keyword">if</span> (<span class="hljs-type">ProofOfWork</span>.validProof(hash, proof)){
        log.info(<span class="hljs-string">"proof is valid!"</span>)
        sender() ! <span class="hljs-type">Success</span>
      }
      <span class="hljs-keyword">else</span>{
        log.info(<span class="hljs-string">"proof is not valid"</span>)
        sender() ! <span class="hljs-type">Failure</span>(<span class="hljs-keyword">new</span> <span class="hljs-type">InvalidProofException</span>(hash, proof))
      }
    }
  }
</code></pre>
<p>A couple of things to highlight here.</p>
<ol>
<li><p>The state transition is triggered using the <code>become</code> function, provided by the Akka Framework. This takes as an argument a function that returns a <code>Receive</code> object, like the ones we defined for the <code>validation</code>, <code>busy</code>, and <code>ready</code> state.</p>
</li>
<li><p>When a mining request is received by the Miner, it responds with a <code>Future</code> containing the execution of the PoW algorithm. In this way we can work asynchronously, making the Miner free to do other tasks, such as the validation one.</p>
</li>
<li><p>The <strong>supervisor</strong> of this Actor controls the state transition. The reason of this choice is that the Miner is agnostic about the state of the system. It doesn’t know when the mining computation in the <code>Future</code> will be completed, and it can’t know if the block that it is mining has been already mined from another node. This would require to stop mining the current hash, and start mining the hash of the new block.</p>
</li>
</ol>
<p>The last thing is to provide an initial state overriding the <code>receive</code> function.</p>
<pre><code class="lang-scala"><span class="hljs-keyword">override</span> <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">receive</span></span>: <span class="hljs-type">Receive</span> = {
    <span class="hljs-keyword">case</span> <span class="hljs-type">Ready</span> =&gt; become(ready)
  }
</code></pre>
<p>We start waiting for a <code>Ready</code> message. When it comes, we start our Miner.</p>
<h4 id="heading-blockchain-a-persistent-actor">Blockchain, a persistent actor</h4>
<p>The Blockchain Actor interacts with the business logic of the blockchain. It can add a new block to the blockchain, and it can retrieve information about the state of the blockchain. This actor has another superpower: it can <strong>persist</strong> and recover the state of the blockchain. This is possible implementing the <code>PersistentActor</code> trait provided by the Akka Framework.</p>
<pre><code class="lang-scala"><span class="hljs-class"><span class="hljs-keyword">object</span> <span class="hljs-title">Blockchain</span> </span>{
  <span class="hljs-keyword">sealed</span> <span class="hljs-class"><span class="hljs-keyword">trait</span> <span class="hljs-title">BlockchainEvent</span></span>
  <span class="hljs-keyword">case</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">AddBlockEvent</span>(<span class="hljs-params">transactions: <span class="hljs-type">List</span>[<span class="hljs-type">Transaction</span>], proof: <span class="hljs-type">Long</span></span>) <span class="hljs-keyword">extends</span> <span class="hljs-title">BlockchainEvent</span></span>

  <span class="hljs-keyword">sealed</span> <span class="hljs-class"><span class="hljs-keyword">trait</span> <span class="hljs-title">BlockchainCommand</span></span>
  <span class="hljs-keyword">case</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">AddBlockCommand</span>(<span class="hljs-params">transactions: <span class="hljs-type">List</span>[<span class="hljs-type">Transaction</span>], proof: <span class="hljs-type">Long</span></span>) <span class="hljs-keyword">extends</span> <span class="hljs-title">BlockchainCommand</span></span>
  <span class="hljs-keyword">case</span> <span class="hljs-class"><span class="hljs-keyword">object</span> <span class="hljs-title">GetChain</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">BlockchainCommand</span></span>
  <span class="hljs-keyword">case</span> <span class="hljs-class"><span class="hljs-keyword">object</span> <span class="hljs-title">GetLastHash</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">BlockchainCommand</span></span>
  <span class="hljs-keyword">case</span> <span class="hljs-class"><span class="hljs-keyword">object</span> <span class="hljs-title">GetLastIndex</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">BlockchainCommand</span></span>

  <span class="hljs-keyword">case</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">State</span>(<span class="hljs-params">chain: <span class="hljs-type">Chain</span></span>)</span>

  <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">props</span></span>(chain: <span class="hljs-type">Chain</span>, nodeId: <span class="hljs-type">String</span>): <span class="hljs-type">Props</span> = <span class="hljs-type">Props</span>(<span class="hljs-keyword">new</span> <span class="hljs-type">Blockchain</span>(chain, nodeId))
}
view raw
</code></pre>
<p>We can see that the <code>companion object</code> of this actor has more elements than the other ones. The <code>State</code> class is where we store the state of our blockchain, that is its <code>Chain</code>. The idea is to update the state every time a new block is created.</p>
<p>For this purpose, there are two different traits: <code>BlockchainEvent</code> and <code>BlockchainCommand</code>. The former is to handle the events that will trigger the persistence logic, the latter is used to send direct commands to the actor. The <code>AddBlockEvent</code> message is the event that will update our state. The <code>AddBlockCommand</code>, <code>GetChain</code>, <code>GetLastHash</code>, and <code>LastIndex</code> commands are the one used to interact with the underlying blockchain.</p>
<p>The usual <code>props</code> function initializes the Blockchain Actor with the initial <code>Chain</code> and the <code>nodeId</code> of the Scalachain node.</p>
<pre><code class="lang-scala"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Blockchain</span>(<span class="hljs-params">chain: <span class="hljs-type">Chain</span>, nodeId: <span class="hljs-type">String</span></span>) <span class="hljs-keyword">extends</span> <span class="hljs-title">PersistentActor</span> <span class="hljs-keyword">with</span> <span class="hljs-title">ActorLogging</span></span>{
  <span class="hljs-keyword">import</span> <span class="hljs-type">Blockchain</span>._

  <span class="hljs-keyword">var</span> state = <span class="hljs-type">State</span>(chain)

  <span class="hljs-keyword">override</span> <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">persistenceId</span></span>: <span class="hljs-type">String</span> = <span class="hljs-string">s"chainer-<span class="hljs-subst">$nodeId</span>"</span>

  <span class="hljs-comment">//Code...</span>
}
</code></pre>
<p>The Blockchain Actor extends the trait <code>PersistentActor</code> provided by the Akka framework. In this way we have out-of-the-box all the logic required to persist and recover our state.</p>
<p>We initialize the state using the <code>Chain</code> provided as an argument upon creation. The <code>nodeId</code> is part of the <code>persistenceId</code> that we override. The persistence logic will use it to identify the persisted state. Since we can have multiple Scalachain nodes running in the same machine, we need this value to correctly persist and recover the state of each node.</p>
<pre><code class="lang-scala"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">updateState</span></span>(event: <span class="hljs-type">BlockchainEvent</span>) = event <span class="hljs-keyword">match</span> {
    <span class="hljs-keyword">case</span> <span class="hljs-type">AddBlockEvent</span>(transactions, proof) =&gt;
      {
        state = <span class="hljs-type">State</span>(<span class="hljs-type">ChainLink</span>(state.chain.index + <span class="hljs-number">1</span>, proof, transactions) :: state.chain)
        log.info(<span class="hljs-string">s"Added block <span class="hljs-subst">${state.chain.index}</span> containing <span class="hljs-subst">${transactions.size}</span> transactions"</span>)
      }
  }
</code></pre>
<p>The <code>updateState</code> function executes the update of the Actor state when the <code>AddBlockEvent</code> is received.</p>
<pre><code class="lang-scala"><span class="hljs-keyword">override</span> <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">receiveRecover</span></span>: <span class="hljs-type">Receive</span> = {
    <span class="hljs-keyword">case</span> <span class="hljs-type">SnapshotOffer</span>(metadata, snapshot: <span class="hljs-type">State</span>) =&gt; {
      log.info(<span class="hljs-string">s"Recovering from snapshot <span class="hljs-subst">${metadata.sequenceNr}</span> at block <span class="hljs-subst">${snapshot.chain.index}</span>"</span>)
      state = snapshot
    }
    <span class="hljs-keyword">case</span> <span class="hljs-type">RecoveryCompleted</span> =&gt; log.info(<span class="hljs-string">"Recovery completed"</span>)
    <span class="hljs-keyword">case</span> evt: <span class="hljs-type">AddBlockEvent</span> =&gt; updateState(evt)
  }
</code></pre>
<p>The <code>receiveRecover</code> function reacts to the recovery messages sent by the persistence logic. During the creation of an actor a persisted state (<strong>snapshot</strong>) may be offered to it using the <code>SnapshotOffer</code> message. In this case the current state becomes the one provided by the snapshot.</p>
<p><code>RecoveryCompleted</code> message informs us that the recovery process completed successfully. The <code>AddBlockEvent</code> triggers the <code>updateState</code> function passing the event itself.</p>
<pre><code class="lang-scala"><span class="hljs-keyword">override</span> <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">receiveCommand</span></span>: <span class="hljs-type">Receive</span> = {
    <span class="hljs-keyword">case</span> <span class="hljs-type">SaveSnapshotSuccess</span>(metadata) =&gt; log.info(<span class="hljs-string">s"Snapshot <span class="hljs-subst">${metadata.sequenceNr}</span> saved successfully"</span>)
    <span class="hljs-keyword">case</span> <span class="hljs-type">SaveSnapshotFailure</span>(metadata, reason) =&gt; log.error(<span class="hljs-string">s"Error saving snapshot <span class="hljs-subst">${metadata.sequenceNr}</span>: <span class="hljs-subst">${reason.getMessage}</span>"</span>)
    <span class="hljs-keyword">case</span> <span class="hljs-type">AddBlockCommand</span>(transactions : <span class="hljs-type">List</span>[<span class="hljs-type">Transaction</span>], proof: <span class="hljs-type">Long</span>) =&gt; {
      persist(<span class="hljs-type">AddBlockEvent</span>(transactions, proof)) {event =&gt;
        updateState(event)
      }

      <span class="hljs-comment">// This is a workaround to wait until the state is persisted</span>
      deferAsync(<span class="hljs-type">Nil</span>) { _ =&gt;
        saveSnapshot(state)
        sender() ! state.chain.index
      }
    }
    <span class="hljs-keyword">case</span> <span class="hljs-type">AddBlockCommand</span>(_, _) =&gt; log.error(<span class="hljs-string">"invalid add block command"</span>)
    <span class="hljs-keyword">case</span> <span class="hljs-type">GetChain</span> =&gt; sender() ! state.chain
    <span class="hljs-keyword">case</span> <span class="hljs-type">GetLastHash</span> =&gt; sender() ! state.chain.hash
    <span class="hljs-keyword">case</span> <span class="hljs-type">GetLastIndex</span> =&gt; sender() ! state.chain.index
  }
</code></pre>
<p>The <code>receiveCommand</code> function is used to react to the direct commands sent to the actor. Let’s skip the <code>GetChain</code>, <code>GetLastHash</code>, and <code>GetLastIndex</code> commands, since they are trivial. The <code>AddBlockCommand</code> is the interesting part: it creates and fires an <code>AddBlock</code> event, that is persisted in the event journal of the Actor. In this way events can be replayed in case of recovery.</p>
<p>The <code>deferAsync</code> function waits until the state is updated after the processing of the event. Once the event has been executed the actor can save the snapshot of the state, and inform the sender of the message with the updated last index of the <code>Chain</code>. The <code>SaveSnapshotSucces</code> and <code>SaveSnapshotFailure</code> messages helps us to keep track of possible failures.</p>
<h4 id="heading-node-an-actor-to-rule-them-all">Node, an actor to rule them all</h4>
<p>The Node Actor is the backbone of our Scalachain node. It is the <strong>supervisor</strong> of all the other actors (Broker, Miner, and Blockchain), and the one communicating with the outside world through the REST API.</p>
<pre><code class="lang-scala"><span class="hljs-class"><span class="hljs-keyword">object</span> <span class="hljs-title">Node</span> </span>{

  <span class="hljs-keyword">sealed</span> <span class="hljs-class"><span class="hljs-keyword">trait</span> <span class="hljs-title">NodeMessage</span></span>

  <span class="hljs-keyword">case</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">AddTransaction</span>(<span class="hljs-params">transaction: <span class="hljs-type">Transaction</span></span>) <span class="hljs-keyword">extends</span> <span class="hljs-title">NodeMessage</span></span>

  <span class="hljs-keyword">case</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">CheckPowSolution</span>(<span class="hljs-params">solution: <span class="hljs-type">Long</span></span>) <span class="hljs-keyword">extends</span> <span class="hljs-title">NodeMessage</span></span>

  <span class="hljs-keyword">case</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">AddBlock</span>(<span class="hljs-params">proof: <span class="hljs-type">Long</span></span>) <span class="hljs-keyword">extends</span> <span class="hljs-title">NodeMessage</span></span>

  <span class="hljs-keyword">case</span> <span class="hljs-class"><span class="hljs-keyword">object</span> <span class="hljs-title">GetTransactions</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">NodeMessage</span></span>

  <span class="hljs-keyword">case</span> <span class="hljs-class"><span class="hljs-keyword">object</span> <span class="hljs-title">Mine</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">NodeMessage</span></span>

  <span class="hljs-keyword">case</span> <span class="hljs-class"><span class="hljs-keyword">object</span> <span class="hljs-title">StopMining</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">NodeMessage</span></span>

  <span class="hljs-keyword">case</span> <span class="hljs-class"><span class="hljs-keyword">object</span> <span class="hljs-title">GetStatus</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">NodeMessage</span></span>

  <span class="hljs-keyword">case</span> <span class="hljs-class"><span class="hljs-keyword">object</span> <span class="hljs-title">GetLastBlockIndex</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">NodeMessage</span></span>

  <span class="hljs-keyword">case</span> <span class="hljs-class"><span class="hljs-keyword">object</span> <span class="hljs-title">GetLastBlockHash</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">NodeMessage</span></span>

  <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">props</span></span>(nodeId: <span class="hljs-type">String</span>): <span class="hljs-type">Props</span> = <span class="hljs-type">Props</span>(<span class="hljs-keyword">new</span> <span class="hljs-type">Node</span>(nodeId))

  <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">createCoinbaseTransaction</span></span>(nodeId: <span class="hljs-type">String</span>) = <span class="hljs-type">Transaction</span>(<span class="hljs-string">"coinbase"</span>, nodeId, <span class="hljs-number">100</span>)
}
</code></pre>
<p>The Node Actor has to handle all the high level messages that coming from the REST API. This is the reason why we find in the <code>companion object</code> more or less the same messages we implemented in the children actors. The props function takes a nodeId as an argument to create our Node Actor. This will be the one used for the initialization of Blockchain Actor. The <code>createCoinbaseTransaction</code> simply creates a transaction assigning a predefined coin amount to the node itself. This will be the <strong>reward</strong> for the successful mining of a new block of the blockchain.</p>
<pre><code class="lang-scala"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Node</span>(<span class="hljs-params">nodeId: <span class="hljs-type">String</span></span>) <span class="hljs-keyword">extends</span> <span class="hljs-title">Actor</span> <span class="hljs-keyword">with</span> <span class="hljs-title">ActorLogging</span> </span>{

  <span class="hljs-keyword">import</span> <span class="hljs-type">Node</span>._

  <span class="hljs-keyword">implicit</span> <span class="hljs-keyword">lazy</span> <span class="hljs-keyword">val</span> timeout = <span class="hljs-type">Timeout</span>(<span class="hljs-number">5.</span>seconds)

  <span class="hljs-keyword">val</span> broker = context.actorOf(<span class="hljs-type">Broker</span>.props)
  <span class="hljs-keyword">val</span> miner = context.actorOf(<span class="hljs-type">Miner</span>.props)
  <span class="hljs-keyword">val</span> blockchain = context.actorOf(<span class="hljs-type">Blockchain</span>.props(<span class="hljs-type">EmptyChain</span>, nodeId))

  miner ! <span class="hljs-type">Ready</span>

  <span class="hljs-comment">//Code...</span>
}
</code></pre>
<p>Let’s look at the initialization of the Node Actor. The timeout value is used by the <code>ask</code> (<code>?</code>) operator (this will be explained shortly). All our actors are created in the actor <code>context</code>, using the <code>props</code> function we defined in each actor.</p>
<p>The Blockchain Actor is initialized with the <code>EmptyChain</code> and the <code>nodeId</code> of the Node. Once everything is created, we inform the Miner Actor to be ready to mine sending it a <code>Ready</code> message. Ok, we are now ready to receive some message and react to it.</p>
<pre><code class="lang-scala"><span class="hljs-keyword">override</span> <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">receive</span></span>: <span class="hljs-type">Receive</span> = {
    <span class="hljs-keyword">case</span> <span class="hljs-type">AddTransaction</span>(transaction) =&gt; {
      <span class="hljs-comment">//Code...</span>
    }
    <span class="hljs-keyword">case</span> <span class="hljs-type">CheckPowSolution</span>(solution) =&gt; {
      <span class="hljs-comment">//Code...</span>
    }
    <span class="hljs-keyword">case</span> <span class="hljs-type">AddBlock</span>(proof) =&gt; {
      <span class="hljs-comment">//Code...</span>
    }
    <span class="hljs-keyword">case</span> <span class="hljs-type">Mine</span> =&gt; {
      <span class="hljs-comment">//Code...</span>
    }
    <span class="hljs-keyword">case</span> <span class="hljs-type">GetTransactions</span> =&gt; broker forward <span class="hljs-type">Broker</span>.<span class="hljs-type">GetTransactions</span>
    <span class="hljs-keyword">case</span> <span class="hljs-type">GetStatus</span> =&gt; blockchain forward <span class="hljs-type">GetChain</span>
    <span class="hljs-keyword">case</span> <span class="hljs-type">GetLastBlockIndex</span> =&gt; blockchain forward <span class="hljs-type">GetLastIndex</span>
    <span class="hljs-keyword">case</span> <span class="hljs-type">GetLastBlockHash</span> =&gt; blockchain forward <span class="hljs-type">GetLastHash</span>
  }
</code></pre>
<p>This is an overview of the usual <code>receive</code> function that we should override. I will analyze the logic of the most complex <code>case</code>s later, now let’s look at the last four. Here we forward the messages to the Blockchain Actor, since it isn’t required any processing. Using the <code>forward</code> operator the <code>sender()</code> of the message will be the one that originated the message, not the Node Actor. In this way the Blockchain Actor will respond to the original sender of the message (the REST API layer).</p>
<pre><code class="lang-scala"><span class="hljs-keyword">override</span> <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">receive</span></span>: <span class="hljs-type">Receive</span> = {
    <span class="hljs-keyword">case</span> <span class="hljs-type">AddTransaction</span>(transaction) =&gt; {
      <span class="hljs-keyword">val</span> node = sender()
      broker ! <span class="hljs-type">Broker</span>.<span class="hljs-type">AddTransaction</span>(transaction)
      (blockchain ? <span class="hljs-type">GetLastIndex</span>).mapTo[<span class="hljs-type">Int</span>] onComplete {
        <span class="hljs-keyword">case</span> <span class="hljs-type">Success</span>(index) =&gt; node ! (index + <span class="hljs-number">1</span>)
        <span class="hljs-keyword">case</span> <span class="hljs-type">Failure</span>(e) =&gt; node ! akka.actor.<span class="hljs-type">Status</span>.<span class="hljs-type">Failure</span>(e)
      }
    }

  <span class="hljs-comment">//Code...</span>
}
</code></pre>
<p>The <code>AddTransaction</code> message triggers the logic to store a new transaction in the list of pending ones of our blockchain. The Node Actor responds with the <code>index</code> of the block that will contain the transaction.</p>
<p>First of all we store the “address” of the <code>sender()</code> of the message in a <code>node</code> value to use it later. We send to the Broker Actor a message to add a new transaction, then we <code>ask</code> to the Blockchain Actor the last index of the chain. The <code>ask</code> operator — the one expressed with <code>?</code> — is used to send a message to an actor and wait for a response. The response (mapped to an <code>Int</code> value) can be a <code>Success</code> or a <code>Failure</code>.</p>
<p>In the first case we send back to the sender (<code>node</code>) the <code>index+1</code>, since it will be the index of the next mined block. In case of failure, we respond to the sender with a <code>Failure</code> containing the reason of the failure. Remember this pattern:</p>
<p><strong>ask → wait for a response → handle success/failure</strong></p>
<p>because we will see it again.</p>
<pre><code class="lang-scala"><span class="hljs-keyword">override</span> <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">receive</span></span>: <span class="hljs-type">Receive</span> = {
    <span class="hljs-comment">//Code...</span>

    <span class="hljs-keyword">case</span> <span class="hljs-type">CheckPowSolution</span>(solution) =&gt; {
      <span class="hljs-keyword">val</span> node = sender()
      (blockchain ? <span class="hljs-type">GetLastHash</span>).mapTo[<span class="hljs-type">String</span>] onComplete {
        <span class="hljs-keyword">case</span> <span class="hljs-type">Success</span>(hash: <span class="hljs-type">String</span>) =&gt; miner.tell(<span class="hljs-type">Validate</span>(hash, solution), node)
        <span class="hljs-keyword">case</span> <span class="hljs-type">Failure</span>(e) =&gt; node ! akka.actor.<span class="hljs-type">Status</span>.<span class="hljs-type">Failure</span>(e)
      }
    }

  <span class="hljs-comment">//Code...</span>
}
view raw
</code></pre>
<p>This time we have to check if a solution to the PoW algorithm is correct. We ask to the Blockchain Actor the hash of the last block, and we tell the Miner Actor to validate the solution against the hash. In the <code>tell</code> function we pass to the Miner the <code>Validate</code> message along with the address of the sender, so that the miner can respond directly to it. This is another approach, like the <code>forward</code> one we saw before.</p>
<pre><code class="lang-scala"><span class="hljs-keyword">override</span> <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">receive</span></span>: <span class="hljs-type">Receive</span> = {
    <span class="hljs-comment">//Code...</span>

    <span class="hljs-keyword">case</span> <span class="hljs-type">AddBlock</span>(proof) =&gt; {
      <span class="hljs-keyword">val</span> node = sender()
      (self ? <span class="hljs-type">CheckPowSolution</span>(proof)) onComplete {
        <span class="hljs-keyword">case</span> <span class="hljs-type">Success</span>(_) =&gt; {
          (broker ? <span class="hljs-type">Broker</span>.<span class="hljs-type">GetTransactions</span>).mapTo[<span class="hljs-type">List</span>[<span class="hljs-type">Transaction</span>]] onComplete {
            <span class="hljs-keyword">case</span> <span class="hljs-type">Success</span>(transactions) =&gt; blockchain.tell(<span class="hljs-type">AddBlockCommand</span>(transactions, proof), node)
            <span class="hljs-keyword">case</span> <span class="hljs-type">Failure</span>(e) =&gt; node ! akka.actor.<span class="hljs-type">Status</span>.<span class="hljs-type">Failure</span>(e)
          }
          broker ! <span class="hljs-type">Clear</span>
        }
        <span class="hljs-keyword">case</span> <span class="hljs-type">Failure</span>(e) =&gt; node ! akka.actor.<span class="hljs-type">Status</span>.<span class="hljs-type">Failure</span>(e)
      }
    }

    <span class="hljs-comment">//Code...</span>
}
</code></pre>
<p>Other nodes can mine blocks, so we may receive a request to add a block that we didn’t mine. The proof is enough to add the new block, since we assume that all the nodes share the same list of pending transactions.</p>
<pre><code class="lang-scala"><span class="hljs-keyword">override</span> <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">receive</span></span>: <span class="hljs-type">Receive</span> = {
    <span class="hljs-comment">//Code...</span>

    <span class="hljs-keyword">case</span> <span class="hljs-type">Mine</span> =&gt; {
      <span class="hljs-keyword">val</span> node = sender()
      (blockchain ? <span class="hljs-type">GetLastHash</span>).mapTo[<span class="hljs-type">String</span>] onComplete {
        <span class="hljs-keyword">case</span> <span class="hljs-type">Success</span>(hash) =&gt; (miner ? <span class="hljs-type">Miner</span>.<span class="hljs-type">Mine</span>(hash)).mapTo[<span class="hljs-type">Future</span>[<span class="hljs-type">Long</span>]] onComplete {
          <span class="hljs-keyword">case</span> <span class="hljs-type">Success</span>(solution) =&gt; waitForSolution(solution)
          <span class="hljs-keyword">case</span> <span class="hljs-type">Failure</span>(e) =&gt; log.error(<span class="hljs-string">s"Error finding PoW solution: <span class="hljs-subst">${e.getMessage}</span>"</span>)
        }
        <span class="hljs-keyword">case</span> <span class="hljs-type">Failure</span>(e) =&gt; node ! akka.actor.<span class="hljs-type">Status</span>.<span class="hljs-type">Failure</span>(e)
      }
    }

    <span class="hljs-comment">//Code...</span>
  }

  <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">waitForSolution</span></span>(solution: <span class="hljs-type">Future</span>[<span class="hljs-type">Long</span>]) = <span class="hljs-type">Future</span> {
    solution onComplete {
      <span class="hljs-keyword">case</span> <span class="hljs-type">Success</span>(proof) =&gt; {
        broker ! <span class="hljs-type">Broker</span>.<span class="hljs-type">AddTransaction</span>(createCoinbaseTransaction(nodeId))
        self ! <span class="hljs-type">AddBlock</span>(proof)
        miner ! <span class="hljs-type">Ready</span>
      }
      <span class="hljs-keyword">case</span> <span class="hljs-type">Failure</span>(e) =&gt; log.error(<span class="hljs-string">s"Error finding PoW solution: <span class="hljs-subst">${e.getMessage}</span>"</span>)
    }
  }
</code></pre>
<p>This is a simplification, in the Bitcoin network there cannot be such assumption. First of all we should check if the solution is valid. We do this sending a message to the node itself: <code>self ? CheckPowSolution(proof)</code>. If the proof is valid, we get the list of pending transaction from the Broker Actor, then we <code>tell</code> to the Blockchain Actor to add to the chain a new block containing the transactions and the validated proof. The last thing to do is to command the Broker Actor to clear the list of pending transactions.</p>
<p>The last message is the request to start mining a new block. We need the hash of the last block in the chain, so we request it to the Blockchain Actor. Once we have the hash, we can start mining a new block.</p>
<p>The PoW algorithm is a long-running operation, so the Miner Actor responds immediately with a <code>Future</code> containing the computation. The <code>waitForSolution</code> function waits for the computation to complete, while the Node Actor keeps doing its business.</p>
<p>When we have a solution, we reward ourselves adding the <strong>coinbase transaction</strong> to the list of pending transactions. Then we add the new block to the chain and tell the Miner Actor to be ready to mine another block.</p>
<h4 id="heading-rest-api-with-akka-http">REST API with Akka HTTP</h4>
<p>This last section describes the server and REST API. This is the most “external” part of our application, the one connecting the outside world to the Scalachain node. We will make use of Akka HTTP library, which is part of the Akka Framework. Let’s start looking at the server, the entry point of our application.</p>
<pre><code class="lang-scala"><span class="hljs-class"><span class="hljs-keyword">object</span> <span class="hljs-title">Server</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">App</span> <span class="hljs-keyword">with</span> <span class="hljs-title">NodeRoutes</span> </span>{

  <span class="hljs-keyword">val</span> address = <span class="hljs-keyword">if</span> (args.length &gt; <span class="hljs-number">0</span>) args(<span class="hljs-number">0</span>) <span class="hljs-keyword">else</span> <span class="hljs-string">"localhost"</span>
  <span class="hljs-keyword">val</span> port = <span class="hljs-keyword">if</span> (args.length &gt; <span class="hljs-number">1</span>) args(<span class="hljs-number">1</span>).toInt <span class="hljs-keyword">else</span> <span class="hljs-number">8080</span>

  <span class="hljs-keyword">implicit</span> <span class="hljs-keyword">val</span> system: <span class="hljs-type">ActorSystem</span> = <span class="hljs-type">ActorSystem</span>(<span class="hljs-string">"scalachain"</span>)

  <span class="hljs-keyword">implicit</span> <span class="hljs-keyword">val</span> materializer: <span class="hljs-type">ActorMaterializer</span> = <span class="hljs-type">ActorMaterializer</span>()

  <span class="hljs-keyword">val</span> node: <span class="hljs-type">ActorRef</span> = system.actorOf(<span class="hljs-type">Node</span>.props(<span class="hljs-string">"scalaChainNode0"</span>))

  <span class="hljs-keyword">lazy</span> <span class="hljs-keyword">val</span> routes: <span class="hljs-type">Route</span> = statusRoutes ~ transactionRoutes ~ mineRoutes

  <span class="hljs-type">Http</span>().bindAndHandle(routes, address, port)

  println(<span class="hljs-string">s"Server online at http://<span class="hljs-subst">$address</span>:<span class="hljs-subst">$port</span>/"</span>)

  <span class="hljs-type">Await</span>.result(system.whenTerminated, <span class="hljs-type">Duration</span>.<span class="hljs-type">Inf</span>)

}
</code></pre>
<p>Since the <code>Server</code> is our entry point, it needs to extend the <code>App</code> trait. It extends also <code>NodeRoutes</code>, a trait that contains all the http routes to the various endpoint of the node.</p>
<p>The <code>system</code> value is where we store our <code>ActorSystem</code>. Every actor created in this system will be able to talk to the others inside it. Akka HTTP requires also the definition of another value, the <code>ActorMaterializer</code>. This relates to the Akka Streams module, but since Akka HTTP is built on top of it, we still need this object to be initialized in our server (if you want to go deep on the relation with streams, look <a target="_blank" href="https://doc.akka.io/docs/akka-http/current/implications-of-streaming-http-entity.html">here</a>).</p>
<p>The Node Actor is created along with the HTTP routes of the node, that are chained using the <code>~</code> operator. Don’t worry about the routes now, we will be back to them in a moment.</p>
<p>The last thing to do is to start our server using the function <code>Http().bindHandle</code>, that will also bind the routes we pass to it as an argument. The <code>Await.result</code> function will wait the termination signal to stop the server.</p>
<p>The server will be useless without the routes to trigger the business logic of the application. We define the routes in the trait <code>NodeRoutes</code>, differentiating them according to the different logic they trigger:</p>
<ul>
<li><code>statusRoutes</code> contains the endpoints to ask the Scalachain node for its status.</li>
<li><code>transactionRoutes</code> handles everything related to transactions.</li>
<li><code>mineRoutes</code> has the endpoint to start the mining process</li>
</ul>
<p>Notice that this differentiation is a logic one, just to keep things ordered and readable. The three routes will be chained in a single one after their initialization in the server.</p>
<pre><code class="lang-scala"><span class="hljs-comment">//Imports...</span>
<span class="hljs-keyword">import</span> com.elleflorio.scalachain.utils.<span class="hljs-type">JsonSupport</span>._
<span class="hljs-comment">// Imports...</span>

<span class="hljs-class"><span class="hljs-keyword">trait</span> <span class="hljs-title">NodeRoutes</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">SprayJsonSupport</span> </span>{

  <span class="hljs-keyword">implicit</span> <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">system</span></span>: <span class="hljs-type">ActorSystem</span>

  <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">node</span></span>: <span class="hljs-type">ActorRef</span>

  <span class="hljs-keyword">implicit</span> <span class="hljs-keyword">lazy</span> <span class="hljs-keyword">val</span> timeout = <span class="hljs-type">Timeout</span>(<span class="hljs-number">5.</span>seconds)

  <span class="hljs-comment">//Code...</span>
}
</code></pre>
<p>The <code>NodeRoutes</code> trait extends <code>SprayJsonSupport</code> to add <code>JSON</code> serialization/deserialization. <a target="_blank" href="https://github.com/spray/spray-json">SprayJson</a> is a Scala library analogous to Jackson in Java, and it comes for free with Akka HTTP.</p>
<p>To convert our objects to a JSON string we import the class <code>JsonSupport</code> defined in the <code>utils</code> package, which contains custom reader/writer for every object. I won’t go into the details, you can find the <a target="_blank" href="https://github.com/elleFlorio/scalachain/blob/master/src/main/scala/com/elleflorio/scalachain/utils/JsonSupport.scala">class</a> in the repository if you want to look at the implementation.</p>
<p>We have a couple of implicit values. The <code>ActorSystem</code> is used to define the system of actors, while the <code>Timeout</code> is used by the <code>OnSuccess</code> function that waits for a response from the actors. The <code>ActorRef</code> is defined by overriding in the server implementation.</p>
<pre><code class="lang-scala"><span class="hljs-comment">//Code...</span>

<span class="hljs-keyword">lazy</span> <span class="hljs-keyword">val</span> statusRoutes: <span class="hljs-type">Route</span> = pathPrefix(<span class="hljs-string">"status"</span>) {
    concat(
      pathEnd {
        concat(
          get {
            <span class="hljs-keyword">val</span> statusFuture: <span class="hljs-type">Future</span>[<span class="hljs-type">Chain</span>] = (node ? <span class="hljs-type">GetStatus</span>).mapTo[<span class="hljs-type">Chain</span>]
            onSuccess(statusFuture) { status =&gt;
              complete(<span class="hljs-type">StatusCodes</span>.<span class="hljs-type">OK</span>, status)
            }
          }
        )
      }
    )
  }

<span class="hljs-comment">//Code...</span>
</code></pre>
<p>The endpoint to get the status of the blockchain is defined in the statusRoutes. We define the pathPrefix as "status" so the node will respond to the path ` http://</p><address>:&lt;port/status. After that there is the definition of the HTTP actions we want to enable on the path. Here we want to get the status of the blockchain, so we define only the get action. Inside that we ask the Node Actor to get the current Chain. When the actor responds, the Chain is sent as a JSON along with an ok status in the complete method.<p></p>
<pre><code class="lang-scala"><span class="hljs-comment">//Code...</span>

<span class="hljs-keyword">lazy</span> <span class="hljs-keyword">val</span> transactionRoutes: <span class="hljs-type">Route</span> = pathPrefix(<span class="hljs-string">"transactions"</span>) {
    concat(
      pathEnd {
        concat(
          get {
            <span class="hljs-keyword">val</span> transactionsRetrieved: <span class="hljs-type">Future</span>[<span class="hljs-type">List</span>[<span class="hljs-type">Transaction</span>]] =
              (node ? <span class="hljs-type">GetTransactions</span>).mapTo[<span class="hljs-type">List</span>[<span class="hljs-type">Transaction</span>]]
            onSuccess(transactionsRetrieved) { transactions =&gt;
              complete(transactions.toList)
            }
          },
          post {
            entity(as[<span class="hljs-type">Transaction</span>]) { transaction =&gt;
              <span class="hljs-keyword">val</span> transactionCreated: <span class="hljs-type">Future</span>[<span class="hljs-type">Int</span>] =
                (node ? <span class="hljs-type">AddTransaction</span>(transaction)).mapTo[<span class="hljs-type">Int</span>]
              onSuccess(transactionCreated) { done =&gt;
                complete((<span class="hljs-type">StatusCodes</span>.<span class="hljs-type">Created</span>, done.toString))
              }
            }
          }
        )
      }
    )
  }

<span class="hljs-comment">//Code...</span>
</code></pre>
<p>The <code>transactionRoutes</code> allows the interaction with the pending transactions of the node. We define the HTTP action <code>get</code> to retrieve the list of pending transactions. This time we also define the HTTP action <code>post</code> to add a new transaction to the list of pending ones. The <code>entity(as[Transaction])</code> function is used to deserialize the <code>JSON</code> body into a <code>Transaction</code> object.</p>
<pre><code class="lang-scala"><span class="hljs-comment">//Code...</span>
<span class="hljs-keyword">lazy</span> <span class="hljs-keyword">val</span> mineRoutes: <span class="hljs-type">Route</span> = pathPrefix(<span class="hljs-string">"mine"</span>) {
    concat(
      pathEnd {
        concat(
          get {
            node ! <span class="hljs-type">Mine</span>
            complete(<span class="hljs-type">StatusCodes</span>.<span class="hljs-type">OK</span>)
          }
        )
      }
    )
  }

<span class="hljs-comment">//Code...</span>
</code></pre>
<p>The last route is the <code>MineRoutes</code>. This is a very simple one, used only to ask the Scalachain node to start mine a new block. We define a <code>get</code> action since we do not need to send anything to start the mining process. It is not required to wait for a response, since it may take some time, so we immediately respond with an <code>Ok</code> status.</p>
<p>The API to interact with the Scalachain node are documented <a target="_blank" href="https://documenter.getpostman.com/view/4636741/RWaHw8yx">here</a>.</p>
<h4 id="heading-conclusion">Conclusion</h4>
<p>With the last section, we concluded our tour inside Scalachain. This prototype of a blockchain is far from a real implementation, but we learned a lot of interesting things:</p>
<ul>
<li>How a blockchain works, at least from an high level perspective.</li>
<li>How to use functional programming (Scala) to build a blockchain.</li>
<li>How the Actor Model works, and its application to our use case using the Akka Framework.</li>
<li>How to use the Akka HTTP library to create a sever to run our blockchain, along with the APIs to interact with it.</li>
</ul>
<p>The code is not perfect, and some things can be implemented in a better way. For this reason, <strong>feel free to contribute to the project!</strong> ;-)</p>
</address> ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ How to (Un)marshal JSON in Akka HTTP with Circe ]]>
                </title>
                <description>
                    <![CDATA[ By Miguel Lopez Even though the usual library to (un)marshal JSON in Akka HTTP applications is spray-json, I decided to give circe a try. I use it in the Akka HTTP beginners course I’m working on. cough it’s free ? c**_ough_** In this post I’d like t... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/un-marshalling-json-in-akka-http-with-circe-3dcc2764eedb/</link>
                <guid isPermaLink="false">66c363d61a1cf73cbc81f147</guid>
                
                    <category>
                        <![CDATA[ api ]]>
                    </category>
                
                    <category>
                        <![CDATA[ json ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Scala ]]>
                    </category>
                
                    <category>
                        <![CDATA[ tech  ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Web Development ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ freeCodeCamp ]]>
                </dc:creator>
                <pubDate>Fri, 26 Oct 2018 08:38:53 +0000</pubDate>
                <media:content url="https://cdn-media-1.freecodecamp.org/images/1*RtFazsYIcVhn1w1cr4CyeQ.png" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>By Miguel Lopez</p>
<p>Even though the usual library to (un)marshal JSON in Akka HTTP applications is spray-json, I decided to give circe a try. I use it in the Akka HTTP beginners course I’m working on. <strong><em><em>cough</em></em></strong> <a target="_blank" href="http://link.codemunity.io/circe-akka-http-quickstart-course">it’s free</a> ? <em>c**_ough</em>_**</p>
<p>In this post I’d like to show you why I tried it out.</p>
<p>To use circe with Akka HTTP — and other JSON libraries for that matter — we have to create the marshalers and unmarshalers manually. Thankfully, there is an <a target="_blank" href="https://github.com/hseeberger/akka-http-json">additional library</a> that already does that for us.</p>
<h3 id="heading-project-setup-and-overview">Project setup and overview</h3>
<p>Clone the <a target="_blank" href="https://github.com/Codemunity/akkahttp-quickstart">project’s repository</a>, and checkout the <code>branch 3.3-repository-implementation</code>.</p>
<p>Under <code>src/main/scala</code> you'll find the following files:</p>
<pre><code>$ tree srcsrc└── main    └── scala        ├── Main.scala        ├── Todo.scala        └── TodoRepository.scala2 directories, <span class="hljs-number">3</span> files
</code></pre><p>The <code>Main</code> object is the application's entry point. So far it has a hello world route and it binds it to a given host and route.</p>
<p><code>Todo</code> is our application’s model. And the <code>TodoRepository</code> is in charge of persisting and accessing todos. So far it only has an in-memory implementation to keep things simple and focused.</p>
<h3 id="heading-listing-all-the-todos">Listing all the todos</h3>
<p>We’ll change the <code>Main</code> object’s route to list all the todos in a repository. We will also add some initial todos for testing:</p>
<pre><code><span class="hljs-keyword">import</span> akka.actor.ActorSystemimport akka.http.scaladsl.Httpimport akka.stream.ActorMaterializerimport scala.concurrent.Awaitimport scala.util.{Failure, Success}object Main <span class="hljs-keyword">extends</span> App {  val host = <span class="hljs-string">"0.0.0.0"</span>  val port = <span class="hljs-number">9000</span>  implicit val system: ActorSystem = ActorSystem(name = <span class="hljs-string">"todoapi"</span>)  implicit val materializer: ActorMaterializer = ActorMaterializer()  <span class="hljs-keyword">import</span> system.dispatcher  val todos = Seq(    Todo(<span class="hljs-string">"1"</span>, <span class="hljs-string">"Clean the house"</span>, <span class="hljs-string">""</span>, done = <span class="hljs-literal">false</span>),    Todo(<span class="hljs-string">"2"</span>, <span class="hljs-string">"Learn Scala"</span>, <span class="hljs-string">""</span>, done = <span class="hljs-literal">true</span>),  )  val todoRepository = <span class="hljs-keyword">new</span> InMemoryTodoRepository(todos)  <span class="hljs-keyword">import</span> akka.http.scaladsl.server.Directives._  def route = path(<span class="hljs-string">"todos"</span>) {    get {      complete(todoRepository.all())    }  }  val binding = Http().bindAndHandle(route, host, port)  binding.onComplete {    <span class="hljs-keyword">case</span> Success(_) =&gt; println(<span class="hljs-string">"Success!"</span>)    <span class="hljs-keyword">case</span> Failure(error) =&gt; println(s<span class="hljs-string">"Failed: ${error.getMessage}"</span>)  }  <span class="hljs-keyword">import</span> scala.concurrent.duration._  Await.result(binding, <span class="hljs-number">3.</span>seconds)}
</code></pre><p>Now we’re listening to requests under <code>/todos</code> and we respond with all the todos we have in our <code>todoRepository</code>.</p>
<p>However, if we try to run this it won’t compile:</p>
<pre><code><span class="hljs-built_in">Error</span>:(<span class="hljs-number">26</span>, <span class="hljs-number">34</span>) type mismatch; found   : scala.concurrent.Future[Seq[Todo]] required: akka.http.scaladsl.marshalling.ToResponseMarshallable      complete(todoRepository.all())
</code></pre><p>The compilation error is telling us it doesn’t know how to marshal our todos into JSON.</p>
<p>We need to import circe and the support library:</p>
<pre><code><span class="hljs-keyword">import</span> akka.http.scaladsl.server.Directives._import de.heikoseeberger.akkahttpcirce.FailFastCirceSupport._import io.circe.generic.auto._def route = path(<span class="hljs-string">"todos"</span>) {  get {    complete(todoRepository.all())  }}
</code></pre><p>With those two extra lines we can now run our <code>Main</code> object and test our new route.</p>
<p>Make a GET request to <code>http://localhost:9000/todos</code> :</p>
<p><img src="https://cdn-media-1.freecodecamp.org/images/xuRxZ8giASz62AEFcSr0SZRIVRlXXOEoMZqL" alt="Image" width="1728" height="1080" loading="lazy"></p>
<p>And we get our todos back! ?</p>
<h3 id="heading-creating-todos">Creating todos</h3>
<p>Turns out that unmarshaling JSON into our models doesn’t take much effort either. But our <code>TodoRepository</code> doesn’t support saving todos at the moment. Let’s add that functionality first:</p>
<pre><code><span class="hljs-keyword">import</span> scala.concurrent.{ExecutionContext, Future}trait TodoRepository {  def all(): Future[Seq[Todo]]  def done(): Future[Seq[Todo]]  def pending(): Future[Seq[Todo]]  def save(todo: Todo): Future[Todo]}<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">InMemoryTodoRepository</span>(<span class="hljs-title">initialTodos</span>: <span class="hljs-title">Seq</span>[<span class="hljs-title">Todo</span>] </span>= Seq.empty)(implicit ec: ExecutionContext) <span class="hljs-keyword">extends</span> TodoRepository {  private <span class="hljs-keyword">var</span> todos: Vector[Todo] = initialTodos.toVector  override def all(): Future[Seq[Todo]] = Future.successful(todos)  override def done(): Future[Seq[Todo]] = Future.successful(todos.filter(_.done))  override def pending(): Future[Seq[Todo]] = Future.successful(todos.filterNot(_.done))  override def save(todo: Todo): Future[Todo] = Future.successful {    todos = todos :+ todo    todo  }}
</code></pre><p>We added a method <code>save</code> to the trait and the implementation. Because we are using a <code>Vector</code> our implementation of <code>save</code> will store duplicated todos. That’s fine for the purposes of this tutorial.</p>
<p>Let’s add a new route that will listen to POST requests. This route receive a <code>Todo</code> as the request’s body and saves it into our repository:</p>
<pre><code>def route = path(<span class="hljs-string">"todos"</span>) {  get {    complete(todoRepository.all())  } ~ post {    entity(<span class="hljs-keyword">as</span>[Todo]) { <span class="hljs-function"><span class="hljs-params">todo</span> =&gt;</span>      complete(todoRepository.save(todo))    }  }}
</code></pre><p>Using the <code>entity</code> directive we can build a route that automatically parses incoming JSON to our model. It also rejects requests with invalid JSON:</p>
<p><img src="https://cdn-media-1.freecodecamp.org/images/ew2sqvGf4H8BuBFuDM5iVG-RQsHTV5DVd9yx" alt="Image" width="1728" height="1080" loading="lazy"></p>
<p>We sent the <code>done</code> field as a string, and it should have been a boolean, which our API responded to with bad request.</p>
<p>Let’s make a valid request to create a new todo:</p>
<p><img src="https://cdn-media-1.freecodecamp.org/images/qqLEGI-7ia2pcDesaClXCoTW7D21h5DOC7v1" alt="Image" width="1728" height="1080" loading="lazy"></p>
<p>This time we sent the property as <code>done := false</code>, which tells HTTPie to send the value as <code>Boolean</code> instead of <code>String</code>.</p>
<p>We get our todo back and a 200 status code, which means it went well. We can confirm it worked by querying the todos again:</p>
<p><img src="https://cdn-media-1.freecodecamp.org/images/bHQixnuljInXUXX7QaKPjX6JfX-9rm-Yxtln" alt="Image" width="1728" height="1080" loading="lazy"></p>
<p>We get three todos, the hardcoded ones and the new one we created.</p>
<h3 id="heading-wrapping-up">Wrapping up</h3>
<p>We added JSON marshaling and unmarshaling to our application by adding the dependencies (it was already done in the project) and by importing both libraries.</p>
<p>Circe figures out how to handle our models without much intervention from us.</p>
<p>In a future post, we will explore how to accomplish the same functionality with spray-json instead.</p>
<p>Stay tuned!</p>
<p>If you liked this tutorial and want to learn how to build an API for a todo application, check out our new <strong>free</strong> course! ???</p>
<p><a target="_blank" href="http://link.codemunity.io/circe-akka-http-quickstart-course"><strong>Akka HTTP Quickstart</strong></a><br><a target="_blank" href="http://link.codemunity.io/circe-akka-http-quickstart-course">_Learn how to create web applications and APIs with Akka HTTP in this free course!_link.codemunity.io</a></p>
<p><em>Originally published at <a target="_blank" href="https://www.codemunity.io/tutorials/akka-http-json-circe/">www.codemunity.io</a>.</em></p>
 ]]>
                </content:encoded>
            </item>
        
    </channel>
</rss>
