<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:atom="http://www.w3.org/2005/Atom" version="2.0">
    <channel>
      <title>Daniel Westheide on making software</title>
      <link>https://danielwestheide.com</link>
      <description>Daniel Westheide&#x27;s personal website about functional programming and everything related to the software development process</description>
      <generator>Zola</generator>
      <language>en</language>
      <atom:link href="https://danielwestheide.com/rss.xml" rel="self" type="application/rss+xml"/>
      <lastBuildDate>Sun, 29 Mar 2026 00:00:00 +0000</lastBuildDate>
      <item>
          <title>Pair Programming Considered Unnecessary: The Costs of Productive Solitude</title>
          <pubDate>Sun, 29 Mar 2026 00:00:00 +0000</pubDate>
          <author>Unknown</author>
          <link>https://danielwestheide.com/blog/pair-programming-considered-unnecessary/</link>
          <guid>https://danielwestheide.com/blog/pair-programming-considered-unnecessary/</guid>
          <description xml:base="https://danielwestheide.com/blog/pair-programming-considered-unnecessary/">&lt;p&gt;JetBrains recently announced that they are &lt;a href=&quot;https:&#x2F;&#x2F;blog.jetbrains.com&#x2F;platform&#x2F;2026&#x2F;03&#x2F;sunsetting-code-with-me&#x2F;&quot;&gt;sunsetting Code With Me&lt;&#x2F;a&gt;, their IDE feature for remote collaborative coding. They explain this decision with declining demand after the pandemic, stating that teams have found &quot;different collaboration workflows&quot;.&lt;&#x2F;p&gt;
&lt;p&gt;As someone who has experienced the value of pair and ensemble programming, both sitting at the same computer and remotely, I find it sad to see that this kind of working is apparently in decline, or even at risk of dying out, if a product specifically developed for supporting this way of collaboration remotely is no longer economically viable.&lt;&#x2F;p&gt;
&lt;p&gt;Pair programming, and even more so ensemble programming, can be draining, yes, so I totally understand that not everyone likes this way of working. But in my experience, the shared mental model you build up over time by regularly working on a code base in a pair or ensemble is absolutely priceless. I have never experienced this level of alignment within a team with any other method of collaboration. It&#x27;s hands down one of the best methods of building a shared theory of the program in a team, as described by Naur&#x27;s classic paper &quot;Programming as Theory Building&quot;. Such a shared understanding cannot be built through documentation, which, according to Naur, and confirmed by my experience, is always lossy. It cannot be built through code reviews either, but only through interaction, working together on the same task in the same code base.&lt;&#x2F;p&gt;
&lt;p&gt;I very much suspect that the declining popularity of Code With Me has a lot to do with the rising popularity of AI assisted coding and agentic software development. As I mentioned in my previous blog post, I have long tried to ignore this rising tide completely. Meanwhile, I have been using JetBrains Junie for a year or so. And I noticed repeatedly that the fact that everyone in my team has adopted an AI-assisted way of working in some way or another has reduced the amount of time we work together. I often use AI as a rubber duck and thinking aid, and it works. As a consequence, the threshold for reaching out to a colleague when struggling with a problem or having a question has quietly risen. This is not a conscious decision. It just no longer feels necessary a lot of times to ask a co-worker if your AI assistant can help you as well. We all know how difficult it is to get productive again after an interruption, there are whole studies that have examined and quantified this problem. So having fewer of those interruptions has some merit.&lt;&#x2F;p&gt;
&lt;p&gt;And no, this doesn&#x27;t mean that we don&#x27;t collaborate any more at all. However, a lot of the collaboration has become asynchronous and artefact-based, discussing merge requests or architecture decision records. It&#x27;s a rare occasion that we come together synchronously for exploratory thinking, for example for technical refinements.&lt;&#x2F;p&gt;
&lt;p&gt;For theory building in the Naur sense, this is not great. AI doesn&#x27;t accumulate understanding of the system across sessions, and if I use AI as a thinking partner, that thinking does not propagate to the team. We&#x27;re as far away from the mind-meld experience of ensemble programming as you can get. While we probably get better at producing and reviewing artefacts, we become worse at building a shared understanding of the system. I also imagine that onboarding new team members will be more difficult because if you hardly ever work together on a problem, there are very few chances for transfer of tacit knowledge. Somehow, an AI-assisted team seems to have a tendency towards creating silos of knowledge.&lt;&#x2F;p&gt;
&lt;p&gt;As a team you have to find a good balance between individual efficiency (in this case, by reducing interruptions) and long-term productivity of the team (through collaborative problem solving, leading to a shared mental model of the system in the whole team). The question is whether you are aware of this shift, and whether you take a conscious decision of accepting the trade-offs or whether you take active measures against it.&lt;&#x2F;p&gt;
</description>
      </item>
      <item>
          <title>Developing with AI through the Cognitive Lens</title>
          <pubDate>Fri, 20 Mar 2026 00:00:00 +0000</pubDate>
          <author>Unknown</author>
          <link>https://danielwestheide.com/blog/developing-with-ai-through-the-cognitive-lens/</link>
          <guid>https://danielwestheide.com/blog/developing-with-ai-through-the-cognitive-lens/</guid>
          <description xml:base="https://danielwestheide.com/blog/developing-with-ai-through-the-cognitive-lens/">&lt;p&gt;Wow, I can&#x27;t believe that this blog has been dormant for more than six years! And the last post was frankly not more than a sign of life, informing about the migration from Octopress to Zola.&lt;&#x2F;p&gt;
&lt;p&gt;It&#x27;s not that I haven&#x27;t had anything to say in all those years. It&#x27;s just that I have exclusively published my blog posts on the &lt;a href=&quot;https:&#x2F;&#x2F;www.innoq.com&#x2F;en&#x2F;written&#x2F;?by=daniel-westheide&quot;&gt;INNOQ company blog&lt;&#x2F;a&gt;. While I plan to be an even more active contributor to the company blog this year, I also decided it was time to come out of hibernation on my personal blog.&lt;&#x2F;p&gt;
&lt;p&gt;In this post, I provide some meta discussion of the latest blog series I started on the INNOQ company blog, titled &lt;em&gt;Developing with AI through the Cognitive Lens&lt;&#x2F;em&gt;. Here are two interesting facts:&lt;&#x2F;p&gt;
&lt;p&gt;Firstly, even though I studied Applied Communication and Media Science, an interdisciplinary study course integrating general psychology, computer science, and sociology, with a focus in human-computer-interaction and usability, I started my career as a Java backend developer. The relentless forces of path dependency led to me to continue that road.&lt;&#x2F;p&gt;
&lt;p&gt;Secondly, I have long held a very critical view of the current LLM offerings both for ethical and environmental reasons.&lt;&#x2F;p&gt;
&lt;p&gt;That&#x27;s still true. However, AI-assisted coding and fully agentic software development have become so pervasive that I found it increasingly hard to ignore them — especially as someone clients expect to be familiar with current trends.&lt;&#x2F;p&gt;
&lt;p&gt;Some time last year, I finally had time to read &lt;a href=&quot;https:&#x2F;&#x2F;www.felienne.nl&quot;&gt;Felienne Hermans&#x27;&lt;&#x2F;a&gt; great book &lt;a href=&quot;https:&#x2F;&#x2F;www.manning.com&#x2F;books&#x2F;the-programmers-brain&quot;&gt;The Programmer&#x27;s Brain&lt;&#x2F;a&gt;, which teaches software developers all they need to know about cognition.&lt;&#x2F;p&gt;
&lt;p&gt;Reading this book rekindled my interest in cognitive psychology. I wanted to explore what cognitive psychology can teach us about the likely effects of using AI assistants or agents in software development. Knowing how humans learn, how they encode knowledge, and about the differences in how juniors and experts encode and retrieve knowledge, should tell us something about patterns of interacting with AI tools we should avoid, and which ones may prove beneficial.&lt;&#x2F;p&gt;
&lt;p&gt;In the first post, &lt;a href=&quot;https:&#x2F;&#x2F;www.innoq.com&#x2F;en&#x2F;blog&#x2F;2025&#x2F;11&#x2F;ai-cognitive-lens-speed-vs-skill&#x2F;&quot;&gt;Speed vs Skill&lt;&#x2F;a&gt;, I examine the tension between increasing short-term productivity and skill retention.&lt;&#x2F;p&gt;
&lt;p&gt;In the second post, I explain &lt;a href=&quot;https:&#x2F;&#x2F;www.innoq.com&#x2F;en&#x2F;blog&#x2F;2025&#x2F;12&#x2F;ai-cognitive-lens-elaboration&#x2F;&quot;&gt;elaboration&lt;&#x2F;a&gt;, a term from the science of learning, why it&#x27;s important for learning, which AI interaction patterns eliminate the need for elaboration and which ones maintain it.&lt;&#x2F;p&gt;
&lt;p&gt;In the third post, the last one so far, I &lt;a href=&quot;https:&#x2F;&#x2F;www.innoq.com&#x2F;en&#x2F;blog&#x2F;2026&#x2F;03&#x2F;ai-cognitive-lens-cognitive-load-theory&#x2F;&quot;&gt;apply the concept of cognitive load theory to AI-assisted coding&lt;&#x2F;a&gt;, using it to explain the limitations of what we can learn from &lt;a href=&quot;https:&#x2F;&#x2F;www.anthropic.com&#x2F;research&#x2F;AI-assistance-coding-skills&quot;&gt;a recent study by Anthropic&lt;&#x2F;a&gt; about the effect of AI assistance on coding skills.&lt;&#x2F;p&gt;
&lt;p&gt;So far, writing this series has been really exciting. It&#x27;s immensely satisfying to finally combine my interest and knowledge in general psychology with my expertise in software development and computer science. I definitely plan to write a few more posts in the series.&lt;&#x2F;p&gt;
</description>
      </item>
      <item>
          <title>Scala from Scratch: Understanding</title>
          <pubDate>Tue, 25 Aug 2020 00:00:00 +0000</pubDate>
          <author>Unknown</author>
          <link>https://danielwestheide.com/books/scala-from-scratch-understanding/</link>
          <guid>https://danielwestheide.com/books/scala-from-scratch-understanding/</guid>
          <description xml:base="https://danielwestheide.com/books/scala-from-scratch-understanding/">&lt;img src=&quot;&#x2F;images&#x2F;scalafromscratch-understanding-small.jpg&quot; alt=&quot;Scala from Scratch: Understanding Cover&quot;&gt;
&lt;p&gt;&lt;em&gt;Scala from Scratch: Understanding&lt;&#x2F;em&gt; is the second in a series of two books that teach you the Scala programming language. Readers that have read Scala from Scratch: Exploration or have learned some Scala trhough other means will get a deeper understanding of the language features and underlying functional programming concepts that have been explored in the first book. You will also learn about important advanced language features that play a crucial role in many real-life Scala projects. While doing so, you&#x27;ll get to know best practices that have been established over the years. By the end of the book, you will have a grasp of the language, its idioms, and common tools and techniques, allowing you to be a productive member in commercial or open-source Scala projects.&lt;&#x2F;p&gt;
&lt;div class=&quot;button-holder&quot;&gt;
&lt;a class=&quot;button&quot; href=&quot;https:&#x2F;&#x2F;leanpub.com&#x2F;scala-from-scratch-understanding&#x2F;&quot;&gt;Get ebook&lt;&#x2F;a&gt;
&lt;a class=&quot;button&quot; href=&quot;https:&#x2F;&#x2F;www.blurb.de&#x2F;b&#x2F;10331214-scala-from-scratch-understanding&quot;&gt;Get hardcover&lt;&#x2F;a&gt;
&lt;&#x2F;div&gt;
&lt;div style=&quot;clear:both;&quot;&gt;
</description>
      </item>
      <item>
          <title>Scala from Scratch: Exploration</title>
          <pubDate>Sun, 08 Dec 2019 00:00:00 +0000</pubDate>
          <author>Unknown</author>
          <link>https://danielwestheide.com/books/scala-from-scratch-exploration/</link>
          <guid>https://danielwestheide.com/books/scala-from-scratch-exploration/</guid>
          <description xml:base="https://danielwestheide.com/books/scala-from-scratch-exploration/">&lt;img src=&quot;&#x2F;images&#x2F;scalafromscratch-exploration-small.jpg&quot; alt=&quot;Scala from Scratch: Exploration Cover&quot;&gt;
&lt;p&gt;&lt;em&gt;Scala from Scratch: Exploration&lt;&#x2F;em&gt; is the first in a series of two books that teach you the Scala programming language. Readers that have some experience with an object-oriented or imperative language, like Java, Ruby, or Python, will get a comprehensive overview of what Scala brings to the table. The goal is to get you excited about Scala and give you a strong foundation for writing your first applications and continuing your journey. By the end of the book, you will have a good idea of what it&#x27;s like to work with Scala, and you will have written a small, but complete command-line application based on a &lt;em&gt;Functional Core, Imperative Shell&lt;&#x2F;em&gt; architecture.&lt;&#x2F;p&gt;
&lt;div class=&quot;button-holder&quot;&gt;
&lt;a class=&quot;button&quot; href=&quot;https:&#x2F;&#x2F;leanpub.com&#x2F;scala-from-scratch-exploration&#x2F;&quot;&gt;Get ebook&lt;&#x2F;a&gt;
&lt;a class=&quot;button&quot; href=&quot;https:&#x2F;&#x2F;www.blurb.de&#x2F;b&#x2F;10339512-scala-from-scratch-exploration&quot;&gt;Get hardcover&lt;&#x2F;a&gt;
&lt;&#x2F;div&gt;
&lt;div style=&quot;clear:both;&quot;&gt;
&lt;h3 id=&quot;bundle&quot;&gt;Bundle&lt;&#x2F;h3&gt;
&lt;p&gt;The ebook and the hardcover book are available at two different shops. Only Leanpub allows me to create discount codes. If you would like to get both the hardcover and the ebook, here&#x27;s what you can do: &lt;a href=&quot;mailto:dwestheide@posteo.eu&quot;&gt;Send me an email&lt;&#x2F;a&gt; with a picture of your hardcover book, and I&#x27;ll send you a Leanpub coupon code allowing you to get the ebook for free.&lt;&#x2F;p&gt;
</description>
      </item>
      <item>
          <title>Goodbye Octopress, hello Zola!</title>
          <pubDate>Mon, 23 Sep 2019 00:00:00 +0000</pubDate>
          <author>Unknown</author>
          <link>https://danielwestheide.com/blog/goodbye-octopress-hello-zola/</link>
          <guid>https://danielwestheide.com/blog/goodbye-octopress-hello-zola/</guid>
          <description xml:base="https://danielwestheide.com/blog/goodbye-octopress-hello-zola/">&lt;p&gt;If you have visited my website before, you will notice that things look a bit different from what they used to now. This is because I finally took the time to get rid of &lt;a href=&quot;http:&#x2F;&#x2F;octopress.org&#x2F;&quot;&gt;Octopress&lt;&#x2F;a&gt;, the static-site generator on which my website was based. In doing so, I took the opportunity to rewrite the HTML and CSS as well. If you&#x27;re interested in why I put so many hours into replacing something that seemed just fine, read on.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;the-joys-of-using-unmaintained-software&quot;&gt;The joys of using unmaintained software&lt;&#x2F;h2&gt;
&lt;p&gt;Octopress was great when I started my website. It provided a decent default theme and all the features I needed to get started blogging quickly. For quite a while, though, Octopress has been a project that&#x27;s no longer maintained. I was in the situation that could only generate my website with a specific ancient version of Ruby and the dependencies specified in my &lt;code&gt;Gemfile.lock&lt;&#x2F;code&gt; a few years ago. I couldn&#x27;t update Octopress to the latest version before it stopped being maintained. Even worse, when I got a new laptop, I wasn&#x27;t even able to install the version of Ruby I needed for Octopress to work. I spent a few hours trying to create a Docker image that would do the trick, but gave up when my attempts lead nowhere. This brought me to the ridiculous situation where there was only exactly one machine from which I was able to generate my website when I wrote a new article — my old Macbook Pro. I really didn&#x27;t want to depend on a single piece of hardware any longer. In my job, I would hardly accept such a sitation, so why should I do so for my personal website?&lt;&#x2F;p&gt;
&lt;p&gt;I wanted to go for a static site generator that would be easier to run on future hardware, ideally something consisting of a single, self-contained binary. Since I really like Rust, I didn&#x27;t go for the the obvious choice, which would have been &lt;a href=&quot;https:&#x2F;&#x2F;gohugo.io&#x2F;&quot;&gt;Hugo&lt;&#x2F;a&gt;, but decided to take &lt;a href=&quot;https:&#x2F;&#x2F;www.getzola.org&#x2F;&quot;&gt;Zola&lt;&#x2F;a&gt; for a ride, a static-site generator written in said language.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;less-is-more&quot;&gt;Less is more&lt;&#x2F;h2&gt;
&lt;p&gt;Another thing I didn&#x27;t like about the old website was that, using the default Octopress theme, it was quite bloated. My goal for the re-write was to create something that is visually based on the old look, but is a lot more minimalistic. My website now consists of a few kilobytes of HTML, plus 2KB of CSS. Part of the concept is that, in order to keep the website as lean as possible, I decided against using web fonts and went for a font stack, depending on pre-installed fonts. I don&#x27;t care too much about the website looking 100% consistent across all browsers and machines. What I do care about is that my website loads fast, even when you&#x27;re sitting in a train with a really bad internet connection.&lt;&#x2F;p&gt;
&lt;p&gt;Naturally, without any JavaScript, there is no tracking whatsoever on my website, which is something that was very important to me. In the old version using the default Octopress theme, I was loading some JavaScript from Twitter. As a consequence of removing all JavaScript, I also had to remove Disqus. I&#x27;m still evaluating alternatives, because there were actually quite a lof of comments and discussions in there. They are actually still there, I didn&#x27;t delete anything. However, I decided against embedding Disqus into my website for now.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;migration-anxiety&quot;&gt;Migration anxiety&lt;&#x2F;h2&gt;
&lt;p&gt;Have you ever bookmarked a URL of an interesting blog article, or even linked to it from your own blog, and some time later, you noticed that the link or bookmark was no longer working? Well, it happened to me numerous times. One of my biggest fears while preparing the new website was that I might break things for people as well. Since there are actually quite a few links pointing to specific posts on my website, this would be completely unacceptable. While Zola is not perfectly flexible with regard to the URLs it produces, you can configure redirects for pages and sections. This way, everyone who has bookmarked or linked to a page on my website should still be able to find those pages.&lt;&#x2F;p&gt;
</description>
      </item>
      <item>
          <title>The Complexity Trap: Think Before You Leap</title>
          <pubDate>Fri, 07 Dec 2018 00:00:00 +0000</pubDate>
          <author>Unknown</author>
          <link>https://danielwestheide.com/blog/the-complexity-trap/</link>
          <guid>https://danielwestheide.com/blog/the-complexity-trap/</guid>
          <description xml:base="https://danielwestheide.com/blog/the-complexity-trap/">&lt;img width=&quot;720&quot; height=&quot;540&quot; src=&quot;&#x2F;images&#x2F;content&#x2F;complexity-trap-slides&#x2F;complexity-trap-slide-0.png&quot;&gt;
&lt;p&gt;This blog post is a text version of &lt;a href=&quot;https:&#x2F;&#x2F;www.innoq.com&#x2F;en&#x2F;talks&#x2F;2018&#x2F;10&#x2F;the-complexity-trap-lambda-world-2018&#x2F;&quot;&gt;a talk&lt;&#x2F;a&gt; I gave at &lt;a href=&quot;http:&#x2F;&#x2F;cadiz.lambda.world&#x2F;&quot;&gt;Lambda World Cádiz&lt;&#x2F;a&gt; on October 25, 2018, for those who couldn&#x27;t attend and don&#x27;t like watching &lt;a href=&quot;https:&#x2F;&#x2F;youtu.be&#x2F;OXP-sUS6ffE&quot;&gt;video recordings&lt;&#x2F;a&gt;. Thanks a lot to Jan Stępień for encouraging me to publish a text version of my talk.&lt;&#x2F;p&gt;
&lt;div style=&quot;clear:both&quot;&gt;&lt;&#x2F;div&gt;
&lt;img width=&quot;720&quot; height=&quot;540&quot; src=&quot;&#x2F;images&#x2F;content&#x2F;complexity-trap-slides&#x2F;complexity-trap-slide-1.png&quot;&gt;
&lt;p&gt;In this article, I want to share my thoughts on a trend I have been observing for a few years. It&#x27;s a trend that probably exists in the strongly typed functional programming community at large, and to some extent, it&#x27;s possibly just a specific shape of a general trend in the realm of software development. But I have mainly observed it in the Scala community, because that&#x27;s where I spend most of my time. I want to talk about how we often cause more complexity than necessary with the methods we choose. In fact, it seems to me as if we sometimes cherish complexity instead of fighting it. So let&#x27;s see why that&#x27;s the case and what we could do about it.&lt;&#x2F;p&gt;
&lt;div style=&quot;clear:both&quot;&gt;&lt;&#x2F;div&gt;
&lt;img width=&quot;720&quot; height=&quot;540&quot; src=&quot;&#x2F;images&#x2F;content&#x2F;complexity-trap-slides&#x2F;complexity-trap-slide-2.png&quot;&gt;
&lt;p&gt;While this blog post is based on my experience in the Scala community, you don&#x27;t have to be fluent with Scala in order to take something away from it. Before we can have a conversation about what&#x27;s wrong with our relationship to complexity, though, it&#x27;s necessary to talk about what complexity actually is. After that, I will go into more detail about the phenomemon I call the complexity trap.&lt;&#x2F;p&gt;
&lt;div style=&quot;clear:both&quot;&gt;&lt;&#x2F;div&gt;
&lt;h2 id=&quot;what-is-complexity&quot;&gt;What is complexity?&lt;&#x2F;h2&gt;
&lt;img width=&quot;720&quot; height=&quot;540&quot; loading=&quot;lazy&quot; src=&quot;&#x2F;images&#x2F;content&#x2F;complexity-trap-slides&#x2F;complexity-trap-slide-3.png&quot;&gt;
&lt;p&gt;When we say that something is &quot;complex&quot;, what it means, according to the dictionary, is that it&#x27;s &quot;hard to separate, analyze, or solve&quot;. The word originates from the Latin verb &quot;complectere&quot;, which means &quot;to braid&quot;. Consider a bunch of modules that are completely entangled — so much so that you can neither reason about them individually any more nor change one of them without touching the other as well. That&#x27;s a pretty good example of complexity in software development.&lt;&#x2F;p&gt;
&lt;div style=&quot;clear:both&quot;&gt;&lt;&#x2F;div&gt;
&lt;img width=&quot;720&quot; height=&quot;540&quot; src=&quot;&#x2F;images&#x2F;content&#x2F;complexity-trap-slides&#x2F;complexity-trap-slide-4.png&quot;&gt;
&lt;p&gt;But it also helps to look at the origin of the word &quot;simple&quot; — the opposite of &quot;complex&quot;. &quot;Simple&quot; has its roots in the Latin adjective &quot;simplex&quot;, which means &quot;single&quot; or &quot;having one ingredient&quot;. In software development, we have a few principles that aim to achieve simplicity in this original sense. Two prominent examples of that are The Single Responsibility Principle and Separation of Concerns.&lt;&#x2F;p&gt;
&lt;p&gt;On such separation of concerns that we really love to enforce as functional programmers is the separation between effects and business logic. If these two concerns are not separated, it makes our code harder to reason about and more difficult to test. Hence, we try to isolate business logic from effects and implement the former as pure functions.&lt;&#x2F;p&gt;
&lt;div style=&quot;clear:both&quot;&gt;&lt;&#x2F;div&gt;
&lt;img width=&quot;720&quot; height=&quot;540&quot; loading=&quot;lazy&quot; src=&quot;&#x2F;images&#x2F;content&#x2F;complexity-trap-slides&#x2F;complexity-trap-slide-5.png&quot;&gt;
&lt;p&gt;In his &lt;em&gt;classic&lt;&#x2F;em&gt; paper &quot;No Silver Bullet&quot;, Fred Brooks identified two different types of complexity. Essential complexity is inherent in the problem domain. We cannot remove it because it stems from the very problem that we are supposed to solve with our software. Accidental complexity, on the other hand, is of our own making. This is complexity that is created by software developers, and in principle, it&#x27;s possible to remove complexity of this type.&lt;&#x2F;p&gt;
&lt;p&gt;According to Brooks, we spend most of our time tacking essential complexity these days, and there is not much accidental complexity left. This means that reducing accidental complexity further will not yield a lot of benefit. But that statement is from 1986. Today, it&#x27;s 2018, and I beg to disagree. I think we are spending a crazy amount of time managing accidental complexity, and we should put our focus much more on minimizing it.&lt;&#x2F;p&gt;
&lt;div style=&quot;clear:both&quot;&gt;&lt;&#x2F;div&gt;
&lt;img width=&quot;720&quot; height=&quot;540&quot; loading=&quot;lazy&quot; src=&quot;&#x2F;images&#x2F;content&#x2F;complexity-trap-slides&#x2F;complexity-trap-slide-6.png&quot;&gt;
&lt;p&gt;Over time, various metrics for complexity have been created, especially, but not exclusively, for code complexity. The most well-known of those metrics is probably   McCabe&#x27;s cyclomatic complexity: The more different code paths there are in a function, the higher its complexity. So one of the things that increased a function&#x27;s complexity is branching, for example by means of if expressions of pattern matching expressions. The higher the cyclomatic complexity of a function, the more test cases are necessary to achieve full path coverage.&lt;&#x2F;p&gt;
&lt;p&gt;Of course, there are other relevant metrics related to complexity in software development. Complexity can be measured at different levels, not only at the code level. For example, we can measure the extent of coupling between modules. The more dependencies there are between the modules of a system, the higher its overall complexity.&lt;&#x2F;p&gt;
&lt;div style=&quot;clear:both&quot;&gt;&lt;&#x2F;div&gt;
&lt;img width=&quot;720&quot; height=&quot;540&quot; loading=&quot;lazy&quot; src=&quot;&#x2F;images&#x2F;content&#x2F;complexity-trap-slides&#x2F;complexity-trap-slide-7.png&quot;&gt;
&lt;p&gt;But let&#x27;s come back to cyclomatic complexity. It&#x27;s certainly okay as a metric of how hard it is to test a function. However, I am actually more interested in how difficult it is to reason about what a function or a program is doing — or to reason about a system or an entire system of systems. In other words, I am interested in cognitive complexity.&lt;&#x2F;p&gt;
&lt;p&gt;In the imperative version of the &lt;code&gt;sumOfPrimes&lt;&#x2F;code&gt; function, a lot of complexity stems from mutable state and flow control statements. The functional solution doesn&#x27;t have any of that. So you could say that we are a bit at an advantage as functional programmers. However, we shouldn&#x27;t be too smug about it either.&lt;&#x2F;p&gt;
&lt;p&gt;What I find interesting is that Sonarsource actually created a metric for the cognitive complexity of code. However, I think that not every aspect of what constitutes cognitive complexity can easily be captured in metrics. For example, syntax, boilerplate, indirections can all add to a program&#x27;s cognitive complexity as well.&lt;&#x2F;p&gt;
&lt;div style=&quot;clear:both&quot;&gt;&lt;&#x2F;div&gt;
&lt;img width=&quot;720&quot; height=&quot;540&quot; loading=&quot;lazy&quot; src=&quot;&#x2F;images&#x2F;content&#x2F;complexity-trap-slides&#x2F;complexity-trap-slide-8.png&quot;&gt;
&lt;p&gt;But why is it so important to keep complexity in check anyway? Sadly, we human beings are incredibly bad at juggling with many things at once. According to a classic study by George Miller, a cognitive psychologist, human beings can only hold seven items in working memory at the same time, plus or minus two, on average. So the more concerns are entangled, the more code paths there are, and the more syntax and boilerplate there is, the more difficult it will be for us to reason about a program.&lt;&#x2F;p&gt;
&lt;div style=&quot;clear:both&quot;&gt;&lt;&#x2F;div&gt;
&lt;img width=&quot;720&quot; height=&quot;540&quot; loading=&quot;lazy&quot; src=&quot;&#x2F;images&#x2F;content&#x2F;complexity-trap-slides&#x2F;complexity-trap-slide-9.png&quot;&gt;
&lt;p&gt;In order to mitigate this, we use abstractions. Ideally, our programs are composed of many small functions, and we put related functions into the same module. In order to reduce coupling and the complexity of the whole system, we hide implementation details of our modules from others that depend on them.&lt;&#x2F;p&gt;
&lt;p&gt;In functional programming, we take abstraction much further than in other paradigms. We often abstract over things like iteration or data types in a way that is rarely seen outside of functional programming. This faciliates the definition of very simple functions that can be composed in many different ways — which is exactly what allowed us to write our &lt;code&gt;sumOfPrimes&lt;&#x2F;code&gt; function in a way that is much easier to reason about than the imperative version.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;the-complexity-trap-and-how-to-escape-it&quot;&gt;The complexity trap and how to escape it&lt;&#x2F;h2&gt;
&lt;div style=&quot;clear:both&quot;&gt;&lt;&#x2F;div&gt;
&lt;img width=&quot;720&quot; height=&quot;540&quot; loading=&quot;lazy&quot; src=&quot;&#x2F;images&#x2F;content&#x2F;complexity-trap-slides&#x2F;complexity-trap-slide-10.png&quot;&gt;
&lt;p&gt;Now that we have achieved a common understanding of what I mean when I talk about complexity, let me introduce you to my notion of the complexity trap. It might be fair to say that there is not a single complexity trap but rather multiple traps, even though they are all related to each other in one way or another.&lt;&#x2F;p&gt;
&lt;div style=&quot;clear:both&quot;&gt;&lt;&#x2F;div&gt;
&lt;img width=&quot;720&quot; height=&quot;540&quot; loading=&quot;lazy&quot; src=&quot;&#x2F;images&#x2F;content&#x2F;complexity-trap-slides&#x2F;complexity-trap-slide-11.png&quot;&gt;
&lt;p&gt;The first trap that I want to talk about is &lt;em&gt;neglecting the costs&lt;&#x2F;em&gt;. We often seem to ignore the costs of the approaches and technique we choose to implement our programs. Usually, the intentions are good: Often, we choose a technique because we explicitly want to reduce the complexity in our code base. But sometimes, doing so comes with a cost — and that cost can even be new complexity at a different level, which might be worst than what we had before.&lt;&#x2F;p&gt;
&lt;div style=&quot;clear:both&quot;&gt;&lt;&#x2F;div&gt;
&lt;img width=&quot;720&quot; height=&quot;540&quot; loading=&quot;lazy&quot; src=&quot;&#x2F;images&#x2F;content&#x2F;complexity-trap-slides&#x2F;complexity-trap-slide-12.png&quot;&gt;
&lt;p&gt;Let me illustrate this with an example. In general, &lt;em&gt;scrap your boilerplate&lt;&#x2F;em&gt; is a very desirable principle, because the more boilerplate code we have to write, the higher the complexity of our program and the higher the chance that we introduce bugs that could easily be avoided.&lt;&#x2F;p&gt;
&lt;p&gt;There is one specific way of scrapping boilerplate that is as popular in the Scala community as it is problematic. In Scala, the way that you encode values of a certain type to JSON and back is often defined via typeclasses. Instances of these typeclasses can often be derived automatically for arbitrary data and record types with a single line of code. This feature is heavily used in many Scala projects.&lt;&#x2F;p&gt;
&lt;p&gt;However, people rarely seem to consider the cost-value ratio of the way they use these automatically derived JSON codecs: In almost all projects I have seen, people use automatically derived JSON codecs to expose their internal domain model to the outside world.&lt;&#x2F;p&gt;
&lt;p&gt;Doing that certainly means that you scrap quite a bit of boilerplate and reduce the complexity in your code. However, if you look at the bigger picture, this is the opposite of removing complexity. Exposing your domain model like this almost always leads to strongly coupled systems that are very difficult to evolve. If you ask me, this is one of the most promising ways to create a distributed monolith. It really amazes me how many people are totally okay with simply making their internal model their public API, especially since functional programmers are usually crazy about abstractions.&lt;&#x2F;p&gt;
&lt;div style=&quot;clear:both&quot;&gt;&lt;&#x2F;div&gt;
&lt;img width=&quot;720&quot; height=&quot;540&quot; loading=&quot;lazy&quot; src=&quot;&#x2F;images&#x2F;content&#x2F;complexity-trap-slides&#x2F;complexity-trap-slide-13.png&quot;&gt;
&lt;p&gt;Apparently, we are even crazier about removing boilerplate. However, there are alternatives. If you don&#x27;t want to expose your domain model, but you want to avoid having to define your JSON codecs manually, another way to decouple your model and your API is to introduce a DTO layer. This is an abstraction layer that is often ridiculed these days While it would allow you to use automatically derived JSON codecs for your separate DTO type hierarchy, it would still be necessary to implement a mapping between your domain model and your DTOs. You will probably end up with as much boilerplate code as if you had manually defined your JSON codecs for your domain model types.&lt;&#x2F;p&gt;
&lt;p&gt;In the end, you have to choose your poison. Simply exposing your domain model with automatically derived JSON codecs is probably the most addictive poison, because it gives you instant gratification. In the long run, however, you will almost certainly have to pay the price.&lt;&#x2F;p&gt;
&lt;div style=&quot;clear:both&quot;&gt;&lt;&#x2F;div&gt;
&lt;img width=&quot;720&quot; height=&quot;540&quot; loading=&quot;lazy&quot; src=&quot;&#x2F;images&#x2F;content&#x2F;complexity-trap-slides&#x2F;complexity-trap-slide-14.png&quot;&gt;
&lt;p&gt;There is a variant of neglecting the costs that deserves to be discussed on its own. In many projects I have seen, an approach or technology is picked because it is being perceived as common courtesy. The reasoning is that &quot;this is what you do in 2018&quot;. If you ask people why they use approach or technology X, one of the most common reactions is that they are completely baffled to even hear such a question. So, in a slightly anxious and reproachful tone, they answer with a counter question: &quot;Well, isn&#x27;t that industry standard?&quot;&lt;&#x2F;p&gt;
&lt;div style=&quot;clear:both&quot;&gt;&lt;&#x2F;div&gt;
&lt;img width=&quot;720&quot; height=&quot;540&quot; loading=&quot;lazy&quot; src=&quot;&#x2F;images&#x2F;content&#x2F;complexity-trap-slides&#x2F;complexity-trap-slide-15.png&quot;&gt;
&lt;p&gt;You know what&#x27;s industry standard? Shipping containers! However, when people say &quot;industry standard&quot;, what they actually refer to is often just the latest fad — something that will definitely not be industry standard any more in a couple of years. If we follow this approach, it means that we adopt techniques without thinking. We don&#x27;t think about their costs, and we certainly don&#x27;t think about whether the benefits of that technique can even be realized in our specific situation.&lt;&#x2F;p&gt;
&lt;div style=&quot;clear:both&quot;&gt;&lt;&#x2F;div&gt;
&lt;img width=&quot;720&quot; height=&quot;540&quot; loading=&quot;lazy&quot; src=&quot;&#x2F;images&#x2F;content&#x2F;complexity-trap-slides&#x2F;complexity-trap-slide-16.png&quot;&gt;
&lt;p&gt;Here is a prime example that pops up in many Scala projects: Many people seem to think that they will be excommunicated from the Church of Functional Programming if they don&#x27;t use free monads or tagless final. There is this mantra that &lt;em&gt;you must not commit to a specific effect monad too early&lt;&#x2F;em&gt;. So what is a Scala developer going to do? Well, one thing is for sure: They are not going to commit to anything. Instead, they are going to abstract over monads and over many other things — everywhere and all the time.&lt;&#x2F;p&gt;
&lt;div style=&quot;clear:both&quot;&gt;&lt;&#x2F;div&gt;
&lt;img width=&quot;720&quot; height=&quot;540&quot; loading=&quot;lazy&quot; src=&quot;&#x2F;images&#x2F;content&#x2F;complexity-trap-slides&#x2F;complexity-trap-slide-17.png&quot;&gt;
&lt;p&gt;There are good reasons why you want to abstract over the effect monad when you develop a library. For example, when you provide an HTTP client library, you want to enable people to use it regardless of which effect monad they prefer. Also, if you really have the need to interpret some instructions in different ways in your application, free monads or tagless final are a perfectly reasonable choice. For example, you may want to define a data processing pipeline that and then be able to execute it either in a Hadoop cluster or on a single machine.&lt;&#x2F;p&gt;
&lt;div style=&quot;clear:both&quot;&gt;&lt;&#x2F;div&gt;
&lt;img width=&quot;720&quot; height=&quot;540&quot; loading=&quot;lazy&quot; src=&quot;&#x2F;images&#x2F;content&#x2F;complexity-trap-slides&#x2F;complexity-trap-slide-18.png&quot;&gt;
&lt;p&gt;However, most of the time when we write applications, such a need doesn&#x27;t exist. So what other reasons to abstract over the effect monad are there? Well, preparing for the ability to switch from Monix &lt;code&gt;Task&lt;&#x2F;code&gt; to ZIO is a bit like staying away from Postgres-specific SQL because you might switch to MySQL one day. So the main reason that remains for people to abstract over the effect type in application development is the desire to make their effectful code unit-testable.&lt;&#x2F;p&gt;
&lt;p&gt;How does this work? You have a function that mixes logic as well as effects, and in your production code, you execute it in your favourite effect monad, talking to a real external system. In unit tests, on the other hand, you execute it in the &lt;code&gt;Id&lt;&#x2F;code&gt; monad, using hard-coded test data. Object-oriented programmers call this technique stubbing, and they get mocked for doing it, mostly by people like us.&lt;&#x2F;p&gt;
&lt;div style=&quot;clear:both&quot;&gt;&lt;&#x2F;div&gt;
&lt;img width=&quot;720&quot; height=&quot;540&quot; loading=&quot;lazy&quot; src=&quot;&#x2F;images&#x2F;content&#x2F;complexity-trap-slides&#x2F;complexity-trap-slide-19.png&quot;&gt;
&lt;p&gt;It&#x27;s important to understand and keep in mind that abstracting over effect monads comes with a cost. For one, there is quite a bit of boilerplate code involved. Some of that is just syntactic noise that is due to the fact that Scala&#x27;s type inference is quite limited. But there is also the whole interpreter for your algebra, which I&#x27;m not even showing. And if your program has some conditional logic, things can get rather messy pretty quickly. Also, by their very definition, monads are strictly sequential. If you want to execute some effects in parallel, because they are actually independent from one another, you will have to abstract over applicatives as well. Getting those to actually execute in parallel in your target effect tyoe, however, is surprisingly easy to get wrong in my experience. Finally, abstracting over effect monads seems to lead people to writing big functions in which logic and effects are completely entangled, because they are still abel to unit test the whole big function using the &lt;code&gt;Id&lt;&#x2F;code&gt; monad.&lt;&#x2F;p&gt;
&lt;div style=&quot;clear:both&quot;&gt;&lt;&#x2F;div&gt;
&lt;img width=&quot;720&quot; height=&quot;540&quot; loading=&quot;lazy&quot; src=&quot;&#x2F;images&#x2F;content&#x2F;complexity-trap-slides&#x2F;complexity-trap-slide-20.png&quot;&gt;
&lt;p&gt;There is an alternative that seems to be almost taboo, which is to not abstract over your effect monad at all in your application code. If you&#x27;re honest, when testing with the &lt;code&gt;Id&lt;&#x2F;code&gt; monad, you&#x27;re only testing the few pieces of application logic within your big effectful function and you&#x27;re verifying that your test interpreter is working as expected. But you have no clue if your production interpreter, which is talking to an external system, is working correctly at all. So abstracting over the effect monad and using the &lt;code&gt;Id&lt;&#x2F;code&gt; monad in tests doesn&#x27;t even help you to write meaningful tests. If you forego this abstraction, you will be able to use your effect type in all its glory. Among other things, this makes stuff like parallel execution very straightforward.&lt;&#x2F;p&gt;
&lt;div style=&quot;clear:both&quot;&gt;&lt;&#x2F;div&gt;
&lt;img width=&quot;720&quot; height=&quot;540&quot; loading=&quot;lazy&quot; src=&quot;&#x2F;images&#x2F;content&#x2F;complexity-trap-slides&#x2F;complexity-trap-slide-21.png&quot;&gt;
&lt;p&gt;If you follow this approach, you have to be very strict about separating the logic of your program from the parts involving IO or other effects. One way of doing this is to employ a &lt;em&gt;Functional Core, Imperative Shell&lt;&#x2F;em&gt; architecture. What this means is that you&#x27;ll move all the logic into the purely functional core. If necessary, you will have to extract it from your big effectful functions. This way, you can minimize the branching happening in the imperative shell and, since you can use the whole functionality of your favourite effect type there.&lt;&#x2F;p&gt;
&lt;div style=&quot;clear:both&quot;&gt;&lt;&#x2F;div&gt;
&lt;img width=&quot;720&quot; height=&quot;540&quot; loading=&quot;lazy&quot; src=&quot;&#x2F;images&#x2F;content&#x2F;complexity-trap-slides&#x2F;complexity-trap-slide-22.png&quot;&gt;
&lt;p&gt;With all the logic being implemented in rather small, pure, and effect-free functions, it becomes a lot easier to write unit tests for your application&#x27;s logic. The imperative shell will be almost free of logic, so that it can be covered with only a small number of integration tests. This does a better job at testing what is happening in a production-like environment. Free monads or tagless final are not a great choice if you only care about testability. There are alternatives that are less complex and at least as adequate for achieveing this goal.&lt;&#x2F;p&gt;
&lt;div style=&quot;clear:both&quot;&gt;&lt;&#x2F;div&gt;
&lt;img width=&quot;720&quot; height=&quot;540&quot; loading=&quot;lazy&quot; src=&quot;&#x2F;images&#x2F;content&#x2F;complexity-trap-slides&#x2F;complexity-trap-slide-23.png&quot;&gt;
&lt;p&gt;So why is it that people jump to certain solutions so quickly? I have the impression that the discourse in our community is characterized by a focus on &lt;em&gt;how&lt;&#x2F;em&gt; to use certain abstractions. At best, we find discussions of when to use one abstraction over another. But we rarely see discussions about when not using any of those abstractions might be a viable solution.&lt;&#x2F;p&gt;
&lt;div style=&quot;clear:both&quot;&gt;&lt;&#x2F;div&gt;
&lt;img width=&quot;720&quot; height=&quot;540&quot; loading=&quot;lazy&quot; src=&quot;&#x2F;images&#x2F;content&#x2F;complexity-trap-slides&#x2F;complexity-trap-slide-24.png&quot;&gt;
&lt;p&gt;I think we should all talk and write more about the costs of certain abstractions and have critical discussions of the trade-offs we need to make. Abstractions are supposed to help us keep complexity in check, but using them can come with its own complexity as well. Usually, a given abstraction is a very good fit for a few particular use cases. However, there are often alternative solutions that are more adequate for other use cases. These solutions are often both less complex and more boring.&lt;&#x2F;p&gt;
&lt;div style=&quot;clear:both&quot;&gt;&lt;&#x2F;div&gt;
&lt;img width=&quot;720&quot; height=&quot;540&quot; loading=&quot;lazy&quot; src=&quot;&#x2F;images&#x2F;content&#x2F;complexity-trap-slides&#x2F;complexity-trap-slide-25.png&quot;&gt;
&lt;p&gt;Unfortunately, we don&#x27;t like boring. We like to take the language and the compiler to their limits. We also like to adopt the latest fad. But the software systems we work on in our jobs should not be a playground or a test bed for the latest fad. So let&#x27;s talk and write more about when certain abstractions are a good fit and when they are not, and about when a more boring solution might be a better fit.&lt;&#x2F;p&gt;
&lt;p&gt;And when someone in your team tells you, &quot;This problem must be solved with free monads&quot;, don&#x27;t be afraid to ask why. In general, make a habit of demanding to hear why and approach was chosen, which alternatives have been considered, and why they&#x27;ve been discarded. A great technique for documenting these decisions for future reference is to write Architecture Decision Records (ADR).&lt;&#x2F;p&gt;
&lt;div style=&quot;clear:both&quot;&gt;&lt;&#x2F;div&gt;
&lt;img width=&quot;720&quot; height=&quot;540&quot; loading=&quot;lazy&quot; src=&quot;&#x2F;images&#x2F;content&#x2F;complexity-trap-slides&#x2F;complexity-trap-slide-26.png&quot;&gt;
&lt;p&gt;The last aspect of the complexity trap that I want to discuss is what I call &lt;em&gt;maximum zoom&lt;&#x2F;em&gt;. Have you ever used one of those coin-operated telescopes, or binoculars, in order to zoom in to some detail of a scenic view? Maybe you have also experienced how easy it is to lose your bearings and how difficult it can be to keep track of the broader context. I think the same thing happens a lot in software engineering.&lt;&#x2F;p&gt;
&lt;div style=&quot;clear:both&quot;&gt;&lt;&#x2F;div&gt;
&lt;img width=&quot;720&quot; height=&quot;540&quot; loading=&quot;lazy&quot; src=&quot;&#x2F;images&#x2F;content&#x2F;complexity-trap-slides&#x2F;complexity-trap-slide-27.png&quot;&gt;
&lt;p&gt;Let me explain this with an example. These days, many teams face the challenge that the system they are working on needs to make a lot of requests to various other systems. Very often, this happens in a Netflix-style microservice architecture, where you have tons of tiny microservices and another service in front of those, orchestrating the others, aggregating their response data and doing some additional processing, for example filtering. One particular pattern in which such an orchestration service occurs is &lt;a href=&quot;https:&#x2F;&#x2F;samnewman.io&#x2F;patterns&#x2F;architectural&#x2F;bff&#x2F;&quot;&gt;BFF (backend for frontend)&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;Very often, having to make all these requests to multiple backend microservices leads to high response times for the end user, which is then mitigated by introducing batching and caching of previous results, and also by making requests that are independent from one another in parallel. Imagine we&#x27;d be taking care of all these different concerns in the same place. That would certainly not be simple, as in &quot;having one ingredient&quot; — which is exactly why there are libraries that provide abstractions over these concerns, for example Stitch, Clump, or Fetch.&lt;&#x2F;p&gt;
&lt;div style=&quot;clear:both&quot;&gt;&lt;&#x2F;div&gt;
&lt;img width=&quot;720&quot; height=&quot;540&quot; loading=&quot;lazy&quot; src=&quot;&#x2F;images&#x2F;content&#x2F;complexity-trap-slides&#x2F;complexity-trap-slide-28.png&quot;&gt;
&lt;p&gt;Keep in mind that I don&#x27;t know anything about the context in which these libraries were created. For all I know, following that approach may have been the only viable solution to the problem the developers were facing in their specific situation, after careful analysis. Nevertheless, in my experience from other projects, the motivation to build abstractions like these is often to solve problems of a Netflix-style microservice architecture as the one I outlined above. As such, it serves as a nice example of the phenomenon that we often implement technological solutions to problems prematurely. I think that the reason why we see this phenomenon is that we don&#x27;t apply systematic approaches at improving our software systems often enough.&lt;&#x2F;p&gt;
&lt;div style=&quot;clear:both&quot;&gt;&lt;&#x2F;div&gt;
&lt;img width=&quot;720&quot; height=&quot;540&quot; loading=&quot;lazy&quot; src=&quot;&#x2F;images&#x2F;content&#x2F;complexity-trap-slides&#x2F;complexity-trap-slide-29.png&quot;&gt;
&lt;p&gt;Coming back to our example, it&#x27;s fair to assume that caching, batching and parallel execution of requests, all nicely abstracted away, probably works kind of okay. However, if we apply a systematic approach at improving our software, we may very well end up with a different solution. One such systematic apporach is called the &lt;a href=&quot;https:&#x2F;&#x2F;arc42.org&#x2F;&quot;&gt;Architecture Improvement Method (arc42)&lt;&#x2F;a&gt;. Using this method, you work iteratively in three phases in order to improve your software system:&lt;&#x2F;p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;em&gt;Analyse&lt;&#x2F;em&gt; in order to identify issues and improvements&lt;&#x2F;li&gt;
&lt;li&gt;&lt;em&gt;Evaluate&lt;&#x2F;em&gt; in order to estimate the value of issues and the effort or cost associated with improvements&lt;&#x2F;li&gt;
&lt;li&gt;&lt;em&gt;Improve&lt;&#x2F;em&gt; by applying selected improvements&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;div style=&quot;clear:both&quot;&gt;&lt;&#x2F;div&gt;
&lt;img width=&quot;720&quot; height=&quot;540&quot; loading=&quot;lazy&quot; src=&quot;&#x2F;images&#x2F;content&#x2F;complexity-trap-slides&#x2F;complexity-trap-slide-30.png&quot;&gt;
&lt;p&gt;One of the most important techniques in the analysis phase is &lt;em&gt;root cause analysis&lt;&#x2F;em&gt;. Unfortunately, it&#x27;s also a tremendously underused technique. In my experience, people often jump to solutions too quickly. When we face a problem, we tend to immediately search for a solution for exactly this problem. Root cause analysis differentiates between problem and cause. The idea is to trace a problem to its origin and to identify root causes of symptoms or problems. When doing a root cause analysis, you continue to ask why until you have found the root cause in the causal chain.&lt;&#x2F;p&gt;
&lt;div style=&quot;clear:both&quot;&gt;&lt;&#x2F;div&gt;
&lt;img width=&quot;720&quot; height=&quot;540&quot; loading=&quot;lazy&quot; src=&quot;&#x2F;images&#x2F;content&#x2F;complexity-trap-slides&#x2F;complexity-trap-slide-31.png&quot;&gt;
&lt;p&gt;Let&#x27;s apply this to our high latency problem from our example. We could ask ourselves why our latency is so high and get to the answer that it&#x27;s because of all the requests we need to make in our BFF. But why do we need to make so many requests? The answer could be that it&#x27;s because we need to fetch data from many different services. And why do we need to do that? Maybe it&#x27;s because all our services are designed around a single entity. And why is that?&lt;&#x2F;p&gt;
&lt;p&gt;At some point, we realize that we have an architectural problem at the macro level. Maybe, if we continue to ask why long enough, we will come to the conclusion that all these services we need to communicate with exist in exactly this way because of how the company is organized.&lt;&#x2F;p&gt;
&lt;div style=&quot;clear:both&quot;&gt;&lt;&#x2F;div&gt;
&lt;img width=&quot;720&quot; height=&quot;540&quot; loading=&quot;lazy&quot; src=&quot;&#x2F;images&#x2F;content&#x2F;complexity-trap-slides&#x2F;complexity-trap-slide-32.png&quot;&gt;
&lt;p&gt;In any case, high latency of our BFF is only a symptom, and the root cause, the thing we should try to solve, lies a lot deeper. By making the symptom less painful, we don&#x27;t reduce complexity in the system as a whole. Think of all the complexity that could be eliminated if we changed our architecture at the macro level — for example, if instead of a service in front of all those tiny microservices, we had a few slightly bigger systems that are not just built around a single entity or aggregate, but a whole &lt;em&gt;Bounded Context&lt;&#x2F;em&gt;. The whole orchestration and aggregation layer would be gone, and browsers would talk to those bigger systems directly, because they are entirely &lt;a href=&quot;https:&#x2F;&#x2F;scs-architecture.org&#x2F;&quot;&gt;self-contained&lt;&#x2F;a&gt;, including their own UI.&lt;&#x2F;p&gt;
&lt;div style=&quot;clear:both&quot;&gt;&lt;&#x2F;div&gt;
&lt;img width=&quot;720&quot; height=&quot;540&quot; loading=&quot;lazy&quot; src=&quot;&#x2F;images&#x2F;content&#x2F;complexity-trap-slides&#x2F;complexity-trap-slide-33.png&quot;&gt;
&lt;p&gt;But the maximum zoop trap also applies to our issue with the JSON codecs. If we do a root cause analysis here, at some point, we may ask ourselves why we have a JSON API in the first place. Often, the sole consumer of such an API is a single page application (SPA) running in the browser. While there are perfectly valid use cases for SPAs, they are often chosen merely because it&#x27;s &quot;industry standard&quot; to use them these days — which brings us back to the trap of embracing industry standards.&lt;&#x2F;p&gt;
&lt;p&gt;Moreover, we are programmers, and we like to write code. Having to build an SPA allows us to write even more code. It&#x27;s a new playground for doing functional programming — applicative functors in the browser, how amazing is that?&lt;&#x2F;p&gt;
&lt;div style=&quot;clear:both&quot;&gt;&lt;&#x2F;div&gt;
&lt;img width=&quot;720&quot; height=&quot;540&quot; loading=&quot;lazy&quot; src=&quot;&#x2F;images&#x2F;content&#x2F;complexity-trap-slides&#x2F;complexity-trap-slide-34.png&quot;&gt;
&lt;p&gt;But imagine the unthinkable for a minute: What if we did server-side rendering of HTML instead? There wouldn&#x27;t be any need to design an evolvable JSON API, which, unlike simply exposing your domain model, is not trivial, and there would definitely be no need for defining any JSON codecs. And that&#x27;s just what you get on the server side. On the frontend side, you would have one piece of software less to maintain, you&#x27;d get faster load times and you could get rid of duplicated business logic. To sum it up, you could reduce the complexity of your frontend JavaScript code a lot.&lt;&#x2F;p&gt;
&lt;p&gt;In the words of Jorge Manrubia, who wrote &lt;a href=&quot;https:&#x2F;&#x2F;medium.com&#x2F;@jmanrubia&#x2F;escaping-the-spa-rabbit-hole-with-turbolinks-903f942bf52c&quot;&gt;a great article&lt;&#x2F;a&gt; about how the complexity introduced by single-page applications can often be entirely avoided: &quot;[..] what about if, instead of trying to find a solution, you put yourself in a situation where those problems don’t exist?&quot; Eliminating problems altogether is always better than writing code to solve them, regardless of how purely functional that could is.&lt;&#x2F;p&gt;
&lt;div style=&quot;clear:both&quot;&gt;&lt;&#x2F;div&gt;
&lt;img width=&quot;720&quot; height=&quot;540&quot; loading=&quot;lazy&quot; src=&quot;&#x2F;images&#x2F;content&#x2F;complexity-trap-slides&#x2F;complexity-trap-slide-35.png&quot;&gt;
&lt;p&gt;But why is it that we fall for technological solutions so often, especially ones that don&#x27;t reduce complexity at all? I think that maybe we love progrmaming too much. I was recently talking to someone about Rust, and that person said, &quot;A language without higher-kinded types is completely useless!&quot;. Yes, certain abstractions are not possible without them, but I have never seen a project fail due to a lack of higher-kinded types. Projects fail because the problem domain is not well understood, or because the architecture is not a good fit for the requirements. They also fail because of how the organisation is structured and because of politics, be it intra- or inter-organizational. Projects fail because of people, and because of people not talking to each other.&lt;&#x2F;p&gt;
&lt;div style=&quot;clear:both&quot;&gt;&lt;&#x2F;div&gt;
&lt;img width=&quot;720&quot; height=&quot;540&quot; loading=&quot;lazy&quot; src=&quot;&#x2F;images&#x2F;content&#x2F;complexity-trap-slides&#x2F;complexity-trap-slide-36.png&quot;&gt;
&lt;p&gt;When we jump towards technological solutions, we often do this because we love programming. In our case, we love functional programming. We also love technological challenges. Solving a problem by writing code is our comfort zone, so we take this route even if the actual problem is not a technological one. We prefer to work around non-technical problems using technological solutions because that&#x27;s what we know, and it&#x27;s the more interesting way out.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;conclusions&quot;&gt;Conclusions&lt;&#x2F;h2&gt;
&lt;div style=&quot;clear:both&quot;&gt;&lt;&#x2F;div&gt;
&lt;img width=&quot;720&quot; height=&quot;540&quot; loading=&quot;lazy&quot; src=&quot;&#x2F;images&#x2F;content&#x2F;complexity-trap-slides&#x2F;complexity-trap-slide-37.png&quot;&gt;
&lt;p&gt;I work at a consultancy, and we are often hired to solve challenging technological problems. Often, it turns out that people only think that their challenges are about technology, but the important problems are not technological at all. I believe that as developers, our job is to solve problems and create value for the business and the users of the product we build. Our job is not to write code. Sometimes, doing our job means that we have to search for the root cause of a problem. Instead of solving a symptom by writing code, we have to tackle communication problems, deal with organizational issues, or improve the architecture of our system at the macro level.&lt;&#x2F;p&gt;
&lt;p&gt;I think that jumping towards technological solutions for symptoms also happens because everything that is not solvable with code is often seen as a given, something you can&#x27;t change anyway. I think it&#x27;s worth giving it a try more often, whether you are a consultant or not. Things are not always set in stone. And if we are serious about reducing accidental complexity, we should question our self-concept, our idea of what it means to be a software developer.&lt;&#x2F;p&gt;
&lt;div style=&quot;clear:both&quot;&gt;&lt;&#x2F;div&gt;
&lt;img width=&quot;720&quot; height=&quot;540&quot; loading=&quot;lazy&quot; src=&quot;&#x2F;images&#x2F;content&#x2F;complexity-trap-slides&#x2F;complexity-trap-slide-38.png&quot;&gt;
&lt;p&gt;To summarize, we have to deal with a lot of accidental complexity these days, for multiple reasons. First of all, we adopt techniques and abstractions without analying and evaluating their costs and their usefulness in our specific situation. Moreover, we love solving technological problems.&lt;&#x2F;p&gt;
&lt;p&gt;So let&#x27;s shift our discourse and talk more about when certain abstractions are a good fit and when they aren&#x27;t, and let&#x27;s bring more boring but possibly simpler solutions to the table. Also, don&#x27;t be afraid to demand explanations for architectural decisions.&lt;&#x2F;p&gt;
&lt;p&gt;Finally, let&#x27;s slow down when facing a problem. Let&#x27;s take the time to analyze its root cause and let&#x27;s jump out of our comfort zone, if necessary — even if it means that we will eliminate the problem and rob ourselved of a chance to solve it.&lt;&#x2F;p&gt;
&lt;img width=&quot;720&quot; height=&quot;540&quot; loading=&quot;lazy&quot; src=&quot;&#x2F;images&#x2F;content&#x2F;complexity-trap-slides&#x2F;complexity-trap-slide-39.png&quot;&gt;
&lt;div style=&quot;clear:both&quot;&gt;&lt;&#x2F;div&gt;
&lt;h2 id=&quot;credits&quot;&gt;Credits&lt;&#x2F;h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;https:&#x2F;&#x2F;scs-architecture.org&quot;&gt;self-contained systems diagram&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
&lt;li&gt;&lt;a href=&quot;https:&#x2F;&#x2F;unsplash.com&#x2F;photos&#x2F;IO4wIzn4wEo&quot;&gt;Photo by Domenico Gentile on Unsplash&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
&lt;li&gt;&lt;a href=&quot;https:&#x2F;&#x2F;unsplash.com&#x2F;photos&#x2F;Yvaej69Nuyw&quot;&gt;Photo by drmakete lab on Unsplash&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
&lt;li&gt;&lt;a href=&quot;https:&#x2F;&#x2F;unsplash.com&#x2F;photos&#x2F;OgaY0k2K6TM&quot;&gt;Photo by Johny Goerend on Unsplash&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
&lt;li&gt;&lt;a href=&quot;https:&#x2F;&#x2F;unsplash.com&#x2F;photos&#x2F;umyBR1m5MRQ&quot;&gt;Photo by juan pablo rodriguez on Unsplash&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
&lt;li&gt;&lt;a href=&quot;https:&#x2F;&#x2F;unsplash.com&#x2F;photos&#x2F;DY2miYwMchk&quot;&gt;Photo by Mario Azzi on Unsplash&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
&lt;li&gt;&lt;a href=&quot;https:&#x2F;&#x2F;unsplash.com&#x2F;photos&#x2F;hN63jauq1cI&quot;&gt;Photo by Marissa Rodriguez on Unsplash&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
&lt;li&gt;&lt;a href=&quot;https:&#x2F;&#x2F;unsplash.com&#x2F;photos&#x2F;NrtC3y108Ys&quot;&gt;Photo by Verena Yunita Yapi on Unsplash&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
&lt;li&gt;&lt;a href=&quot;https:&#x2F;&#x2F;unsplash.com&#x2F;photos&#x2F;vx5WFJoi3XY&quot;&gt;Photo by Samuel Zeller on Unsplash&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
&lt;li&gt;&lt;a href=&quot;https:&#x2F;&#x2F;unsplash.com&#x2F;photos&#x2F;SSAbwzqz2Kc&quot;&gt;Photo by Markus Spiske on Unsplash&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
&lt;li&gt;&lt;a href=&quot;https:&#x2F;&#x2F;unsplash.com&#x2F;photos&#x2F;ZLc9yTIFzNk&quot;&gt;Photo by Kelly Sikkema on Unsplash&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
&lt;li&gt;&lt;a href=&quot;https:&#x2F;&#x2F;unsplash.com&#x2F;photos&#x2F;DT9d0RzDfe4&quot;&gt;Photo by Jaciel Melnik on Unsplash&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
&lt;li&gt;&lt;a href=&quot;https:&#x2F;&#x2F;unsplash.com&#x2F;photos&#x2F;OqWaJrlJBNQ&quot;&gt;Photo by Shawn McKay on Unsplash&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
&lt;li&gt;&lt;a href=&quot;https:&#x2F;&#x2F;unsplash.com&#x2F;photos&#x2F;_Sr03VSKIeg&quot;&gt;Photo by Drew Graham on Unsplash&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
&lt;li&gt;&lt;a href=&quot;https:&#x2F;&#x2F;unsplash.com&#x2F;photos&#x2F;x-LqkefxFoQ&quot;&gt;Photo by Marcos Gabarda on Unsplash&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
&lt;li&gt;&lt;a href=&quot;https:&#x2F;&#x2F;unsplash.com&#x2F;photos&#x2F;p0FTPPhgZdQ&quot;&gt;Photo by Kylli Kittus on Unsplash&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
</description>
      </item>
      <item>
          <title>Introducing kontextfrei</title>
          <pubDate>Tue, 31 Oct 2017 00:00:00 +0000</pubDate>
          <author>Unknown</author>
          <link>https://danielwestheide.com/blog/introducing-kontextfrei/</link>
          <guid>https://danielwestheide.com/blog/introducing-kontextfrei/</guid>
          <description xml:base="https://danielwestheide.com/blog/introducing-kontextfrei/">&lt;p&gt;For the past 15 months, I have been working on a new library on and off. So far, I have been mostly silent about it, because I didn&#x27;t feel like it was ready for a wider audience to use – even though we had been using it successfully in production for a while. However, since I broke my silence as long ago as April this year, when I did a talk about it at this year&#x27;s ScalarConf in Warsaw, a blog post is overdue in which I explain what this library does and why I set out to write it in the first place.&lt;&#x2F;p&gt;
&lt;p&gt;Last year, I was involved in a project that required my team to implement a few Spark applications. For most of them, the business logic was rather complex, so we tried to implement this business logic in a test-driven way, using property-driven tests.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;the-pain-of-unit-testing-spark-applications&quot;&gt;The pain of unit-testing Spark applications&lt;&#x2F;h2&gt;
&lt;p&gt;At first glance, it looks like this is a great match. When it comes down to it, a Spark application consists of IO stages (reading from and writing to data sources) and transformations of data sets. The latter constitute our business logic and are relatively easy to separate from the IO parts. They are mostly built from pure functions. Functions like these are usually a perfect fit for test-driven development as well as for property-based testing.&lt;&#x2F;p&gt;
&lt;p&gt;However, all was not great. It may be old news to you if you have been working with Apache Spark for a while, but it turns out that writing real unit tests is not actually supported that well by Spark, and as a result, it can be quite painful. The thing is that in order to create an &lt;code&gt;RDD&lt;&#x2F;code&gt;, we always need a &lt;code&gt;SparkContext&lt;&#x2F;code&gt;, and the most light-weight mechanism for getting one is to create a local &lt;code&gt;SparkContext&lt;&#x2F;code&gt;. Creating a local &lt;code&gt;SparkContext&lt;&#x2F;code&gt; means that we start up a server, which takes a few seconds, and testing our properties with lots of different generated input data takes a really long time. Most certainly, we are losing the fast feedback loop we are used to from developing web applications, for example.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;abstracting-over-rdds-with-kontextfrei&quot;&gt;Abstracting over RDDs with kontextfrei&lt;&#x2F;h2&gt;
&lt;p&gt;Now, we could confine ourselves to only unit-testing the functions that we pass to &lt;code&gt;RDD&lt;&#x2F;code&gt; operators, so that our unit tests do not have any dependency on Spark and can be verified as quickly as we are used to. However, this leaves quite a lot of business logic uncovered. Instead, at a Scala hackathon last May, I started to experiment with the idea of abstracting over Spark&#x27;s &lt;code&gt;RDD&lt;&#x2F;code&gt;, and &lt;em&gt;kontextfrei&lt;&#x2F;em&gt; was born.&lt;&#x2F;p&gt;
&lt;p&gt;The idea is the following: By abstracting over &lt;code&gt;RDD&lt;&#x2F;code&gt;, we can write business logic that has no dependency on the &lt;code&gt;RDD&lt;&#x2F;code&gt; type. This means that we can also write test properties that are Spark-agnostic. Any Spark-agnostic code like this can either be executed on an &lt;code&gt;RDD&lt;&#x2F;code&gt; (which you would do in your actual Spark application and in your integration tests), or on a local and fast Scala collection (which is really great for unit tests that you continously run locally during development).&lt;&#x2F;p&gt;
&lt;h2 id=&quot;obtaining-the-library&quot;&gt;Obtaining the library&lt;&#x2F;h2&gt;
&lt;p&gt;It&#x27;s probably easier to show how this works than to describe it with words alone, so let&#x27;s look at a really minimalistic example, the traditional &lt;em&gt;word count&lt;&#x2F;em&gt;. First, we need to add the necessary dependencies to our SBT build file. Kontextfrei consists of two different modules, &lt;code&gt;kontextfrei-core&lt;&#x2F;code&gt; and &lt;code&gt;kontextfrei-scalatest&lt;&#x2F;code&gt;. The former is what you need to abstract over &lt;code&gt;RDD&lt;&#x2F;code&gt; in your main code base, the former to get some additional support for writing your RDD-independent tests using ScalaTest with ScalaCheck. Let&#x27;s add them to our &lt;code&gt;build.sbt&lt;&#x2F;code&gt; file, together with the usual
Spark dependency you would need anyway:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;scala&quot; style=&quot;background-color:#002b36;color:#839496;&quot; class=&quot;language-scala &quot;&gt;&lt;code class=&quot;language-scala&quot; data-lang=&quot;scala&quot;&gt;&lt;span&gt;resolvers += &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;dwestheide&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; at &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;https:&#x2F;&#x2F;dl.bintray.com&#x2F;dwestheide&#x2F;maven&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;libraryDependencies += &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;com.danielwestheide&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; %% &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;kontextfrei-core-spark-2.2.0&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; % &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;0.6.0&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;libraryDependencies += &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;com.danielwestheide&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; %% &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;kontextfrei-scalatest-spark-2.2.0&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; % &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;0.6.0&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; % &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;test,it&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;libraryDependencies += &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;org.apache.spark&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; %% &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;spark-core&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; % &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;2.2.0&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Please note that in this simple example, we create a Spark application that you can execute in a self-contained way. In the real world, you would add &lt;code&gt;spark-core&lt;&#x2F;code&gt; as a &lt;code&gt;provided&lt;&#x2F;code&gt; dependency and create an assembly JAR that you pass to &lt;code&gt;spark-submit&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;implementing-the-business-logic&quot;&gt;Implementing the business logic&lt;&#x2F;h2&gt;
&lt;p&gt;Now, let&#x27;s see how we can implement the business logic of our word count application using &lt;em&gt;kontextfrei&lt;&#x2F;em&gt;. In our example, we define all of our business logic in a trait called &lt;code&gt;WordCount&lt;&#x2F;code&gt;:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;scala&quot; style=&quot;background-color:#002b36;color:#839496;&quot; class=&quot;language-scala &quot;&gt;&lt;code class=&quot;language-scala&quot; data-lang=&quot;scala&quot;&gt;&lt;span style=&quot;color:#859900;&quot;&gt;package &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;com.danielwestheide.kontextfrei.wordcount
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;import&lt;&#x2F;span&gt;&lt;span&gt; com.danielwestheide.kontextfrei.DCollectionOps
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;import&lt;&#x2F;span&gt;&lt;span&gt; com.danielwestheide.kontextfrei.syntax.SyntaxSupport
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;trait&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt; WordCount &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;extends &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;SyntaxSupport &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;counts&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;F&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d33682;&quot;&gt;_&lt;&#x2F;span&gt;&lt;span&gt;]&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;: DCollectionOps&lt;&#x2F;span&gt;&lt;span&gt;]&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;text&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;F&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;String&lt;&#x2F;span&gt;&lt;span&gt;]&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;F&lt;&#x2F;span&gt;&lt;span&gt;[(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;String&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;Long&lt;&#x2F;span&gt;&lt;span&gt;)] &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;=
&lt;&#x2F;span&gt;&lt;span&gt;    text
&lt;&#x2F;span&gt;&lt;span&gt;      .flatMap&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;line =&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; line.split&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;))
&lt;&#x2F;span&gt;&lt;span&gt;      .map&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;word =&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;word, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;L&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;))
&lt;&#x2F;span&gt;&lt;span&gt;      .reduceByKey&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d33682;&quot;&gt;_&lt;&#x2F;span&gt;&lt;span&gt; + &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d33682;&quot;&gt;_&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;      .sortBy&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d33682;&quot;&gt;_&lt;&#x2F;span&gt;&lt;span&gt;._2, ascending &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;false&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;formatted&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;F&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d33682;&quot;&gt;_&lt;&#x2F;span&gt;&lt;span&gt;]&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;: DCollectionOps&lt;&#x2F;span&gt;&lt;span&gt;]&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;counts&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;F&lt;&#x2F;span&gt;&lt;span&gt;[(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;String&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;Long&lt;&#x2F;span&gt;&lt;span&gt;)]&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;F&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;String&lt;&#x2F;span&gt;&lt;span&gt;] &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;=
&lt;&#x2F;span&gt;&lt;span&gt;    counts.map &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;      &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;case &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;word&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;count&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;=&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;s&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;$word&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;$count&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;}
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The first thing you&#x27;ll notice is that the implementations of &lt;code&gt;counts&lt;&#x2F;code&gt; and &lt;code&gt;formatted&lt;&#x2F;code&gt; look exactly the same as they would if you were programming against Spark&#x27;s &lt;code&gt;RDD&lt;&#x2F;code&gt; type. You could literally copy and paste &lt;code&gt;RDD&lt;&#x2F;code&gt;-based code into a program written with &lt;em&gt;kontextfrei&lt;&#x2F;em&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;The second thing you notice is that the method signatures of &lt;code&gt;counts&lt;&#x2F;code&gt; and &lt;code&gt;formatted&lt;&#x2F;code&gt; contain a type constructor, declared as &lt;code&gt;F[_]&lt;&#x2F;code&gt;, which is constrained by a context bound: For any concrete type constructor we pass in here, there must be an instance of kontextfrei&#x27;s &lt;code&gt;DCollectionOps&lt;&#x2F;code&gt; typeclass. In our business logic, we do not care what concrete type constructor is used for &lt;code&gt;F&lt;&#x2F;code&gt;, as long as the operations defined in &lt;code&gt;DCollectionOps&lt;&#x2F;code&gt; are supported for it. This way, we are liberating our business logic from any dependency on Spark, and specifically on the annoying &lt;code&gt;SparkContext&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;In order to be able to use the familiar syntax we know from the &lt;code&gt;RDD&lt;&#x2F;code&gt; type, we mix in kontextfrei&#x27;s &lt;code&gt;SyntaxSupport&lt;&#x2F;code&gt; trait, but you could just as well use an import instead, if that&#x27;s more to your liking.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;plugging-our-business-logic-into-the-spark-application&quot;&gt;Plugging our business logic into the Spark application&lt;&#x2F;h2&gt;
&lt;p&gt;At the end of the day, we want to be able to have a runnable Spark application. In order to achieve that, we must plug our Spark-agnostic business logic together with the Spark-dependent IO parts of our application. Here is what this looks like in our word count example:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;scala&quot; style=&quot;background-color:#002b36;color:#839496;&quot; class=&quot;language-scala &quot;&gt;&lt;code class=&quot;language-scala&quot; data-lang=&quot;scala&quot;&gt;&lt;span style=&quot;color:#859900;&quot;&gt;package &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;com.danielwestheide.kontextfrei.wordcount
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;import&lt;&#x2F;span&gt;&lt;span&gt; com.danielwestheide.kontextfrei.rdd.RDDOpsSupport
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;import&lt;&#x2F;span&gt;&lt;span&gt; org.apache.spark.SparkContext
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;object&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt; Main &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;extends &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;App &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;with &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;WordCount &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;with &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;RDDOpsSupport &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#93a1a1;&quot;&gt;implicit &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;val sparkContext&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;SparkContext &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;=
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;new SparkContext&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;local[1]&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;word-count&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;val inputFilePath &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt; args&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;val outputFilePath &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt; args&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;try &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;val textFile   &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt; sparkContext.textFile&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;inputFilePath, minPartitions &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;2&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;val wordCounts &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt; counts&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;textFile&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;    formatted&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;wordCounts&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span&gt;.saveAsTextFile&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;outputFilePath&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;} &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;finally &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;    sparkContext.stop&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;()
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;}
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Our &lt;code&gt;Main&lt;&#x2F;code&gt; object mixes in our &lt;code&gt;WordCount&lt;&#x2F;code&gt; trait as well as &lt;em&gt;kontextfrei&lt;&#x2F;em&gt;&#x27;s &lt;code&gt;RDDOpsSupport&lt;&#x2F;code&gt;, which proves to the compiler that we have an instance of the &lt;code&gt;DCollectionOps&lt;&#x2F;code&gt; typeclass for the &lt;code&gt;RDD&lt;&#x2F;code&gt; type constructor. In order to prove this, we also need an implicit &lt;code&gt;SparkContext&lt;&#x2F;code&gt;. Again, instead of mixing in this trait, we can also use an import.&lt;&#x2F;p&gt;
&lt;p&gt;Now, our &lt;code&gt;Main&lt;&#x2F;code&gt; object is all about doing some IO and integrating our business logi into it.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;writing-spark-agnostic-tests&quot;&gt;Writing Spark-agnostic tests&lt;&#x2F;h2&gt;
&lt;p&gt;So far so good. We have liberated our business logic from any dependency on Spark, but what do we gain from this? Well, now we are able to write our unit tests in a Spark-agnostic way as well. First, we define a &lt;code&gt;BaseSpec&lt;&#x2F;code&gt; which inherits from kontextfrei&#x27;s &lt;code&gt;KontextfreiSpec&lt;&#x2F;code&gt; and mixes in a few other goodies from &lt;em&gt;kontextfrei-scalatest&lt;&#x2F;em&gt; and from ScalaTest itself:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;scala&quot; style=&quot;background-color:#002b36;color:#839496;&quot; class=&quot;language-scala &quot;&gt;&lt;code class=&quot;language-scala&quot; data-lang=&quot;scala&quot;&gt;&lt;span style=&quot;color:#859900;&quot;&gt;package &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;com.danielwestheide.kontextfrei.wordcount
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;import&lt;&#x2F;span&gt;&lt;span&gt; com.danielwestheide.kontextfrei.scalatest.KontextfreiSpec
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;import&lt;&#x2F;span&gt;&lt;span&gt; com.danielwestheide.kontextfrei.syntax.DistributionSyntaxSupport
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;import&lt;&#x2F;span&gt;&lt;span&gt; org.scalactic.anyvals.PosInt
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;import&lt;&#x2F;span&gt;&lt;span&gt; org.scalatest.prop.GeneratorDrivenPropertyChecks
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;import&lt;&#x2F;span&gt;&lt;span&gt; org.scalatest.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{&lt;&#x2F;span&gt;&lt;span&gt;MustMatchers, PropSpecLike&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;}
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;trait&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt; BaseSpec&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;F&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d33682;&quot;&gt;_&lt;&#x2F;span&gt;&lt;span&gt;]]
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;extends &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;KontextfreiSpec&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;F&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;with &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;DistributionSyntaxSupport
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;with &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;PropSpecLike
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;with &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;GeneratorDrivenPropertyChecks
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;with &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;MustMatchers &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#93a1a1;&quot;&gt;implicit &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;val config&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;PropertyCheckConfiguration &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;=
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;PropertyCheckConfiguration&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;minSuccessful &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;PosInt&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;100&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;))
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;&lt;code&gt;BaseSpec&lt;&#x2F;code&gt;, like our &lt;code&gt;WordCount&lt;&#x2F;code&gt; trait, takes a type constructor, which it simply passes along to the &lt;code&gt;KontextfreiSpec&lt;&#x2F;code&gt; trait. We will get back to that one in a minute.&lt;&#x2F;p&gt;
&lt;p&gt;Our actual test properties can now be implemented for any type constructor &lt;code&gt;F[_]&lt;&#x2F;code&gt; for which there is an instance of &lt;code&gt;DCollectionOps&lt;&#x2F;code&gt;. We define them in a trait &lt;code&gt;WordCountProperties&lt;&#x2F;code&gt;, which also has to be parameterized by a type constructor:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;scala&quot; style=&quot;background-color:#002b36;color:#839496;&quot; class=&quot;language-scala &quot;&gt;&lt;code class=&quot;language-scala&quot; data-lang=&quot;scala&quot;&gt;&lt;span style=&quot;color:#859900;&quot;&gt;package &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;com.danielwestheide.kontextfrei.wordcount
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;trait&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt; WordCountProperties&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;F&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d33682;&quot;&gt;_&lt;&#x2F;span&gt;&lt;span&gt;]] &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;extends &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;BaseSpec&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;F&lt;&#x2F;span&gt;&lt;span&gt;] &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;with &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;WordCount &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;import&lt;&#x2F;span&gt;&lt;span&gt; collection.immutable.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d33682;&quot;&gt;_
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;  property&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;sums word counts across lines&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;) {
&lt;&#x2F;span&gt;&lt;span&gt;    forAll &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{ (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;wordA&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;String&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;=&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;      whenever&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;wordA.nonEmpty&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;) {
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;val wordB &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt; wordA.reverse + wordA
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;val result &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;=
&lt;&#x2F;span&gt;&lt;span&gt;          counts&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Seq&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;s&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;$wordB $wordA $wordB&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, wordB&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span&gt;.distributed&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;            .collectAsMap&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;()
&lt;&#x2F;span&gt;&lt;span&gt;        assert&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;result&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;wordB&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span&gt; === &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;3&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;      &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;}
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;}
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;}
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;  property&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;does not have duplicate keys&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;) {
&lt;&#x2F;span&gt;&lt;span&gt;    forAll &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{ (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;wordA&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;String&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;=&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;      whenever&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;wordA.nonEmpty&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;) {
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;val wordB &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt; wordA.reverse + wordA
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;val result &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;=
&lt;&#x2F;span&gt;&lt;span&gt;          counts&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Seq&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;s&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;$wordA $wordB&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;s&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;$wordB $wordA&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span&gt;.distributed&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;        assert&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(
&lt;&#x2F;span&gt;&lt;span&gt;          result.keys.distinct&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;()&lt;&#x2F;span&gt;&lt;span&gt;.collect&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;()&lt;&#x2F;span&gt;&lt;span&gt;.toList === result.keys
&lt;&#x2F;span&gt;&lt;span&gt;            .collect&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;()
&lt;&#x2F;span&gt;&lt;span&gt;            .toList&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;      &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;}
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;}
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;}
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;We want to be able to test our Spark-agnostic properties both against fast Scala collections as well as against &lt;code&gt;RDD&lt;&#x2F;code&gt;s in a local Spark cluster. To get there, we will need to define two test classes, one in the &lt;code&gt;test&lt;&#x2F;code&gt; sources directory, the other one in the &lt;code&gt;it&lt;&#x2F;code&gt; sources directory. Here is the unit test:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;scala&quot; style=&quot;background-color:#002b36;color:#839496;&quot; class=&quot;language-scala &quot;&gt;&lt;code class=&quot;language-scala&quot; data-lang=&quot;scala&quot;&gt;&lt;span style=&quot;color:#859900;&quot;&gt;package &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;com.danielwestheide.kontextfrei.wordcount
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;import&lt;&#x2F;span&gt;&lt;span&gt; com.danielwestheide.kontextfrei.scalatest.StreamSpec
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;class&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt; WordCountSpec &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;extends &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;BaseSpec&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Stream&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;with &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;StreamSpec
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;with &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;WordCountProperties&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Stream&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;We mix in &lt;code&gt;BaseSpec&lt;&#x2F;code&gt; and pass it the &lt;code&gt;Stream&lt;&#x2F;code&gt; type constructor. &lt;code&gt;Stream&lt;&#x2F;code&gt; has the same shape as &lt;code&gt;RDD&lt;&#x2F;code&gt;, but it is a Scala collection. The &lt;code&gt;KontextfreiSpec&lt;&#x2F;code&gt; trait extended by &lt;code&gt;BaseSpec&lt;&#x2F;code&gt; defines an abstract implicit &lt;code&gt;DCollectionOps&lt;&#x2F;code&gt; for its type constructor. By mixing in &lt;code&gt;StreamSpec&lt;&#x2F;code&gt;, we get an instance of &lt;code&gt;DCollectionOps&lt;&#x2F;code&gt; for &lt;code&gt;Stream&lt;&#x2F;code&gt;. When we implement our business logic, we can run the &lt;code&gt;WordCountSpec&lt;&#x2F;code&gt; test and get instantaneous feedback. We can use SBT&#x27;s triggered execution and have it run our unit tests upon every detected source change, using &lt;code&gt;~test&lt;&#x2F;code&gt;, and it will be really fast.&lt;&#x2F;p&gt;
&lt;p&gt;In order to make sure that none of the typical bugs that you would only notice in a Spark cluster have sneaked in, we also define an integration test, which tests exactly the same properties:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;scala&quot; style=&quot;background-color:#002b36;color:#839496;&quot; class=&quot;language-scala &quot;&gt;&lt;code class=&quot;language-scala&quot; data-lang=&quot;scala&quot;&gt;&lt;span style=&quot;color:#859900;&quot;&gt;package &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;com.danielwestheide.kontextfrei.wordcount
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;import&lt;&#x2F;span&gt;&lt;span&gt; com.danielwestheide.kontextfrei.scalatest.RDDSpec
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;import&lt;&#x2F;span&gt;&lt;span&gt; org.apache.spark.rdd.RDD
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;class&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt; WordCountIntegrationSpec &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;extends &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;BaseSpec&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;RDD&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;with &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;RDDSpec
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;with &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;WordCountProperties&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;RDD&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This time, we mix in &lt;code&gt;RDDSpec&lt;&#x2F;code&gt; because we pass parameterize &lt;code&gt;BaseSpec&lt;&#x2F;code&gt; with the &lt;code&gt;RDD&lt;&#x2F;code&gt; type constructor.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;design-goals&quot;&gt;Design goals&lt;&#x2F;h2&gt;
&lt;p&gt;It was an explicit design goal to stick to the existing Spark API as closely as possible, allowing people with existing Spark code bases to switch to &lt;em&gt;kontextfrei&lt;&#x2F;em&gt; as smoothly as possible, or even to migrate parts of their application without too much hassle, with the benefit of now being able to cover their business logic with missing tests without the usual pain.&lt;&#x2F;p&gt;
&lt;p&gt;An alternative to this, of course, would have been to build this library based on the ever popular interpreter pattern. To be honest, I wish Spark itself was using this pattern – other libraries like Apache Crunch have shown successfully that this can help tremendously with enabling developers to write tests for the business logic of their applications. If Spark was built on those very principles, there wouldn&#x27;t ne any reason for &lt;em&gt;kontextfrei&lt;&#x2F;em&gt; to exist at all.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;limitations&quot;&gt;Limitations&lt;&#x2F;h2&gt;
&lt;p&gt;&lt;em&gt;kontextfrei&lt;&#x2F;em&gt; is still a young library, and while we have been using it in production in one project, I do not know of any other adopters. One if its limitations is that it doesn&#x27;t yet support all operations defined on the &lt;code&gt;RDD&lt;&#x2F;code&gt; type – but we are getting closer. In addition, I have yet to find a clever way to support broadcast variables and accumulators. And of course, who is using &lt;code&gt;RDD&lt;&#x2F;code&gt;s anyway in 2017? While I do think that there is still room for &lt;code&gt;RDD&lt;&#x2F;code&gt;-based Spark applications, I am aware that many people have long moved on to &lt;code&gt;Dataset&lt;&#x2F;code&gt;s and to Spark Streaming. It would be nice to create a similar typeclass-based abstraction for datasets and for streaming applications, but I haven&#x27;t had the time to look deeper into what would be necessary to implement either of those.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;summary&quot;&gt;Summary&lt;&#x2F;h2&gt;
&lt;p&gt;&lt;em&gt;kontextfrei&lt;&#x2F;em&gt; is a Scala library that aims to provide developers with a faster feedback loop when developing Apache Spark applications. To achieve that, it enables you to write the business logic of your Spark application, as well as your test code, against an abstraction over Spark’s RDD.&lt;&#x2F;p&gt;
&lt;p&gt;I would love to hear your thoughts on this approach. Do you think it&#x27;s worth it defining the biggest typeclass ever and reimplementing the &lt;code&gt;RDD&lt;&#x2F;code&gt; logic for Scala collections for test purposes? Please, if this looks interesting, do try it out. I am always interested in feedback and in contributions of all kind.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;links&quot;&gt;Links&lt;&#x2F;h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;https:&#x2F;&#x2F;dwestheide.github.io&#x2F;kontextfrei&#x2F;index.html&quot;&gt;kontextfrei project website&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
&lt;li&gt;&lt;a href=&quot;https:&#x2F;&#x2F;github.com&#x2F;dwestheide&#x2F;kontextfrei&quot;&gt;kontextfrei GitHub repo&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
&lt;li&gt;&lt;a href=&quot;https:&#x2F;&#x2F;github.com&#x2F;dwestheide&#x2F;kontextfrei-wordcount&quot;&gt;kontextfrei wordcount example&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
</description>
      </item>
      <item>
          <title>The Empathic Programmer</title>
          <pubDate>Mon, 16 Jan 2017 00:00:00 +0000</pubDate>
          <author>Unknown</author>
          <link>https://danielwestheide.com/blog/the-empathic-programmer/</link>
          <guid>https://danielwestheide.com/blog/the-empathic-programmer/</guid>
          <description xml:base="https://danielwestheide.com/blog/the-empathic-programmer/">&lt;p&gt;In 1999, Andrew Hunt and Dave Thomas, in their seminal book, demanded that programmers be &lt;a href=&quot;https:&#x2F;&#x2F;www.goodreads.com&#x2F;book&#x2F;show&#x2F;4099.The_Pragmatic_Programmer&quot;&gt;pragmatic&lt;&#x2F;a&gt;. Ten years later, Chad Fowler, in his excellent book on career development, asked programmers to be &lt;a href=&quot;https:&#x2F;&#x2F;www.goodreads.com&#x2F;book&#x2F;show&#x2F;6399113-the-passionate-programmer&quot;&gt;passionate&lt;&#x2F;a&gt;. Even today, I still consider a lot of the advice in both of these books to be incredibly valuable, especially Fowler&#x27;s book that helped me a lot, personally.&lt;&#x2F;p&gt;
&lt;p&gt;Nevertheless, in recent years, I have witnessed again and again that one other quality in programmers is at least as important and that it hasn&#x27;t even seen a fraction of the attention it deserves. The programmer we should all strive to be is &lt;em&gt;the empathic programmer&lt;&#x2F;em&gt;. Of course, I am not the only one, let alone the first one, to realize that. For starters, in my bubble, Benjamin Reitzimmer wrote an excellent post about what he considers to be &lt;a href=&quot;http:&#x2F;&#x2F;squeakyvessel.com&#x2F;2015&#x2F;05&#x2F;12&#x2F;mature-developers&#x2F;&quot;&gt;important qualities of a mature developer&lt;&#x2F;a&gt; a while ago, and empathy is one of them. I consider a lack of empathy to be the root cause for some of the biggest problems in our industry and in the tech community. In this post, I want to share some observations on how a lack of empathy leads to problems. Consider it a call to strive for more empathy.&lt;&#x2F;p&gt;
&lt;p&gt;So what is empathy? Here is a &lt;a href=&quot;https:&#x2F;&#x2F;www.merriam-webster.com&#x2F;dictionary&#x2F;empathy&quot;&gt;definition from Merriam-Webster&lt;&#x2F;a&gt;:&lt;&#x2F;p&gt;
&lt;blockquote&gt;
the action of understanding, being aware of, being sensitive to, and vicariously experiencing the feelings, thoughts, and experience of another of either the past or present without having the feelings, thoughts, and experience fully communicated in an objectively explicit manner; also: the capacity for this
&lt;&#x2F;blockquote&gt;
&lt;h2 id=&quot;empathy-at-the-workplace&quot;&gt;Empathy at the workplace&lt;&#x2F;h2&gt;
&lt;p&gt;It shouldn&#x27;t come as a surprise that the ability to show empathy can come in handy in any kind of job that involves working with other people, including the job as a programmer. This is true even if you work remotely – the other messages you see in your Slack channels are not &lt;em&gt;all&lt;&#x2F;em&gt; coming from bots. There are actual human beings behind them.&lt;&#x2F;p&gt;
&lt;p&gt;One of the situations where we often forget to think about that is code reviews. Just writing down what is wrong with a pull request without thinking about tone can easily lead to the creator of the pull request feeling personally offended. April Wensel has some &lt;a href=&quot;http:&#x2F;&#x2F;engineering.usertesting.com&#x2F;2016&#x2F;02&#x2F;3-common-code-review-pitfalls&#x2F;&quot;&gt;good advice&lt;&#x2F;a&gt; on code reviews. What&#x27;s crucial is to develop some sensitivity for how your words will be perceived by the receiver, which requires to put yourself into their shoes, see through their eyes and reflect how they will feel. This is easier the better you know the person, otherwise you will have to make some assumptions, but I think that&#x27;s still far better than not reflecting at all on how the other person will feel.&lt;&#x2F;p&gt;
&lt;p&gt;Another workplace situation where I have often seen a lack of empathy is when members of two different teams need to collaborate to solve a problem or get a feature done. In some companies, I have seen an odd, competitive &quot;us versus them&quot; attitude between teams. This phenomenon has been &lt;a href=&quot;https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Ingroups_and_outgroups&quot;&gt;explored by social and evolutionary psychologists&lt;&#x2F;a&gt;, and while such a behaviour might still be in our nature, that doesn&#x27;t mean that we cannot try to overcome it. A variant of &quot;us versus them&quot; is &quot;developers versus managers&quot;. We developers have a hard time understanding why managers do what they do, but frankly, often, we don&#x27;t try very hard. I have often seen developers taking on a very defensive stance against managers, and of course, the relationship between managers and developers in these cases was rather chilly. Getting to know &quot;the other side&quot; would certainly help to empathize with managers. Understanding why they act in a specific way is absolutely necessary in order to get to a healthy relationship with them.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;empathy-in-the-tech-community&quot;&gt;Empathy in the tech community&lt;&#x2F;h2&gt;
&lt;p&gt;Empathy is not only important at your workplace, but also very much so when you are interacting with others in our community, be it on mailing lists, conferences, or when communicating with users of your open source library, or developers of an open source library you are using. In some of these situations, a lack of empathy can strengthen exclusion, ultimately leading to a closed community that is perceived as elitist and arrogant.&lt;&#x2F;p&gt;
&lt;p&gt;As a developer using an open source library, empathize with the developers of the library before you start complaining about a bug, or better yet, a missing feature. Sam Halliday wrote an interesting post called &lt;a href=&quot;https:&#x2F;&#x2F;medium.com&#x2F;@fommil&#x2F;the-open-source-entitlement-complex-bcb718e2326d#.qtqmmnul7&quot;&gt;The Open Source Entitlement Complex&lt;&#x2F;a&gt;. It&#x27;s hard to believe, but apparently, many users of open source libraries have this attitude that the developers of these libraries are some kind of service provider, happily working for free to do exactly what you want. This is not how it works. The same way that wording and tone are important in code reviews, try to empathize with the developers who spend their free time on this library you use. Serving you and helping you out because you didn&#x27;t read the documentation is probably not their highest priority in life, so don&#x27;t treat them as if it is.&lt;&#x2F;p&gt;
&lt;p&gt;On the other hand, when presenting your open source library to potential users, consider how these people will feel about that presentation. Does it make them feel respected? Does it make them feel welcome? I am sorry to disappoint you, but I think that a foo bar &quot;for reasonable people&quot; does not have that effect. Personally, I find this to be very condescending and think it will intimidate a lot of people and turn them away. It implies that any other way than yours is &lt;em&gt;not&lt;&#x2F;em&gt; reasonable, and that, hence, people who have not used your library yet, but some different approach, are unreasonable people. As library authors, let&#x27;s show some empathy as well towards our potential users. As always in tech, there is no silver bullet, and there are trade-offs. There are probably perfectly good reasons why someone has been using a different library so far, and maybe even after looking at your library, there will still be good reasons not to use yours. Even if you are convinced that your library is so much better, you aren&#x27;t exactly creating an open and welcoming atmosphere by basically telling people visiting your project page that they are unreasonable for using anything else.&lt;&#x2F;p&gt;
&lt;p&gt;If you are at a tech conference, and you ask women whether they are developers at the very beginning of your conversation, but don&#x27;t do the same with men, you are probably not doing that out of malignity, but because you don&#x27;t see many women at tech conferences who are actually developers. Nevertheless, to the receiver, this seemingly harmless and neutral question doesn&#x27;t come across like that at all. She has probably heard this question many times, and constantly hearing doubts about whether you are really a programmer doesn&#x27;t exactly make you feel welcome, or confident. Show some empathy when you talk to other people at tech conferences. Imagine what it would be like to constantly be doubted, for example. If you don&#x27;t see a need for being inclusive, that&#x27;s probably because you had no problem being included in the community. This likely means that you are a man, and probably white. Since most people around you are like you, chances are you don&#x27;t even know any women or other unprivileged people who are developers. The problem of being privileged is that you don&#x27;t notice it. Talk to women on conferences and let them tell you about their experiences. By showing empathy, you can create a more welcoming environment of inclusion and foster diversity.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;summary&quot;&gt;Summary&lt;&#x2F;h2&gt;
&lt;p&gt;These are my two cents about empathy, and lack thereof, in the tech community, and how it relates to inclusion and diversity. Empathy is important not only at the workplace, when interacting with co-workers, but also when we are participating in the tech community, as conference visitors, open source developers, and users of open source libraries. Only by showing empathy, we can create an inclusive and open community. Let&#x27;s try to be more aware of the effects we have on each other, and act accordingly. Thanks!&lt;&#x2F;p&gt;
</description>
      </item>
      <item>
          <title>When Option is not good enough</title>
          <pubDate>Tue, 26 Apr 2016 00:00:00 +0000</pubDate>
          <author>Unknown</author>
          <link>https://danielwestheide.com/blog/when-option-is-not-good-enough/</link>
          <guid>https://danielwestheide.com/blog/when-option-is-not-good-enough/</guid>
          <description xml:base="https://danielwestheide.com/blog/when-option-is-not-good-enough/">&lt;p&gt;Recently, I tweeted about an observation I have made a couple of times in various Scala projects:&lt;&#x2F;p&gt;
&lt;blockquote class=&quot;twitter-tweet&quot; data-lang=&quot;en&quot;&gt;&lt;p lang=&quot;en&quot; dir=&quot;ltr&quot;&gt;Observation: People are using Option too often where their business logic clearly indicates they should use their own, custom ADT.&lt;&#x2F;p&gt;&amp;mdash; Daniel Westheide (@kaffeecoder) &lt;a href=&quot;https:&#x2F;&#x2F;twitter.com&#x2F;kaffeecoder&#x2F;status&#x2F;722815077619904512&quot;&gt;April 20, 2016&lt;&#x2F;a&gt;&lt;&#x2F;blockquote&gt;
&lt;p&gt;Since 140 characters are not nearly enough to discuss this issue appropriately and there were a few questions around this whole topic, I decided to put my thoughts down in a blog post, providing a few examples of what I actually talking about.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;option-is-great-option-is-good&quot;&gt;Option is great, Option is good!&lt;&#x2F;h2&gt;
&lt;p&gt;As Scala programmers, we can all give ourselves a pat on the back for being upright users of the &lt;code&gt;Option&lt;&#x2F;code&gt; type. &lt;code&gt;Option&lt;&#x2F;code&gt; is great! It allows us to express in types that a value may or may not be there. Option is good! It allows us to work with potentially missing values in a safe and elegant way, without having to check for presence all the time or risking those dreaded &lt;code&gt;NullPointerException&lt;&#x2F;code&gt;s. All hail &lt;code&gt;Option&lt;&#x2F;code&gt;!&lt;&#x2F;p&gt;
&lt;h2 id=&quot;the-problem-of-overloaded-semantics&quot;&gt;The problem of overloaded semantics&lt;&#x2F;h2&gt;
&lt;p&gt;Option is also bad! Or, more precisely, the way that this type is being used is somewhat problematic. The semantics of the &lt;code&gt;Option&lt;&#x2F;code&gt; type are pretty clear: it is about potential absence of a value. For example, if you try to get a value for a specific key from a &lt;code&gt;Map&lt;&#x2F;code&gt;, the result may or may not be there:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;scala&quot; style=&quot;background-color:#002b36;color:#839496;&quot; class=&quot;language-scala &quot;&gt;&lt;code class=&quot;language-scala&quot; data-lang=&quot;scala&quot;&gt;&lt;span&gt;scala&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Map&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;DE&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; -&amp;gt; &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Germany&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;FR&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; -&amp;gt; &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;France&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span&gt; get &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;DE&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;res0: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Option&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;String&lt;&#x2F;span&gt;&lt;span&gt;] &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Some&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Germany&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This usage of the &lt;code&gt;Option&lt;&#x2F;code&gt; type is consistent with its semantics. However, sometimes, we attach different or additional meaning to this type. An example of this has been summarised by &lt;a href=&quot;https:&#x2F;&#x2F;twitter.com&#x2F;tksfz&quot;&gt;@tksfz&lt;&#x2F;a&gt; as a reply to my tweet:&lt;&#x2F;p&gt;
&lt;blockquote class=&quot;twitter-tweet&quot; data-conversation=&quot;none&quot; data-lang=&quot;en&quot;&gt;&lt;p lang=&quot;en&quot; dir=&quot;ltr&quot;&gt;&lt;a href=&quot;https:&#x2F;&#x2F;twitter.com&#x2F;kaffeecoder&quot;&gt;@kaffeecoder&lt;&#x2F;a&gt; An example: A Query type where empty means match everything. Have that in our code right now.&lt;&#x2F;p&gt;&amp;mdash; tksfz (@tksfz) &lt;a href=&quot;https:&#x2F;&#x2F;twitter.com&#x2F;tksfz&#x2F;status&#x2F;722893576695205890&quot;&gt;April 20, 2016&lt;&#x2F;a&gt;&lt;&#x2F;blockquote&gt;
&lt;p&gt;Since this is such a good example, I decided to shamelessly make use of it to explain the problem. Imagine you are developing a system that allows users to search for various offers, both by retailers and private sellers. A very simplified version of the search function could look like this:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;scala&quot; style=&quot;background-color:#002b36;color:#839496;&quot; class=&quot;language-scala &quot;&gt;&lt;code class=&quot;language-scala&quot; data-lang=&quot;scala&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;searchOffers&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;productTitle&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Option&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;String&lt;&#x2F;span&gt;&lt;span&gt;],
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;retailer&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Option&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Retailer&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Seq&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Offer&lt;&#x2F;span&gt;&lt;span&gt;] &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;???
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Apparently, there are two search criteria you can provide to filter the results: the product, and the retailer offering the product.&lt;&#x2F;p&gt;
&lt;p&gt;But what does this really mean? It looks like we have attached some new semantics to the &lt;code&gt;Option&lt;&#x2F;code&gt; type. It seems that if &lt;code&gt;productTitle&lt;&#x2F;code&gt; is &lt;code&gt;None&lt;&#x2F;code&gt;, the user wants to search for all offers, regardless of the product title, which means that &lt;code&gt;None&lt;&#x2F;code&gt; has the meaning of a wildcard. However, if &lt;code&gt;productTitle&lt;&#x2F;code&gt; is a &lt;code&gt;Some&lt;&#x2F;code&gt;, is this supposed to be an exact match or does the provided search string just have to be contained in the product title?&lt;&#x2F;p&gt;
&lt;p&gt;For the retailer, the same semantics might apply if the &lt;code&gt;Option&lt;&#x2F;code&gt; is undefined. Or maybe, in this case, &lt;code&gt;None&lt;&#x2F;code&gt; actually means that the user wants to search for offers that are not provided by a professional retailer.&lt;&#x2F;p&gt;
&lt;p&gt;Who knows? As a team member seeing this code for the first time, or coming back to it after a few months, I would probably be confused.&lt;&#x2F;p&gt;
&lt;p&gt;The problem is that we are overloading the semantics of &lt;code&gt;None&lt;&#x2F;code&gt; and &lt;code&gt;Some&lt;&#x2F;code&gt;. The former is very similar to how &lt;code&gt;null&lt;&#x2F;code&gt; in Java is sometimes used with meanings that are different from simple absence.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;towards-meaningful-types&quot;&gt;Towards meaningful types&lt;&#x2F;h2&gt;
&lt;p&gt;Luckily, we can do better in Scala. Whenever someone in your team is confused about the exact meaning of an &lt;code&gt;Option&lt;&#x2F;code&gt; in your code, this is a good indicator that you should introduce your own algebraic data type that captures the implicit semantics of your domain. In the example above, something like this would probably be clearer for the reader:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;scala&quot; style=&quot;background-color:#002b36;color:#839496;&quot; class=&quot;language-scala &quot;&gt;&lt;code class=&quot;language-scala&quot; data-lang=&quot;scala&quot;&gt;&lt;span style=&quot;color:#93a1a1;&quot;&gt;sealed &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;trait&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt; SearchCriteria
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;object&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt; SearchCriteria &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#93a1a1;&quot;&gt;final &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;case object&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt; MatchAll &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;extends &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;SearchCriteria
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#93a1a1;&quot;&gt;final &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;case class&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt; Contains&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;s&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;String&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;extends &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;SearchCriteria
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;}
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#93a1a1;&quot;&gt;sealed &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;trait&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt; RetailerCriteria
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;object&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt; RetailerCriteria &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#93a1a1;&quot;&gt;final &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;case object&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt; AnyRetailer &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;extends &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;RetailerCriteria
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#93a1a1;&quot;&gt;final &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;case class&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt; Only&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;retailer&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Retailer&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;extends &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;RetailerCriteria
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;}
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;searchOffers&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;product&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;SearchCriteria&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;retailer&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;RetailerCriteria
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Seq&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Offer&lt;&#x2F;span&gt;&lt;span&gt;] &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;???
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Introducing your own algebraic data types here allows you to use to close the gap between the language of your domain and the one used in your code, bringing you closer to a ubiquitous language, one of the core values in domain-driven design.&lt;&#x2F;p&gt;
&lt;p&gt;In this example, any confusion about whether the product title is an exact search in the &lt;code&gt;Some&lt;&#x2F;code&gt; case or whether &lt;code&gt;None&lt;&#x2F;code&gt; is a wildcard is now eliminated. In the &lt;code&gt;searchOffers&lt;&#x2F;code&gt; implementation, we can simply use pattern matching on the &lt;code&gt;SearchCriteria&lt;&#x2F;code&gt; and &lt;code&gt;RetailerCriteria&lt;&#x2F;code&gt;, and since they are sealed traits, we will get a warning if our pattern matching is not exhaustive, i.e. it does not cover all the cases. If you want to read more about algebraic data types and sealed traits in Scala, read this &lt;a href=&quot;https:&#x2F;&#x2F;noelwelsh.com&#x2F;posts&#x2F;2015-06-02-everything-about-sealed.html&quot;&gt;excellent blog post by Noel Welsh&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;Now, you might say that &lt;code&gt;SearchCriteria&lt;&#x2F;code&gt; and &lt;code&gt;RetailerCriteria&lt;&#x2F;code&gt; have the same shape as &lt;code&gt;Option&lt;&#x2F;code&gt;, apart from the fact that they are not generic. Wouldn&#x27;t it be good enough to use some type aliases then in order to get the benefits of the ubiquitous language?&lt;&#x2F;p&gt;
&lt;p&gt;You could certainly do that, but having your own algebraic data types is more future-proof. You can easily extend your algebraic data types to enable additional functionality. If we want to allow users to do an exact search for the product title, and to specify that they are only interested in articles that are not offered by retailers, the following will do the trick:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;scala&quot; style=&quot;background-color:#002b36;color:#839496;&quot; class=&quot;language-scala &quot;&gt;&lt;code class=&quot;language-scala&quot; data-lang=&quot;scala&quot;&gt;&lt;span style=&quot;color:#93a1a1;&quot;&gt;sealed &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;trait&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt; SearchCriteria
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;object&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt; SearchCriteria &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#93a1a1;&quot;&gt;final &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;case object&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt; MatchAll &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;extends &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;SearchCriteria
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#93a1a1;&quot;&gt;final &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;case class&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt; Contains&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;s&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;String&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;extends &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;SearchCriteria
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#93a1a1;&quot;&gt;final &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;case class&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt; Exactly&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;s&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;String&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;extends &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;SearchCriteria
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;}
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#93a1a1;&quot;&gt;sealed &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;trait&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt; RetailerCriteria
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;object&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt; RetailerCriteria &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#93a1a1;&quot;&gt;final &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;case object&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt; AnyRetailer &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;extends &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;RetailerCriteria
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#93a1a1;&quot;&gt;final &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;case class&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt; Only&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;retailer&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Retailer&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;extends &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;RetailerCriteria
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#93a1a1;&quot;&gt;final &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;case object&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt; NoRetailer &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;extends &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;RetailerCriteria
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Since we are already using our own algebraic data types, no big refactorings should be necessary.&lt;&#x2F;p&gt;
&lt;p&gt;Of course, it is possible to encode the retailer criteria as an &lt;code&gt;Either[Unit, Option[Retailer]]&lt;&#x2F;code&gt;, but can you tell me immediately what each of the possible cases is supposed to mean?&lt;&#x2F;p&gt;
&lt;h2 id=&quot;more-examples&quot;&gt;More examples&lt;&#x2F;h2&gt;
&lt;p&gt;The previous example was mainly about &lt;code&gt;Option&lt;&#x2F;code&gt; being used in function arguments. It seems like this is rarely a good idea. Here is another example where &lt;code&gt;Option&lt;&#x2F;code&gt; is used as a field of a case class and as a return type of a function, with confusing semantics.&lt;&#x2F;p&gt;
&lt;p&gt;Imagine you are working at Nextflix, the next big platform for watching TV series online. Things being as they are, you need to block certain TV series from users located in specific countries. To do that, you could make use of a filter chain in your web application. One filter in this scenario needs to immediately return a response if the content is blocked in the user&#x27;s country, or forward to the next filter in the chain if the content is accessible from the user&#x27;s country. Here is what this could look like in Scala code:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;scala&quot; style=&quot;background-color:#002b36;color:#839496;&quot; class=&quot;language-scala &quot;&gt;&lt;code class=&quot;language-scala&quot; data-lang=&quot;scala&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;checkTerritory&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;request&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Request&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Option&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Block&lt;&#x2F;span&gt;&lt;span&gt;] &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= {
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;val territory &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt; territoryFrom&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;request&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;if &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;accepted&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;territory&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;None
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;else Some&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Block&lt;&#x2F;span&gt;&lt;span&gt;.requestsFrom&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;territory&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;))
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;contentFilter&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;request&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Request&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;next&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Request =&amp;gt; Response&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span&gt;:  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Response &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= {
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;val blockedResponse &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;for &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;restriction &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;&amp;lt;-&lt;&#x2F;span&gt;&lt;span&gt; checkTerritory&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;request&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;} &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;yield&lt;&#x2F;span&gt;&lt;span&gt; requestBlockedResponse&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;restriction&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;  blockedResponse getOrElse next&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;request&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;If the &lt;code&gt;Option&lt;&#x2F;code&gt; returned by &lt;code&gt;checkTerritory&lt;&#x2F;code&gt; is &lt;code&gt;None&lt;&#x2F;code&gt;, that&#x27;s the happy path and we can call the next filter in the chain. If it is defined, however, we need to short-circuit and immediately return a response informing the user that the content is blocked. Since &lt;code&gt;Option&lt;&#x2F;code&gt; is success-biased and short-circuits if it is &lt;code&gt;None&lt;&#x2F;code&gt;, this doesn&#x27;t look very intuitive to me.&lt;&#x2F;p&gt;
&lt;p&gt;What if we had our own algebraic data type for the verdict on the territory that a request is from? Such a solution could look like this:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;scala&quot; style=&quot;background-color:#002b36;color:#839496;&quot; class=&quot;language-scala &quot;&gt;&lt;code class=&quot;language-scala&quot; data-lang=&quot;scala&quot;&gt;&lt;span style=&quot;color:#93a1a1;&quot;&gt;sealed abstract &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;class&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt; Verdict
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#93a1a1;&quot;&gt;final &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;case object&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt; Pass &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;extends &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;Verdict
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#93a1a1;&quot;&gt;final &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;case class&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt; Block&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;reason&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;String&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;extends &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;Verdict
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;checkTerritory&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;request&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Request&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Verdict &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= {
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;val territory &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt; territoryFrom&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;request&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;if &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;accepted&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;territory&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Pass
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;else Block&lt;&#x2F;span&gt;&lt;span&gt;.requestsFrom&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;territory&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;contentFilter&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;request&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Request&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;next&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Request =&amp;gt; Response&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span&gt;:  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Response &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= {
&lt;&#x2F;span&gt;&lt;span&gt;  checkTerritory&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;request&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;match &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;case Pass &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;=&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; next&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;request&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;case Block&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;reason&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;=&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; requestBlockedResponse&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;reason&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;}
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;I find that to be a lot more readable, and again, it has the advantage that it is easier to extend this in the future with additional verdict types.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;summary&quot;&gt;Summary&lt;&#x2F;h2&gt;
&lt;p&gt;It is often possible to express a concept from your domain as an &lt;code&gt;Option&lt;&#x2F;code&gt;, or if that doesn&#x27;t work, as an &lt;code&gt;Either&lt;&#x2F;code&gt;. Nevertheless, it is sometimes better to not use these generic types. If your actual semantics are different from potential absence of values, don&#x27;t force it, as this causes unnecessary indirection when reasoning about the code and the domain. Unsurprisingly, this can easily lead to bugs. The domain logic is easier to understand if you use your own types from the ubiquitous language.&lt;&#x2F;p&gt;
&lt;p&gt;Do you have more examples from your projects where the lack of custom algebraic data types has led to bugs or code that is difficult to understand? Feel free to share those in the comments or &lt;a href=&quot;https:&#x2F;&#x2F;twitter.com&#x2F;kaffeecoder&quot;&gt;via Twitter&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
</description>
      </item>
      <item>
          <title>Put your writes where your master is: Compile-time restriction of Slick effect types</title>
          <pubDate>Sun, 28 Jun 2015 00:00:00 +0000</pubDate>
          <author>Unknown</author>
          <link>https://danielwestheide.com/blog/put-your-writes-where-your-master-is-compile-time-restriction-of-slick-effect-types/</link>
          <guid>https://danielwestheide.com/blog/put-your-writes-where-your-master-is-compile-time-restriction-of-slick-effect-types/</guid>
          <description xml:base="https://danielwestheide.com/blog/put-your-writes-where-your-master-is-compile-time-restriction-of-slick-effect-types/">&lt;p&gt;When the access patterns of a service are such that there are a lot more reads than writes, a common practice for scaling it horizontally is to have the service talk to a master database only for operations that result in state changes, while all read-only operations are performed against one or more read slaves. Those read replicas are only eventually consistent with the master.&lt;&#x2F;p&gt;
&lt;p&gt;This practice is an easy solution if you don&#x27;t want to or cannot go the full CQRS way. All you need to do is maintain two separate data sources and point all your write operations to the master, and all your read operations to your slave.&lt;&#x2F;p&gt;
&lt;p&gt;However, how do you make sure that you&#x27;re actually putting your writes where your master is? It&#x27;s easy to accidentally get this wrong, and usually, you will only find out at runtime, when the read slave is denying your service to perform its writes.&lt;&#x2F;p&gt;
&lt;p&gt;Wouldn&#x27;t it be nice if you could already verify at compile-time that your operations are hitting the correct database?&lt;&#x2F;p&gt;
&lt;p&gt;With Slick 3, the latest version of Typesafe&#x27;s functional-relational mapper, this is actually very easy to do, provided you know a bit about Scala&#x27;s type system and Slick&#x27;s notion of database actions and effect types.&lt;&#x2F;p&gt;
&lt;p&gt;In this blog post, I will explain all you need to know in order to restrict the evaluation of Slick database actions based on their effect types, resulting in the elimination of yet another source of bugs.&lt;&#x2F;p&gt;
&lt;p&gt;The full example application on which the code snippets in this blog post are based &lt;a href=&quot;https:&#x2F;&#x2F;github.com&#x2F;dwestheide&#x2F;slick-effect-types&quot;&gt;is available on GitHub&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;database-actions&quot;&gt;Database actions&lt;&#x2F;h2&gt;
&lt;p&gt;Whether you want to query a table, insert or update a row, or update your database schema, in Slick 3, each of those things is expressed as a &lt;code&gt;DBIOAction[+R, +S &amp;lt;: NoStream, -E &amp;lt;: Effect]&lt;&#x2F;code&gt;.
&lt;code&gt;R&lt;&#x2F;code&gt; is the result type of the action, and &lt;code&gt;S&lt;&#x2F;code&gt; indicates the result type for streaming results, where the super-type &lt;code&gt;NoStream&lt;&#x2F;code&gt; refers to non-streaming database actions.&lt;&#x2F;p&gt;
&lt;p&gt;For this article, those two type parameters are not of importance, however. Let&#x27;s look at the third parameter instead.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;the-effect-type&quot;&gt;The Effect type&lt;&#x2F;h3&gt;
&lt;p&gt;&lt;code&gt;E&lt;&#x2F;code&gt; is some type of &lt;code&gt;Effect&lt;&#x2F;code&gt; and describes the database action&#x27;s effect type. Slick 3 defines the following sub-types of &lt;code&gt;Effect&lt;&#x2F;code&gt;:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;scala&quot; style=&quot;background-color:#002b36;color:#839496;&quot; class=&quot;language-scala &quot;&gt;&lt;code class=&quot;language-scala&quot; data-lang=&quot;scala&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;trait&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt; Effect
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;object&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt; Effect &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;trait&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt; Read &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;extends &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;Effect
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;trait&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt; Write &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;extends &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;Effect
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;trait&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt; Schema &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;extends &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;Effect
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;trait&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt; Transactional &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;extends &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;Effect
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;trait&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt; All &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;extends &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;Read &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;with &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;Write &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;with &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;Schema &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;with &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;Transactional
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The &lt;code&gt;Effect&lt;&#x2F;code&gt; type is a so-called &lt;a href=&quot;http:&#x2F;&#x2F;ktoso.github.io&#x2F;scala-types-of-types&#x2F;#phantom-type&quot;&gt;phantom type&lt;&#x2F;a&gt;. This means that we never create any instances of &lt;code&gt;Effect&lt;&#x2F;code&gt; at runtime. Rather, the sole purpose of this type is to give additional information to the compiler, so that it can prevent certain error conditions before running the application.&lt;&#x2F;p&gt;
&lt;p&gt;If you use Slick&#x27;s &lt;em&gt;lifted embedding&lt;&#x2F;em&gt; syntax for creating database actions, those actions will always have the appropriate subtype of &lt;code&gt;Effect&lt;&#x2F;code&gt;. For example, if you have a table &lt;code&gt;statuses&lt;&#x2F;code&gt;, you might implement the following &lt;code&gt;StatusRepository&lt;&#x2F;code&gt;:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;scala&quot; style=&quot;background-color:#002b36;color:#839496;&quot; class=&quot;language-scala &quot;&gt;&lt;code class=&quot;language-scala&quot; data-lang=&quot;scala&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;class&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt; StatusRepository &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;save&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;status&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Status&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;) =&lt;&#x2F;span&gt;&lt;span&gt; statuses.insertOrUpdate&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;status&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;forId&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;statusId&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;StatusId&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;) =&lt;&#x2F;span&gt;&lt;span&gt; statuses.filter&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d33682;&quot;&gt;_&lt;&#x2F;span&gt;&lt;span&gt;.id === statusId&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span&gt;.result.headOption
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Here, the type of action returned by &lt;code&gt;save&lt;&#x2F;code&gt; is automatically inferred to be &lt;code&gt;DBIOAction[Int, NoStream, Write]&lt;&#x2F;code&gt;. The action returned by &lt;code&gt;forId&lt;&#x2F;code&gt;, on the other hand, is &lt;code&gt;DBIOAction[Option[Status], NoStream, Read]&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;effect-types-of-composed-actions&quot;&gt;Effect types of composed actions&lt;&#x2F;h3&gt;
&lt;p&gt;If you compose several database actions using one of Slick&#x27;s provided combinators, the correct &lt;em&gt;intersection type&lt;&#x2F;em&gt; will automatically be inferred. For instance, a common approach for updating the state of an aggregate is to have an &lt;em&gt;application service&lt;&#x2F;em&gt; load the aggregate from a repository, perform some business logic on the aggregate, and then save the updated version back to the repository. The application service is providing the transactional boundary for changing the state of the aggregate. This may look similar to the following, hopefully with a more complex business logic than in this oversimplified example:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;scala&quot; style=&quot;background-color:#002b36;color:#839496;&quot; class=&quot;language-scala &quot;&gt;&lt;code class=&quot;language-scala&quot; data-lang=&quot;scala&quot;&gt;&lt;span&gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;categorize&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;statusId&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;StatusId&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;newCategory&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;String&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;) = {
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;val actions &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;for &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;statusOpt &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;&amp;lt;-&lt;&#x2F;span&gt;&lt;span&gt; statusRepository.forId&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;statusId&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;result &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;&amp;lt;-&lt;&#x2F;span&gt;&lt;span&gt; statusOpt &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;match &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;      &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;case Some&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;status&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;=&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;val newStatus &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt; status.copy&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;category &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt; newCategory&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;        statusRepository.save&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;newStatus&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span&gt;.map&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d33682;&quot;&gt;_ &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;=&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Right&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;newStatus&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;))
&lt;&#x2F;span&gt;&lt;span&gt;      &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;case None &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;=&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;DBIO&lt;&#x2F;span&gt;&lt;span&gt;.successful&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Left&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;unknown status&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;))
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;}
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;} &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;yield&lt;&#x2F;span&gt;&lt;span&gt; result
&lt;&#x2F;span&gt;&lt;span&gt;  actions.transactionally
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Here, the inferred effect type of the composed action – &lt;code&gt;actions.transactionally&lt;&#x2F;code&gt;, will be:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;scala&quot; style=&quot;background-color:#002b36;color:#839496;&quot; class=&quot;language-scala &quot;&gt;&lt;code class=&quot;language-scala&quot; data-lang=&quot;scala&quot;&gt;&lt;span style=&quot;color:#859900;&quot;&gt;DBIOAction&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Either&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;String&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Status&lt;&#x2F;span&gt;&lt;span&gt;], &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;NoStream&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Read with Write with Transactional&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h3 id=&quot;evaluating-a-database-action&quot;&gt;Evaluating a database action&lt;&#x2F;h3&gt;
&lt;p&gt;A &lt;code&gt;DBIOAction&lt;&#x2F;code&gt; merely describes what you want to do – in order to actually have it executed, you need to run it like this:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;scala&quot; style=&quot;background-color:#002b36;color:#839496;&quot; class=&quot;language-scala &quot;&gt;&lt;code class=&quot;language-scala&quot; data-lang=&quot;scala&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;class&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt; StatusReadService&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;database&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;DatabaseDef&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;) {
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;statusesByAuthor&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;author&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;String&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;offset&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;Int&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;limit&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;Int&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Future&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Seq&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Status&lt;&#x2F;span&gt;&lt;span&gt;]] &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= {
&lt;&#x2F;span&gt;&lt;span&gt;    database.run &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;      statuses
&lt;&#x2F;span&gt;&lt;span&gt;        .filter&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d33682;&quot;&gt;_&lt;&#x2F;span&gt;&lt;span&gt;.author === author&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;        .sortBy&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d33682;&quot;&gt;_&lt;&#x2F;span&gt;&lt;span&gt;.createdAt.desc&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;        .drop&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;offset&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;        .take&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;limit&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;        .result
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;}
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;}
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;If you haved used Slick 3 before, you will probably be familiar with this. Let&#x27;s look at the signature of &lt;code&gt;run&lt;&#x2F;code&gt; as defined in &lt;code&gt;DatabaseDef&lt;&#x2F;code&gt;:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;scala&quot; style=&quot;background-color:#002b36;color:#839496;&quot; class=&quot;language-scala &quot;&gt;&lt;code class=&quot;language-scala&quot; data-lang=&quot;scala&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;run&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;R&lt;&#x2F;span&gt;&lt;span&gt;]&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;a&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;DBIOAction&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;R&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;NoStream&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;Nothing&lt;&#x2F;span&gt;&lt;span&gt;]&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Future&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;R&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Having an effect type of &lt;code&gt;Nothing&lt;&#x2F;code&gt; means that Slick accepts database actions with any effect type, not caring about whether it is a &lt;code&gt;Write&lt;&#x2F;code&gt;, a &lt;code&gt;Read&lt;&#x2F;code&gt;, or something else.&lt;&#x2F;p&gt;
&lt;p&gt;This is perfectly fine for a generic library. However, when you are working with a master and a slave database, it means that your code will look like this:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;scala&quot; style=&quot;background-color:#002b36;color:#839496;&quot; class=&quot;language-scala &quot;&gt;&lt;code class=&quot;language-scala&quot; data-lang=&quot;scala&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;class&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt; DatabaseModule &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;val masterDatabase&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;DatabaseDef &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Database&lt;&#x2F;span&gt;&lt;span&gt;.forConfig&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;databases.master&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;val slaveDatabase&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;DatabaseDef &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Database&lt;&#x2F;span&gt;&lt;span&gt;.forConfig&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;databases.slave&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;}
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;class&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt; StatusReadService&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;database&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;DatabaseDef&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;) {
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;&#x2F;&#x2F; ...
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;}
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;class&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt; StatusService&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;statusRepository&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;StatusRepository&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;database&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;DatabaseDef&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;) {
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;&#x2F;&#x2F; ...
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;On the type level, you are not able to differentiate between your master and slave databases, so performing an undesired effect against one of your databases by accident is a very real issue.&lt;&#x2F;p&gt;
&lt;p&gt;Luckily, with its &lt;code&gt;Effect&lt;&#x2F;code&gt; type, Slick 3 gives you all the compile-time information you need to implement a restriction of effect types for your own application.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;restricting-effect-types&quot;&gt;Restricting effect types&lt;&#x2F;h2&gt;
&lt;p&gt;In order to restrict the effect types that are allowed for a certain database, we are going to attach a role (e.g. master or slave) to each database and associate certain privileges to each role that will then be checked when trying to run a database action – all at compile time.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;roles&quot;&gt;Roles&lt;&#x2F;h3&gt;
&lt;p&gt;The first thing we need to introduce is another phantom type called &lt;code&gt;Role&lt;&#x2F;code&gt;:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;scala&quot; style=&quot;background-color:#002b36;color:#839496;&quot; class=&quot;language-scala &quot;&gt;&lt;code class=&quot;language-scala&quot; data-lang=&quot;scala&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;trait&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt; Role
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;trait&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt; Master &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;extends &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;Role
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;trait&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt; Slave &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;extends &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;Role
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Like Slick&#x27;s &lt;code&gt;Effect&lt;&#x2F;code&gt;, our &lt;code&gt;Role&lt;&#x2F;code&gt; trait will never be instantiated in our application.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;privileges&quot;&gt;Privileges&lt;&#x2F;h3&gt;
&lt;p&gt;Instead, &lt;code&gt;Role&lt;&#x2F;code&gt; will appear as a type parameter in yet another new phantom type:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;scala&quot; style=&quot;background-color:#002b36;color:#839496;&quot; class=&quot;language-scala &quot;&gt;&lt;code class=&quot;language-scala&quot; data-lang=&quot;scala&quot;&gt;&lt;span&gt;@&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;implicitNotFound&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;&amp;#39;${R}&amp;#39; database is not privileged to to perform effect &amp;#39;${E}&amp;#39;.&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;trait&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt; HasPrivilege&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;R &amp;lt;: Role&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;E &amp;lt;: Effect&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The &lt;code&gt;HasPrivilege&lt;&#x2F;code&gt; phantom type is meant to provide implicit evidence that a certain role &lt;code&gt;R&lt;&#x2F;code&gt; is allowed to perform the Slick effect type &lt;code&gt;E&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;The &lt;code&gt;@implicitNotFound&lt;&#x2F;code&gt; annotation allows us to provide a custom error message for the case that no implicit evidence of &lt;code&gt;HasPrivilege&lt;&#x2F;code&gt; can be found, where it is required.&lt;&#x2F;p&gt;
&lt;p&gt;We are going to spell out the privileges for our two roles to the compiler like this:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;scala&quot; style=&quot;background-color:#002b36;color:#839496;&quot; class=&quot;language-scala &quot;&gt;&lt;code class=&quot;language-scala&quot; data-lang=&quot;scala&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;type &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;ReadWriteTransaction &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Read with Write with Transactional
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#93a1a1;&quot;&gt;implicit &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;val slaveCanRead&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Slave HasPrivilege Read &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;null
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#93a1a1;&quot;&gt;implicit &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;val masterCanRead&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Master HasPrivilege Read &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;null
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#93a1a1;&quot;&gt;implicit &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;val masterCanWrite&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Master HasPrivilege Write &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;null
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#93a1a1;&quot;&gt;implicit &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;val masterCanPerformTransactions&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Master HasPrivilege ReadWriteTransaction &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;null
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;As you can see, while we do provide evidences in terms of &lt;code&gt;implicit val&lt;&#x2F;code&gt;s, we don&#x27;t actually create any instances of &lt;code&gt;HasPrivilege&lt;&#x2F;code&gt;. Since the code that will make use of our evidence will never work with our implicit evidences at runtime, we can safely assign &lt;code&gt;null&lt;&#x2F;code&gt; here.&lt;&#x2F;p&gt;
&lt;p&gt;Also, please not that &lt;code&gt;Slave HasPrivilege Read&lt;&#x2F;code&gt; is just another notation for &lt;code&gt;HasPrivilege[Slave, Read]&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;Unfortunately, we have to provide an implicit evidence for every combination of effect types we want to allow.&lt;&#x2F;p&gt;
&lt;p&gt;In this example, we want to allow combining reads with writes and transactions, but it&#x27;s not allowed to combine reads and writes without also using a transaction.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;check-your-privileges&quot;&gt;Check your privileges!&lt;&#x2F;h3&gt;
&lt;p&gt;Now, in order to actually restrict the database actions that can be run according to the role of the database, we need to introduce a wrapper around Slick&#x27;s &lt;code&gt;DatabaseDef&lt;&#x2F;code&gt; – as we saw earlier, the latter does not care about the type of effect, and of course, it doesn&#x27;t know anything about our roles.&lt;&#x2F;p&gt;
&lt;p&gt;Hence, we are introducing a class &lt;code&gt;DB&lt;&#x2F;code&gt;:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;scala&quot; style=&quot;background-color:#002b36;color:#839496;&quot; class=&quot;language-scala &quot;&gt;&lt;code class=&quot;language-scala&quot; data-lang=&quot;scala&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;class&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt; DB&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;R &amp;lt;: Role&lt;&#x2F;span&gt;&lt;span&gt;]&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;databaseConfiguration&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;DatabaseConfiguration&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;R&lt;&#x2F;span&gt;&lt;span&gt;]&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;) {
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#93a1a1;&quot;&gt;private &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;val underlyingDatabase &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt; databaseConfiguration.createDatabase&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;()
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;run&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;A&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;E &amp;lt;: Effect&lt;&#x2F;span&gt;&lt;span&gt;]&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;a&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;DBIOAction&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;A&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;NoStream&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;E&lt;&#x2F;span&gt;&lt;span&gt;]&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;                         &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#93a1a1;&quot;&gt;implicit &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;p&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;R HasPrivilege E&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Future&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;A&lt;&#x2F;span&gt;&lt;span&gt;] &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt; underlyingDatabase.run&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;a&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Our new wrapper type has a type parameter &lt;code&gt;R&lt;&#x2F;code&gt; that specifies its role, and it creates the underlying Slick &lt;code&gt;DatabaseDef&lt;&#x2F;code&gt; from an instance of &lt;code&gt;DatabaseConfiguration&lt;&#x2F;code&gt; with the same role, which we will look at in a moment.&lt;&#x2F;p&gt;
&lt;p&gt;For now, the important thing is the &lt;code&gt;run&lt;&#x2F;code&gt; method on our &lt;code&gt;DB&lt;&#x2F;code&gt; class, which looks very similar to the Slick&#x27;s &lt;code&gt;run&lt;&#x2F;code&gt; method we saw earlier.&lt;&#x2F;p&gt;
&lt;p&gt;The crucial difference is that our &lt;code&gt;run&lt;&#x2F;code&gt; method has a second type parameter &lt;code&gt;E&lt;&#x2F;code&gt; that specifies the type of effect of our action, and that our &lt;code&gt;DBIOAction&lt;&#x2F;code&gt;s effect type parameter is that &lt;code&gt;E&lt;&#x2F;code&gt; instead of just &lt;code&gt;Nothing&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;Moreover, our &lt;code&gt;run&lt;&#x2F;code&gt; method has a second, implicit parameter list, with evidence of &lt;code&gt;HasPrivilege[R, E]&lt;&#x2F;code&gt;, i.e. that our database role &lt;code&gt;R&lt;&#x2F;code&gt; is privileged to execute the effect &lt;code&gt;E&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;Instead of using &lt;code&gt;DatabaseDef&lt;&#x2F;code&gt; directly, we will now always make use of our role-annotated &lt;code&gt;DB&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;providing-role-annotated-databases&quot;&gt;Providing role-annotated databases&lt;&#x2F;h3&gt;
&lt;p&gt;To achieve that, we introduce a type &lt;code&gt;DatabaseConfiguration&lt;&#x2F;code&gt; which, like &lt;code&gt;DB&lt;&#x2F;code&gt;, is annotated with a role:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;scala&quot; style=&quot;background-color:#002b36;color:#839496;&quot; class=&quot;language-scala &quot;&gt;&lt;code class=&quot;language-scala&quot; data-lang=&quot;scala&quot;&gt;&lt;span style=&quot;color:#93a1a1;&quot;&gt;sealed &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;trait&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt; DatabaseConfiguration&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;R &amp;lt;: Role&lt;&#x2F;span&gt;&lt;span&gt;] &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;createDatabase&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;()&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;DatabaseDef
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;}
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;object&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt; DatabaseConfiguration &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;object&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt; Master &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;extends &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;DatabaseConfiguration&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Master&lt;&#x2F;span&gt;&lt;span&gt;] &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;createDatabase&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;() = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Database&lt;&#x2F;span&gt;&lt;span&gt;.forConfig&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;databases.master&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;}
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;object&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt; Slave &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;extends &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;DatabaseConfiguration&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Slave&lt;&#x2F;span&gt;&lt;span&gt;] &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;createDatabase&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;() = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Database&lt;&#x2F;span&gt;&lt;span&gt;.forConfig&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;databases.slave&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;}
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The &lt;code&gt;DatabaseConfiguration&lt;&#x2F;code&gt; is the one place where we interact with the untyped outside world, reading our database connection information for the respective key, &lt;code&gt;databases.master&lt;&#x2F;code&gt; or &lt;code&gt;databases.slave&lt;&#x2F;code&gt;. Hence, the only place where we can still get things wrong is in our &lt;code&gt;application.conf&lt;&#x2F;code&gt; configuration file, if we accidentally provide the wrong database host, for example.&lt;&#x2F;p&gt;
&lt;p&gt;Our database module providing the master and slave databases to our application will now look like this:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;scala&quot; style=&quot;background-color:#002b36;color:#839496;&quot; class=&quot;language-scala &quot;&gt;&lt;code class=&quot;language-scala&quot; data-lang=&quot;scala&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;class&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt; DatabaseModule &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;val masterDatabase&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;DB&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Master&lt;&#x2F;span&gt;&lt;span&gt;] &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;new DB&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;DatabaseConfiguration&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Master&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;val slaveDatabase&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;DB&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Slave&lt;&#x2F;span&gt;&lt;span&gt;] &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;new DB&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;DatabaseConfiguration&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Slave&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Unlike the previous version, the &lt;code&gt;masterDatabase&lt;&#x2F;code&gt; and &lt;code&gt;slaveDatabase&lt;&#x2F;code&gt; fields are now properly typed, and our service implementations that made use of &lt;code&gt;DatabaseDef&lt;&#x2F;code&gt; before must now explicitly pick a database with the correct &lt;code&gt;Role&lt;&#x2F;code&gt; for their purposes:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;scala&quot; style=&quot;background-color:#002b36;color:#839496;&quot; class=&quot;language-scala &quot;&gt;&lt;code class=&quot;language-scala&quot; data-lang=&quot;scala&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;class&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt; StatusService&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;statusRepository&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;StatusRepository&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;database&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;DB&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Master&lt;&#x2F;span&gt;&lt;span&gt;]&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;) {
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;&#x2F;&#x2F; ...
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;}
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;class&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt; StatusReadService&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;database&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;DB&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Slave&lt;&#x2F;span&gt;&lt;span&gt;]&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;) {
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;&#x2F;&#x2F; ...
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h3 id=&quot;oops-i-used-the-slave-for-a-write&quot;&gt;Oops, I used the slave for a write...&lt;&#x2F;h3&gt;
&lt;p&gt;To verify that all of this has the desired effect, let&#x27;s make our &lt;code&gt;StatusService&lt;&#x2F;code&gt; use the slave database:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;scala&quot; style=&quot;background-color:#002b36;color:#839496;&quot; class=&quot;language-scala &quot;&gt;&lt;code class=&quot;language-scala&quot; data-lang=&quot;scala&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;class&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt; StatusService&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;statusRepository&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;StatusRepository&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;database&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;DB&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Slave&lt;&#x2F;span&gt;&lt;span&gt;]&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;) {
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;&#x2F;&#x2F; ...
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;When we try to compile this, we will receive this nice error message from the compiler:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#002b36;color:#839496;&quot;&gt;&lt;code&gt;&lt;span&gt;&amp;#39;com.danielwestheide.slickeffecttypes.db.Slave&amp;#39; database is not privileged to
&lt;&#x2F;span&gt;&lt;span&gt;perform effect &amp;#39;slick.dbio.Effect.Write&amp;#39;.
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h3 id=&quot;plain-sql-queries&quot;&gt;Plain SQL queries&lt;&#x2F;h3&gt;
&lt;p&gt;For plain SQL queries, of course, Slick cannot infer any effect types automatically. Hence, if you need to fall back from lifted embedding to plain SQL queries, you have to annotate the resulting database actions explicitly:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;scala&quot; style=&quot;background-color:#002b36;color:#839496;&quot; class=&quot;language-scala &quot;&gt;&lt;code class=&quot;language-scala&quot; data-lang=&quot;scala&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;statusesByCategory&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;category&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;String&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;offset&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;Int&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;limit&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;Int&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Future&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Seq&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Status&lt;&#x2F;span&gt;&lt;span&gt;]] &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= {
&lt;&#x2F;span&gt;&lt;span&gt;   &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;val action&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;SqlStreamingAction&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Seq&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Status&lt;&#x2F;span&gt;&lt;span&gt;], &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Status&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Read&lt;&#x2F;span&gt;&lt;span&gt;] &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;=
&lt;&#x2F;span&gt;&lt;span&gt;     &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;sql&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&amp;quot;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;select id, created_at, author, text, category from statuses
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;         where category = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;$category
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;         order by created_at desc limit &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;$limit&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt; offset &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;$offset&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&amp;quot;&amp;quot;.as[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Status&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;span&gt;    database.run&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;action&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;If you don&#x27;t do provide an explicit type here, the actions&#x27;s effect type parameter will be inferred to be &lt;code&gt;Effect&lt;&#x2F;code&gt;, the super type of all effect types.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;custom-effect-types&quot;&gt;Custom effect types&lt;&#x2F;h3&gt;
&lt;p&gt;Since &lt;code&gt;Effect&lt;&#x2F;code&gt; is not a sealed trait, you may introduce your own effect types and prevent certain databases from performing those effects.&lt;&#x2F;p&gt;
&lt;p&gt;For example, you may want to disallow certain expensive queries to be performed against the master database. To do that, you could introduce an effect type &lt;code&gt;ExpensiveRead&lt;&#x2F;code&gt;, and only allow slave databases to run actions with that type:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;scala&quot; style=&quot;background-color:#002b36;color:#839496;&quot; class=&quot;language-scala &quot;&gt;&lt;code class=&quot;language-scala&quot; data-lang=&quot;scala&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;trait&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt; ExpensiveRead &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;extends &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;Read
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#93a1a1;&quot;&gt;implicit &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;val slaveCanDoExpensiveReads&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Slave HasPrivilege ExpensiveRead &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;null
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Just as with plain SQL queries, you can annotate your database action explicitly to be of type &lt;code&gt;ExpensiveRead&lt;&#x2F;code&gt;:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;scala&quot; style=&quot;background-color:#002b36;color:#839496;&quot; class=&quot;language-scala &quot;&gt;&lt;code class=&quot;language-scala&quot; data-lang=&quot;scala&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;statusesByCategory&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;category&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;String&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;offset&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;Int&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;limit&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;Int&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Future&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Seq&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Status&lt;&#x2F;span&gt;&lt;span&gt;]] &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= {
&lt;&#x2F;span&gt;&lt;span&gt;   &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;val action&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;SqlStreamingAction&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Seq&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Status&lt;&#x2F;span&gt;&lt;span&gt;], &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Status&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;ExpensiveRead&lt;&#x2F;span&gt;&lt;span&gt;] &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;=
&lt;&#x2F;span&gt;&lt;span&gt;     &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;sql&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&amp;quot;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;select id, created_at, author, text, category from statuses
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;         where category = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;$category
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;         order by created_at desc limit &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;$limit&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt; offset &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;$offset&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&amp;quot;&amp;quot;.as[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Status&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;span&gt;    database.run&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;action&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;If you accidentally use a &lt;code&gt;DB[Master]&lt;&#x2F;code&gt; in this read service, you will get a compile error.&lt;&#x2F;p&gt;
&lt;p&gt;Of course, once you have to start annotating your database action explicitly in order to benefit from these compile-time checks, you are prone to another source of errors. Failing to annotate your actions correctly may lead to the same kinds of runtime errors we wanted to prevent.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;conclusion&quot;&gt;Conclusion&lt;&#x2F;h2&gt;
&lt;p&gt;In this article I have shown how to use Slick&#x27;s &lt;code&gt;Effect&lt;&#x2F;code&gt; type, together with a few other phantom types, in order to have your compiler help you verify that you run your Slick database actions against the correct database. While it&#x27;s possible to use this technique with plain SQL queries and custom effect types, the greatest benefit will come in cases where you only use lifted embedding and the standard effect types.&lt;&#x2F;p&gt;
&lt;p&gt;Thanks a lot to &lt;a href=&quot;https:&#x2F;&#x2F;twitter.com&#x2F;missingfaktor&quot;&gt;@missingfaktor&lt;&#x2F;a&gt; who collaborated with me on developing this technique on top of Slick 3 and gave a lot of valuable input.&lt;&#x2F;p&gt;
</description>
      </item>
      <item>
          <title>The Neophyte&#x27;s Guide to Scala Part 16: Where to Go from Here</title>
          <pubDate>Wed, 03 Apr 2013 00:00:00 +0000</pubDate>
          <author>Unknown</author>
          <link>https://danielwestheide.com/blog/the-neophytes-guide-to-scala-part-16-where-to-go-from-here/</link>
          <guid>https://danielwestheide.com/blog/the-neophytes-guide-to-scala-part-16-where-to-go-from-here/</guid>
          <description xml:base="https://danielwestheide.com/blog/the-neophytes-guide-to-scala-part-16-where-to-go-from-here/">&lt;p&gt;As I have already hinted at in the previous article, the &lt;em&gt;Neophyte&#x27;s Guide to Scala&lt;&#x2F;em&gt; is coming to an end. Over the last five months, we have delved into numerous language and library features of Scala, hopefully deepening your understanding of those features and their underlying ideas and concepts.&lt;&#x2F;p&gt;
&lt;p&gt;As such, I hope this guide has served you well as a supplement to whatever introductory resources you have been using to learn Scala, whether you attended the &lt;a href=&quot;https:&#x2F;&#x2F;www.coursera.org&#x2F;course&#x2F;progfun&quot;&gt;Scala course at Coursera&lt;&#x2F;a&gt; or learned from a book. I have tried to cover all the quirks I stumbled over back when I learned the language – things that were only mentioned briefly or not covered at all in the books and tutorials available to me – and I hope that especially those explanations were of value to you.&lt;&#x2F;p&gt;
&lt;p&gt;As the series progressed, we ventured into more advanced territory, covering ideas like type classes and path-dependent types. While I could go on writing about more and more arcane features, I felt like this would go against the idea of this series, which is clearly targeted at aspiring neophytes.&lt;&#x2F;p&gt;
&lt;p&gt;Hence, I will conclude this series with some suggestions of where to go from here if you want more. Rest assured that I will continue blogging about Scala, just not within the context of this series.&lt;&#x2F;p&gt;
&lt;p&gt;How you want to continue your journey with Scala, of course, depends a lot on your individual preferences: Maybe you are now at a point where you would like to &lt;strong&gt;teach Scala to others&lt;&#x2F;strong&gt;, or maybe you are excited about Scala&#x27;s type system and would like to explore some of the language&#x27;s more arcane features by delving into &lt;strong&gt;type-level programming&lt;&#x2F;strong&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;More often than not, a good way to really get comfortable with a new language and its whole ecosystem of libraries is to use it for &lt;strong&gt;creating something useful&lt;&#x2F;strong&gt;, i.e. a real application that is more than just toying around. Personally, I have also gained a lot from &lt;strong&gt;contributing&lt;&#x2F;strong&gt; to open source projects early on.&lt;&#x2F;p&gt;
&lt;p&gt;In the following, I will elaborate those four paths, which, of course, are not mutually exclusive, and provide you with numerous links to highly recommendable additional resources.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;teaching-scala&quot;&gt;Teaching Scala&lt;&#x2F;h2&gt;
&lt;p&gt;Having followed this series, you should be familiar enough with Scala to be able to teach the basics. Maybe you are in a Java or Ruby shop and want to get your coworkers excited about Scala and functional programming.&lt;&#x2F;p&gt;
&lt;p&gt;Great, then why not organize a workshop? A nice way of introducing people to a new language is to not do a talk with lots of slides, but to teach by example, introducing a language in small steps by solving tiny problems together. Active participation is key!&lt;&#x2F;p&gt;
&lt;p&gt;If that&#x27;s something you&#x27;d like to do, the Scala community has you covered. Have a look at &lt;a href=&quot;http:&#x2F;&#x2F;www.scalakoans.org&#x2F;&quot;&gt;Scala Koans&lt;&#x2F;a&gt;, a collection of small lessons, each of which provides a problem to be solved by fixing an initially failing test. The project is inspired by the &lt;a href=&quot;http:&#x2F;&#x2F;rubykoans.com&#x2F;&quot;&gt;Ruby Koans&lt;&#x2F;a&gt; project and is a really good resource for teaching the language to others in small collaborative coding sessions.&lt;&#x2F;p&gt;
&lt;p&gt;Another amazing project ideally suited for workshops or other events is &lt;a href=&quot;http:&#x2F;&#x2F;scalatron.github.com&#x2F;&quot;&gt;Scalatron&lt;&#x2F;a&gt;, a game in which bots fight against each other in a virtual arena. Why not teach the language by developing such a bot together in a workshop, that will then fight against the computer? Once the participants are familiar enough with the language, organize a tournament, where each participant will develop their own bot.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;mastering-arcane-powers&quot;&gt;Mastering arcane powers&lt;&#x2F;h2&gt;
&lt;p&gt;We have only seen a tiny bit of what the Scala type system allows you to do. If this small hint at the high wizardry that&#x27;s possible got you excited and you want to master the arcane powers of type-level programming, a good starting resource is the blog series &lt;a href=&quot;http:&#x2F;&#x2F;apocalisp.wordpress.com&#x2F;2010&#x2F;06&#x2F;08&#x2F;type-level-programming-in-scala&#x2F;&quot;&gt;Type-Level Programming in Scala&lt;&#x2F;a&gt; by Mark Harrah.&lt;&#x2F;p&gt;
&lt;p&gt;After that, I recommend to have a look at &lt;a href=&quot;https:&#x2F;&#x2F;github.com&#x2F;milessabin&#x2F;shapeless&quot;&gt;Shapeless&lt;&#x2F;a&gt;, a library in which Miles Sabin explores the limits of the Scala language in terms of generic and polytypic programming.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;creating-something-useful&quot;&gt;Creating something useful&lt;&#x2F;h2&gt;
&lt;p&gt;Reading books, doing tutorials and toying around with a new language is all fine to get a certain understanding of it, but in order to become really comfortable with Scala and its paradigm and learn how to think the Scala way, I highly recommend that you start creating something useful with it – something that is clearly more than a toy application (this is true for learning any language, in my opinion).&lt;&#x2F;p&gt;
&lt;p&gt;By tackling a real-world problem and trying to create a useful and usable application, you will also get a good overview of the ecosystem of libraries and get a feeling for which of those can be of service to you in specific situations.&lt;&#x2F;p&gt;
&lt;p&gt;In order to find relevant libraries or get updates of ones you are interested in, you should subscribe to &lt;a href=&quot;http:&#x2F;&#x2F;notes.implicit.ly&#x2F;&quot;&gt;implicit.ly&lt;&#x2F;a&gt; and regularly take a look at &lt;a href=&quot;https:&#x2F;&#x2F;github.com&#x2F;trending?l=scala&quot;&gt;Scala projects on GitHub&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;it-s-all-about-the-web&quot;&gt;It&#x27;s all about the web&lt;&#x2F;h3&gt;
&lt;p&gt;These days, most applications written in Scala will be some kind of server applications, often with a RESTful interface exposed via HTTP and a web frontend.&lt;&#x2F;p&gt;
&lt;p&gt;If the actor model for concurrency is a good fit for your use case and you hence choose to use the &lt;a href=&quot;http:&#x2F;&#x2F;akka.io&quot;&gt;Akka&lt;&#x2F;a&gt; toolkit, an excellent choice for exposing a REST API via HTTP is &lt;a href=&quot;http:&#x2F;&#x2F;spray.io&#x2F;documentation&#x2F;spray-routing&#x2F;&quot;&gt;Spray Routing&lt;&#x2F;a&gt;. This is a great tool if you don&#x27;t need a web frontend, or if you want to develop a single-page web application that will talk to your backend by means of a REST API.&lt;&#x2F;p&gt;
&lt;p&gt;If you need something less minimalistic, of course, &lt;a href=&quot;http:&#x2F;&#x2F;playframework.org&quot;&gt;Play&lt;&#x2F;a&gt;, which is part of the Typesafe stack, is a good choice, especially if you seek something that is widely adopted and hence well supported.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;living-in-a-concurrent-world&quot;&gt;Living in a concurrent world&lt;&#x2F;h3&gt;
&lt;p&gt;If after our two parts on actors and Akka, you think that Akka is a good fit for your application, you will likely want to learn a lot more about it before getting serious with it.&lt;&#x2F;p&gt;
&lt;p&gt;While the &lt;a href=&quot;http:&#x2F;&#x2F;akka.io&#x2F;docs&#x2F;&quot;&gt;Akka documentation&lt;&#x2F;a&gt; is pretty exhaustive and thus serves well as a reference, I think that the best choice for actually &lt;em&gt;learning&lt;&#x2F;em&gt; Akka is Derek Wyatt&#x27;s book &lt;a href=&quot;http:&#x2F;&#x2F;www.artima.com&#x2F;shop&#x2F;akka_concurrency&quot;&gt;Akka Concurrency&lt;&#x2F;a&gt;, a preliminary version of which is already available as a PDF.&lt;&#x2F;p&gt;
&lt;p&gt;Once you have got serious with Akka, you should definitely subscribe to &lt;a href=&quot;http:&#x2F;&#x2F;letitcrash.com&#x2F;&quot;&gt;Let It Crash&lt;&#x2F;a&gt;, which provides you with news and advanced tips and tricks and regarding all things Akka.&lt;&#x2F;p&gt;
&lt;p&gt;If actors are not your thing and you prefer a concurrency model allowing you to leverage the composability of &lt;em&gt;Futures&lt;&#x2F;em&gt;, your library of choice is probably Twitter&#x27;s &lt;a href=&quot;http:&#x2F;&#x2F;twitter.github.com&#x2F;finagle&quot;&gt;Finagle&lt;&#x2F;a&gt;. It allows you to modularize your application as a bunch of small remote services, with support for numerous popular protocols out of the box.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;contributing&quot;&gt;Contributing&lt;&#x2F;h2&gt;
&lt;p&gt;Another really great way to learn a lot about Scala quickly is to start contributing to one or more open source projects – preferably to libraries you have been using while working on your own application.&lt;&#x2F;p&gt;
&lt;p&gt;Of course, this is nothing that is specific to Scala, but still I think it deserves to be mentioned. If you have only just learned Scala and are not using it at your day job already, it&#x27;s nearly the only choice you have to learn from other, more experienced Scala developers.&lt;&#x2F;p&gt;
&lt;p&gt;It forces you to read a lot of Scala code from other people, discovering how to do things differently, possibly more idiomatically, and you can have those experienced developers review your code in pull requests.&lt;&#x2F;p&gt;
&lt;p&gt;I have found the Scala community at large to be very friendly and helpful, so don&#x27;t shy away from contributing, even if you think you&#x27;re too much of a rookie when it comes to Scala.&lt;&#x2F;p&gt;
&lt;p&gt;While some projects might have their own way of doing things, it&#x27;s certainly a good idea to study the &lt;a href=&quot;http:&#x2F;&#x2F;docs.scala-lang.org&#x2F;style&#x2F;&quot;&gt;Scala Style Guide&lt;&#x2F;a&gt; to get familiar with common coding conventions.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;connecting&quot;&gt;Connecting&lt;&#x2F;h2&gt;
&lt;p&gt;By contributing to open source projects, you have already started connecting with the Scala community. However, you may not have the time to do that, or you may prefer other ways of connecting to like-minded people.&lt;&#x2F;p&gt;
&lt;p&gt;Try finding a local Scala user group or meetup. If you don&#x27;t like connecting in meatspace, the &lt;a href=&quot;https:&#x2F;&#x2F;groups.google.com&#x2F;forum&#x2F;#!forum&#x2F;scala-user&quot;&gt;scala-user mailing list&#x2F;Google group&lt;&#x2F;a&gt; and the &lt;a href=&quot;https:&#x2F;&#x2F;gitter.im&#x2F;scala&#x2F;scala&quot;&gt;Scala Gitter channel&lt;&#x2F;a&gt; may be good alternatives.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;other-resources&quot;&gt;Other resources&lt;&#x2F;h2&gt;
&lt;p&gt;Regardless of which of the paths outlined above you follow, there are a few resources I would like to recommend:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;https:&#x2F;&#x2F;www.manning.com&#x2F;bjarnason&#x2F;&quot;&gt;Functional Programming in Scala&lt;&#x2F;a&gt; by Paul Chiusano and Rúnar Bjarnason, which is currently available as an Early Access Edition and teaches you a lot more about functional programming and thinking about problems with a functional mindset.&lt;&#x2F;li&gt;
&lt;li&gt;The &lt;a href=&quot;https:&#x2F;&#x2F;docs.scala-lang.org&#x2F;&quot;&gt;Scala Documentation Site&lt;&#x2F;a&gt;, which, for some reason, is not linked very prominently from the main Scala website, especially the available &lt;a href=&quot;https:&#x2F;&#x2F;docs.scala-lang.org&#x2F;overviews&#x2F;&quot;&gt;guides&lt;&#x2F;a&gt; and &lt;a href=&quot;https:&#x2F;&#x2F;docs.scala-lang.org&#x2F;tutorials&#x2F;&quot;&gt;tutorials&lt;&#x2F;a&gt;.&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;h2 id=&quot;conclusion&quot;&gt;Conclusion&lt;&#x2F;h2&gt;
&lt;p&gt;I hope you have enjoyed this series and that I could get you excited about Scala. While this series is coming to an end, I seriously hope that it&#x27;s just the beginning of your journey through Scala land. Let me know in the comments how your journey went so far and where you think it will go from here.&lt;&#x2F;p&gt;
</description>
      </item>
      <item>
          <title>The Neophyte&#x27;s Guide to Scala Part 15: Dealing with Failure in Actor Systems</title>
          <pubDate>Wed, 20 Mar 2013 00:00:00 +0000</pubDate>
          <author>Unknown</author>
          <link>https://danielwestheide.com/blog/the-neophytes-guide-to-scala-part-15-dealing-with-failure-in-actor-systems/</link>
          <guid>https://danielwestheide.com/blog/the-neophytes-guide-to-scala-part-15-dealing-with-failure-in-actor-systems/</guid>
          <description xml:base="https://danielwestheide.com/blog/the-neophytes-guide-to-scala-part-15-dealing-with-failure-in-actor-systems/">&lt;p&gt;In the previous part of this series, I introduced you to the second cornerstone of Scala concurrency: The &lt;em&gt;actor&lt;&#x2F;em&gt; model, which complements the model based on composable &lt;em&gt;futures&lt;&#x2F;em&gt; backed by &lt;em&gt;promises&lt;&#x2F;em&gt;. You learnt how to define and create actors, how to send messages to them and how an actor processes these messages, possibly modifying its internal state as a result or asynchronously sending a response message to the sender.&lt;&#x2F;p&gt;
&lt;p&gt;While that was hopefully enough to get you interested in the actor model for concurrency, I left out some crucial concepts you will want to know about before starting to develop actor-based applications that consist of more than a simple echo actor.&lt;&#x2F;p&gt;
&lt;p&gt;The actor model is meant to help you achieve a high level of fault tolerance. In this article, we are going to have a look at how to deal with failure in an actor-based application, which is fundamentally different from error handling in a traditional layered server architecture.&lt;&#x2F;p&gt;
&lt;p&gt;The way you deal with failure is closely linked to some core Akka concepts and to some of the elements an actor system in Akka consists of. Hence, this article will also serve as a guide to those ideas and components.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;actor-hierarchies&quot;&gt;Actor hierarchies&lt;&#x2F;h2&gt;
&lt;p&gt;Before going into what happens when an error occurs in one of your actors, it&#x27;s essential to introduce one crucial idea underlying the actor approach to concurrency – an idea that is the very foundation for allowing you to build fault-tolerant concurrent applications: Actors are organized in a hierarchy.&lt;&#x2F;p&gt;
&lt;p&gt;So what does this mean? First of all, it means that every single of your actors has got a parent actor, and that each actor can create child actors. Basically, you can think of an actor system as a pyramid of actors. Parent actors watch over their children, just as in real life, taking care that they get back on their feet if they stumble. You will see shortly how exactly this is done.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;the-guardian-actor&quot;&gt;The guardian actor&lt;&#x2F;h3&gt;
&lt;p&gt;In the previous article, we only had two different actors, a &lt;code&gt;Barista&lt;&#x2F;code&gt; actor and a &lt;code&gt;Customer&lt;&#x2F;code&gt; actor. I will not repeat their rather trivial implementations, but focus on how we created instances of these actor types:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;scala&quot; style=&quot;background-color:#002b36;color:#839496;&quot; class=&quot;language-scala &quot;&gt;&lt;code class=&quot;language-scala&quot; data-lang=&quot;scala&quot;&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;import&lt;&#x2F;span&gt;&lt;span&gt; akka.actor.ActorSystem
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;val system &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;ActorSystem&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Coffeehouse&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;val barista &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt; system.actorOf&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Props&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Barista&lt;&#x2F;span&gt;&lt;span&gt;], &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Barista&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;val customer &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt; system.actorOf&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Props&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;classOf[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Customer&lt;&#x2F;span&gt;&lt;span&gt;], barista&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span&gt;, &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Customer&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;As you can see, we create these two actors by calling the &lt;code&gt;actorOf&lt;&#x2F;code&gt; method defined on the &lt;code&gt;ActorSystem&lt;&#x2F;code&gt; type.&lt;&#x2F;p&gt;
&lt;p&gt;So what is the parent of these two actors? Is it the actor system? Not quite, but close. The actor system is not an actor itself, but it has got a so-called &lt;em&gt;guardian actor&lt;&#x2F;em&gt; that serves as the parent of all root-level user actors, i.e. actors we create by calling &lt;code&gt;actorOf&lt;&#x2F;code&gt; on our actor system.&lt;&#x2F;p&gt;
&lt;p&gt;There shouldn&#x27;t be a whole lot of actors in your system that are children of the guardian actor. It really makes more sense to have only a few top-level actors, each of them delegating most of the work to their children.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;actor-paths&quot;&gt;Actor paths&lt;&#x2F;h3&gt;
&lt;p&gt;The hierarchical structure of an actor system becomes apparent when looking at the &lt;em&gt;actor paths&lt;&#x2F;em&gt; of the actors you create. These are basically URLs by which actors can be addressed. You can get an actor&#x27;s path by calling &lt;code&gt;path&lt;&#x2F;code&gt; on its &lt;code&gt;ActorRef&lt;&#x2F;code&gt;:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;scala&quot; style=&quot;background-color:#002b36;color:#839496;&quot; class=&quot;language-scala &quot;&gt;&lt;code class=&quot;language-scala&quot; data-lang=&quot;scala&quot;&gt;&lt;span&gt;barista.path &lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;&#x2F;&#x2F; =&amp;gt; akka.actor.ActorPath = akka:&#x2F;&#x2F;Coffeehouse&#x2F;user&#x2F;Barista
&lt;&#x2F;span&gt;&lt;span&gt;customer.path &lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;&#x2F;&#x2F; =&amp;gt; akka.actor.ActorPath = akka:&#x2F;&#x2F;Coffeehouse&#x2F;user&#x2F;Customer
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The &lt;code&gt;akka&lt;&#x2F;code&gt; protocol is followed by the name of our actor system, the name of the user guardian actor and, finally, the name we gave our actor when calling &lt;code&gt;actorOf&lt;&#x2F;code&gt; on the system. In the case of remote actors, running on different machines, you would additionally see a host and a port.&lt;&#x2F;p&gt;
&lt;p&gt;Actor paths can be used to look up another actor. For example, instead of requiring the barista reference in its constructor, the &lt;code&gt;Customer&lt;&#x2F;code&gt; actor could call the &lt;code&gt;actorSelection&lt;&#x2F;code&gt; method on its &lt;code&gt;ActorContext&lt;&#x2F;code&gt;, passing in a relative path to retrieve a reference to the barista:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;scala&quot; style=&quot;background-color:#002b36;color:#839496;&quot; class=&quot;language-scala &quot;&gt;&lt;code class=&quot;language-scala&quot; data-lang=&quot;scala&quot;&gt;&lt;span&gt;context.actorSelection&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;..&#x2F;Barista&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;However, while being able to look up an actor by its path can sometimes come in handy, it&#x27;s often a much better idea to pass in dependencies in the constructor, just as we did before. Too much intimate knowledge about where your dependencies are located in the actor system makes your system more susceptible to bugs, and it will be difficult to refactor later on.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;an-example-hierarchy&quot;&gt;An example hierarchy&lt;&#x2F;h3&gt;
&lt;p&gt;To illustrate how parents watch over their children and what this has got to do with keeping your system fault-tolerant, I&#x27;m going to stick to the domain of the coffeehouse. Let&#x27;s give the &lt;code&gt;Barista&lt;&#x2F;code&gt; actor a child actor to which it can delegate some of the work involved in running a coffeehouse.&lt;&#x2F;p&gt;
&lt;p&gt;If we really were to model the work of a barista, we were likely giving them a bunch of child actors for all the various subtasks. But to keep this article focused, we have to be a little simplistic with our example.&lt;&#x2F;p&gt;
&lt;p&gt;Let&#x27;s assume that the barista has got a register. It processes transactions, printing appropriate receipts and incrementing the day&#x27;s sales so far. Here is a first version of it:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;scala&quot; style=&quot;background-color:#002b36;color:#839496;&quot; class=&quot;language-scala &quot;&gt;&lt;code class=&quot;language-scala&quot; data-lang=&quot;scala&quot;&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;import&lt;&#x2F;span&gt;&lt;span&gt; akka.actor.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d33682;&quot;&gt;_
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;object&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt; Register &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#93a1a1;&quot;&gt;sealed &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;trait&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt; Article
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;case object&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt; Espresso &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;extends &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;Article
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;case object&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt; Cappuccino &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;extends &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;Article
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;case class&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt; Transaction&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;article&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Article&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;}
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;class&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt; Register &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;extends &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;Actor &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;import&lt;&#x2F;span&gt;&lt;span&gt; Register.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d33682;&quot;&gt;_
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;import&lt;&#x2F;span&gt;&lt;span&gt; Barista.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d33682;&quot;&gt;_
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;var revenue &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;0
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;val prices &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Map&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Article&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;Int&lt;&#x2F;span&gt;&lt;span&gt;]&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Espresso&lt;&#x2F;span&gt;&lt;span&gt; -&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;150&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Cappuccino&lt;&#x2F;span&gt;&lt;span&gt; -&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;250&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;receive &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= {
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;case Transaction&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;article&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;=&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;      &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;val price &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt; prices&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;article&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;      sender ! createReceipt&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;price&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;      revenue += price
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;}
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;createReceipt&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;price&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;Int&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Receipt &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Receipt&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;price&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;It contains an immutable map of the prices for each article, and an integer variable representing the revenue. Whenever it receives a &lt;code&gt;Transaction&lt;&#x2F;code&gt; message, it increments the revenue accordingly and returns a printed receipt to the sender.&lt;&#x2F;p&gt;
&lt;p&gt;The &lt;code&gt;Register&lt;&#x2F;code&gt; actor, as already mentioned, is supposed to be a child actor of the &lt;code&gt;Barista&lt;&#x2F;code&gt; actor, which means that we will not create it from our actor system, but from within our &lt;code&gt;Barista&lt;&#x2F;code&gt; actor. The initial version of our actor-come-parent looks like this:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;scala&quot; style=&quot;background-color:#002b36;color:#839496;&quot; class=&quot;language-scala &quot;&gt;&lt;code class=&quot;language-scala&quot; data-lang=&quot;scala&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;object&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt; Barista &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;case object&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt; EspressoRequest
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;case object&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt; ClosingTime
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;case class&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt; EspressoCup&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;state&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;EspressoCup&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;State&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;object&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt; EspressoCup &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#93a1a1;&quot;&gt;sealed &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;trait&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt; State
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;case object&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt; Clean &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;extends &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;State
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;case object&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt; Filled &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;extends &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;State
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;case object&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt; Dirty &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;extends &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;State
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;}
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;case class&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt; Receipt&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;amount&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;Int&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;}
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;class&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt; Barista &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;extends &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;Actor &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;import&lt;&#x2F;span&gt;&lt;span&gt; Barista.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d33682;&quot;&gt;_
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;import&lt;&#x2F;span&gt;&lt;span&gt; Register.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d33682;&quot;&gt;_
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;import&lt;&#x2F;span&gt;&lt;span&gt; EspressoCup.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d33682;&quot;&gt;_
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;import&lt;&#x2F;span&gt;&lt;span&gt; context.dispatcher
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;import&lt;&#x2F;span&gt;&lt;span&gt; akka.util.Timeout
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;import&lt;&#x2F;span&gt;&lt;span&gt; akka.pattern.ask
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;import&lt;&#x2F;span&gt;&lt;span&gt; akka.pattern.pipe
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;import&lt;&#x2F;span&gt;&lt;span&gt; concurrent.duration.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d33682;&quot;&gt;_
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#93a1a1;&quot;&gt;implicit &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;val timeout &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Timeout&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;4&lt;&#x2F;span&gt;&lt;span&gt;.seconds&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;val register &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt; context.actorOf&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Props&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Register&lt;&#x2F;span&gt;&lt;span&gt;], &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Register&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;receive &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= {
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;case EspressoRequest &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;=&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;      &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;val receipt &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt; register ? &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Transaction&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Espresso&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;      receipt.map&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;((&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;EspressoCup&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Filled&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d33682;&quot;&gt;_&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;))&lt;&#x2F;span&gt;&lt;span&gt;.pipeTo&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;sender&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;case ClosingTime &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;=&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; context.stop&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;self&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;}
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;First off, we define the message types that our &lt;code&gt;Barista&lt;&#x2F;code&gt; actor is able to deal with. An &lt;code&gt;EspressoCup&lt;&#x2F;code&gt; can have one out of a fixed set of states, which we ensure by using a sealed trait.&lt;&#x2F;p&gt;
&lt;p&gt;The more interesting part is to be found in the implementation of the &lt;code&gt;Barista&lt;&#x2F;code&gt; class. The &lt;code&gt;dispatcher&lt;&#x2F;code&gt;, &lt;code&gt;ask&lt;&#x2F;code&gt;, and &lt;code&gt;pipe&lt;&#x2F;code&gt; imports as well as the implicit timeout are required because we make use of Akka&#x27;s ask syntax and futures in our &lt;code&gt;Receive&lt;&#x2F;code&gt; partial function: When we receive an &lt;code&gt;EspressoRequest&lt;&#x2F;code&gt;, we ask the &lt;code&gt;Register&lt;&#x2F;code&gt; actor for a &lt;code&gt;Receipt&lt;&#x2F;code&gt; for our &lt;code&gt;Transaction&lt;&#x2F;code&gt;. This is then combined with a filled espresso cup and piped to the sender, which will thus receive a tuple of type &lt;code&gt;(EspressoCup, Receipt)&lt;&#x2F;code&gt;. This kind of delegating subtasks to child actors and then aggregating or amending their work is typical for actor-based applications.&lt;&#x2F;p&gt;
&lt;p&gt;Also, note how we create our child actor by calling &lt;code&gt;actorOf&lt;&#x2F;code&gt; on our &lt;code&gt;ActorContext&lt;&#x2F;code&gt; instead of the &lt;code&gt;ActorSystem&lt;&#x2F;code&gt;. By doing so, the actor we create becomes a child actor of the one who called this method, instead of a top-level actor whose parent is the guardian actor.&lt;&#x2F;p&gt;
&lt;p&gt;Finally, here is our &lt;code&gt;Customer&lt;&#x2F;code&gt; actor, which, like the &lt;code&gt;Barista&lt;&#x2F;code&gt; actor, will sit at the top level, just below the guardian actor:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;scala&quot; style=&quot;background-color:#002b36;color:#839496;&quot; class=&quot;language-scala &quot;&gt;&lt;code class=&quot;language-scala&quot; data-lang=&quot;scala&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;object&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt; Customer &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;case object&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt; CaffeineWithdrawalWarning
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;}
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;class&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt; Customer&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;coffeeSource&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;ActorRef&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;extends &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;Actor &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;with &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;ActorLogging &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;import&lt;&#x2F;span&gt;&lt;span&gt; Customer.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d33682;&quot;&gt;_
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;import&lt;&#x2F;span&gt;&lt;span&gt; Barista.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d33682;&quot;&gt;_
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;import&lt;&#x2F;span&gt;&lt;span&gt; EspressoCup.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d33682;&quot;&gt;_
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;receive &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= {
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;case CaffeineWithdrawalWarning &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;=&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; coffeeSource ! &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;EspressoRequest
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;case &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;EspressoCup&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Filled&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Receipt&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;amount&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;=&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;      log.info&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;s&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;yay, caffeine for ${&lt;&#x2F;span&gt;&lt;span&gt;self&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;}!&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;}
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;It is not terribly interesting for our tutorial, which focuses more on the &lt;code&gt;Barista&lt;&#x2F;code&gt; actor hierarchy. What&#x27;s new is the use of the &lt;code&gt;ActorLogging&lt;&#x2F;code&gt; trait, which allows us to write to the log instead of printing to the console.&lt;&#x2F;p&gt;
&lt;p&gt;Now, if we create our actor system and populate it with a &lt;code&gt;Barista&lt;&#x2F;code&gt; and two &lt;code&gt;Customer&lt;&#x2F;code&gt; actors, we can happily feed our two under-caffeinated addicts with a shot of black gold:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;scala&quot; style=&quot;background-color:#002b36;color:#839496;&quot; class=&quot;language-scala &quot;&gt;&lt;code class=&quot;language-scala&quot; data-lang=&quot;scala&quot;&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;import&lt;&#x2F;span&gt;&lt;span&gt; Customer.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d33682;&quot;&gt;_
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;val system &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;ActorSystem&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Coffeehouse&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;val barista &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt; system.actorOf&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Props&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Barista&lt;&#x2F;span&gt;&lt;span&gt;], &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Barista&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;val customerJohnny &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt; system.actorOf&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Props&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;classOf[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Customer&lt;&#x2F;span&gt;&lt;span&gt;], barista&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span&gt;, &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Johnny&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;val customerAlina &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt; system.actorOf&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Props&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;classOf[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Customer&lt;&#x2F;span&gt;&lt;span&gt;], barista&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span&gt;, &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Alina&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;customerJohnny ! &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;CaffeineWithdrawalWarning
&lt;&#x2F;span&gt;&lt;span&gt;customerAlina ! &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;CaffeineWithdrawalWarning
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;If you try this out, you should see two log messages from happy customers.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;to-crash-or-not-to-crash&quot;&gt;To crash or not to crash?&lt;&#x2F;h2&gt;
&lt;p&gt;Of course, what we are really interested in, at least in this article, is not happy customers, but the question of what happens if things go wrong.&lt;&#x2F;p&gt;
&lt;p&gt;Our register is a fragile device – its printing functionality is not as reliable as it should be. Every so often, a paper jam causes it to fail. Let&#x27;s add a &lt;code&gt;PaperJamException&lt;&#x2F;code&gt; type to the &lt;code&gt;Register&lt;&#x2F;code&gt; companion object:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;scala&quot; style=&quot;background-color:#002b36;color:#839496;&quot; class=&quot;language-scala &quot;&gt;&lt;code class=&quot;language-scala&quot; data-lang=&quot;scala&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;class&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt; PaperJamException&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;msg&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;String&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;extends &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;Exception&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;msg&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Then, let&#x27;s change the &lt;code&gt;createReceipt&lt;&#x2F;code&gt; method in our &lt;code&gt;Register&lt;&#x2F;code&gt; actor accordingly:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;scala&quot; style=&quot;background-color:#002b36;color:#839496;&quot; class=&quot;language-scala &quot;&gt;&lt;code class=&quot;language-scala&quot; data-lang=&quot;scala&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;createReceipt&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;price&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;Int&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Receipt &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= {
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;import&lt;&#x2F;span&gt;&lt;span&gt; util.Random
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;if &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Random&lt;&#x2F;span&gt;&lt;span&gt;.nextBoolean&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;())
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;throw new PaperJamException&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;OMG, not again!&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Receipt&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;price&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Now, when processing a &lt;code&gt;Transaction&lt;&#x2F;code&gt; message, our &lt;code&gt;Register&lt;&#x2F;code&gt; actor will throw a &lt;code&gt;PaperJamException&lt;&#x2F;code&gt; in about half of the cases.&lt;&#x2F;p&gt;
&lt;p&gt;What effect does this have on our actor system, or on our whole application? Luckily, Akka is very robust and not affected by exceptions in our code at all. What happens, though, is that the parent of the misbehaving child is notified – remember that parents are watching over their children, and this is the situation where they have to decide what to do.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;supervisor-strategies&quot;&gt;Supervisor strategies&lt;&#x2F;h3&gt;
&lt;p&gt;The whole act of being notified about exceptions in child actors, however, is not handled by the parent actor&#x27;s &lt;code&gt;Receive&lt;&#x2F;code&gt; partial function, as that would confound the parent actor&#x27;s own behaviour with the logic for dealing with failure in its children. Instead, the two responsibilities are clearly separated.&lt;&#x2F;p&gt;
&lt;p&gt;Each actor defines its own &lt;em&gt;supervisor strategy&lt;&#x2F;em&gt;, which tells Akka how to deal with certain types of errors occurring in your children.&lt;&#x2F;p&gt;
&lt;p&gt;There are basically two different types of supervisor strategy, the &lt;code&gt;OneForOneStrategy&lt;&#x2F;code&gt; and the &lt;code&gt;AllForOneStrategy&lt;&#x2F;code&gt;. Choosing the former means that the way you want to deal with an error in one of your children will only affect the child actor from which the error originated, whereas the latter will affect all of your child actors. Which of those strategies is best depends a lot on your individual application.&lt;&#x2F;p&gt;
&lt;p&gt;Regardless of which type of &lt;code&gt;SupervisorStrategy&lt;&#x2F;code&gt; you choose for your actor, you will have to specify a &lt;code&gt;Decider&lt;&#x2F;code&gt;, which is a &lt;code&gt;PartialFunction[Throwable, Directive]&lt;&#x2F;code&gt; – this allows you to match against certain subtypes of &lt;code&gt;Throwable&lt;&#x2F;code&gt; and decide for each of them what&#x27;s supposed to happen to your problematic child actor (or all your child actors, if you chose the all-for-one strategy).&lt;&#x2F;p&gt;
&lt;h3 id=&quot;directives&quot;&gt;Directives&lt;&#x2F;h3&gt;
&lt;p&gt;Here is a list of the available directives:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;scala&quot; style=&quot;background-color:#002b36;color:#839496;&quot; class=&quot;language-scala &quot;&gt;&lt;code class=&quot;language-scala&quot; data-lang=&quot;scala&quot;&gt;&lt;span style=&quot;color:#93a1a1;&quot;&gt;sealed &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;trait&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt; Directive
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;case object&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt; Resume &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;extends &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;Directive
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;case object&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt; Restart &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;extends &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;Directive
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;case object&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt; Stop &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;extends &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;Directive
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;case object&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt; Escalate &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;extends &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;Directive
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Resume:&lt;&#x2F;strong&gt; If you choose to &lt;code&gt;Resume&lt;&#x2F;code&gt;, this probably means that you think of your child actor as a little bit of a drama queen. You decide that the exception was not so exceptional after all – the child actor or actors will simply resume processing messages as if nothing extraordinary had happened.&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Restart:&lt;&#x2F;strong&gt; The &lt;code&gt;Restart&lt;&#x2F;code&gt; directive causes Akka to create a new instance of your child actor or actors. The reasoning behind this is that you assume that the internal state of the child&#x2F;children is corrupted in some way so that it can no longer process any further messages. By restarting the actor, you hope to put it into a clean state again.&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Stop:&lt;&#x2F;strong&gt; You effectively kill the actor. It will not be restarted.&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Escalate:&lt;&#x2F;strong&gt; If you choose to &lt;code&gt;Escalate&lt;&#x2F;code&gt;, you probably don&#x27;t know how to deal with the failure at hand. You delegate the decision about what to do to your own parent actor, hoping they are wiser than you. If an actor escalates, they may very well be restarted themselves by their parent, as the parent will only decide about its own child actors.&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;h3 id=&quot;the-default-strategy&quot;&gt;The default strategy&lt;&#x2F;h3&gt;
&lt;p&gt;You don&#x27;t have to specify your own supervisor strategy in each and every actor. In fact, we haven&#x27;t done that so far. This means that the default supervisor strategy will take effect. It looks like this:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;scala&quot; style=&quot;background-color:#002b36;color:#839496;&quot; class=&quot;language-scala &quot;&gt;&lt;code class=&quot;language-scala&quot; data-lang=&quot;scala&quot;&gt;&lt;span style=&quot;color:#93a1a1;&quot;&gt;final &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;val defaultStrategy&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;SupervisorStrategy &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= {
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;defaultDecider&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Decider &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= {
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;case &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d33682;&quot;&gt;_&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;ActorInitializationException &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;⇒ &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Stop
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;case &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d33682;&quot;&gt;_&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;ActorKilledException         &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;⇒ &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Stop
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;case &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d33682;&quot;&gt;_&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Exception                    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;⇒ &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Restart
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;}
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;OneForOneStrategy&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;()(&lt;&#x2F;span&gt;&lt;span&gt;defaultDecider&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This means that for exceptions other than &lt;code&gt;ActorInitializationException&lt;&#x2F;code&gt; or &lt;code&gt;ActorKilledException&lt;&#x2F;code&gt;, the respective child actor in which the exception was thrown will be restarted.&lt;&#x2F;p&gt;
&lt;p&gt;Hence, when a &lt;code&gt;PaperJamException&lt;&#x2F;code&gt; occurs in our &lt;code&gt;Register&lt;&#x2F;code&gt; actor, the supervisor strategy of the parent actor (the barista) will cause the &lt;code&gt;Register&lt;&#x2F;code&gt; to be restarted, because we haven&#x27;t overridden the default strategy.&lt;&#x2F;p&gt;
&lt;p&gt;If you try this out, you will likely see an exception stacktrace in the log, but nothing about the &lt;code&gt;Register&lt;&#x2F;code&gt; actor being restarted.&lt;&#x2F;p&gt;
&lt;p&gt;Let&#x27;s verify that this is really happening. To do so, however, you will need to learn about the &lt;em&gt;actor lifecycle&lt;&#x2F;em&gt;.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;the-actor-lifecycle&quot;&gt;The actor lifecycle&lt;&#x2F;h3&gt;
&lt;p&gt;To understand what the directives of a supervisor strategy actually do, it&#x27;s crucial to know a little bit about an actor&#x27;s lifecycle. Basically, it boils down to this: when created via &lt;code&gt;actorOf&lt;&#x2F;code&gt;, an actor is &lt;em&gt;started&lt;&#x2F;em&gt;. It can then be &lt;em&gt;restarted&lt;&#x2F;em&gt; an arbitrary number of times, in case there is a problem with it. Finally, an actor can be &lt;em&gt;stopped&lt;&#x2F;em&gt;, ultimately leading to its death.&lt;&#x2F;p&gt;
&lt;p&gt;There are numerous lifecycle hook methods that an actor implementation can override. It&#x27;s also important to know their default implementations. Let&#x27;s go through them briefly:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;preStart:&lt;&#x2F;strong&gt; Called when an actor is started, allowing you to do some initialization logic. The default implementation is empty.&lt;&#x2F;li&gt;
&lt;li&gt;&lt;strong&gt;postStop:&lt;&#x2F;strong&gt; Empty by default, allowing you to clean up resources. Called after &lt;code&gt;stop&lt;&#x2F;code&gt; has been called for the actor.&lt;&#x2F;li&gt;
&lt;li&gt;&lt;strong&gt;preRestart:&lt;&#x2F;strong&gt; Called right before a crashed actor is restarted. By default, it stops all children of that actor and then calls &lt;code&gt;postStop&lt;&#x2F;code&gt; to allow cleaning up of resources.&lt;&#x2F;li&gt;
&lt;li&gt;&lt;strong&gt;postRestart:&lt;&#x2F;strong&gt; Called immediately after an actor has been restarted. Simply calls &lt;code&gt;preStart&lt;&#x2F;code&gt; by default.&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;Let&#x27;s see if our &lt;code&gt;Register&lt;&#x2F;code&gt; gets indeed restarted upon failure by simply adding some log output to its &lt;code&gt;postRestart&lt;&#x2F;code&gt; method. Make the &lt;code&gt;Register&lt;&#x2F;code&gt; type extend the &lt;code&gt;ActorLogging&lt;&#x2F;code&gt; trait and add the following method to it:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;scala&quot; style=&quot;background-color:#002b36;color:#839496;&quot; class=&quot;language-scala &quot;&gt;&lt;code class=&quot;language-scala&quot; data-lang=&quot;scala&quot;&gt;&lt;span style=&quot;color:#93a1a1;&quot;&gt;override &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;postRestart&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;reason&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Throwable&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;) {
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d33682;&quot;&gt;super&lt;&#x2F;span&gt;&lt;span&gt;.postRestart&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;reason&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;  log.info&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;s&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Restarted because of ${&lt;&#x2F;span&gt;&lt;span&gt;reason.getMessage&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;}&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Now, if you send the two &lt;code&gt;Customer&lt;&#x2F;code&gt; actors a bunch of &lt;code&gt;CaffeineWithdrawalWarning&lt;&#x2F;code&gt; messages, you should see the one or the other of those log outputs, confirming that our &lt;code&gt;Register&lt;&#x2F;code&gt; actor has been restarted.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;death-of-an-actor&quot;&gt;Death of an actor&lt;&#x2F;h3&gt;
&lt;p&gt;Often, it doesn&#x27;t make sense to restart an actor again and again – think of an actor that talks to some other service over the network, and that service has been unreachable for a while. In such cases, it is a very good idea to tell Akka how often to restart an actor within a certain period of time. If that limit is exceeded, the actor is instead stopped and hence dies. Such a limit can be configured in the constructor of the supervisor strategy:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;scala&quot; style=&quot;background-color:#002b36;color:#839496;&quot; class=&quot;language-scala &quot;&gt;&lt;code class=&quot;language-scala&quot; data-lang=&quot;scala&quot;&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;import&lt;&#x2F;span&gt;&lt;span&gt; scala.concurrent.duration.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d33682;&quot;&gt;_
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;import&lt;&#x2F;span&gt;&lt;span&gt; akka.actor.OneForOneStrategy
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;import&lt;&#x2F;span&gt;&lt;span&gt; akka.actor.SupervisorStrategy.Restart
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;OneForOneStrategy&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;10&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;2&lt;&#x2F;span&gt;&lt;span&gt;.minutes&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;) {
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;case &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d33682;&quot;&gt;_ &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;=&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Restart
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h3 id=&quot;the-self-healing-system&quot;&gt;The self-healing system?&lt;&#x2F;h3&gt;
&lt;p&gt;So, is our system running smoothly, healing itself whenever this damn paper jam occurs? Let&#x27;s change our log output:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;scala&quot; style=&quot;background-color:#002b36;color:#839496;&quot; class=&quot;language-scala &quot;&gt;&lt;code class=&quot;language-scala&quot; data-lang=&quot;scala&quot;&gt;&lt;span style=&quot;color:#93a1a1;&quot;&gt;override &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;postRestart&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;reason&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Throwable&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;) {
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d33682;&quot;&gt;super&lt;&#x2F;span&gt;&lt;span&gt;.postRestart&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;reason&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;  log.info&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;s&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Restarted, and revenue is &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;$revenue&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt; cents&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;And while we are at it, let&#x27;s also add some more logging to our &lt;code&gt;Receive&lt;&#x2F;code&gt; partial function, making it look like this:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;scala&quot; style=&quot;background-color:#002b36;color:#839496;&quot; class=&quot;language-scala &quot;&gt;&lt;code class=&quot;language-scala&quot; data-lang=&quot;scala&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;receive &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= {
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;case Transaction&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;article&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;=&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;val price &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt; prices&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;article&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;    sender ! createReceipt&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;price&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;    revenue += price
&lt;&#x2F;span&gt;&lt;span&gt;    log.info&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;s&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Revenue incremented to &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;$revenue&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt; cents&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Ouch! Something is clearly not as it should be. In the log, you will see the revenue increasing, but as soon as there is a paper jam and the &lt;code&gt;Register&lt;&#x2F;code&gt; actor restarts, it is reset to &lt;code&gt;0&lt;&#x2F;code&gt;. This is because restarting indeed means that the old instance is discarded and a new one created as per the &lt;code&gt;Props&lt;&#x2F;code&gt; we initially passed to &lt;code&gt;actorOf&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;Of course, we could change our supervisor strategy, so that it resumes in case of a &lt;code&gt;PaperJamException&lt;&#x2F;code&gt;. We would have to add this to the &lt;code&gt;Barista&lt;&#x2F;code&gt; actor:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;scala&quot; style=&quot;background-color:#002b36;color:#839496;&quot; class=&quot;language-scala &quot;&gt;&lt;code class=&quot;language-scala&quot; data-lang=&quot;scala&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;val decider&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;PartialFunction&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Throwable&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Directive&lt;&#x2F;span&gt;&lt;span&gt;] &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= {
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;case &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d33682;&quot;&gt;_&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;PaperJamException &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;=&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Resume
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;}
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#93a1a1;&quot;&gt;override &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;supervisorStrategy&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;SupervisorStrategy &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;=
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;OneForOneStrategy&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;()(&lt;&#x2F;span&gt;&lt;span&gt;decider.orElse&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;SupervisorStrategy&lt;&#x2F;span&gt;&lt;span&gt;.defaultStrategy.decider&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;))
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Now, the actor is not restarted upon a &lt;code&gt;PaperJamException&lt;&#x2F;code&gt;, so its state is not reset.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;error-kernel&quot;&gt;Error kernel&lt;&#x2F;h3&gt;
&lt;p&gt;So we just found a nice solution to preserve the state of our &lt;code&gt;Register&lt;&#x2F;code&gt; actor, right?&lt;&#x2F;p&gt;
&lt;p&gt;Well, sometimes, simply resuming might be the best thing to do. But let&#x27;s assume that we really &lt;em&gt;have&lt;&#x2F;em&gt; to restart it, because otherwise the paper jam will not disappear. We can simulate this by maintaining a boolean flag that says if we are in a paper jam situation or not. Let&#x27;s change our &lt;code&gt;Register&lt;&#x2F;code&gt; like so:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;scala&quot; style=&quot;background-color:#002b36;color:#839496;&quot; class=&quot;language-scala &quot;&gt;&lt;code class=&quot;language-scala&quot; data-lang=&quot;scala&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;class&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt; Register &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;extends &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;Actor &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;with &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;ActorLogging &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;import&lt;&#x2F;span&gt;&lt;span&gt; Register.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d33682;&quot;&gt;_
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;import&lt;&#x2F;span&gt;&lt;span&gt; Barista.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d33682;&quot;&gt;_
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;var revenue &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;0
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;val prices &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Map&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Article&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;Int&lt;&#x2F;span&gt;&lt;span&gt;]&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Espresso&lt;&#x2F;span&gt;&lt;span&gt; -&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;150&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Cappuccino&lt;&#x2F;span&gt;&lt;span&gt; -&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;250&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;var paperJam &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;false
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#93a1a1;&quot;&gt;override &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;postRestart&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;reason&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Throwable&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;) {
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d33682;&quot;&gt;super&lt;&#x2F;span&gt;&lt;span&gt;.postRestart&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;reason&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;    log.info&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;s&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Restarted, and revenue is &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;$revenue&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt; cents&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;}
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;receive &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= {
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;case Transaction&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;article&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;=&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;      &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;val price &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt; prices&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;article&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;      sender ! createReceipt&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;price&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;      revenue += price
&lt;&#x2F;span&gt;&lt;span&gt;      log.info&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;s&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Revenue incremented to &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;$revenue&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt; cents&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;}
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;createReceipt&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;price&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;Int&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Receipt &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= {
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;import&lt;&#x2F;span&gt;&lt;span&gt; util.Random
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;if &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Random&lt;&#x2F;span&gt;&lt;span&gt;.nextBoolean&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;())&lt;&#x2F;span&gt;&lt;span&gt; paperJam &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;true
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;if &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;paperJam&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;throw new PaperJamException&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;OMG, not again!&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Receipt&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;price&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;}
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Also remove the supervisor strategy we added to the &lt;code&gt;Barista&lt;&#x2F;code&gt; actor.&lt;&#x2F;p&gt;
&lt;p&gt;Now, the paper jam remains forever, until we have restarted the actor. Alas, we cannot do that without also losing important state regarding our revenue.&lt;&#x2F;p&gt;
&lt;p&gt;This is where the &lt;em&gt;error kernel&lt;&#x2F;em&gt; pattern comes in. Basically, it is just a simple guideline you should always try to follow, stating that if an actor carries important internal state, then it should delegate dangerous tasks to child actors, so as to prevent the state-carrying actor from crashing. Sometimes, it may make sense to spawn a new child actor for each such task, but that&#x27;s not a necessity.&lt;&#x2F;p&gt;
&lt;p&gt;The essence of the pattern is to keep important state as far at the top of the actor hierarchy as possible, while pushing error-prone tasks as far to the bottom of the hierarchy as possible.&lt;&#x2F;p&gt;
&lt;p&gt;Let&#x27;s apply this pattern to our &lt;code&gt;Register&lt;&#x2F;code&gt; actor. We will keep the revenue state in the &lt;code&gt;Register&lt;&#x2F;code&gt; actor, but move the error-prone behaviour of printing the receipt to a new child actor, which we appropriately enough call &lt;code&gt;ReceiptPrinter&lt;&#x2F;code&gt;. Here is the latter:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;scala&quot; style=&quot;background-color:#002b36;color:#839496;&quot; class=&quot;language-scala &quot;&gt;&lt;code class=&quot;language-scala&quot; data-lang=&quot;scala&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;object&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt; ReceiptPrinter &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;case class&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt; PrintJob&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;amount&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;Int&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;class&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt; PaperJamException&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;msg&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;String&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;extends &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;Exception&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;msg&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;}
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;class&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt; ReceiptPrinter &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;extends &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;Actor &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;with &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;ActorLogging &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;var paperJam &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;false
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#93a1a1;&quot;&gt;override &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;postRestart&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;reason&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Throwable&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;) {
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d33682;&quot;&gt;super&lt;&#x2F;span&gt;&lt;span&gt;.postRestart&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;reason&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;    log.info&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;s&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Restarted, paper jam == &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;$paperJam&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;}
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;receive &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= {
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;case PrintJob&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;amount&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;=&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; sender ! createReceipt&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;amount&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;}
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;createReceipt&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;price&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;Int&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Receipt &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= {
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;if &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Random&lt;&#x2F;span&gt;&lt;span&gt;.nextBoolean&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;())&lt;&#x2F;span&gt;&lt;span&gt; paperJam &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;true
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;if &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;paperJam&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;throw new PaperJamException&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;OMG, not again!&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Receipt&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;price&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;}
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Again, we simulate the paper jam with a boolean flag and throw an exception each time someone asks us to print a receipt while in a paper jam. Other than the new message type, &lt;code&gt;PrintJob&lt;&#x2F;code&gt;, this is really just extracted from the &lt;code&gt;Register&lt;&#x2F;code&gt; type.&lt;&#x2F;p&gt;
&lt;p&gt;This is a good thing, not only because it moves away this dangerous operation from the stateful &lt;code&gt;Register&lt;&#x2F;code&gt; actor, but it also makes our code simpler and consequently easier to reason about: The &lt;code&gt;ReceiptPrinter&lt;&#x2F;code&gt; actor is responsible for exactly one thing, and the &lt;code&gt;Register&lt;&#x2F;code&gt; actor has become simpler, too, now being only responsible for managing the revenue, delegating the remaining functionality to a child actor:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;scala&quot; style=&quot;background-color:#002b36;color:#839496;&quot; class=&quot;language-scala &quot;&gt;&lt;code class=&quot;language-scala&quot; data-lang=&quot;scala&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;class&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt; Register &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;extends &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;Actor &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;with &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;ActorLogging &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;import&lt;&#x2F;span&gt;&lt;span&gt; akka.pattern.ask
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;import&lt;&#x2F;span&gt;&lt;span&gt; akka.pattern.pipe
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;import&lt;&#x2F;span&gt;&lt;span&gt; context.dispatcher
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#93a1a1;&quot;&gt;implicit &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;val timeout &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Timeout&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;4&lt;&#x2F;span&gt;&lt;span&gt;.seconds&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;var revenue &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;0
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;val prices &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Map&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Article&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;Int&lt;&#x2F;span&gt;&lt;span&gt;]&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Espresso&lt;&#x2F;span&gt;&lt;span&gt; -&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;150&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Cappuccino&lt;&#x2F;span&gt;&lt;span&gt; -&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;250&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;val printer &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt; context.actorOf&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Props&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;ReceiptPrinter&lt;&#x2F;span&gt;&lt;span&gt;], &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Printer&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#93a1a1;&quot;&gt;override &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;postRestart&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;reason&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Throwable&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;) {
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d33682;&quot;&gt;super&lt;&#x2F;span&gt;&lt;span&gt;.postRestart&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;reason&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;    log.info&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;s&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Restarted, and revenue is &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;$revenue&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt; cents&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;}
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;receive &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= {
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;case Transaction&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;article&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;=&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;      &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;val price &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt; prices&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;article&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;      &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;val requester &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt; sender
&lt;&#x2F;span&gt;&lt;span&gt;      &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;printer ? &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;PrintJob&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;price&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;))&lt;&#x2F;span&gt;&lt;span&gt;.map&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;((&lt;&#x2F;span&gt;&lt;span&gt;requester, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d33682;&quot;&gt;_&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;))&lt;&#x2F;span&gt;&lt;span&gt;.pipeTo&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;self&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;case &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;requester&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;ActorRef&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;receipt&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Receipt&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;=&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;      revenue += receipt.amount
&lt;&#x2F;span&gt;&lt;span&gt;      log.info&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;s&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;revenue is &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;$revenue&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt; cents&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;      requester ! receipt
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;}
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;We don&#x27;t spawn a new &lt;code&gt;ReceiptPrinter&lt;&#x2F;code&gt; for each &lt;code&gt;Transaction&lt;&#x2F;code&gt; message we get. Instead, we use the default supervisor strategy to have the printer actor restart upon failure.&lt;&#x2F;p&gt;
&lt;p&gt;One part that merits explanation is the weird way we increment our revenue: First we ask the printer for a receipt. We map the future to a tuple containing the answer as well as the requester, which is the sender of the &lt;code&gt;Transaction&lt;&#x2F;code&gt; message and pipe this to ourselves. When processing that message, we finally increment the revenue and send the receipt to the requester.&lt;&#x2F;p&gt;
&lt;p&gt;The reason for that indirection is that we want to make sure that we only increment our revenue if the receipt was successfully printed. Since it is vital to never ever modify the internal state of an actor inside of a future, we have to use this level of indirection. It helps us make sure that we only change the revenue within the confines of our actor, and not on some other thread.&lt;&#x2F;p&gt;
&lt;p&gt;Assigning the &lt;code&gt;sender&lt;&#x2F;code&gt; to a &lt;code&gt;val&lt;&#x2F;code&gt; is necessary for similar reasons: When mapping a future, we are no longer in the context of our actor either – since &lt;code&gt;sender&lt;&#x2F;code&gt; is a method, it would now likely return the reference to some other actor that has sent us a message, not the one we intended.&lt;&#x2F;p&gt;
&lt;p&gt;Now, our &lt;code&gt;Register&lt;&#x2F;code&gt; actor is safe from constantly being restarted, yay!&lt;&#x2F;p&gt;
&lt;p&gt;Of course, the very idea of having the printing of the receipt and the management of the revenue in one place is questionable. Having them together came in handy for demonstrating the error kernel pattern. Yet, it would certainly be a lot better to seperate the receipt printing from the revenue management altogether, as these are two concerns that don&#x27;t really belong together.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;timeouts&quot;&gt;Timeouts&lt;&#x2F;h3&gt;
&lt;p&gt;Another thing that we may want to improve upon is the handling of timeouts. Currently, when an exception occurs in the &lt;code&gt;ReceiptPrinter&lt;&#x2F;code&gt;, this leads to an &lt;code&gt;AskTimeoutException&lt;&#x2F;code&gt;, which, since we are using the ask syntax, comes back to the &lt;code&gt;Barista&lt;&#x2F;code&gt; actor in an unsuccessfully completed &lt;code&gt;Future&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;Since the &lt;code&gt;Barista&lt;&#x2F;code&gt; actor simply maps over that future (which is success-biased) and then pipes the transformed result to the customer, the customer will also receive a &lt;code&gt;Failure&lt;&#x2F;code&gt; containing an &lt;code&gt;AskTimeoutException&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;The &lt;code&gt;Customer&lt;&#x2F;code&gt; didn&#x27;t ask for anything, though, so it is certainly not expecting such a message, and in fact, it currently doesn&#x27;t handle these messages. Let&#x27;s be friendly and send customers a &lt;code&gt;ComebackLater&lt;&#x2F;code&gt; message – this is a message they already understand, and it makes them try to get an espresso at a later point. This is clearly better, as the current solution means they will never know that they will not get their espresso.&lt;&#x2F;p&gt;
&lt;p&gt;To achieve this, let&#x27;s recover from &lt;code&gt;AskTimeoutException&lt;&#x2F;code&gt; failures by mapping them to &lt;code&gt;ComebackLater&lt;&#x2F;code&gt; messages. The &lt;code&gt;Receive&lt;&#x2F;code&gt; partial function of our &lt;code&gt;Barista&lt;&#x2F;code&gt; actor thus now looks like this:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;scala&quot; style=&quot;background-color:#002b36;color:#839496;&quot; class=&quot;language-scala &quot;&gt;&lt;code class=&quot;language-scala&quot; data-lang=&quot;scala&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;receive &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= {
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;case EspressoRequest &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;=&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;val receipt &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt; register ? &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Transaction&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Espresso&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;    receipt.map&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;((&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;EspressoCup&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Filled&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d33682;&quot;&gt;_&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;))&lt;&#x2F;span&gt;&lt;span&gt;.recover &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;      &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;case &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d33682;&quot;&gt;_&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;AskTimeoutException &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;=&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;ComebackLater
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;}&lt;&#x2F;span&gt;&lt;span&gt; pipeTo&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;sender&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;case ClosingTime &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;=&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; context.system.shutdown&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;()
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Now, the &lt;code&gt;Customer&lt;&#x2F;code&gt; actors know they can try their luck later, and after trying often enough, they should finally get their eagerly anticipated espresso.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;death-watch&quot;&gt;Death Watch&lt;&#x2F;h3&gt;
&lt;p&gt;Another principle that is important in order to keep your system fault-tolerant is to keep a watch on important dependencies – dependencies as opposed to children.&lt;&#x2F;p&gt;
&lt;p&gt;Sometimes, you have actors that depend on other actors without the latter being their children. This means that they can&#x27;t be their supervisors. Yet, it is important to keep a watch on their state and be notified if bad things happen.&lt;&#x2F;p&gt;
&lt;p&gt;Think, for instance, of an actor that is responsible for database access. You will want actors that require this actor to be alive and healthy to know when that is no longer the case. Maybe you want to switch your system to a maintenance mode in such a situation. For other use cases, simply using some kind of backup actor as a replacement for the dead actor may be a viable solution.&lt;&#x2F;p&gt;
&lt;p&gt;In any case, you will need to place a watch on an actor you depend on in order to get the sad news of its passing away. This is done by calling the &lt;code&gt;watch&lt;&#x2F;code&gt; method defined on &lt;code&gt;ActorContext&lt;&#x2F;code&gt;. To illustrate, let&#x27;s have our &lt;code&gt;Customer&lt;&#x2F;code&gt; actors watch the &lt;code&gt;Barista&lt;&#x2F;code&gt; – they are highly addicted to caffeine, so it&#x27;s fair to say they depend on the barista:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;scala&quot; style=&quot;background-color:#002b36;color:#839496;&quot; class=&quot;language-scala &quot;&gt;&lt;code class=&quot;language-scala&quot; data-lang=&quot;scala&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;class&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt; Customer&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;coffeeSource&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;ActorRef&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;extends &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;Actor &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;with &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;ActorLogging &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;import&lt;&#x2F;span&gt;&lt;span&gt; context.dispatcher
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;  context.watch&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;coffeeSource&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;receive &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= {
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;case CaffeineWithdrawalWarning &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;=&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; coffeeSource ! &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;EspressoRequest
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;case &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;EspressoCup&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Filled&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Receipt&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;amount&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;=&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;      log.info&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;s&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;yay, caffeine for ${&lt;&#x2F;span&gt;&lt;span&gt;self&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;}!&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;case ComebackLater &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;=&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;      log.info&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;grumble, grumble&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;      context.system.scheduler.scheduleOnce&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;300&lt;&#x2F;span&gt;&lt;span&gt;.millis&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;) {
&lt;&#x2F;span&gt;&lt;span&gt;        coffeeSource ! &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;EspressoRequest
&lt;&#x2F;span&gt;&lt;span&gt;      &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;}
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;case Terminated&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;barista&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;=&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;      log.info&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Oh well, let&amp;#39;s find another coffeehouse...&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;}
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;We start watching our &lt;code&gt;coffeeSource&lt;&#x2F;code&gt; in our constructor, and we added a new case for messages of type &lt;code&gt;Terminated&lt;&#x2F;code&gt; – this is the kind of message we will receive from Akka if an actor we watch dies.&lt;&#x2F;p&gt;
&lt;p&gt;Now, if we send a &lt;code&gt;ClosingTime&lt;&#x2F;code&gt; to the message and the &lt;code&gt;Barista&lt;&#x2F;code&gt; tells its context to stop itself, the &lt;code&gt;Customer&lt;&#x2F;code&gt; actors will be notified. Give it a try, and you should see their output in the log.&lt;&#x2F;p&gt;
&lt;p&gt;Instead of simply logging that we are not amused, this could just as well initiate some failover logic, for instance.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;summary&quot;&gt;Summary&lt;&#x2F;h2&gt;
&lt;p&gt;In this part of the series, which is the second one dealing with actors and Akka, you got to know some of the important components of an actor system, all while learning how to put the tools provided by Akka and the ideas behind it to use in order to make your system more fault-tolerant.&lt;&#x2F;p&gt;
&lt;p&gt;While there is still a lot more to learn about the actor model and Akka, we shall leave it at that for now, as this would go beyond the scope of this series. In the next part, which shall bring this series to a conclusion, I will point you to a bunch of Scala resources you may want to peruse to continue your journey through Scala land, and if actors and Akka got you excited, there will be something in there for you, too.&lt;&#x2F;p&gt;
</description>
      </item>
      <item>
          <title>The Neophyte&#x27;s Guide to Scala Part 14: The Actor approach to concurrency</title>
          <pubDate>Wed, 27 Feb 2013 00:00:00 +0000</pubDate>
          <author>Unknown</author>
          <link>https://danielwestheide.com/blog/the-neophytes-guide-to-scala-part-14-the-actor-approach-to-concurrency/</link>
          <guid>https://danielwestheide.com/blog/the-neophytes-guide-to-scala-part-14-the-actor-approach-to-concurrency/</guid>
          <description xml:base="https://danielwestheide.com/blog/the-neophytes-guide-to-scala-part-14-the-actor-approach-to-concurrency/">&lt;p&gt;After several articles about how you can leverage the Scala type system to achieve a great amount of both flexibility and compile-time safety, we are now shifting back to a topic that we already tackled previously in this series: Scala&#x27;s take on concurrency.&lt;&#x2F;p&gt;
&lt;p&gt;In &lt;a href=&quot;http:&#x2F;&#x2F;danielwestheide.com&#x2F;blog&#x2F;2013&#x2F;01&#x2F;09&#x2F;the-neophytes-guide-to-scala-part-8-welcome-to-the-future.html&quot;&gt;these&lt;&#x2F;a&gt; earlier &lt;a href=&quot;http:&#x2F;&#x2F;danielwestheide.com&#x2F;blog&#x2F;2013&#x2F;01&#x2F;16&#x2F;the-neophytes-guide-to-scala-part-9-promises-and-futures-in-practice.html&quot;&gt;articles&lt;&#x2F;a&gt;, you learned about an approach that allows you to work asynchronously by making use of composable Futures.&lt;&#x2F;p&gt;
&lt;p&gt;This approach is a very good fit for numerous problems. However, it&#x27;s not the only one Scala has to offer. A second cornerstone of Scala concurrency is the &lt;em&gt;Actor&lt;&#x2F;em&gt; model. It provides an approach to concurrency that is entirely based on passing messages between processes.&lt;&#x2F;p&gt;
&lt;p&gt;Actors are not a new idea – the most prominent implementation of this model can be found in Erlang. The Scala core library has had its own actors library for a long time, but it faces the destiny of deprecation in the coming Scala version 2.11, when it will ultimately be replaced by the actors implementation provided by the &lt;a href=&quot;http:&#x2F;&#x2F;akka.io&quot;&gt;Akka&lt;&#x2F;a&gt; toolkit, which has been a de-facto standard for actor-based development with Scala for quite a while.&lt;&#x2F;p&gt;
&lt;p&gt;In this article, you will be introduced to the rationale behind Akka&#x27;s actor model and learn the basics of coding within this paradigm using the Akka toolkit. It is by no means an in-depth discussion of everything you need to know about Akka actors, and in that, it differs from most of the previous articles in this series. Rather, the intention is to familiarize you with the Akka mindset and serve as an initial spark to get you excited about it.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;the-problems-with-shared-mutable-state&quot;&gt;The problems with shared mutable state&lt;&#x2F;h2&gt;
&lt;p&gt;The predominant approach to concurrency today is that of shared mutable state – a large number of stateful objects whose state can be changed by multiple parts of your application, each running in their own thread. Typically, the code is interspersed with read and write locks, to make sure that the state can only be changed in a controlled way and prevent multiple threads from mutating it simultaneously. At the same time, we are trying hard not to lock too big a block of code, as this can drastically slow down the application.&lt;&#x2F;p&gt;
&lt;p&gt;More often than not, code like this has originally been written without having concurrency in mind at all – only to be made fit for a multi-threaded world once the need arose. While writing software without the need for concurrency like this leads to very straightforward code, adapting it to the needs of a concurrent world leads to code that is really, really difficult to read and understand.&lt;&#x2F;p&gt;
&lt;p&gt;The core problem is that low-level synchronization constructs like locks and threads are &lt;em&gt;very hard to reason about&lt;&#x2F;em&gt;. As a consequence, it&#x27;s very hard to get it right: If you can&#x27;t easily reason about what&#x27;s going on, you can be sure that nasty bugs will ensue, from race conditions to deadlocks or just strange behaviour – maybe you&#x27;ll only notice after some months, long after your code has been deployed to your production servers.&lt;&#x2F;p&gt;
&lt;p&gt;Also, working with these low-level constructs makes it a real challenge to achieve an acceptable performance.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;the-actor-model&quot;&gt;The Actor model&lt;&#x2F;h2&gt;
&lt;p&gt;The Actor programming model is aimed at avoiding all the problems described above, allowing you to write highly performant concurrent code that is easy to reason about. Unlike the widely used approach of shared mutable state, it requires you to design and write your application from the ground up with concurrency in mind – it&#x27;s not really possible to add support for it later on.&lt;&#x2F;p&gt;
&lt;p&gt;The idea is that your application consists of lots of light-weight entities called actors. Each of these actors is responsible for only a very small task, and is thus easy to reason about. A more complex business logic arises out of the interaction between several actors, delegating tasks to others or passing messages to collaborators for other reasons.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;the-actor-system&quot;&gt;The Actor System&lt;&#x2F;h3&gt;
&lt;p&gt;Actors are pitiful creatures: They cannot live on their own. Rather, each and every actor in Akka resides in and is created by an &lt;em&gt;actor system&lt;&#x2F;em&gt;. Aside from allowing you to create and find actors, an &lt;code&gt;ActorSystem&lt;&#x2F;code&gt; provides for a whole bunch of additional functionality, none of which shall concern us right now.&lt;&#x2F;p&gt;
&lt;p&gt;In order to try out the example code, please add the following resolver and dependency to your SBT-based Scala 2.10 project first:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;scala&quot; style=&quot;background-color:#002b36;color:#839496;&quot; class=&quot;language-scala &quot;&gt;&lt;code class=&quot;language-scala&quot; data-lang=&quot;scala&quot;&gt;&lt;span&gt;resolvers += &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Typesafe Releases&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; at &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;http:&#x2F;&#x2F;repo.typesafe.com&#x2F;typesafe&#x2F;releases&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;libraryDependencies += &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;com.typesafe.akka&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; %% &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;akka-actor&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; % &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;2.2.3&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Now, let&#x27;s create an &lt;code&gt;ActorSystem&lt;&#x2F;code&gt;. We&#x27;ll need it as an environment for our actors:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;scala&quot; style=&quot;background-color:#002b36;color:#839496;&quot; class=&quot;language-scala &quot;&gt;&lt;code class=&quot;language-scala&quot; data-lang=&quot;scala&quot;&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;import&lt;&#x2F;span&gt;&lt;span&gt; akka.actor.ActorSystem
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;object&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt; Barista &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;extends &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;App &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;val system &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;ActorSystem&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Barista&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;  system.shutdown&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;()
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;We created a new instance of &lt;code&gt;ActorSystem&lt;&#x2F;code&gt; and gave it the name &lt;code&gt;&quot;Barista&quot;&lt;&#x2F;code&gt; – we are returning to the domain of coffee, which should be familiar from the article on &lt;a href=&quot;http:&#x2F;&#x2F;danielwestheide.com&#x2F;blog&#x2F;2013&#x2F;01&#x2F;09&#x2F;the-neophytes-guide-to-scala-part-8-welcome-to-the-future.html&quot;&gt;composable futures&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;Finally, we are good citizens and shut down our actor system once we no longer need it.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;defining-an-actor&quot;&gt;Defining an actor&lt;&#x2F;h3&gt;
&lt;p&gt;Whether your application consists of a few dozen or a few million actors totally depends on your use case, but Akka is absolutely okay with a few million. You might be baffled by this insanely high number. It&#x27;s important to understand that there is not a one-to-one relationship between an actor and a thread. You would soon run out of memory if that were the case. Rather, due to the non-blocking nature of actors, one thread can execute many actors – switching between them depending on which of them has messages to be processed.&lt;&#x2F;p&gt;
&lt;p&gt;To understand what is actually happening, let&#x27;s first create a very simple actor, a &lt;code&gt;Barista&lt;&#x2F;code&gt; that can receive orders but doesn&#x27;t really do anything apart from printing messages to the console:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;scala&quot; style=&quot;background-color:#002b36;color:#839496;&quot; class=&quot;language-scala &quot;&gt;&lt;code class=&quot;language-scala&quot; data-lang=&quot;scala&quot;&gt;&lt;span style=&quot;color:#93a1a1;&quot;&gt;sealed &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;trait&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt; CoffeeRequest
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;case object&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt; CappuccinoRequest &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;extends &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;CoffeeRequest
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;case object&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt; EspressoRequest &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;extends &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;CoffeeRequest
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;import&lt;&#x2F;span&gt;&lt;span&gt; akka.actor.Actor
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;class&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt; Barista &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;extends &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;Actor &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;receive &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= {
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;case CappuccinoRequest &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;=&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; println&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;I have to prepare a cappuccino!&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;case EspressoRequest &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;=&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; println&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Let&amp;#39;s prepare an espresso.&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;}
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;First, we define the types of messages that our actor understands. Typically, case classes are used for messages sent between actors if you need to pass along any parameters. If all the actor needs is an unparameterized message, this message is typically represented as a case object – which is exactly what we are doing here.&lt;&#x2F;p&gt;
&lt;p&gt;In any case, it&#x27;s crucial that your messages are immutable, or else bad things will happen.&lt;&#x2F;p&gt;
&lt;p&gt;Next, let&#x27;s have a look at our class &lt;code&gt;Barista&lt;&#x2F;code&gt;, which is the actual actor, extending the aptly named &lt;code&gt;Actor&lt;&#x2F;code&gt; trait. Said trait defines a method &lt;code&gt;receive&lt;&#x2F;code&gt; which returns a value of type &lt;code&gt;Receive&lt;&#x2F;code&gt;. The latter is really only a type alias for &lt;code&gt;PartialFunction[Any, Unit]&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;processing-messages&quot;&gt;Processing messages&lt;&#x2F;h3&gt;
&lt;p&gt;So what&#x27;s the meaning of this &lt;code&gt;receive&lt;&#x2F;code&gt; method? The return type, &lt;code&gt;PartialFunction[Any, Unit]&lt;&#x2F;code&gt; may seem strange to you in more than one respect.&lt;&#x2F;p&gt;
&lt;p&gt;In a nutshell, the partial function returned by the &lt;code&gt;receive&lt;&#x2F;code&gt; method is responsible for processing your messages. Whenever another part of your software – be it another actor or not – sends your actor a message, Akka will &lt;em&gt;eventually&lt;&#x2F;em&gt; let it process this message by calling the partial function returned by your actor&#x27;s &lt;code&gt;receive&lt;&#x2F;code&gt; method, passing it the message as an argument.&lt;&#x2F;p&gt;
&lt;h4 id=&quot;side-effecting&quot;&gt;Side-effecting&lt;&#x2F;h4&gt;
&lt;p&gt;When processing a message, an actor can do whatever you want it to, apart from returning a value.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;em&gt;Wat!?&lt;&#x2F;em&gt;&lt;&#x2F;p&gt;
&lt;p&gt;As the return type of &lt;code&gt;Unit&lt;&#x2F;code&gt; suggests, your partial function is side-effecting. This might come as a bit of a shock to you after we emphasized the usage of pure functions all the time. For a concurrent programming model, this actually makes a lot of sense. Actors are where your state is located, and having some clearly defined places where side-effects will occur in a controllable manner is totally fine – each message your actor receives is processed in isolation, one after another, so there is no need to reason about synchronization or locks.&lt;&#x2F;p&gt;
&lt;h4 id=&quot;untyped&quot;&gt;Untyped&lt;&#x2F;h4&gt;
&lt;p&gt;But... this partial function is not only side-effecting, it&#x27;s also as untyped as you can get in Scala, expecting an argument of type &lt;code&gt;Any&lt;&#x2F;code&gt;. Why is that, when we have such a powerful type system at our fingertips?&lt;&#x2F;p&gt;
&lt;p&gt;This has a lot to do with some important design choices in Akka that allow you to do things like forwarding messages to other actors, installing load balancing or proxying actors without the sender having to know anything about them and so on.&lt;&#x2F;p&gt;
&lt;p&gt;In practice, this is usually not a problem. With the messages themselves being strongly typed, you typically use pattern matching for processing those types of messages you are interested in, just as we did in our tiny example above.&lt;&#x2F;p&gt;
&lt;p&gt;Sometimes though, the weakly typed actors can indeed lead to nasty bugs the compiler can&#x27;t catch for you. If you have grown to love the benefits of a strong type system and think you don&#x27;t want to go away from that at any costs for some parts of your application, you may want to look at &lt;a href=&quot;https:&#x2F;&#x2F;doc.akka.io&#x2F;docs&#x2F;akka&#x2F;current&#x2F;typed&#x2F;&quot;&gt;Akka Typed&lt;&#x2F;a&gt; feature.&lt;&#x2F;p&gt;
&lt;h4 id=&quot;asynchronous-and-non-blocking&quot;&gt;Asynchronous and non-blocking&lt;&#x2F;h4&gt;
&lt;p&gt;I wrote above that Akka would let your actor &lt;em&gt;eventually&lt;&#x2F;em&gt; process a message sent to it. This is important to keep in mind: Sending a message and processing it is done in an asynchronous and non-blocking fashion. The sender will not be blocked until the message has been processed by the receiver. Instead, they can immediately continue with their own work. Maybe they expect to get a messsage from your actor in return after a while, or maybe they are not interested in hearing back from your actor at all.&lt;&#x2F;p&gt;
&lt;p&gt;What really happens when some component sends a message to an actor is that this message is delivered to the actor&#x27;s &lt;em&gt;mailbox&lt;&#x2F;em&gt;, which is basically a queue. Placing a message in an actor&#x27;s mailbox is a non-blocking operation, i.e. the sender doesn&#x27;t have to wait until the message is actually enqueued in the recipient&#x27;s mailbox.&lt;&#x2F;p&gt;
&lt;p&gt;The &lt;em&gt;dispatcher&lt;&#x2F;em&gt; will notice the arrival of a new message in an actor&#x27;s mailbox, again asynchronously. If the actor is not already processing a previous message, it is now allocated to one of the threads available in the execution context. Once the actor is done processing any previous messages, the dispatcher sends it the next message from its mailbox for processing.&lt;&#x2F;p&gt;
&lt;p&gt;The actor blocks the thread to which it is allocated for as long as it takes to process the message. While this doesn&#x27;t block the sender of the message, it means that lengthy operations degrade overall performance, as all the other actors have to be scheduled for processing messages on one of the remaining threads.&lt;&#x2F;p&gt;
&lt;p&gt;Hence, a core principle to follow for your &lt;code&gt;Receive&lt;&#x2F;code&gt; partial functions is to spend as little time inside them as possible. Most importantly, avoid calling blocking code inside your message processing code, if possible at all.&lt;&#x2F;p&gt;
&lt;p&gt;Of course, this is something you can&#x27;t prevent doing completely – the majority of database drivers nowadays is still blocking, and you will want to be able to persist data or query for it from your actor-based application. There are solutions to this dilemma, but we won&#x27;t cover them in this introductory article.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;creating-an-actor&quot;&gt;Creating an actor&lt;&#x2F;h3&gt;
&lt;p&gt;Defining an actor is all well and good, but how do we actually use our &lt;code&gt;Barista&lt;&#x2F;code&gt; actor in our application? To do that, we have to create a new instance of our &lt;code&gt;Barista&lt;&#x2F;code&gt; actor. You might be tempted to do it the usual way, by calling its constructor like so:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;scala&quot; style=&quot;background-color:#002b36;color:#839496;&quot; class=&quot;language-scala &quot;&gt;&lt;code class=&quot;language-scala&quot; data-lang=&quot;scala&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;val barista &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;new Barista &lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;&#x2F;&#x2F; will throw exception
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This will not work! Akka will thank you with an &lt;code&gt;ActorInitializationException&lt;&#x2F;code&gt;. The thing is, in order for the whole actor thingie to work properly, your actors need to be managed by the &lt;code&gt;ActorSystem&lt;&#x2F;code&gt; and its components. Hence, you have to ask the actor system for a new instance of your actor:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;scala&quot; style=&quot;background-color:#002b36;color:#839496;&quot; class=&quot;language-scala &quot;&gt;&lt;code class=&quot;language-scala&quot; data-lang=&quot;scala&quot;&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;import&lt;&#x2F;span&gt;&lt;span&gt; akka.actor.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{&lt;&#x2F;span&gt;&lt;span&gt;ActorRef, Props&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;}
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;val barista&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;ActorRef &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt; system.actorOf&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Props&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Barista&lt;&#x2F;span&gt;&lt;span&gt;], &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Barista&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The &lt;code&gt;actorOf&lt;&#x2F;code&gt; method defined on &lt;code&gt;ActorSystem&lt;&#x2F;code&gt; expects a &lt;code&gt;Props&lt;&#x2F;code&gt; instance, which provides a means of configuring newly created actors, and, optionally, a name for your actor instance. We are using the simplest form of creating such a &lt;code&gt;Props&lt;&#x2F;code&gt; instance, providing the &lt;code&gt;apply&lt;&#x2F;code&gt; method of the companion object with a type parameter. Akka will then create a new instance of the actor of the given type by calling its default constructor.&lt;&#x2F;p&gt;
&lt;p&gt;Be aware that the type of the object returned by &lt;code&gt;actorOf&lt;&#x2F;code&gt; is not &lt;code&gt;Barista&lt;&#x2F;code&gt;, but &lt;code&gt;ActorRef&lt;&#x2F;code&gt;. Actors never communicate with another directly and hence there are supposed to be no direct references to actor instances. Instead, actors or other components of your application aquire references to the actors they need to send messages to.&lt;&#x2F;p&gt;
&lt;p&gt;Thus, an &lt;code&gt;ActorRef&lt;&#x2F;code&gt; acts as some kind of proxy to the actual actor. This is convenient because an &lt;code&gt;ActorRef&lt;&#x2F;code&gt; can be serialized, allowing it to be a proxy for a remote actor on some other machine. For the component aquiring an &lt;code&gt;ActorRef&lt;&#x2F;code&gt;, the location of the actor – local in the same JVM or remote on some other machine – is completely transparent. We call this property &lt;em&gt;location transparency&lt;&#x2F;em&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;Please note that &lt;code&gt;ActorRef&lt;&#x2F;code&gt; is not parameterized by type. Any &lt;code&gt;ActorRef&lt;&#x2F;code&gt; can be exchanged for another, allowing you to send arbitrary messages to any &lt;code&gt;ActorRef&lt;&#x2F;code&gt;. This is by design and, as already mentioned above, allows for easily modifying the topology of your actor system wihout having to make any changes to the senders.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;sending-messages&quot;&gt;Sending messages&lt;&#x2F;h3&gt;
&lt;p&gt;Now that we have created an instance of our &lt;code&gt;Barista&lt;&#x2F;code&gt; actor and got an &lt;code&gt;ActorRef&lt;&#x2F;code&gt; linked to it, we can send it a message. This is done by calling the &lt;code&gt;!&lt;&#x2F;code&gt; method on the &lt;code&gt;ActorRef&lt;&#x2F;code&gt;:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;scala&quot; style=&quot;background-color:#002b36;color:#839496;&quot; class=&quot;language-scala &quot;&gt;&lt;code class=&quot;language-scala&quot; data-lang=&quot;scala&quot;&gt;&lt;span&gt;barista ! &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;CappuccinoRequest
&lt;&#x2F;span&gt;&lt;span&gt;barista ! &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;EspressoRequest
&lt;&#x2F;span&gt;&lt;span&gt;println&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;I ordered a cappuccino and an espresso&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Calling the &lt;code&gt;!&lt;&#x2F;code&gt; is a fire-and-forget operation: You &lt;em&gt;tell&lt;&#x2F;em&gt; the &lt;code&gt;Barista&lt;&#x2F;code&gt; that you want a cappuccino, but you don&#x27;t wait for their response. It&#x27;s the most common way in Akka for interacting with other actors. By calling this method, you tell Akka to enqueue your message in the recipient&#x27;s mailbox. As described above, this doesn&#x27;t block, and eventually the recipient actor will process your message.&lt;&#x2F;p&gt;
&lt;p&gt;Due to the asynchronous nature, the result of the above code is not deterministic. It might look like this:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#002b36;color:#839496;&quot;&gt;&lt;code&gt;&lt;span&gt;I have to prepare a cappuccino!
&lt;&#x2F;span&gt;&lt;span&gt;I ordered a cappuccino and an espresso
&lt;&#x2F;span&gt;&lt;span&gt;Let&amp;#39;s prepare an espresso.
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Even though we first sent the two messages to the &lt;code&gt;Barista&lt;&#x2F;code&gt; actor&#x27;s mailbox, between the processing of the first and second message, our own output is printed to the console.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;answering-to-messages&quot;&gt;Answering to messages&lt;&#x2F;h3&gt;
&lt;p&gt;Sometimes, being able to tell others what to do just doesn&#x27;t cut it. You would like to be able to answer by in turn sending a message to the sender of a message you got – all asynchronously of course.&lt;&#x2F;p&gt;
&lt;p&gt;To enable you to do that and lots of other things that are of no concern to us right now, actors have a method called &lt;code&gt;sender&lt;&#x2F;code&gt;, which returns the &lt;code&gt;ActorRef&lt;&#x2F;code&gt; of the sender of the last message, i.e. the one you are currently processing.&lt;&#x2F;p&gt;
&lt;p&gt;But how does it know about that sender? The answer can be found in the signature of the &lt;code&gt;!&lt;&#x2F;code&gt; method, which has a second, implicit parameter list:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;scala&quot; style=&quot;background-color:#002b36;color:#839496;&quot; class=&quot;language-scala &quot;&gt;&lt;code class=&quot;language-scala&quot; data-lang=&quot;scala&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;message&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;Any&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#93a1a1;&quot;&gt;implicit &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;sender&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;ActorRef &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Actor&lt;&#x2F;span&gt;&lt;span&gt;.noSender&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;Unit
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;When called from an actor, its &lt;code&gt;ActorRef&lt;&#x2F;code&gt; is passed on as the implicit &lt;code&gt;sender&lt;&#x2F;code&gt; argument.&lt;&#x2F;p&gt;
&lt;p&gt;Let&#x27;s change our &lt;code&gt;Barista&lt;&#x2F;code&gt; so that they immediately send a &lt;code&gt;Bill&lt;&#x2F;code&gt; to the sender of a &lt;code&gt;CoffeeRequest&lt;&#x2F;code&gt; before printing their usual output to the console:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;scala&quot; style=&quot;background-color:#002b36;color:#839496;&quot; class=&quot;language-scala &quot;&gt;&lt;code class=&quot;language-scala&quot; data-lang=&quot;scala&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;case class&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt; Bill&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;cents&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;Int&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;case object&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt; ClosingTime
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;class&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt; Barista &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;extends &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;Actor &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;receive &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= {
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;case CappuccinoRequest &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;=&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;      sender ! &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Bill&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;250&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;      println&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;I have to prepare a cappuccino!&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;case EspressoRequest &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;=&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;      sender ! &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Bill&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;200&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;      println&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Let&amp;#39;s prepare an espresso.&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;case ClosingTime &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;=&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; context.system.shutdown&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;()
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;}
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;While we are at it, we are introducing a new message, &lt;code&gt;ClosingTime&lt;&#x2F;code&gt;. The &lt;code&gt;Barista&lt;&#x2F;code&gt; reacts to it by shutting down the actor system, which they, like all actors, can access via their &lt;code&gt;ActorContext&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;Now, let&#x27;s introduce a second actor representing a customer:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;scala&quot; style=&quot;background-color:#002b36;color:#839496;&quot; class=&quot;language-scala &quot;&gt;&lt;code class=&quot;language-scala&quot; data-lang=&quot;scala&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;case object&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt; CaffeineWithdrawalWarning
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;class&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt; Customer&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;caffeineSource&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;ActorRef&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;extends &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;Actor &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;receive &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= {
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;case CaffeineWithdrawalWarning &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;=&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; caffeineSource ! &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;EspressoRequest
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;case Bill&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;cents&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;=&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; println&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;s&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;I have to pay &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;$cents&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt; cents, or else!&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;}
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This actor is a real coffee junkie, so it needs to be able to order new coffee. We pass it an &lt;code&gt;ActorRef&lt;&#x2F;code&gt; in the constructor – for the &lt;code&gt;Customer&lt;&#x2F;code&gt;, this is simply its &lt;code&gt;caffeineSource&lt;&#x2F;code&gt; – it doesn&#x27;t know whether this &lt;code&gt;ActorRef&lt;&#x2F;code&gt; points to a &lt;code&gt;Barista&lt;&#x2F;code&gt; or something else. It knows that it can send &lt;code&gt;CoffeeRequest&lt;&#x2F;code&gt; messages to it, and that is all that matters to them.&lt;&#x2F;p&gt;
&lt;p&gt;Finally, we need to create these two actors and send the customer a &lt;code&gt;CaffeineWithdrawalWarning&lt;&#x2F;code&gt; to get things rolling:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;scala&quot; style=&quot;background-color:#002b36;color:#839496;&quot; class=&quot;language-scala &quot;&gt;&lt;code class=&quot;language-scala&quot; data-lang=&quot;scala&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;val barista &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt; system.actorOf&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Props&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Barista&lt;&#x2F;span&gt;&lt;span&gt;], &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Barista&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;val customer &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt; system.actorOf&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Props&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;classOf[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Customer&lt;&#x2F;span&gt;&lt;span&gt;], barista&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span&gt;, &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Customer&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;customer ! &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;CaffeineWithdrawalWarning
&lt;&#x2F;span&gt;&lt;span&gt;barista ! &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;ClosingTime
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Here, for the &lt;code&gt;Customer&lt;&#x2F;code&gt; actor, we are using a different factory method for creating a &lt;code&gt;Props&lt;&#x2F;code&gt; instance: We pass in the type of the actor we want to have instantiated as well as the constructor arguments that actor takes. We need to do this because we want to pass the &lt;code&gt;ActorRef&lt;&#x2F;code&gt; of our &lt;code&gt;Barista&lt;&#x2F;code&gt; actor to the constructor of the &lt;code&gt;Customer&lt;&#x2F;code&gt; actor.&lt;&#x2F;p&gt;
&lt;p&gt;Sending the &lt;code&gt;CaffeineWithdrawalWarning&lt;&#x2F;code&gt; to the customer makes it send an &lt;code&gt;EspressoRequest&lt;&#x2F;code&gt; to the barista who will then send a &lt;code&gt;Bill&lt;&#x2F;code&gt; back to the customer. The output of this may look like this:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#002b36;color:#839496;&quot;&gt;&lt;code&gt;&lt;span&gt;Let&amp;#39;s prepare an espresso.
&lt;&#x2F;span&gt;&lt;span&gt;I have to pay 200 cents, or else!
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;First, while processing the &lt;code&gt;EspressoRequest&lt;&#x2F;code&gt; message, the &lt;code&gt;Barista&lt;&#x2F;code&gt; sends a message to the sender of that message, the &lt;code&gt;Customer&lt;&#x2F;code&gt; actor. However, this operation doesn&#x27;t block until the latter processes it. The &lt;code&gt;Barista&lt;&#x2F;code&gt; actor can continue processing the &lt;code&gt;EspressoRequest&lt;&#x2F;code&gt; immediately, and does this by printing to the console. Shortly after, the &lt;code&gt;Customer&lt;&#x2F;code&gt; starts to process the &lt;code&gt;Bill&lt;&#x2F;code&gt; message and in turn prints to the console.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;asking-questions&quot;&gt;Asking questions&lt;&#x2F;h3&gt;
&lt;p&gt;Sometimes, sending an actor a message and expecting a message in return at some later time isn&#x27;t an option – the most common place where this is the case is in components that need to interface with actors, but are not actors themselves. Living outside of the actor world, they cannot receive messages.&lt;&#x2F;p&gt;
&lt;p&gt;For situations such as these, there is Akka&#x27;s &lt;em&gt;ask&lt;&#x2F;em&gt; support, which provides some sort of bridge between actor-based and future-based concurrency. From the client perspective, it works like this:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;scala&quot; style=&quot;background-color:#002b36;color:#839496;&quot; class=&quot;language-scala &quot;&gt;&lt;code class=&quot;language-scala&quot; data-lang=&quot;scala&quot;&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;import&lt;&#x2F;span&gt;&lt;span&gt; akka.pattern.ask
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;import&lt;&#x2F;span&gt;&lt;span&gt; akka.util.Timeout
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;import&lt;&#x2F;span&gt;&lt;span&gt; scala.concurrent.duration.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d33682;&quot;&gt;_
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#93a1a1;&quot;&gt;implicit &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;val timeout &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Timeout&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;2&lt;&#x2F;span&gt;&lt;span&gt;.second&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#93a1a1;&quot;&gt;implicit &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;val ec &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt; system.dispatcher
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;val f&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Future&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;Any&lt;&#x2F;span&gt;&lt;span&gt;] &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt; barista2 ? &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;CappuccinoRequest
&lt;&#x2F;span&gt;&lt;span&gt;f.onSuccess &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;case Bill&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;cents&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;=&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; println&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;s&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Will pay &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;$cents&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt; cents for a cappuccino&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;First, you need to import support for the &lt;em&gt;ask&lt;&#x2F;em&gt; syntax and create an implicit timeout for the &lt;code&gt;Future&lt;&#x2F;code&gt; returned by the  &lt;code&gt;?&lt;&#x2F;code&gt; method. Also, the &lt;code&gt;Future&lt;&#x2F;code&gt; needs an &lt;code&gt;ExecutionContext&lt;&#x2F;code&gt;. Here, we simply use the default dispatcher of our &lt;code&gt;ActorSystem&lt;&#x2F;code&gt;, which is conveniently also an &lt;code&gt;ExecutionContext&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;As you can see, the returned future is untyped – it&#x27;s a &lt;code&gt;Future[Any]&lt;&#x2F;code&gt;. This shouldn&#x27;t come as a surprise, since it&#x27;s really a received message from an actor, and those are untyped, too.&lt;&#x2F;p&gt;
&lt;p&gt;For the actor that is being asked, this is actually the same as sending some message to the sender of a processed message. This is why asking our &lt;code&gt;Barista&lt;&#x2F;code&gt; works out of the box without having to change anything in our &lt;code&gt;Barista&lt;&#x2F;code&gt; actor.&lt;&#x2F;p&gt;
&lt;p&gt;Once the actor being asked sends a message to the sender, the &lt;code&gt;Promise&lt;&#x2F;code&gt; belonging to the returned &lt;code&gt;Future&lt;&#x2F;code&gt; is completed.&lt;&#x2F;p&gt;
&lt;p&gt;Generally, telling is preferable to asking, because it&#x27;s more resource-sparing. Akka is not for polite people! However, there are situations where you really need to ask, and then it&#x27;s perfectly fine to do so.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;stateful-actors&quot;&gt;Stateful actors&lt;&#x2F;h3&gt;
&lt;p&gt;Each actor may maintain an internal state, but that&#x27;s not strictly necessary. Sometimes, a large part of the overall application state consists of the information carried by the immutable messages passed between actors.&lt;&#x2F;p&gt;
&lt;p&gt;An actor only ever processes one message at a time. While doing so, it may modify its internal state. This means that there is some kind of mutable state in an actor, but since each message is processed in isolation, there is no way the internal state of our actor can get messed up due to concurrency problems.&lt;&#x2F;p&gt;
&lt;p&gt;To illustrate, let&#x27;s turn our stateless &lt;code&gt;Barista&lt;&#x2F;code&gt; into an actor carrying state, by simply counting the number of orders:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;scala&quot; style=&quot;background-color:#002b36;color:#839496;&quot; class=&quot;language-scala &quot;&gt;&lt;code class=&quot;language-scala&quot; data-lang=&quot;scala&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;class&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt; Barista &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;extends &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;Actor &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;var cappuccinoCount &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;0
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;var espressoCount &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;0
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;receive &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= {
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;case CappuccinoRequest &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;=&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;      sender ! &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Bill&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;250&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;      cappuccinoCount += &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;1
&lt;&#x2F;span&gt;&lt;span&gt;      println&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;s&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;I have to prepare cappuccino #&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;$cappuccinoCount&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;case EspressoRequest &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;=&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;      sender ! &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Bill&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;200&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;      espressoCount += &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;1
&lt;&#x2F;span&gt;&lt;span&gt;      println&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;s&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Let&amp;#39;s prepare espresso #&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;$espressoCount&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;case ClosingTime &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;=&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; context.system.shutdown&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;()
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;}
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;We introduced two &lt;code&gt;vars&lt;&#x2F;code&gt;, &lt;code&gt;cappuccinoCount&lt;&#x2F;code&gt; and &lt;code&gt;espressoCount&lt;&#x2F;code&gt; that are incremented with each respective order. This is actually the first time in this series that we have used a &lt;code&gt;var&lt;&#x2F;code&gt;. While to be avoided in functional programming, they are really the only way to allow your actors to carry state. Since each message is processed in isolation, our above code is similar to using &lt;code&gt;AtomicInteger&lt;&#x2F;code&gt; values in a non-actor environment.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;conclusion&quot;&gt;Conclusion&lt;&#x2F;h2&gt;
&lt;p&gt;And here ends our introduction to the actor programming model for concurrency and how to work within this paradigm using Akka. While we have really only scratched the surface and have ignored some important concepts of Akka, I hope to have given enough of an insight into this approach to concurrency to give you a basic understanding and get you interested in learning more.&lt;&#x2F;p&gt;
&lt;p&gt;In the coming articles, I will elaborate our little example, adding some meaningful behaviour to it while introducing more of the ideas behind Akka actors, among them the question of how errors are handled in an actor system.&lt;&#x2F;p&gt;
&lt;p&gt;P.S. Please note that starting with this article I have switched to a biweekly schedule for the remaining parts of this series.&lt;&#x2F;p&gt;
</description>
      </item>
      <item>
          <title>The Neophyte&#x27;s Guide to Scala Part 13: Path-dependent types</title>
          <pubDate>Wed, 13 Feb 2013 00:00:00 +0000</pubDate>
          <author>Unknown</author>
          <link>https://danielwestheide.com/blog/the-neophytes-guide-to-scala-part-13-path-dependent-types/</link>
          <guid>https://danielwestheide.com/blog/the-neophytes-guide-to-scala-part-13-path-dependent-types/</guid>
          <description xml:base="https://danielwestheide.com/blog/the-neophytes-guide-to-scala-part-13-path-dependent-types/">&lt;p&gt;In last week&#x27;s article, I &lt;a href=&quot;http:&#x2F;&#x2F;danielwestheide.com&#x2F;blog&#x2F;2013&#x2F;02&#x2F;06&#x2F;the-neophytes-guide-to-scala-part-12-type-classes.html&quot;&gt;introduced you to the idea of type classes&lt;&#x2F;a&gt; – a pattern that allows you to design your programs to be open for extension without giving up important information about concrete types. This week, I&#x27;m going to stick with Scala&#x27;s type system and talk about one of its features that distinguishes it from most other mainstream programming languages: Scala&#x27;s form of dependent types, in particular path-dependent types and dependent method types.&lt;&#x2F;p&gt;
&lt;p&gt;One of the most widely used arguments against static typing is that &lt;em&gt;&quot;the compiler is just in the way&quot;&lt;&#x2F;em&gt; and that in the end, it&#x27;s all only data, so why care about building up a complex hierarchy of types?&lt;&#x2F;p&gt;
&lt;p&gt;In the end, having static types is all about preventing bugs by allowing the über-smart compiler to humiliate you on a regular basis, making sure you&#x27;re doing the right thing before it&#x27;s too late.&lt;&#x2F;p&gt;
&lt;p&gt;Using path-dependent types is one powerful way to help the compiler prevent you from introducing bugs, as it places logic that is usually only available at runtime into types.&lt;&#x2F;p&gt;
&lt;p&gt;Sometimes, accidentally introducing path-dependent types can lead to frustation, though, especially if you have never heard of them. Hence, it&#x27;s definitely a good idea to get familiar with them, whether you decide to put them to use or not.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The problem&lt;&#x2F;h2&gt;
&lt;p&gt;I will start by presenting a problem that path-dependent types can help us solving: In the realm of fan fiction, the most atrocious things happen – usually, the involved characters will end up making out with each other, regardless how inappropriate it is. There is even crossover fan fiction, in which two characters from different franchises are making out with each other.&lt;&#x2F;p&gt;
&lt;p&gt;However, elitist fan fiction writers look down on this. Surely there is a way to prevent such wrongdoing! Here is a first version of our domain model:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;scala&quot; style=&quot;background-color:#002b36;color:#839496;&quot; class=&quot;language-scala &quot;&gt;&lt;code class=&quot;language-scala&quot; data-lang=&quot;scala&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;object&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt; Franchise &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;   &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;case class&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt; Character&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;name&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;String&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span&gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;}
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;class&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt; Franchise&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;name&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;String&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;) {
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;import&lt;&#x2F;span&gt;&lt;span&gt; Franchise.Character
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;createFanFiction&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;lovestruck&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Character&lt;&#x2F;span&gt;&lt;span&gt;, 
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;objectOfDesire&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Character&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span&gt;: (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Character&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Character&lt;&#x2F;span&gt;&lt;span&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= (&lt;&#x2F;span&gt;&lt;span&gt;lovestruck, objectOfDesire&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Characters are represented by instances of the &lt;code&gt;Character&lt;&#x2F;code&gt; case class, and the &lt;code&gt;Franchise&lt;&#x2F;code&gt; class has a method to create a new piece of fan fiction about two characters. Let&#x27;s create two franchises and some characters:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;scala&quot; style=&quot;background-color:#002b36;color:#839496;&quot; class=&quot;language-scala &quot;&gt;&lt;code class=&quot;language-scala&quot; data-lang=&quot;scala&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;val starTrek &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;new Franchise&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Star Trek&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;val starWars &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;new Franchise&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Star Wars&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;val quark &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Franchise&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Character&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Quark&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;val jadzia &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Franchise&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Character&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Jadzia Dax&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;val luke &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Franchise&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Character&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Luke Skywalker&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;val yoda &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Franchise&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Character&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Yoda&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Unfortunately, at the moment we are unable to prevent bad things from happening:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;scala&quot; style=&quot;background-color:#002b36;color:#839496;&quot; class=&quot;language-scala &quot;&gt;&lt;code class=&quot;language-scala&quot; data-lang=&quot;scala&quot;&gt;&lt;span&gt;starTrek.createFanFiction&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;lovestruck &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt; jadzia, objectOfDesire &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt; luke&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Horrors of horrors! Someone has created a piece of fan fiction in which Jadzia Dax is making out with Luke Skywalker. Preposterous! Clearly, we should not allow this. Your first intuition might be to somehow check at runtime that two characters making out are from the same franchise. For example, we could change the model like so:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;scala&quot; style=&quot;background-color:#002b36;color:#839496;&quot; class=&quot;language-scala &quot;&gt;&lt;code class=&quot;language-scala&quot; data-lang=&quot;scala&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;object&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt; Franchise &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;case class&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt; Character&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;name&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;String&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;franchise&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Franchise&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;}
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;class&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt; Franchise&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;name&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;String&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;) {
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;import&lt;&#x2F;span&gt;&lt;span&gt; Franchise.Character
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;createFanFiction&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(
&lt;&#x2F;span&gt;&lt;span&gt;      &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;lovestruck&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Character&lt;&#x2F;span&gt;&lt;span&gt;, 
&lt;&#x2F;span&gt;&lt;span&gt;      &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;objectOfDesire&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Character&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span&gt;: (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Character&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Character&lt;&#x2F;span&gt;&lt;span&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= {
&lt;&#x2F;span&gt;&lt;span&gt;    require&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;lovestruck.franchise &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;==&lt;&#x2F;span&gt;&lt;span&gt; objectOfDesire.franchise&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;lovestruck, objectOfDesire&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;}
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Now, the &lt;code&gt;Character&lt;&#x2F;code&gt; instances have a reference to their &lt;code&gt;Franchise&lt;&#x2F;code&gt;, and trying to create a fan fiction with characters from different franchises will lead to an &lt;code&gt;IllegalArgumentException&lt;&#x2F;code&gt; (feel free to try this out in a REPL).&lt;&#x2F;p&gt;
&lt;h2 id=&quot;safer-fiction-with-path-dependent-types&quot;&gt;Safer fiction with path-dependent types&lt;&#x2F;h2&gt;
&lt;p&gt;This is pretty good, isn&#x27;t it? It&#x27;s the kind of fail-fast behaviour we have been indoctrinated with for years. However, with Scala, we can do better. There is a way to fail even faster – not at runtime, but at compile time. To achieve that, we need to encode the connection between a &lt;code&gt;Character&lt;&#x2F;code&gt; and its &lt;code&gt;Franchise&lt;&#x2F;code&gt; at the type level.&lt;&#x2F;p&gt;
&lt;p&gt;Luckily, the way Scala&#x27;s nested types work allow us to do that. In Scala, a nested type is bound to a specific instance of the outer type, not to the outer type itself. This means that if you try to use an instance of the inner type outside of the instance of the enclosing type, you will face a compile error:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;scala&quot; style=&quot;background-color:#002b36;color:#839496;&quot; class=&quot;language-scala &quot;&gt;&lt;code class=&quot;language-scala&quot; data-lang=&quot;scala&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;class&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt; A &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;class&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt; B
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;var b&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Option&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;B&lt;&#x2F;span&gt;&lt;span&gt;] &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;None
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;}
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;val a1 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;new A
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;val a2 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;new A
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;val b1 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;new a1&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;B
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;val b2 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;new a2&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;B
&lt;&#x2F;span&gt;&lt;span&gt;a1.b &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Some&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;b1&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;a2.b &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Some&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;b1&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;&#x2F;&#x2F; does not compile
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;You cannot simply assign an instance of the &lt;code&gt;B&lt;&#x2F;code&gt; that is bound to &lt;code&gt;a2&lt;&#x2F;code&gt; to the field on &lt;code&gt;a1&lt;&#x2F;code&gt; – the one is an &lt;code&gt;a2.B&lt;&#x2F;code&gt;, the other expects an &lt;code&gt;a1.B&lt;&#x2F;code&gt;. The dot syntax represents the path to the type, going along concrete instances of other types. Hence the name, path-dependent types.&lt;&#x2F;p&gt;
&lt;p&gt;We can put these to use in order to prevent characters from different franchises making out with each other:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;scala&quot; style=&quot;background-color:#002b36;color:#839496;&quot; class=&quot;language-scala &quot;&gt;&lt;code class=&quot;language-scala&quot; data-lang=&quot;scala&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;class&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt; Franchise&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;name&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;String&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;) {
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;case class&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt; Character&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;name&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;String&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;createFanFictionWith&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;lovestruck&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Character&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;objectOfDesire&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Character&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span&gt;: (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Character&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Character&lt;&#x2F;span&gt;&lt;span&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= (&lt;&#x2F;span&gt;&lt;span&gt;lovestruck, objectOfDesire&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Now, the type &lt;code&gt;Character&lt;&#x2F;code&gt; is nested in the type &lt;code&gt;Franchise&lt;&#x2F;code&gt;, which means that it is dependent on a specific enclosing instance of the &lt;code&gt;Franchise&lt;&#x2F;code&gt; type.&lt;&#x2F;p&gt;
&lt;p&gt;Let&#x27;s create our example franchises and characters again:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;scala&quot; style=&quot;background-color:#002b36;color:#839496;&quot; class=&quot;language-scala &quot;&gt;&lt;code class=&quot;language-scala&quot; data-lang=&quot;scala&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;val starTrek &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;new Franchise&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Star Trek&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;val starWars &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;new Franchise&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Star Wars&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;val quark &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt; starTrek.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Character&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Quark&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;val jadzia &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt; starTrek.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Character&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Jadzia Dax&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;val luke &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt; starWars.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Character&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Luke Skywalker&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;val yoda &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt; starWars.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Character&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Yoda&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;You can already see in how our &lt;code&gt;Character&lt;&#x2F;code&gt; instances are created that their types are bound to a specific franchise. Let&#x27;s see what happens if we try to put some of these characters together:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;scala&quot; style=&quot;background-color:#002b36;color:#839496;&quot; class=&quot;language-scala &quot;&gt;&lt;code class=&quot;language-scala&quot; data-lang=&quot;scala&quot;&gt;&lt;span&gt;starTrek.createFanFictionWith&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;lovestruck &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt; quark, objectOfDesire &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt; jadzia&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;starWars.createFanFictionWith&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;lovestruck &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt; luke, objectOfDesire &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt; yoda&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;These two compile, as expected. They &lt;em&gt;are&lt;&#x2F;em&gt; tasteless, but what can we do?&lt;&#x2F;p&gt;
&lt;p&gt;Now, let&#x27;s see what happens if we try to create some fan fiction about Jadzia Dax and Luke Skywalker:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;scala&quot; style=&quot;background-color:#002b36;color:#839496;&quot; class=&quot;language-scala &quot;&gt;&lt;code class=&quot;language-scala&quot; data-lang=&quot;scala&quot;&gt;&lt;span&gt;starTrek.createFanFictionWith&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;lovestruck &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt; jadzia, objectOfDesire &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt; luke&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Et voilà: The thing that should not be does not even compile! The compiler complains about a type mismatch:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#002b36;color:#839496;&quot;&gt;&lt;code&gt;&lt;span&gt;found   : starWars.Character
&lt;&#x2F;span&gt;&lt;span&gt;required: starTrek.Character
&lt;&#x2F;span&gt;&lt;span&gt;               starTrek.createFanFictionWith(lovestruck = jadzia, objectOfDesire = luke)
&lt;&#x2F;span&gt;&lt;span&gt;                                                                                   ^
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This technique also works if our method is not defined on the &lt;code&gt;Franchise&lt;&#x2F;code&gt; class, but in some other module. In this case, we can make use of dependent method types, where the type of one parameter depends on a previous parameter:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;scala&quot; style=&quot;background-color:#002b36;color:#839496;&quot; class=&quot;language-scala &quot;&gt;&lt;code class=&quot;language-scala&quot; data-lang=&quot;scala&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;createFanFiction&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;f&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Franchise&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;lovestruck&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;f&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Character&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;objectOfDesire&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;f&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Character&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;) =
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;lovestruck, objectOfDesire&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;As you can see, the type of the &lt;code&gt;lovestruck&lt;&#x2F;code&gt; and &lt;code&gt;objectOfDesire&lt;&#x2F;code&gt; parameters depends on the &lt;code&gt;Franchise&lt;&#x2F;code&gt; instance passed to the method. Note that this only works if the instance on which other types depend is in its own parameter list.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;abstract-type-members&quot;&gt;Abstract type members&lt;&#x2F;h2&gt;
&lt;p&gt;Often, dependent method types are used in conjunction with abstract type members. Suppose we want to develop a hipsterrific key-value store. It will only support setting and getting the value for a key, but in a typesafe manner. Here is our oversimplified implementation:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;scala&quot; style=&quot;background-color:#002b36;color:#839496;&quot; class=&quot;language-scala &quot;&gt;&lt;code class=&quot;language-scala&quot; data-lang=&quot;scala&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;object&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt; AwesomeDB &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#93a1a1;&quot;&gt;abstract &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;class&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt; Key&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;name&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;String&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;) {
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;type &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;Value
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;}
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;}
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;import&lt;&#x2F;span&gt;&lt;span&gt; AwesomeDB.Key
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;class&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt; AwesomeDB &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;import&lt;&#x2F;span&gt;&lt;span&gt; collection.mutable.Map
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;val data &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Map&lt;&#x2F;span&gt;&lt;span&gt;.empty[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Key&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;Any&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;get&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;key&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Key&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Option&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;key&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Value&lt;&#x2F;span&gt;&lt;span&gt;] &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt; data.get&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;key&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span&gt;.asInstanceOf[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Option&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;key&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Value&lt;&#x2F;span&gt;&lt;span&gt;]]
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;set&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;key&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Key&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;value&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;key&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Value&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;Unit &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt; data.update&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;key, value&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;We have defined a class &lt;code&gt;Key&lt;&#x2F;code&gt; with an abstract type member &lt;code&gt;Value&lt;&#x2F;code&gt;. The methods on &lt;code&gt;AwesomeDB&lt;&#x2F;code&gt; refer to that type without ever knowing or caring about the specific manifestation of this abstract type.&lt;&#x2F;p&gt;
&lt;p&gt;We can now define some concrete keys that we want to use:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;scala&quot; style=&quot;background-color:#002b36;color:#839496;&quot; class=&quot;language-scala &quot;&gt;&lt;code class=&quot;language-scala&quot; data-lang=&quot;scala&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;trait&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt; IntValued &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;extends &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;Key &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
&lt;&#x2F;span&gt;&lt;span&gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;type &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;Value &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;Int
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;}
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;trait&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt; StringValued &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;extends &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;Key &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;type &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;Value &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;String
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;}
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;object&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt; Keys &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;val foo &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;new Key&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;foo&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;with IntValued
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;val bar &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;new Key&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;bar&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;with StringValued
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Now we can set and get key&#x2F;value pairs in a typesafe manner:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;scala&quot; style=&quot;background-color:#002b36;color:#839496;&quot; class=&quot;language-scala &quot;&gt;&lt;code class=&quot;language-scala&quot; data-lang=&quot;scala&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;val dataStore &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;new AwesomeDB
&lt;&#x2F;span&gt;&lt;span&gt;dataStore.set&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Keys&lt;&#x2F;span&gt;&lt;span&gt;.foo&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;23&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;val i&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Option&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;Int&lt;&#x2F;span&gt;&lt;span&gt;] &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt; dataStore.get&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Keys&lt;&#x2F;span&gt;&lt;span&gt;.foo&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;dataStore.set&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Keys&lt;&#x2F;span&gt;&lt;span&gt;.foo&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)(&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;23&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;&#x2F;&#x2F; does not compile
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h2 id=&quot;path-dependent-types-in-practice&quot;&gt;Path-dependent types in practice&lt;&#x2F;h2&gt;
&lt;p&gt;While path-dependent types are not necessarily omnipresent in your typical Scala code, they do have a lot of practical value beyond modelling the domain of fan fiction.&lt;&#x2F;p&gt;
&lt;p&gt;One of the most widespread uses is probably seen in combination with the &lt;em&gt;cake pattern&lt;&#x2F;em&gt;, which is a technique for composing your components and managing their dependencies, relying solely on features of the language. See the excellent articles &lt;a href=&quot;http:&#x2F;&#x2F;debasishg.blogspot.ie&#x2F;2013&#x2F;02&#x2F;modular-abstractions-in-scala-with.html&quot;&gt;by Debasish Ghosh&lt;&#x2F;a&gt; and &lt;a href=&quot;http:&#x2F;&#x2F;archive.today&#x2F;2Qco&quot;&gt;Precog&#x27;s Daniel Spiewak&lt;&#x2F;a&gt; to learn more about both the cake pattern and how it can be improved by incorporating path-dependent types.&lt;&#x2F;p&gt;
&lt;p&gt;In general, whenever you want to make sure that objects created or managed by a specific instance of another type cannot accidentally or purposely be interchanged or mixed, path-dependent types are the way to go.&lt;&#x2F;p&gt;
&lt;p&gt;Path-dependent types and dependent method types play a crucial role for attempts to encode information into types that is typically only known at runtime, for instance heterogenous lists, type-level representations of natural numbers and collections that carry their size in their type. Miles Sabin is exploring the limits of Scala&#x27;s type system in this respect in his excellent library &lt;a href=&quot;https:&#x2F;&#x2F;github.com&#x2F;milessabin&#x2F;shapeless&quot;&gt;Shapeless&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
</description>
      </item>
      <item>
          <title>The Neophyte&#x27;s Guide to Scala Part 12: Type classes</title>
          <pubDate>Wed, 06 Feb 2013 00:00:00 +0000</pubDate>
          <author>Unknown</author>
          <link>https://danielwestheide.com/blog/the-neophytes-guide-to-scala-part-12-type-classes/</link>
          <guid>https://danielwestheide.com/blog/the-neophytes-guide-to-scala-part-12-type-classes/</guid>
          <description xml:base="https://danielwestheide.com/blog/the-neophytes-guide-to-scala-part-12-type-classes/">&lt;p&gt;After having discussed several functional programming techniques for keeping things DRY and flexible in the last two weeks, in particular function composition, partial function application, and currying, we are going to stick with the general notion of making your code as flexible as possible.&lt;&#x2F;p&gt;
&lt;p&gt;However, this time, we are not looking so much at how you can leverage functions as first-class objects to achieve this goal. Instead, this article is all about using the type system in such a manner that it&#x27;s not in the way, but rather supports you in keeping your code extensible: You&#x27;re going to learn about &lt;em&gt;type classes&lt;&#x2F;em&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;You might think that this is some exotic idea without practical relevance, brought into the Scala community by some vocal Haskell fanatics. This is clearly not the case. Type classes have become an important part of the Scala standard library and even more so of many popular and commonly used third-party open-source libraries, so it&#x27;s generally a good idea to make yourself familiar with them.&lt;&#x2F;p&gt;
&lt;p&gt;I will discuss the idea of type classes, why they are useful, how to benefit from them as a client, and how to implement your own type classes and put them to use for great good.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The problem&lt;&#x2F;h2&gt;
&lt;p&gt;Instead of starting off by giving an abstract explanation of what type classes are, let&#x27;s tackle this subject by means of an – admittedly simplified, but nevertheless resonably practical – example.&lt;&#x2F;p&gt;
&lt;p&gt;Imagine that we want to write a fancy statistics library. This means we want to provide a bunch of functions that operate on collections of numbers, mostly to compute some aggregate values for them. Imagine further that we are restricted to accessing an element from such a collection by index and to using the &lt;code&gt;reduce&lt;&#x2F;code&gt; method defined on Scala collections. We impose this restriction on ourselves because we are going to re-implement a little bit of what the Scala standard library already provides – simply because it&#x27;s a nice example without many distractions, and it&#x27;s small enough for a blog post. Finally, our implementation assumes that the values we get are already sorted.&lt;&#x2F;p&gt;
&lt;p&gt;We will start with a very crude implementation of &lt;code&gt;median&lt;&#x2F;code&gt;, &lt;code&gt;quartiles&lt;&#x2F;code&gt;, and &lt;code&gt;iqr&lt;&#x2F;code&gt; numbers of type &lt;code&gt;Double&lt;&#x2F;code&gt;:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;scala&quot; style=&quot;background-color:#002b36;color:#839496;&quot; class=&quot;language-scala &quot;&gt;&lt;code class=&quot;language-scala&quot; data-lang=&quot;scala&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;object&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt; Statistics &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;median&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;xs&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Vector&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;Double&lt;&#x2F;span&gt;&lt;span&gt;]&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;Double &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt; xs&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;xs.size &#x2F; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;2&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;quartiles&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;xs&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Vector&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;Double&lt;&#x2F;span&gt;&lt;span&gt;]&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span&gt;: (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;Double&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;Double&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;Double&lt;&#x2F;span&gt;&lt;span&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;=
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;xs&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;xs.size &#x2F; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;4&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span&gt;, median&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;xs&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span&gt;, xs&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;xs.size &#x2F; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;4&lt;&#x2F;span&gt;&lt;span&gt; * &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;3&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;))
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;iqr&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;xs&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Vector&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;Double&lt;&#x2F;span&gt;&lt;span&gt;]&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;Double &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt; quartiles&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;xs&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;match &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;case &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;lowerQuartile&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d33682;&quot;&gt;_&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;upperQuartile&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;=&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; upperQuartile - lowerQuartile
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;}
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;mean&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;xs&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Vector&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;Double&lt;&#x2F;span&gt;&lt;span&gt;]&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;Double &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= {
&lt;&#x2F;span&gt;&lt;span&gt;    xs.reduce&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d33682;&quot;&gt;_&lt;&#x2F;span&gt;&lt;span&gt; + &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d33682;&quot;&gt;_&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span&gt; &#x2F; xs.size
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;}
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The median cuts a data set in half, whereas the lower and upper quartile (first and third element of the tuple returned by our &lt;code&gt;quartile&lt;&#x2F;code&gt; method), split the lowest and highest 25 percent of the data, respectively. Our &lt;code&gt;iqr&lt;&#x2F;code&gt; method returns the interquartile range, which is the difference between the upper and lower quartile.&lt;&#x2F;p&gt;
&lt;p&gt;Now, of course, we want to support more than just double numbers. So let&#x27;s implement all these methods again for &lt;code&gt;Int&lt;&#x2F;code&gt; numbers, right?&lt;&#x2F;p&gt;
&lt;p&gt;Well, no! First of all, that would be a tiny little bit repetitious, wouldn&#x27;t it? Also, in situations such as these, we quickly run into situations where we cannot overload a method without some dirty tricks, because the type parameter suffers from &lt;em&gt;type erasure&lt;&#x2F;em&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;If only &lt;code&gt;Int&lt;&#x2F;code&gt; and &lt;code&gt;Double&lt;&#x2F;code&gt; would extend from a common base class or implement a common trait like &lt;code&gt;Number&lt;&#x2F;code&gt;! We could be tempted to change the type required and returned by our methods to that more general type. Our method signatures would look like this:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;scala&quot; style=&quot;background-color:#002b36;color:#839496;&quot; class=&quot;language-scala &quot;&gt;&lt;code class=&quot;language-scala&quot; data-lang=&quot;scala&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;object&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt; Statistics &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;median&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;xs&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Vector&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Number&lt;&#x2F;span&gt;&lt;span&gt;]&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Number &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;???
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;quartiles&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;xs&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Vector&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Number&lt;&#x2F;span&gt;&lt;span&gt;]&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span&gt;: (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Number&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Number&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Number&lt;&#x2F;span&gt;&lt;span&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;???
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;iqr&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;xs&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Vector&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Number&lt;&#x2F;span&gt;&lt;span&gt;]&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Number &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;???
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;mean&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;xs&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Vector&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Number&lt;&#x2F;span&gt;&lt;span&gt;]&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Number &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;???
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Thankfully, in this case there is no such common trait, so we aren&#x27;t tempted to walk this road at all. However, in other cases, that might very well be the case – and still be a bad idea. Not only do we drop previously available type information, we also close our API against future extensions to types whose sources we don&#x27;t control: We cannot make some new number type coming from a third party extend the &lt;code&gt;Number&lt;&#x2F;code&gt; trait.&lt;&#x2F;p&gt;
&lt;p&gt;Ruby&#x27;s answer to that problem is &lt;em&gt;monkey patching&lt;&#x2F;em&gt;, polluting the global namespace with an extension to that new type, making it act like a &lt;code&gt;Number&lt;&#x2F;code&gt; after all. Java developers who have got beaten up by the Gang of Four in their youth, on the other hand, will think that an &lt;em&gt;Adapter&lt;&#x2F;em&gt; may solve all of their problems:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;scala&quot; style=&quot;background-color:#002b36;color:#839496;&quot; class=&quot;language-scala &quot;&gt;&lt;code class=&quot;language-scala&quot; data-lang=&quot;scala&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;object&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt; Statistics &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;trait&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt; NumberLike&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;A&lt;&#x2F;span&gt;&lt;span&gt;] &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;get&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;A
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;plus&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;y&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;NumberLike&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;A&lt;&#x2F;span&gt;&lt;span&gt;]&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;NumberLike&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;A&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;minus&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;y&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;NumberLike&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;A&lt;&#x2F;span&gt;&lt;span&gt;]&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;NumberLike&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;A&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;divide&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;y&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;Int&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;NumberLike&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;A&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;}
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;case class&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt; NumberLikeDouble&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;x&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;Double&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;extends &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;NumberLike&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;Double&lt;&#x2F;span&gt;&lt;span&gt;] &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;get&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;Double &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt; x
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;minus&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;y&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;NumberLike&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;Double&lt;&#x2F;span&gt;&lt;span&gt;]&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;) = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;NumberLikeDouble&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;x - y.get&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;plus&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;y&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;NumberLike&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;Double&lt;&#x2F;span&gt;&lt;span&gt;]&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;) = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;NumberLikeDouble&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;x + y.get&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;divide&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;y&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;Int&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;) = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;NumberLikeDouble&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;x &#x2F; y&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;}
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;type &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;Quartile&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;A&lt;&#x2F;span&gt;&lt;span&gt;] &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;NumberLike&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;A&lt;&#x2F;span&gt;&lt;span&gt;], &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;NumberLike&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;A&lt;&#x2F;span&gt;&lt;span&gt;], &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;NumberLike&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;A&lt;&#x2F;span&gt;&lt;span&gt;])
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;median&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;A&lt;&#x2F;span&gt;&lt;span&gt;]&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;xs&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Vector&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;NumberLike&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;A&lt;&#x2F;span&gt;&lt;span&gt;]]&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;NumberLike&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;A&lt;&#x2F;span&gt;&lt;span&gt;] &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt; xs&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;xs.size &#x2F; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;2&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;quartiles&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;A&lt;&#x2F;span&gt;&lt;span&gt;]&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;xs&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Vector&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;NumberLike&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;A&lt;&#x2F;span&gt;&lt;span&gt;]]&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Quartile&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;A&lt;&#x2F;span&gt;&lt;span&gt;] &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;=
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;xs&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;xs.size &#x2F; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;4&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span&gt;, median&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;xs&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span&gt;, xs&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;xs.size &#x2F; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;4&lt;&#x2F;span&gt;&lt;span&gt; * &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;3&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;))
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;iqr&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;A&lt;&#x2F;span&gt;&lt;span&gt;]&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;xs&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Vector&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;NumberLike&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;A&lt;&#x2F;span&gt;&lt;span&gt;]]&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;NumberLike&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;A&lt;&#x2F;span&gt;&lt;span&gt;] &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt; quartiles&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;xs&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;match &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;case &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;lowerQuartile&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d33682;&quot;&gt;_&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;upperQuartile&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;=&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; upperQuartile.minus&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;lowerQuartile&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;}
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;mean&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;A&lt;&#x2F;span&gt;&lt;span&gt;]&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;xs&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Vector&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;NumberLike&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;A&lt;&#x2F;span&gt;&lt;span&gt;]]&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;NumberLike&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;A&lt;&#x2F;span&gt;&lt;span&gt;] &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= 
&lt;&#x2F;span&gt;&lt;span&gt;    xs.reduce&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d33682;&quot;&gt;_&lt;&#x2F;span&gt;&lt;span&gt;.plus&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d33682;&quot;&gt;_&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;))&lt;&#x2F;span&gt;&lt;span&gt;.divide&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;xs.size&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Now we have solved the problem of extensibility: Users of our library can pass in a &lt;code&gt;NumberLike&lt;&#x2F;code&gt; adapter for &lt;code&gt;Int&lt;&#x2F;code&gt; (which we would likely provide ourselves) or for any possible type that might behave like a number, without having to recompile the module in which our statistics methods are implemented.&lt;&#x2F;p&gt;
&lt;p&gt;However, always wrapping your numbers in an adapter is not only tiresome to write and read, it also means that you have to create a lot of instances of your adapter classes when interacting with our library.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;type-classes-to-the-rescue&quot;&gt;Type classes to the rescue!&lt;&#x2F;h2&gt;
&lt;p&gt;A powerful alternative to the approaches outlined so far is, of course, to define and use a type class. Type classes, one of the prominent features of the Haskell language, despite their name, haven&#x27;t got anything to do with classes in object-oriented programming.&lt;&#x2F;p&gt;
&lt;p&gt;A type class &lt;code&gt;C&lt;&#x2F;code&gt; defines some behaviour in the form of operations that must be supported by a type &lt;code&gt;T&lt;&#x2F;code&gt; for it to be a member of type class &lt;code&gt;C&lt;&#x2F;code&gt;. Whether the type &lt;code&gt;T&lt;&#x2F;code&gt; is a member of the type class &lt;code&gt;C&lt;&#x2F;code&gt; is not inherent in the type. Rather, any developer can declare that a type is a member of a type class simply by providing implementations of the operations the type must support. Now, once &lt;code&gt;T&lt;&#x2F;code&gt; is made a member of the type class &lt;code&gt;C&lt;&#x2F;code&gt;, functions that have constrained one or more of their parameters to be members of &lt;code&gt;C&lt;&#x2F;code&gt; can be called with arguments of type &lt;code&gt;T&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;As such, type classes allow ad-hoc and retroactive polymorphism. Code that relies on type classes is open to extension without the need to create adapter objects.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;creating-a-type-class&quot;&gt;Creating a type class&lt;&#x2F;h3&gt;
&lt;p&gt;In Scala, type classes can be implemented and used by a combination of techniques. It&#x27;s a little more involved than in Haskell, but also gives developers more control.&lt;&#x2F;p&gt;
&lt;p&gt;Creating a type class in Scala involves several steps. First, let&#x27;s define a trait. This is the actual type class:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;scala&quot; style=&quot;background-color:#002b36;color:#839496;&quot; class=&quot;language-scala &quot;&gt;&lt;code class=&quot;language-scala&quot; data-lang=&quot;scala&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;object&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt; Math &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;trait&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt; NumberLike&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;T&lt;&#x2F;span&gt;&lt;span&gt;] &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;plus&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;x&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;T&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;y&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;T&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;T
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;divide&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;x&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;T&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;y&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;Int&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;T
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;minus&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;x&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;T&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;y&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;T&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;T
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;}
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;We have created a type class called &lt;code&gt;NumberLike&lt;&#x2F;code&gt;. Type classes always take one or more type parameters, and they are usually designed to be stateless, i.e. the methods defined on our &lt;code&gt;NumberLike&lt;&#x2F;code&gt; trait operate only on the passed in arguments. In particular, where our adapter above operated on its member of type &lt;code&gt;T&lt;&#x2F;code&gt; and one argument, the methods defined for our &lt;code&gt;NumberLike&lt;&#x2F;code&gt; type class take two parameters of type &lt;code&gt;T&lt;&#x2F;code&gt; each – the member has become the first parameter of the operations supported by &lt;code&gt;NumberLike&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;providing-default-members&quot;&gt;Providing default members&lt;&#x2F;h3&gt;
&lt;p&gt;The second step in implementing a type class is usually to provide some default implementations of your type class trait in its companion object. We will see in a moment why this is generally a good strategy. First, however, let&#x27;s do this, too, by making &lt;code&gt;Double&lt;&#x2F;code&gt; and &lt;code&gt;Int&lt;&#x2F;code&gt; members of our &lt;code&gt;NumberLike&lt;&#x2F;code&gt; type class:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;scala&quot; style=&quot;background-color:#002b36;color:#839496;&quot; class=&quot;language-scala &quot;&gt;&lt;code class=&quot;language-scala&quot; data-lang=&quot;scala&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;object&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt; Math &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;trait&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt; NumberLike&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;T&lt;&#x2F;span&gt;&lt;span&gt;] &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;plus&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;x&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;T&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;y&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;T&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;T
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;divide&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;x&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;T&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;y&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;Int&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;T
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;minus&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;x&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;T&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;y&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;T&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;T
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;}
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;object&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt; NumberLike &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#93a1a1;&quot;&gt;implicit &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;object&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt; NumberLikeDouble &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;extends &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;NumberLike&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;Double&lt;&#x2F;span&gt;&lt;span&gt;] &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;      &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;plus&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;x&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;Double&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;y&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;Double&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;Double &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt; x + y
&lt;&#x2F;span&gt;&lt;span&gt;      &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;divide&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;x&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;Double&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;y&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;Int&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;Double &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt; x &#x2F; y
&lt;&#x2F;span&gt;&lt;span&gt;      &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;minus&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;x&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;Double&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;y&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;Double&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;Double &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt; x - y
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;}
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#93a1a1;&quot;&gt;implicit &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;object&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt; NumberLikeInt &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;extends &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;NumberLike&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;Int&lt;&#x2F;span&gt;&lt;span&gt;] &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;      &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;plus&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;x&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;Int&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;y&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;Int&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;Int &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt; x + y
&lt;&#x2F;span&gt;&lt;span&gt;      &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;divide&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;x&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;Int&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;y&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;Int&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;Int &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt; x &#x2F; y
&lt;&#x2F;span&gt;&lt;span&gt;      &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;minus&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;x&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;Int&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;y&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;Int&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;Int &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt; x - y
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;}
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;}
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Two things: First, you see that the two implementations are basically identical. That is not always the case when creating members of a type classes. Our &lt;code&gt;NumberLike&lt;&#x2F;code&gt; type class is just a rather narrow domain. Later in the article, I will give examples of type classes where there is a lot less room for duplication when implementing them for multiple types. Second, please ignore the fact that we are losing precision in &lt;code&gt;NumberLikeInt&lt;&#x2F;code&gt; by doing integer division. It&#x27;s all to keep things simple for this example.&lt;&#x2F;p&gt;
&lt;p&gt;As you can see, members of type classes are usually singleton objects. Also, please note the &lt;code&gt;implicit&lt;&#x2F;code&gt; keyword before each of the type class implementations. This is one of the crucial elements for making type classes possible in Scala, making type class members implicitly available under certain conditions. More about that in the next section.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;coding-against-type-classes&quot;&gt;Coding against type classes&lt;&#x2F;h3&gt;
&lt;p&gt;Now that we have our type class and two default implementations for common types, we want to code against this type class in our statistics module. Let&#x27;s focus on the &lt;code&gt;mean&lt;&#x2F;code&gt; method for now:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;scala&quot; style=&quot;background-color:#002b36;color:#839496;&quot; class=&quot;language-scala &quot;&gt;&lt;code class=&quot;language-scala&quot; data-lang=&quot;scala&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;object&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt; Statistics &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;import&lt;&#x2F;span&gt;&lt;span&gt; Math.NumberLike
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;mean&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;T&lt;&#x2F;span&gt;&lt;span&gt;]&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;xs&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Vector&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;T&lt;&#x2F;span&gt;&lt;span&gt;]&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#93a1a1;&quot;&gt;implicit &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;ev&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;NumberLike&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;T&lt;&#x2F;span&gt;&lt;span&gt;]&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;T &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;=
&lt;&#x2F;span&gt;&lt;span&gt;    ev.divide&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;xs.reduce&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;ev.plus&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d33682;&quot;&gt;_&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d33682;&quot;&gt;_&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;))&lt;&#x2F;span&gt;&lt;span&gt;, xs.size&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This may look a little intimidating at first, but it&#x27;s actually quite simple. Our method takes a type parameter &lt;code&gt;T&lt;&#x2F;code&gt; and a single parameter of type &lt;code&gt;Vector[T]&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;The idea to constrain a parameter to types that are members of a specific type class is realized by means of the &lt;code&gt;implicit&lt;&#x2F;code&gt; second parameter list. What does this mean? Basically, that a value of type &lt;code&gt;NumberLike[T]&lt;&#x2F;code&gt; must be implicitly available in the current scope. This is the case if an &lt;em&gt;implicit value&lt;&#x2F;em&gt; has been declared and made available in the current scope, very often by importing the package or object in which that implicit value is defined.&lt;&#x2F;p&gt;
&lt;p&gt;If and only if no other implicit value can be found, the compiler will look in the companion object of the type of the implicit parameter. Hence, as a library designer, putting your default type class implementations in the companion object of your type class trait means that users of your library can easily override these implementations with their own ones, which is exactly what you want. Users can also pass in an explicit value for an implicit parameter to override the implicit values that are in scope.&lt;&#x2F;p&gt;
&lt;p&gt;Let&#x27;s see if the default type class implementations can be resolved:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;scala&quot; style=&quot;background-color:#002b36;color:#839496;&quot; class=&quot;language-scala &quot;&gt;&lt;code class=&quot;language-scala&quot; data-lang=&quot;scala&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;val numbers &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Vector&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;Double&lt;&#x2F;span&gt;&lt;span&gt;]&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;13&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;23.0&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;42&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;45&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;61&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;73&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;96&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;100&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;199&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;420&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;900&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;3839&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;println&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Statistics&lt;&#x2F;span&gt;&lt;span&gt;.mean&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;numbers&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;))
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Wonderful! If we try this with a &lt;code&gt;Vector[String]&lt;&#x2F;code&gt;, we get an error at compile time, stating that no implicit value could be found for parameter &lt;code&gt;ev: NumberLike[String]&lt;&#x2F;code&gt;. If you don&#x27;t like this error message, you can customize it by annotating your type class trait with the &lt;code&gt;@implicitNotFound&lt;&#x2F;code&gt; annotation:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;scala&quot; style=&quot;background-color:#002b36;color:#839496;&quot; class=&quot;language-scala &quot;&gt;&lt;code class=&quot;language-scala&quot; data-lang=&quot;scala&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;object&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt; Math &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;import&lt;&#x2F;span&gt;&lt;span&gt; annotation.implicitNotFound
&lt;&#x2F;span&gt;&lt;span&gt;  @&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;implicitNotFound&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;No member of type class NumberLike in scope for ${T}&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;trait&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt; NumberLike&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;T&lt;&#x2F;span&gt;&lt;span&gt;] &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;plus&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;x&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;T&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;y&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;T&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;T
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;divide&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;x&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;T&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;y&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;Int&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;T
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;minus&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;x&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;T&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;y&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;T&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;T
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;}
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h4 id=&quot;context-bounds&quot;&gt;Context bounds&lt;&#x2F;h4&gt;
&lt;p&gt;A second, implicit parameter list on all methods that expect a member of a type class can be a little verbose. As a shortcut  for implicit parameters with only one type parameter, Scala provides so-called &lt;em&gt;context bounds&lt;&#x2F;em&gt;. To show how those are used, we are going to implement our other statistics methods using those instead:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;scala&quot; style=&quot;background-color:#002b36;color:#839496;&quot; class=&quot;language-scala &quot;&gt;&lt;code class=&quot;language-scala&quot; data-lang=&quot;scala&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;object&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt; Statistics &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;import&lt;&#x2F;span&gt;&lt;span&gt; Math.NumberLike
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;mean&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;T&lt;&#x2F;span&gt;&lt;span&gt;]&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;xs&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Vector&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;T&lt;&#x2F;span&gt;&lt;span&gt;]&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#93a1a1;&quot;&gt;implicit &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;ev&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;NumberLike&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;T&lt;&#x2F;span&gt;&lt;span&gt;]&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;T &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;=
&lt;&#x2F;span&gt;&lt;span&gt;    ev.divide&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;xs.reduce&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;ev.plus&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d33682;&quot;&gt;_&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d33682;&quot;&gt;_&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;))&lt;&#x2F;span&gt;&lt;span&gt;, xs.size&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;median&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;T : NumberLike&lt;&#x2F;span&gt;&lt;span&gt;]&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;xs&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Vector&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;T&lt;&#x2F;span&gt;&lt;span&gt;]&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;T &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt; xs&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;xs.size &#x2F; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;2&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;quartiles&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;T: NumberLike&lt;&#x2F;span&gt;&lt;span&gt;]&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;xs&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Vector&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;T&lt;&#x2F;span&gt;&lt;span&gt;]&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span&gt;: (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;T&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;T&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;T&lt;&#x2F;span&gt;&lt;span&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= 
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;xs&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;xs.size &#x2F; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;4&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span&gt;, median&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;xs&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span&gt;, xs&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;xs.size &#x2F; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;4&lt;&#x2F;span&gt;&lt;span&gt; * &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;3&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;))
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;iqr&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;T: NumberLike&lt;&#x2F;span&gt;&lt;span&gt;]&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;xs&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Vector&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;T&lt;&#x2F;span&gt;&lt;span&gt;]&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;T &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt; quartiles&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;xs&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;match &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;case &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;lowerQuartile&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d33682;&quot;&gt;_&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;upperQuartile&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;=&amp;gt; 
&lt;&#x2F;span&gt;&lt;span&gt;      implicitly[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;NumberLike&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;T&lt;&#x2F;span&gt;&lt;span&gt;]].minus&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;upperQuartile, lowerQuartile&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;}
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;A context bound &lt;code&gt;T : NumberLike&lt;&#x2F;code&gt; means that an implicit value of type &lt;code&gt;NumberLike[T]&lt;&#x2F;code&gt; must be available, and so is really equivalent to having a second implicit parameter list with a &lt;code&gt;NumberLike[T]&lt;&#x2F;code&gt; in it. If you want to access that implicitly available value, however, you need to call the &lt;code&gt;implicitly&lt;&#x2F;code&gt; method, as we do in the &lt;code&gt;iqr&lt;&#x2F;code&gt; method. If your type class requires more than one type parameter, you cannot use the context bound syntax.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;custom-type-class-members&quot;&gt;Custom type class members&lt;&#x2F;h3&gt;
&lt;p&gt;As a user of a library that makes use of type classes, you will sooner or later have types that you want to make members of those type classes. For instance, we might want to use the statistics library for instances of the Joda Time &lt;code&gt;Duration&lt;&#x2F;code&gt; type. To do that, we need Joda Time on our classpath, of course:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;scala&quot; style=&quot;background-color:#002b36;color:#839496;&quot; class=&quot;language-scala &quot;&gt;&lt;code class=&quot;language-scala&quot; data-lang=&quot;scala&quot;&gt;&lt;span&gt;libraryDependencies += &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;joda-time&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; % &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;joda-time&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; % &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;2.1&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;libraryDependencies += &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;org.joda&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; % &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;joda-convert&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; % &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;1.3&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Now we just have to create an implicitly available implementation of &lt;code&gt;NumberLike&lt;&#x2F;code&gt; (please make sure you have Joda Time on your classpath when trying this out):&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;scala&quot; style=&quot;background-color:#002b36;color:#839496;&quot; class=&quot;language-scala &quot;&gt;&lt;code class=&quot;language-scala&quot; data-lang=&quot;scala&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;object&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt; JodaImplicits &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;import&lt;&#x2F;span&gt;&lt;span&gt; Math.NumberLike
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;import&lt;&#x2F;span&gt;&lt;span&gt; org.joda.time.Duration
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#93a1a1;&quot;&gt;implicit &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;object&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt; NumberLikeDuration &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;extends &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;NumberLike&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Duration&lt;&#x2F;span&gt;&lt;span&gt;] &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;plus&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;x&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Duration&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;y&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Duration&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Duration &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt; x.plus&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;y&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;divide&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;x&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Duration&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;y&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;Int&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Duration &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Duration&lt;&#x2F;span&gt;&lt;span&gt;.millis&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;x.getMillis &#x2F; y&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;minus&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;x&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Duration&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;y&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Duration&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Duration &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt; x.minus&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;y&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;}
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;If we import the package or object containing this &lt;code&gt;NumberLike&lt;&#x2F;code&gt; implementation, we can now compute the mean value for a bunch of durations:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;scala&quot; style=&quot;background-color:#002b36;color:#839496;&quot; class=&quot;language-scala &quot;&gt;&lt;code class=&quot;language-scala&quot; data-lang=&quot;scala&quot;&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;import&lt;&#x2F;span&gt;&lt;span&gt; Statistics.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d33682;&quot;&gt;_
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;import&lt;&#x2F;span&gt;&lt;span&gt; JodaImplicits.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d33682;&quot;&gt;_
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;import&lt;&#x2F;span&gt;&lt;span&gt; org.joda.time.Duration.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d33682;&quot;&gt;_
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;val durations &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Vector&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;standardSeconds&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;20&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span&gt;, standardSeconds&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;57&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span&gt;, standardMinutes&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;2&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;  standardMinutes&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;17&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span&gt;, standardMinutes&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;30&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span&gt;, standardMinutes&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;58&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span&gt;, standardHours&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;2&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;  standardHours&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;5&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span&gt;, standardHours&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;8&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span&gt;, standardHours&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;17&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span&gt;, standardDays&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;  standardDays&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;4&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;))
&lt;&#x2F;span&gt;&lt;span&gt;println&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;mean&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;durations&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span&gt;.getStandardHours&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h2 id=&quot;use-cases&quot;&gt;Use cases&lt;&#x2F;h2&gt;
&lt;p&gt;Our &lt;code&gt;NumberLike&lt;&#x2F;code&gt; type class was a nice exercise, but Scala already ships with the &lt;code&gt;Numeric&lt;&#x2F;code&gt; type class, which allows you to call methods like &lt;code&gt;sum&lt;&#x2F;code&gt; or &lt;code&gt;product&lt;&#x2F;code&gt; on collections for whose type &lt;code&gt;T&lt;&#x2F;code&gt; a &lt;code&gt;Numeric[T]&lt;&#x2F;code&gt; is available. Another type class in the standard library that you will use a lot is &lt;code&gt;Ordering&lt;&#x2F;code&gt;, which allows you to provide an implicit ordering for your own types, available to the &lt;code&gt;sort&lt;&#x2F;code&gt; method on Scala&#x27;s collections.&lt;&#x2F;p&gt;
&lt;p&gt;There are more type classes in the standard library, but not all of them are ones you have to deal with on a regular basis as a Scala developer.&lt;&#x2F;p&gt;
&lt;p&gt;A very common use case in third-party libraries is that of object serialization and deserialization, most notably to and from JSON. By making your classes members of an appropriate formatter type class, you can customize the way your classes are serialized to JSON, XML or whatever format is currently the new black.&lt;&#x2F;p&gt;
&lt;p&gt;Mapping between Scala types and ones supported by your database driver is also commonly made customizable and extensible via type classes.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;summary&quot;&gt;Summary&lt;&#x2F;h2&gt;
&lt;p&gt;Once you start to do some serious work with Scala, you will inevitably stumble upon type classes. I hope that after reading this article, you are prepared to take advantage of this powerful technique.&lt;&#x2F;p&gt;
&lt;p&gt;Scala type classes allow you to develop your Scala code in such a way that it&#x27;s open for retroactive extension while retaining as much concrete type information as possible. In contrast to approaches from other languages, they give developers full control, as default type class implementations can be overridden without much hassle, and type classes implementations are not made available in the global namespace.&lt;&#x2F;p&gt;
&lt;p&gt;You will see that this technique is especially useful when writing libraries intended to be used by others, but type classes also have their use in application code to decrease coupling between modules.&lt;&#x2F;p&gt;
</description>
      </item>
      <item>
          <title>The Neophyte&#x27;s Guide to Scala Part 11: Currying and partially applied functions</title>
          <pubDate>Wed, 30 Jan 2013 00:00:00 +0000</pubDate>
          <author>Unknown</author>
          <link>https://danielwestheide.com/blog/the-neophytes-guide-to-scala-part-11-currying-and-partially-applied-functions/</link>
          <guid>https://danielwestheide.com/blog/the-neophytes-guide-to-scala-part-11-currying-and-partially-applied-functions/</guid>
          <description xml:base="https://danielwestheide.com/blog/the-neophytes-guide-to-scala-part-11-currying-and-partially-applied-functions/">&lt;p&gt;&lt;a href=&quot;http:&#x2F;&#x2F;danielwestheide.com&#x2F;blog&#x2F;2013&#x2F;01&#x2F;23&#x2F;the-neophytes-guide-to-scala-part-10-staying-dry-with-higher-order-functions.html&quot;&gt;Last week&#x27;s article&lt;&#x2F;a&gt; was all about avoiding code duplication, either by lifting existing functions to match your new requirements or by composing them. In this article, we are going to have a look at two other mechanisms the Scala language provides in order to enable you to reuse your functions: Partial application of functions and currying.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;partially-applied-functions&quot;&gt;Partially applied functions&lt;&#x2F;h2&gt;
&lt;p&gt;Scala, like many other languages following the functional programming paradigm, allows you to apply a function &lt;em&gt;partially&lt;&#x2F;em&gt;. What this means is that, when applying the function, you do not pass in arguments for &lt;em&gt;all&lt;&#x2F;em&gt; of the parameters defined by the function, but only for some of them, leaving the remaining ones blank. What you get back is a new function whose parameter list only contains those parameters from the original function that were left blank.&lt;&#x2F;p&gt;
&lt;p&gt;Do not confuse partially applied functions with &lt;a href=&quot;http:&#x2F;&#x2F;danielwestheide.com&#x2F;blog&#x2F;2012&#x2F;12&#x2F;12&#x2F;the-neophytes-guide-to-scala-part-4-pattern-matching-anonymous-functions.html&quot;&gt;partially defined functions&lt;&#x2F;a&gt;, which are represented by the &lt;code&gt;PartialFunction&lt;&#x2F;code&gt; type in Scala.&lt;&#x2F;p&gt;
&lt;p&gt;To illustrate how partial function application works, let&#x27;s revisit our example from last week: For our imaginary free mail service, we wanted to allow the user to configure a filter so that only emails meeting certain criteria would show up in their inbox, with all others being blocked.&lt;&#x2F;p&gt;
&lt;p&gt;Our &lt;code&gt;Email&lt;&#x2F;code&gt; case class still looks like this:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;scala&quot; style=&quot;background-color:#002b36;color:#839496;&quot; class=&quot;language-scala &quot;&gt;&lt;code class=&quot;language-scala&quot; data-lang=&quot;scala&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;case class&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt; Email&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;subject&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;String&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;text&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;String&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;sender&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;String&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;recipient&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;String&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;type &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;EmailFilter &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Email =&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;Boolean
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The criteria for filtering emails were represented by a predicate &lt;code&gt;Email =&amp;gt; Boolean&lt;&#x2F;code&gt;, which we aliased to the type &lt;code&gt;EmailFilter&lt;&#x2F;code&gt;, and we were able to generate new predicates by calling appropriate factory functions.&lt;&#x2F;p&gt;
&lt;p&gt;Two of the factory functions from last week&#x27;s article created &lt;code&gt;EmailFiter&lt;&#x2F;code&gt; instances that checked if the email text satisfied a given minimum or maximum length. This time we want to make use of partial function application to implement these factory functions. We want to have a general method &lt;code&gt;sizeConstraint&lt;&#x2F;code&gt; and be able to create more specific size constraints by fixing some of its parameters.&lt;&#x2F;p&gt;
&lt;p&gt;Here is our revised &lt;code&gt;sizeConstraint&lt;&#x2F;code&gt; method:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;scala&quot; style=&quot;background-color:#002b36;color:#839496;&quot; class=&quot;language-scala &quot;&gt;&lt;code class=&quot;language-scala&quot; data-lang=&quot;scala&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;type &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;IntPairPred &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;Int&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;Int&lt;&#x2F;span&gt;&lt;span&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;=&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;Boolean
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;sizeConstraint&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;pred&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;IntPairPred&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;n&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;Int&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;email&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Email&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;) =&lt;&#x2F;span&gt;&lt;span&gt; pred&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;email.text.size, n&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;We also define an alias &lt;code&gt;IntPairPred&lt;&#x2F;code&gt; for the type of predicate that checks a pair of integers (the value &lt;code&gt;n&lt;&#x2F;code&gt; and the text size of the email) and returns whether the email text size is okay, given &lt;code&gt;n&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;Note that unlike our &lt;code&gt;sizeConstraint&lt;&#x2F;code&gt; function from last week, this one does not return a new &lt;code&gt;EmailFilter&lt;&#x2F;code&gt; predicate, but simply evaluates all the arguments passed to it, returning a &lt;code&gt;Boolean&lt;&#x2F;code&gt;. The trick is to get such a predicate of type &lt;code&gt;EmailFilter&lt;&#x2F;code&gt; by partially applying &lt;code&gt;sizeConstraint&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;First, however, because we take the DRY principle very seriously, let&#x27;s define all the commonly used instances of &lt;code&gt;IntPairPred&lt;&#x2F;code&gt;. Now, when we call &lt;code&gt;sizeConstraint&lt;&#x2F;code&gt;, we don&#x27;t have to repeatedly write the same anonymous functions, but can simply pass in one of those:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;scala&quot; style=&quot;background-color:#002b36;color:#839496;&quot; class=&quot;language-scala &quot;&gt;&lt;code class=&quot;language-scala&quot; data-lang=&quot;scala&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;val gt&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;IntPairPred &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d33682;&quot;&gt;_&lt;&#x2F;span&gt;&lt;span&gt; &amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d33682;&quot;&gt;_
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;val ge&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;IntPairPred &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d33682;&quot;&gt;_&lt;&#x2F;span&gt;&lt;span&gt; &amp;gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d33682;&quot;&gt;_
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;val lt&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;IntPairPred &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d33682;&quot;&gt;_&lt;&#x2F;span&gt;&lt;span&gt; &amp;lt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d33682;&quot;&gt;_
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;val le&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;IntPairPred &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d33682;&quot;&gt;_&lt;&#x2F;span&gt;&lt;span&gt; &amp;lt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d33682;&quot;&gt;_
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;val eq&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;IntPairPred &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d33682;&quot;&gt;_ &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;== &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d33682;&quot;&gt;_
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Finally, we are ready to do some partial application of the &lt;code&gt;sizeConstraint&lt;&#x2F;code&gt; function, fixing its first parameter with one of our &lt;code&gt;IntPairPred&lt;&#x2F;code&gt; instances:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;scala&quot; style=&quot;background-color:#002b36;color:#839496;&quot; class=&quot;language-scala &quot;&gt;&lt;code class=&quot;language-scala&quot; data-lang=&quot;scala&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;val minimumSize&lt;&#x2F;span&gt;&lt;span&gt;: (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;Int&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Email&lt;&#x2F;span&gt;&lt;span&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;=&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;Boolean &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt; sizeConstraint&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;ge, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d33682;&quot;&gt;_&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;Int&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d33682;&quot;&gt;_&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Email&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;val maximumSize&lt;&#x2F;span&gt;&lt;span&gt;: (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;Int&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Email&lt;&#x2F;span&gt;&lt;span&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;=&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;Boolean &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt; sizeConstraint&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;le, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d33682;&quot;&gt;_&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;Int&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d33682;&quot;&gt;_&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Email&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;As you can see, you have to use the placeholder &lt;code&gt;_&lt;&#x2F;code&gt; for all parameters not bound to an argument value. Unfortunately, you also have to specify the type of those arguments, which makes partial function application in Scala a bit tedious.&lt;&#x2F;p&gt;
&lt;p&gt;The reason is that the Scala compiler cannot infer these types, at least not in all cases – think of overloaded methods where it&#x27;s impossible for the compiler to know which of them you are referring to.&lt;&#x2F;p&gt;
&lt;p&gt;On the other hand, this makes it possible to bind or leave out arbitrary parameters. For example, we can leave out the first one and pass in the size to be used as a constraint:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;scala&quot; style=&quot;background-color:#002b36;color:#839496;&quot; class=&quot;language-scala &quot;&gt;&lt;code class=&quot;language-scala&quot; data-lang=&quot;scala&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;val constr20&lt;&#x2F;span&gt;&lt;span&gt;: (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;IntPairPred&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Email&lt;&#x2F;span&gt;&lt;span&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;=&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;Boolean &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt; sizeConstraint&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d33682;&quot;&gt;_&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;IntPairPred&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;20&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d33682;&quot;&gt;_&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Email&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;val constr30&lt;&#x2F;span&gt;&lt;span&gt;: (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;IntPairPred&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Email&lt;&#x2F;span&gt;&lt;span&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;=&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;Boolean &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt; sizeConstraint&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d33682;&quot;&gt;_&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;IntPairPred&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;30&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d33682;&quot;&gt;_&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Email&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Now we have two functions that take an &lt;code&gt;IntPairPred&lt;&#x2F;code&gt; and an &lt;code&gt;Email&lt;&#x2F;code&gt; and compare the email text size to &lt;code&gt;20&lt;&#x2F;code&gt; and &lt;code&gt;30&lt;&#x2F;code&gt;, respectively, but the comparison logic has not been specified yet, as that&#x27;s exactly what the &lt;code&gt;IntPairPred&lt;&#x2F;code&gt; is good for.&lt;&#x2F;p&gt;
&lt;p&gt;This shows that, while quite verbose, partial function application in Scala is a little more flexible than in Clojure, for example, where you have to pass in arguments from left to right, but can&#x27;t leave out any in the middle.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;from-methods-to-function-objects&quot;&gt;From methods to function objects&lt;&#x2F;h3&gt;
&lt;p&gt;When doing partial application on a method, you can also decide to not bind any parameters whatsoever. The parameter list of the returned function object will be the same as for the method. You have effectively turned a method into a function that can be assigned to a &lt;code&gt;val&lt;&#x2F;code&gt; or passed around:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;scala&quot; style=&quot;background-color:#002b36;color:#839496;&quot; class=&quot;language-scala &quot;&gt;&lt;code class=&quot;language-scala&quot; data-lang=&quot;scala&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;val sizeConstraintFn&lt;&#x2F;span&gt;&lt;span&gt;: (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;IntPairPred&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;Int&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Email&lt;&#x2F;span&gt;&lt;span&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;=&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;Boolean &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt; sizeConstraint &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d33682;&quot;&gt;_
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h3 id=&quot;producing-those-emailfilters&quot;&gt;Producing those EmailFilters&lt;&#x2F;h3&gt;
&lt;p&gt;We still haven&#x27;t got any functions that adhere to the &lt;code&gt;EmailFilter&lt;&#x2F;code&gt; type or that return new predicates of that type – like &lt;code&gt;sizeConstraint&lt;&#x2F;code&gt;, &lt;code&gt;minimumSize&lt;&#x2F;code&gt; and &lt;code&gt;maximumSize&lt;&#x2F;code&gt; don&#x27;t return a new predicate, but a &lt;code&gt;Boolean&lt;&#x2F;code&gt;, as their signature clearly shows.&lt;&#x2F;p&gt;
&lt;p&gt;However, our email filters are just another partial function application away. By fixing the integer parameter of &lt;code&gt;minimumSize&lt;&#x2F;code&gt; and &lt;code&gt;maximumSize&lt;&#x2F;code&gt;, we can create new functions of type &lt;code&gt;EmailFilter&lt;&#x2F;code&gt;:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;scala&quot; style=&quot;background-color:#002b36;color:#839496;&quot; class=&quot;language-scala &quot;&gt;&lt;code class=&quot;language-scala&quot; data-lang=&quot;scala&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;val min20&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;EmailFilter &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt; minimumSize&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;20&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d33682;&quot;&gt;_&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Email&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;val max20&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;EmailFilter &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt; maximumSize&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;20&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d33682;&quot;&gt;_&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Email&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Of course, we could achieve the same by partially applying our &lt;code&gt;constr20&lt;&#x2F;code&gt; we created above:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;scala&quot; style=&quot;background-color:#002b36;color:#839496;&quot; class=&quot;language-scala &quot;&gt;&lt;code class=&quot;language-scala&quot; data-lang=&quot;scala&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;val min20&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;EmailFilter &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt; constr20&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;ge, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d33682;&quot;&gt;_&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Email&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;val max20&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;EmailFilter &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt; constr20&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;le, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d33682;&quot;&gt;_&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Email&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h2 id=&quot;spicing-up-your-functions&quot;&gt;Spicing up your functions&lt;&#x2F;h2&gt;
&lt;p&gt;Maybe you find partial function application in Scala a little too verbose, or simply not very elegant to write and look at. Lucky you, because there is an alternative.&lt;&#x2F;p&gt;
&lt;p&gt;As you should know by now, methods in Scala can have more than one parameter list. Let&#x27;s define our &lt;code&gt;sizeConstraint&lt;&#x2F;code&gt; method such that each parameter is in its own parameter list:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;scala&quot; style=&quot;background-color:#002b36;color:#839496;&quot; class=&quot;language-scala &quot;&gt;&lt;code class=&quot;language-scala&quot; data-lang=&quot;scala&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;sizeConstraint&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;pred&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;IntPairPred&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;n&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;Int&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;email&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Email&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;Boolean &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;=
&lt;&#x2F;span&gt;&lt;span&gt;  pred&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;email.text.size, n&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;If we turn this into a function object that we can assign or pass around, the signature of that function looks like this:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;scala&quot; style=&quot;background-color:#002b36;color:#839496;&quot; class=&quot;language-scala &quot;&gt;&lt;code class=&quot;language-scala&quot; data-lang=&quot;scala&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;val sizeConstraintFn&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;IntPairPred =&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;Int &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;=&amp;gt; Email =&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;Boolean &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt; sizeConstraint &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d33682;&quot;&gt;_
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Such a chain of one-parameter functions is called a &lt;em&gt;curried&lt;&#x2F;em&gt; function, so named after Haskell Curry, who re-discovered this technique and got all the fame for it. In fact, in the Haskell programming language, all functions are in curried form by default.&lt;&#x2F;p&gt;
&lt;p&gt;In our example, it takes an &lt;code&gt;IntPairPred&lt;&#x2F;code&gt; and returns a function that takes an &lt;code&gt;Int&lt;&#x2F;code&gt; and returns a new function. That last function, finally, takes an &lt;code&gt;Email&lt;&#x2F;code&gt; and returns a &lt;code&gt;Boolean&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;Now, if we we want to bind the &lt;code&gt;IntPairPred&lt;&#x2F;code&gt;, we simply apply &lt;code&gt;sizeConstraintFn&lt;&#x2F;code&gt;, which takes exactly this one parameter and returns a new one-parameter function:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;scala&quot; style=&quot;background-color:#002b36;color:#839496;&quot; class=&quot;language-scala &quot;&gt;&lt;code class=&quot;language-scala&quot; data-lang=&quot;scala&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;val minSize&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;Int &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;=&amp;gt; Email =&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;Boolean &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt; sizeConstraint&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;ge&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;val maxSize&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;Int &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;=&amp;gt; Email =&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;Boolean &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt; sizeConstraint&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;le&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;There is no need to use any placeholders for parameters left blank, because we are in fact not doing any partial function application.&lt;&#x2F;p&gt;
&lt;p&gt;We can now create the same &lt;code&gt;EmailFilter&lt;&#x2F;code&gt; predicates as we did using partial function application before, by applying these curried functions:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;scala&quot; style=&quot;background-color:#002b36;color:#839496;&quot; class=&quot;language-scala &quot;&gt;&lt;code class=&quot;language-scala&quot; data-lang=&quot;scala&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;val min20&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Email =&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;Boolean &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt; minSize&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;20&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;val max20&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Email =&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;Boolean &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt; maxSize&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;20&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Of course, it&#x27;s possible to do all this in one step if you want to bind several parameters at once. It just means that you immediately apply the function that was returned from the first function application, without assigning it to a &lt;code&gt;val&lt;&#x2F;code&gt; first:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;scala&quot; style=&quot;background-color:#002b36;color:#839496;&quot; class=&quot;language-scala &quot;&gt;&lt;code class=&quot;language-scala&quot; data-lang=&quot;scala&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;val min20&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Email =&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;Boolean &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt; sizeConstraintFn&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;ge&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;20&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;val max20&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Email =&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;Boolean &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt; sizeConstraintFn&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;le&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;20&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h3 id=&quot;currying-existing-functions&quot;&gt;Currying existing functions&lt;&#x2F;h3&gt;
&lt;p&gt;It&#x27;s not always the case the you know beforehand whether writing your functions in curried form makes sense or not – after all, the usual function application looks a little more verbose than for functions that have only declared a single parameter list for all their parameters. Also, you&#x27;ll sometimes want to work with third-party functions in curried form, when they are written with a single parameter list.&lt;&#x2F;p&gt;
&lt;p&gt;Transforming a function with multiple parameters in one list to curried form is, of course, just another higher-order function, generating a new function from an existing one. This transformation logic is available in form of the &lt;code&gt;curried&lt;&#x2F;code&gt; method on functions with more than one parameter. Hence, if we have a function &lt;code&gt;sum&lt;&#x2F;code&gt;, taking two parameters, we can get the curried version simply calling calling its &lt;code&gt;curried&lt;&#x2F;code&gt; method:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;scala&quot; style=&quot;background-color:#002b36;color:#839496;&quot; class=&quot;language-scala &quot;&gt;&lt;code class=&quot;language-scala&quot; data-lang=&quot;scala&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;val sum&lt;&#x2F;span&gt;&lt;span&gt;: (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;Int&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;Int&lt;&#x2F;span&gt;&lt;span&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;=&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;Int &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d33682;&quot;&gt;_&lt;&#x2F;span&gt;&lt;span&gt; + &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d33682;&quot;&gt;_
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;val sumCurried&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;Int &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;=&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;Int &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;=&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;Int &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt; sum.curried
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;If you need to do the reverse, you have &lt;code&gt;Function.uncurried&lt;&#x2F;code&gt; at your fingertips, which you need to pass the curried function to get it back in uncurried form.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;injecting-your-dependencies-the-functional-way&quot;&gt;Injecting your dependencies the functional way&lt;&#x2F;h3&gt;
&lt;p&gt;To close this article, let&#x27;s have a look at what role curried functions can play in the large. If you come from the enterprisy Java or .NET world, you will be very familiar with the necessity to use more or less fancy dependency injection containers that take the heavy burden of providing all your objects with their respective dependencies off you. In Scala, you don&#x27;t really need any external tool for that, as the language already provides numerous features that make it much less of a pain to this all on your own.&lt;&#x2F;p&gt;
&lt;p&gt;When programming in a very functional way, you will notice that there is still a need to inject dependencies: Your functions residing in the higher layers of your application will have to make calls to other functions. Simply hard-coding the functions to call in the body of your functions makes it difficult to test them in isolation. Hence, you will need to pass the functions your higher-layer function depends on as arguments to that function.&lt;&#x2F;p&gt;
&lt;p&gt;It would not be DRY at all to always pass the same dependencies to your function when calling it, would it? Curried functions to the rescue! Currying and partial function application are one of several ways of injecting dependencies in functional programming.&lt;&#x2F;p&gt;
&lt;p&gt;The following, very simplified example illustrates this technique:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;scala&quot; style=&quot;background-color:#002b36;color:#839496;&quot; class=&quot;language-scala &quot;&gt;&lt;code class=&quot;language-scala&quot; data-lang=&quot;scala&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;case class&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt; User&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;name&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;String&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;trait&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt; EmailRepository &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;getMails&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;user&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;User&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;unread&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;Boolean&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Seq&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Email&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;}
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;trait&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt; FilterRepository &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;getEmailFilter&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;user&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;User&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;EmailFilter
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;}
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;trait&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt; MailboxService &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;getNewMails&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;emailRepo&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;EmailRepository&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;filterRepo&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;FilterRepository&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;user&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;User&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;) =
&lt;&#x2F;span&gt;&lt;span&gt;    emailRepo.getMails&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;user, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;true&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span&gt;.filter&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;filterRepo.getEmailFilter&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;user&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;))
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;val newMails&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;User =&amp;gt; Seq&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Email&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;We have a service that depends on two different repositories, These dependencies are declared as parameters to the &lt;code&gt;getNewMails&lt;&#x2F;code&gt; method, each in their own parameter list.&lt;&#x2F;p&gt;
&lt;p&gt;The &lt;code&gt;MailboxService&lt;&#x2F;code&gt; already has a concrete implementation of that method, but is lacking one for the &lt;code&gt;newMails&lt;&#x2F;code&gt; field. The type of that field is &lt;code&gt;User =&amp;gt; Seq[Email]&lt;&#x2F;code&gt; – that&#x27;s the function that components depending on the &lt;code&gt;MailboxService&lt;&#x2F;code&gt; will call.&lt;&#x2F;p&gt;
&lt;p&gt;We need an object that extends &lt;code&gt;MailboxService&lt;&#x2F;code&gt;. The idea is to implement &lt;code&gt;newMails&lt;&#x2F;code&gt; by currying the &lt;code&gt;getNewMails&lt;&#x2F;code&gt; method and fixing it with concrete implementations of the dependencies, &lt;code&gt;EmailRepository&lt;&#x2F;code&gt; and &lt;code&gt;FilterRepository&lt;&#x2F;code&gt;:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;scala&quot; style=&quot;background-color:#002b36;color:#839496;&quot; class=&quot;language-scala &quot;&gt;&lt;code class=&quot;language-scala&quot; data-lang=&quot;scala&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;object&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt; MockEmailRepository &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;extends &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;EmailRepository &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;getMails&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;user&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;User&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;unread&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;Boolean&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Seq&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Email&lt;&#x2F;span&gt;&lt;span&gt;] &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Nil
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;}
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;object&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt; MockFilterRepository &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;extends &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;FilterRepository &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;getEmailFilter&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;user&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;User&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;EmailFilter &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d33682;&quot;&gt;_ &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;=&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;true
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;}
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;object&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt; MailboxServiceWithMockDeps &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;extends &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;MailboxService &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;val newMails&lt;&#x2F;span&gt;&lt;span&gt;: (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;User&lt;&#x2F;span&gt;&lt;span&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;=&amp;gt; Seq&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Email&lt;&#x2F;span&gt;&lt;span&gt;] &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= 
&lt;&#x2F;span&gt;&lt;span&gt;    getNewMails&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;MockEmailRepository&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;MockFilterRepository&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d33682;&quot;&gt;_
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;We can now call &lt;code&gt;MailboxServiceWithMoxDeps.newMails(User(&quot;daniel&quot;))&lt;&#x2F;code&gt; without having to specify the two repositories to be used. In a real application, of course, we would very likely not use a direct reference to a concrete implementation of the service, but have this injected, too.&lt;&#x2F;p&gt;
&lt;p&gt;This is probably not the most powerful and scaleable way of injecting your dependencies in Scala, but it&#x27;s good to have this in your tool belt, and it&#x27;s a very good example of the benefits that partial function application and currying can provide in the wild. If you want know more about this, I recommend to have a look at the excellent slides for the presentation &quot;&lt;a href=&quot;http:&#x2F;&#x2F;de.slideshare.net&#x2F;debasishg&#x2F;dependency-injection-in-scala-beyond-the-cake-pattern&quot;&gt;Dependency Injection in Scala&lt;&#x2F;a&gt;&quot; by Debasish Ghosh, which is also where I first came across this technique.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;summary&quot;&gt;Summary&lt;&#x2F;h2&gt;
&lt;p&gt;In this article, we discussed two additional functional programming techniques that help you keep your code free of duplication and, on top of that, give you a lot of flexibility, allowing you to reuse your functions in manifold ways. Partial function application and currying have more or less the same effect, but sometimes one of them is the more elegant solution.&lt;&#x2F;p&gt;
&lt;p&gt;In the next part of this series, we will continue to look at ways to keep things flexible, discussing the what and how of &lt;em&gt;type classes&lt;&#x2F;em&gt; in Scala.&lt;&#x2F;p&gt;
</description>
      </item>
      <item>
          <title>The Neophyte&#x27;s Guide to Scala Part 10: Staying DRY with higher-order functions</title>
          <pubDate>Wed, 23 Jan 2013 00:00:00 +0000</pubDate>
          <author>Unknown</author>
          <link>https://danielwestheide.com/blog/the-neophytes-guide-to-scala-part-10-staying-dry-with-higher-order-functions/</link>
          <guid>https://danielwestheide.com/blog/the-neophytes-guide-to-scala-part-10-staying-dry-with-higher-order-functions/</guid>
          <description xml:base="https://danielwestheide.com/blog/the-neophytes-guide-to-scala-part-10-staying-dry-with-higher-order-functions/">&lt;p&gt;In the previous articles, I discussed the composable nature of Scala&#x27;s container types. As it turns out, being composable is a quality that you will not only find in &lt;code&gt;Future&lt;&#x2F;code&gt;, &lt;code&gt;Try&lt;&#x2F;code&gt;, and other container types, but also in functions, which are first class citizens in the Scala language.&lt;&#x2F;p&gt;
&lt;p&gt;Composability naturally entails reusability. While the latter has often been claimed to be one of the big advantages of object-oriented programming, it&#x27;s a trait that is definitely true for pure functions, i.e. functions that do not have any side-effects and are referentially transparent.&lt;&#x2F;p&gt;
&lt;p&gt;One obvious way is to implement a new function by calling already existing functions in its body. However, there are other ways to reuse existing functions: In this blog post, I will discuss some fundementals of functional programming that we have been missing out on so far. You will learn how to follow the &lt;a href=&quot;http:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Don%27t_repeat_yourself&quot;&gt;DRY&lt;&#x2F;a&gt; principle by leveraging higher-order functions in order to reuse existing code in new contexts.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;on-higher-order-functions&quot;&gt;On higher-order functions&lt;&#x2F;h2&gt;
&lt;p&gt;A higher-order function, as opposed to a first-order function, can have one of three forms:&lt;&#x2F;p&gt;
&lt;ol&gt;
&lt;li&gt;One or more of its parameters is a function, and it returns some value.&lt;&#x2F;li&gt;
&lt;li&gt;It returns a function, but none of its parameters is a function.&lt;&#x2F;li&gt;
&lt;li&gt;Both of the above: One or more of its parameters is a function, and it returns a function.&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;p&gt;If you have followed this series, you have seen a lot of usages of higher-order functions of the first type: We called methods like &lt;code&gt;map&lt;&#x2F;code&gt;, &lt;code&gt;filter&lt;&#x2F;code&gt;, or &lt;code&gt;flatMap&lt;&#x2F;code&gt; and passed a function to it that was used to transform or filter a collection in some way. Very often, the functions we passed to these methods were anonymous functions, sometimes involving a little bit of duplication.&lt;&#x2F;p&gt;
&lt;p&gt;In this article, we are only concerned with what the other two types of higher-order functions can do for us: The first of them allows us to produce new functions based on some input data, whereas the other gives us the power and flexibility to compose new functions that are somehow based on existing functions. In both cases, we can eliminate code duplication.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;and-out-of-nowhere-a-function-was-born&quot;&gt;And out of nowhere, a function was born&lt;&#x2F;h2&gt;
&lt;p&gt;You might think that the ability to create new functions based on some input data is not terribly useful. While we mainly want to deal with how to compose new functions based on existing ones, let&#x27;s first have a look at how a function that produces new functions may be used.&lt;&#x2F;p&gt;
&lt;p&gt;Let&#x27;s assume we are implementing a freemail service where users should be able to configure when an email is supposed to be blocked. We are representing emails as instances of a simple case class:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;scala&quot; style=&quot;background-color:#002b36;color:#839496;&quot; class=&quot;language-scala &quot;&gt;&lt;code class=&quot;language-scala&quot; data-lang=&quot;scala&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;case class&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt; Email&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;subject&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;String&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;text&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;String&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;sender&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;String&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;recipient&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;String&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;We want to be able to filter new emails by the criteria specified by the user, so we have a filtering function that makes use of a predicate, a function of type &lt;code&gt;Email =&amp;gt; Boolean&lt;&#x2F;code&gt; to determine whether the email is to be blocked. If the predicate is &lt;code&gt;true&lt;&#x2F;code&gt;, the email is accepted, otherwise it will be blocked:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;scala&quot; style=&quot;background-color:#002b36;color:#839496;&quot; class=&quot;language-scala &quot;&gt;&lt;code class=&quot;language-scala&quot; data-lang=&quot;scala&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;type &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;EmailFilter &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Email =&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;Boolean
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;newMailsForUser&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;mails&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Seq&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Email&lt;&#x2F;span&gt;&lt;span&gt;], &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;f&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;EmailFilter&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;) =&lt;&#x2F;span&gt;&lt;span&gt; mails.filter&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;f&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Note that we are using a type alias for our function, so that we can work with more meaningful names in our code.&lt;&#x2F;p&gt;
&lt;p&gt;Now, in order to allow the user to configure their email filter, we can implement some factory functions that produce &lt;code&gt;EmailFilter&lt;&#x2F;code&gt; functions configured to the user&#x27;s liking:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;scala&quot; style=&quot;background-color:#002b36;color:#839496;&quot; class=&quot;language-scala &quot;&gt;&lt;code class=&quot;language-scala&quot; data-lang=&quot;scala&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;val sentByOneOf&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Set&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;String&lt;&#x2F;span&gt;&lt;span&gt;] &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;=&amp;gt; EmailFilter &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= 
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;senders =&amp;gt; email =&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; senders.contains&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;email.sender&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;val notSentByAnyOf&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Set&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;String&lt;&#x2F;span&gt;&lt;span&gt;] &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;=&amp;gt; EmailFilter &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= 
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;senders =&amp;gt; email =&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; !senders.contains&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;email.sender&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;val minimumSize&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;Int &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;=&amp;gt; EmailFilter &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;n =&amp;gt; email =&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; email.text.size &amp;gt;= n
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;val maximumSize&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;Int &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;=&amp;gt; EmailFilter &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;n =&amp;gt; email =&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; email.text.size &amp;lt;= n
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Each of these four &lt;code&gt;vals&lt;&#x2F;code&gt; is a function that returns an &lt;code&gt;EmailFilter&lt;&#x2F;code&gt;, the first two taking as input a &lt;code&gt;Set[String]&lt;&#x2F;code&gt; representing senders, the other two an &lt;code&gt;Int&lt;&#x2F;code&gt; representing the length of the email body.&lt;&#x2F;p&gt;
&lt;p&gt;We can use any of these functions to create a new &lt;code&gt;EmailFilter&lt;&#x2F;code&gt; that we can pass to the &lt;code&gt;newMailsForUser&lt;&#x2F;code&gt; function:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;scala&quot; style=&quot;background-color:#002b36;color:#839496;&quot; class=&quot;language-scala &quot;&gt;&lt;code class=&quot;language-scala&quot; data-lang=&quot;scala&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;val emailFilter&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;EmailFilter &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt; notSentByAnyOf&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Set&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;johndoe@example.com&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;))
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;val mails &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Email&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(
&lt;&#x2F;span&gt;&lt;span&gt;  subject &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;It&amp;#39;s me again, your stalker friend!&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, 
&lt;&#x2F;span&gt;&lt;span&gt;  text &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Hello my friend! How are you?&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, 
&lt;&#x2F;span&gt;&lt;span&gt;  sender &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;johndoe@example.com&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, 
&lt;&#x2F;span&gt;&lt;span&gt;  recipient &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;me@example.com&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span&gt; :: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Nil
&lt;&#x2F;span&gt;&lt;span&gt;newMailsForUser&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;mails, emailFilter&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;&#x2F;&#x2F; returns an empty list
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This filter removes the one mail in the list because our user decided to put the sender on their black list. We can use our factory functions to create arbitrary &lt;code&gt;EmailFilter&lt;&#x2F;code&gt; functions, depending on the user&#x27;s requirements.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;reusing-existing-functions&quot;&gt;Reusing existing functions&lt;&#x2F;h2&gt;
&lt;p&gt;There are two problems with the current solution. First of all, there is quite a bit of duplication in the predicate factory functions above, when initially I told you that the composable nature of functions made it easy to stick to the DRY principle. So let&#x27;s get rid of the duplication.&lt;&#x2F;p&gt;
&lt;p&gt;To do that for the &lt;code&gt;minimumSize&lt;&#x2F;code&gt; and &lt;code&gt;maximumSize&lt;&#x2F;code&gt;, we introduce a function &lt;code&gt;sizeConstraint&lt;&#x2F;code&gt; that takes a predicate that checks if the size of the email body is okay. That size will be passed to the predicate by the &lt;code&gt;sizeConstraint&lt;&#x2F;code&gt; function:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;scala&quot; style=&quot;background-color:#002b36;color:#839496;&quot; class=&quot;language-scala &quot;&gt;&lt;code class=&quot;language-scala&quot; data-lang=&quot;scala&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;type &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;SizeChecker &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;Int &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;=&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;Boolean
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;val sizeConstraint&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;SizeChecker =&amp;gt; EmailFilter &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;f =&amp;gt; email =&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; f&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;email.text.size&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Now we can express &lt;code&gt;minimumSize&lt;&#x2F;code&gt; and &lt;code&gt;maximumSize&lt;&#x2F;code&gt; in terms of &lt;code&gt;sizeConstraint&lt;&#x2F;code&gt;:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;scala&quot; style=&quot;background-color:#002b36;color:#839496;&quot; class=&quot;language-scala &quot;&gt;&lt;code class=&quot;language-scala&quot; data-lang=&quot;scala&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;val minimumSize&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;Int &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;=&amp;gt; EmailFilter &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;n =&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; sizeConstraint&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d33682;&quot;&gt;_&lt;&#x2F;span&gt;&lt;span&gt; &amp;gt;= n&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;val maximumSize&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;Int &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;=&amp;gt; EmailFilter &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;n =&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; sizeConstraint&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d33682;&quot;&gt;_&lt;&#x2F;span&gt;&lt;span&gt; &amp;lt;= n&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h2 id=&quot;function-composition&quot;&gt;Function composition&lt;&#x2F;h2&gt;
&lt;p&gt;For the other two predicates, &lt;code&gt;sentByOneOf&lt;&#x2F;code&gt; and &lt;code&gt;notSentByAnyOf&lt;&#x2F;code&gt;, we are going to introduce a very generic higher-order function that allows us to express one of the two functions in terms of the other.&lt;&#x2F;p&gt;
&lt;p&gt;Let&#x27;s implement a function &lt;code&gt;complement&lt;&#x2F;code&gt; that takes a predicate &lt;code&gt;A =&amp;gt; Boolean&lt;&#x2F;code&gt; and returns a new function that always returns the opposite of the given predicate:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;scala&quot; style=&quot;background-color:#002b36;color:#839496;&quot; class=&quot;language-scala &quot;&gt;&lt;code class=&quot;language-scala&quot; data-lang=&quot;scala&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;complement&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;A&lt;&#x2F;span&gt;&lt;span&gt;]&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;predicate&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;A =&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;Boolean&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;) = (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;a&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;A&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;=&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; !predicate&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;a&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Now, for an existing predicate &lt;code&gt;p&lt;&#x2F;code&gt; we could get the complement by calling &lt;code&gt;complement(p)&lt;&#x2F;code&gt;. However, &lt;code&gt;sentByAnyOf&lt;&#x2F;code&gt; is not a predicate, but it &lt;em&gt;returns&lt;&#x2F;em&gt; one, namely an &lt;code&gt;EmailFilter&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;Scala functions provide two composing functions that will help us here: Given two functions &lt;code&gt;f&lt;&#x2F;code&gt; and &lt;code&gt;g&lt;&#x2F;code&gt;, &lt;code&gt;f.compose(g)&lt;&#x2F;code&gt; returns a new function that, when called, will first call &lt;code&gt;g&lt;&#x2F;code&gt; and then apply &lt;code&gt;f&lt;&#x2F;code&gt; on the result of it. Similarly, &lt;code&gt;f.andThen(g)&lt;&#x2F;code&gt; returns a new function that, when called, will apply &lt;code&gt;g&lt;&#x2F;code&gt; to the result of &lt;code&gt;f&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;We can put this to use to create our &lt;code&gt;notSentByAnyOf&lt;&#x2F;code&gt; predicate without code duplication:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;scala&quot; style=&quot;background-color:#002b36;color:#839496;&quot; class=&quot;language-scala &quot;&gt;&lt;code class=&quot;language-scala&quot; data-lang=&quot;scala&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;val notSentByAnyOf &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt; sentByOneOf andThen&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;g =&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; complement&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;g&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;))
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;What this means is that we ask for a new function that first applies the &lt;code&gt;sentByOneOf&lt;&#x2F;code&gt; function to its arguments (a &lt;code&gt;Set[String]&lt;&#x2F;code&gt;) and then applies the &lt;code&gt;complement&lt;&#x2F;code&gt; function to the &lt;code&gt;EmailFilter&lt;&#x2F;code&gt; predicate returned by the former function. Using Scala&#x27;s placeholder syntax for anonymous functions, we could write this more concisely as:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;scala&quot; style=&quot;background-color:#002b36;color:#839496;&quot; class=&quot;language-scala &quot;&gt;&lt;code class=&quot;language-scala&quot; data-lang=&quot;scala&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;val notSentByAnyOf &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt; sentByOneOf andThen&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;complement&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d33682;&quot;&gt;_&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;))
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Of course, you will now have noticed that, given a &lt;code&gt;complement&lt;&#x2F;code&gt; function, you could also implement the &lt;code&gt;maximumSize&lt;&#x2F;code&gt; predicate in terms of &lt;code&gt;minimumSize&lt;&#x2F;code&gt; instead of extracting a &lt;code&gt;sizeConstraint&lt;&#x2F;code&gt; function. However, the latter is more flexible, allowing you to specify arbitrary checks on the size of the mail body.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;composing-predicates&quot;&gt;Composing predicates&lt;&#x2F;h3&gt;
&lt;p&gt;Another problem with our email filters is that we can currently only pass a single &lt;code&gt;EmailFilter&lt;&#x2F;code&gt; to our &lt;code&gt;newMailsForUser&lt;&#x2F;code&gt; function. Certainly, our users want to configure multiple criteria. We need a way to create a composite predicate that returns &lt;code&gt;true&lt;&#x2F;code&gt; if either any, none or all of the predicates it consists of return &lt;code&gt;true&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;Here is one way to implement these functions:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;scala&quot; style=&quot;background-color:#002b36;color:#839496;&quot; class=&quot;language-scala &quot;&gt;&lt;code class=&quot;language-scala&quot; data-lang=&quot;scala&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;any&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;A&lt;&#x2F;span&gt;&lt;span&gt;]&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;predicates&lt;&#x2F;span&gt;&lt;span&gt;: (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;A =&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;Boolean&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;*&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;A =&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;Boolean &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= 
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;a =&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; predicates.exists&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;pred =&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; pred&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;a&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;))
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;none&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;A&lt;&#x2F;span&gt;&lt;span&gt;]&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;predicates&lt;&#x2F;span&gt;&lt;span&gt;: (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;A =&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;Boolean&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;*&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;) =&lt;&#x2F;span&gt;&lt;span&gt; complement&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;any&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;predicates: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;_*&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;))
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;every&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;A&lt;&#x2F;span&gt;&lt;span&gt;]&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;predicates&lt;&#x2F;span&gt;&lt;span&gt;: (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;A =&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;Boolean&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;*&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;) =&lt;&#x2F;span&gt;&lt;span&gt; none&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;predicates.view.map&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;complement&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d33682;&quot;&gt;_&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;))&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;_*&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The &lt;code&gt;any&lt;&#x2F;code&gt; function returns a new predicate that, when called with an input &lt;code&gt;a&lt;&#x2F;code&gt;, checks if at least one of its predicates holds true for the value &lt;code&gt;a&lt;&#x2F;code&gt;. Our &lt;code&gt;none&lt;&#x2F;code&gt; function simply returns the complement of the predicate returned by &lt;code&gt;any&lt;&#x2F;code&gt; – if at least one predicate holds true, the condition for &lt;code&gt;none&lt;&#x2F;code&gt; is not satisfied. Finally, our &lt;code&gt;every&lt;&#x2F;code&gt; function works by checking that none of the complements to the predicates passed to it holds true.&lt;&#x2F;p&gt;
&lt;p&gt;We can now use this to create a composite &lt;code&gt;EmailFilter&lt;&#x2F;code&gt; that represents the user&#x27;s configuration:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;scala&quot; style=&quot;background-color:#002b36;color:#839496;&quot; class=&quot;language-scala &quot;&gt;&lt;code class=&quot;language-scala&quot; data-lang=&quot;scala&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;val filter&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;EmailFilter &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt; every&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(
&lt;&#x2F;span&gt;&lt;span&gt;    notSentByAnyOf&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Set&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;johndoe@example.com&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;))&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;    minimumSize&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;100&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;    maximumSize&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;10000&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h3 id=&quot;composing-a-transformation-pipeline&quot;&gt;Composing a transformation pipeline&lt;&#x2F;h3&gt;
&lt;p&gt;As another example of function composition, consider our example scenario again. As a freemail provider, we want not only to allow user&#x27;s to configure their email filter, but also do some processing on emails sent by our users. These are simple functions &lt;code&gt;Email =&amp;gt; Email&lt;&#x2F;code&gt;. Some possible transformations are the following:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;scala&quot; style=&quot;background-color:#002b36;color:#839496;&quot; class=&quot;language-scala &quot;&gt;&lt;code class=&quot;language-scala&quot; data-lang=&quot;scala&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;val addMissingSubject &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;email&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Email&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;=&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;if &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;email.subject.isEmpty&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span&gt; email.copy&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;subject &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;No subject&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;else&lt;&#x2F;span&gt;&lt;span&gt; email
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;val checkSpelling &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;email&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Email&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;=&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;  email.copy&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;text &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt; email.text.replaceAll&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;your&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;you&amp;#39;re&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;))
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;val removeInappropriateLanguage &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;email&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Email&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;=&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;  email.copy&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;text &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt; email.text.replaceAll&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;dynamic typing&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;**CENSORED**&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;))
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;val addAdvertismentToFooter &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;email&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Email&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;=&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;  email.copy&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;text &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt; email.text + &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#dc322f;&quot;&gt;\n&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;This mail sent via Super Awesome Free Mail&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Now, depending on the weather and the mood of our boss, we can configure our pipeline as required, either by multiple &lt;code&gt;andThen&lt;&#x2F;code&gt; calls, or, having the same effect, by using the &lt;code&gt;chain&lt;&#x2F;code&gt; method defined on the &lt;code&gt;Function&lt;&#x2F;code&gt; companion object:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;scala&quot; style=&quot;background-color:#002b36;color:#839496;&quot; class=&quot;language-scala &quot;&gt;&lt;code class=&quot;language-scala&quot; data-lang=&quot;scala&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;val pipeline &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Function&lt;&#x2F;span&gt;&lt;span&gt;.chain&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Seq&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(
&lt;&#x2F;span&gt;&lt;span&gt;  addMissingSubject, 
&lt;&#x2F;span&gt;&lt;span&gt;  checkSpelling, 
&lt;&#x2F;span&gt;&lt;span&gt;  removeInappropriateLanguage, 
&lt;&#x2F;span&gt;&lt;span&gt;  addAdvertismentToFooter&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;))
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h2 id=&quot;higher-order-functions-and-partial-functions&quot;&gt;Higher-order functions and partial functions&lt;&#x2F;h2&gt;
&lt;p&gt;I won&#x27;t go into detail here, but now that you know more about how you can compose or reuse functions by means of higher-order functions, you might want to have a look at partial functions again.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;chaining-partial-functions&quot;&gt;Chaining partial functions&lt;&#x2F;h3&gt;
&lt;p&gt;In the article on &lt;a href=&quot;http:&#x2F;&#x2F;danielwestheide.com&#x2F;blog&#x2F;2012&#x2F;12&#x2F;12&#x2F;the-neophytes-guide-to-scala-part-4-pattern-matching-anonymous-functions.html&quot;&gt;pattern matching anonymous functions&lt;&#x2F;a&gt;, I mentioned that partial functions can be used to create a nice alternative to the chain of responsibility pattern: The &lt;code&gt;orElse&lt;&#x2F;code&gt; method defined on the &lt;code&gt;PartialFunction&lt;&#x2F;code&gt; trait allows you to chain an arbitrary number of partial functions, creating a composite partial function. The first one, however, will only pass on to the next one if it isn&#x27;t defined for the given input. Hence, you can do something like this:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;scala&quot; style=&quot;background-color:#002b36;color:#839496;&quot; class=&quot;language-scala &quot;&gt;&lt;code class=&quot;language-scala&quot; data-lang=&quot;scala&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;val handler &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt; fooHandler orElse barHandler orElse bazHandler
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h3 id=&quot;lifting-partial-functions&quot;&gt;Lifting partial functions&lt;&#x2F;h3&gt;
&lt;p&gt;Also, sometimes a &lt;code&gt;PartialFunction&lt;&#x2F;code&gt; is not what you need. If you think about it, another way to represent the fact that a function is not defined for all input values is to have a standard function whose return type is an &lt;code&gt;Option[A]&lt;&#x2F;code&gt; – if the function is not defined for an input value, it will return &lt;code&gt;None&lt;&#x2F;code&gt;, otherwise a &lt;code&gt;Some[A]&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;If that&#x27;s what you need in a certain context, given a &lt;code&gt;PartialFunction&lt;&#x2F;code&gt; named &lt;code&gt;pf&lt;&#x2F;code&gt;, you can call &lt;code&gt;pf.lift&lt;&#x2F;code&gt; to get the normal function returning an &lt;code&gt;Option&lt;&#x2F;code&gt;. If you have one of the latter and require a partial function, call  &lt;code&gt;Function.unlift(f)&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;summary&quot;&gt;Summary&lt;&#x2F;h2&gt;
&lt;p&gt;In this article, we have seen the value of higher-order functions, which allow you to reuse existing functions in new, unforeseen contexts and compose them in a very flexible way. While in the examples, you didn&#x27;t save much in terms of lines of code, because the functions I showed were rather tiny, the real point is to illustrate the increase in flexibility. Also, composing and reusing functions is something that has benefits not only for small functions, but also on an architectural level.&lt;&#x2F;p&gt;
&lt;p&gt;In the next article, we will continue to examine ways to combine functions by means of partial function application and currying.&lt;&#x2F;p&gt;
</description>
      </item>
      <item>
          <title>The Neophyte&#x27;s Guide to Scala Part 9: Promises and Futures in practice</title>
          <pubDate>Wed, 16 Jan 2013 00:00:00 +0000</pubDate>
          <author>Unknown</author>
          <link>https://danielwestheide.com/blog/the-neophytes-guide-to-scala-part-9-promises-and-futures-in-practice/</link>
          <guid>https://danielwestheide.com/blog/the-neophytes-guide-to-scala-part-9-promises-and-futures-in-practice/</guid>
          <description xml:base="https://danielwestheide.com/blog/the-neophytes-guide-to-scala-part-9-promises-and-futures-in-practice/">&lt;p&gt;In the &lt;a href=&quot;http:&#x2F;&#x2F;danielwestheide.com&#x2F;blog&#x2F;2013&#x2F;01&#x2F;09&#x2F;the-neophytes-guide-to-scala-part-8-welcome-to-the-future.html&quot;&gt;previous article in this series&lt;&#x2F;a&gt;, I introduced you to the &lt;code&gt;Future&lt;&#x2F;code&gt; type, its underlying paradigm, and how to put it to use to write highly readable and composable asynchronously executing code.&lt;&#x2F;p&gt;
&lt;p&gt;In that article, I also mentioned that &lt;code&gt;Future&lt;&#x2F;code&gt; is really only one piece of the puzzle: It&#x27;s a read-only type that allows you to work with the values it will compute and handle failure to do so in an elegant way. In order for you to be able to read a computed value from a &lt;code&gt;Future&lt;&#x2F;code&gt;, however, there must be a way for some other part of your software to put that value there. In this post, I will show you how this is done by means of the &lt;code&gt;Promise&lt;&#x2F;code&gt; type, followed by some guidance on how to use futures and promises in practice.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;promises&quot;&gt;Promises&lt;&#x2F;h2&gt;
&lt;p&gt;In the previous article on futures, we had a sequential block of code that we passed to the &lt;code&gt;apply&lt;&#x2F;code&gt; method of the &lt;code&gt;Future&lt;&#x2F;code&gt; companion object, and, given an &lt;code&gt;ExecutionContext&lt;&#x2F;code&gt; was in scope, it magically executed that code block asynchronously, returning its result as a &lt;code&gt;Future&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;While this is an easy way to get a &lt;code&gt;Future&lt;&#x2F;code&gt; when you want one, there is an alternative way to create &lt;code&gt;Future&lt;&#x2F;code&gt; instances and have them complete with a success or failure. Where &lt;code&gt;Future&lt;&#x2F;code&gt; provides an interface exclusively for &lt;em&gt;querying&lt;&#x2F;em&gt;, &lt;code&gt;Promise&lt;&#x2F;code&gt; is a companion type that allows you to complete a &lt;code&gt;Future&lt;&#x2F;code&gt; by putting a value into it. This can be done exactly once. Once a &lt;code&gt;Promise&lt;&#x2F;code&gt; has been completed, it&#x27;s not possible to change it any more.&lt;&#x2F;p&gt;
&lt;p&gt;A &lt;code&gt;Promise&lt;&#x2F;code&gt; instance is always linked to exactly one instance of &lt;code&gt;Future&lt;&#x2F;code&gt;. If you call the &lt;code&gt;apply&lt;&#x2F;code&gt; method of &lt;code&gt;Future&lt;&#x2F;code&gt; again in the REPL, you will indeed notice that the &lt;code&gt;Future&lt;&#x2F;code&gt; returned is a &lt;code&gt;Promise&lt;&#x2F;code&gt;, too:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;scala&quot; style=&quot;background-color:#002b36;color:#839496;&quot; class=&quot;language-scala &quot;&gt;&lt;code class=&quot;language-scala&quot; data-lang=&quot;scala&quot;&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;import&lt;&#x2F;span&gt;&lt;span&gt; concurrent.Future
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;import&lt;&#x2F;span&gt;&lt;span&gt; concurrent.ExecutionContext.Implicits.global
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;val f&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Future&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;String&lt;&#x2F;span&gt;&lt;span&gt;] &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Future &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{ &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Hello world!&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;}
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;&#x2F;&#x2F; REPL output: 
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;&#x2F;&#x2F; f: scala.concurrent.Future[String] = scala.concurrent.impl.Promise$DefaultPromise@793e6657
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The object you get back is a &lt;code&gt;DefaultPromise&lt;&#x2F;code&gt;, which implements both &lt;code&gt;Future&lt;&#x2F;code&gt; and &lt;code&gt;Promise&lt;&#x2F;code&gt;. This is an implementation detail, however. The &lt;code&gt;Future&lt;&#x2F;code&gt; and the &lt;code&gt;Promise&lt;&#x2F;code&gt; to which it belongs may very well be separate objects.&lt;&#x2F;p&gt;
&lt;p&gt;What this little example shows is that there is obviously no way to complete a &lt;code&gt;Future&lt;&#x2F;code&gt; other than through a &lt;code&gt;Promise&lt;&#x2F;code&gt; – the &lt;code&gt;apply&lt;&#x2F;code&gt; method on &lt;code&gt;Future&lt;&#x2F;code&gt; is just a nice helper function that shields you from this.&lt;&#x2F;p&gt;
&lt;p&gt;Now, let&#x27;s see how we can get our hands dirty and work with the &lt;code&gt;Promise&lt;&#x2F;code&gt; type directly.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;promising-a-rosy-future&quot;&gt;Promising a rosy future&lt;&#x2F;h3&gt;
&lt;p&gt;When talking about promises that may be fulfilled or not, an obvious example domain is that of politicians, elections, campaign pledges, and legislative periods.&lt;&#x2F;p&gt;
&lt;p&gt;Suppose the politicians that then got elected into office promised their voters a tax cut. This can be represented as a &lt;code&gt;Promise[TaxCut]&lt;&#x2F;code&gt;, which you can create by calling the &lt;code&gt;apply&lt;&#x2F;code&gt; method on the &lt;code&gt;Promise&lt;&#x2F;code&gt; companion object, like so:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;scala&quot; style=&quot;background-color:#002b36;color:#839496;&quot; class=&quot;language-scala &quot;&gt;&lt;code class=&quot;language-scala&quot; data-lang=&quot;scala&quot;&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;import&lt;&#x2F;span&gt;&lt;span&gt; concurrent.Promise
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;case class&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt; TaxCut&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;reduction&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;Int&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;&#x2F;&#x2F; either give the type as a type parameter to the factory method:
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;val taxcut &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Promise&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;TaxCut&lt;&#x2F;span&gt;&lt;span&gt;]&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;()
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;&#x2F;&#x2F; or give the compiler a hint by specifying the type of your val:
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;val taxcut2&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Promise&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;TaxCut&lt;&#x2F;span&gt;&lt;span&gt;] &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Promise&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;()
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Once you have created a &lt;code&gt;Promise&lt;&#x2F;code&gt;, you can get the &lt;code&gt;Future&lt;&#x2F;code&gt; belonging to it by calling the &lt;code&gt;future&lt;&#x2F;code&gt; method on the &lt;code&gt;Promise&lt;&#x2F;code&gt; instance:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;scala&quot; style=&quot;background-color:#002b36;color:#839496;&quot; class=&quot;language-scala &quot;&gt;&lt;code class=&quot;language-scala&quot; data-lang=&quot;scala&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;val taxcutF&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Future&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;TaxCut&lt;&#x2F;span&gt;&lt;span&gt;] &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt; taxcut.future
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The returned &lt;code&gt;Future&lt;&#x2F;code&gt; might not be the same object as the &lt;code&gt;Promise&lt;&#x2F;code&gt;, but calling the &lt;code&gt;future&lt;&#x2F;code&gt; method of a &lt;code&gt;Promise&lt;&#x2F;code&gt; multiple times will definitely always return the same object to make sure the one-to-one relationship between a &lt;code&gt;Promise&lt;&#x2F;code&gt; and its &lt;code&gt;Future&lt;&#x2F;code&gt; is preserved.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;completing-a-promise&quot;&gt;Completing a Promise&lt;&#x2F;h3&gt;
&lt;p&gt;Once you have made a &lt;code&gt;Promise&lt;&#x2F;code&gt; and told the world that you will deliver on it in the forseeable &lt;code&gt;Future&lt;&#x2F;code&gt;, you better do your very best to make it happen.&lt;&#x2F;p&gt;
&lt;p&gt;In Scala, you can complete a &lt;code&gt;Promise&lt;&#x2F;code&gt; either with a success or a failure.&lt;&#x2F;p&gt;
&lt;h4 id=&quot;delivering-on-your-promise&quot;&gt;Delivering on your Promise&lt;&#x2F;h4&gt;
&lt;p&gt;To complete a &lt;code&gt;Promise&lt;&#x2F;code&gt; with a success, you call its &lt;code&gt;success&lt;&#x2F;code&gt; method, passing it the value that the &lt;code&gt;Future&lt;&#x2F;code&gt; associated with it is supposed to have:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;scala&quot; style=&quot;background-color:#002b36;color:#839496;&quot; class=&quot;language-scala &quot;&gt;&lt;code class=&quot;language-scala&quot; data-lang=&quot;scala&quot;&gt;&lt;span&gt;taxcut.success&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;TaxCut&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;20&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;))
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Once you have done this, that &lt;code&gt;Promise&lt;&#x2F;code&gt; instance is no longer writable, and future attempts to do so will lead to an exception.&lt;&#x2F;p&gt;
&lt;p&gt;Also, completing your &lt;code&gt;Promise&lt;&#x2F;code&gt; like this leads to the successful completion of the associated &lt;code&gt;Future&lt;&#x2F;code&gt;. Any success or completion handlers on that future will now be called, or if, for instance, you are mapping that future, the mapping function will now be executed.&lt;&#x2F;p&gt;
&lt;p&gt;Usually, the completion of the &lt;code&gt;Promise&lt;&#x2F;code&gt; and the processing of the completed &lt;code&gt;Future&lt;&#x2F;code&gt; will not happen in the same thread. It&#x27;s more likely that you create your &lt;code&gt;Promise&lt;&#x2F;code&gt;, start computing its value in another thread and immediately return the uncompleted &lt;code&gt;Future&lt;&#x2F;code&gt; to the caller.&lt;&#x2F;p&gt;
&lt;p&gt;To illustrate, let&#x27;s do something like that for our taxcut promise:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;scala&quot; style=&quot;background-color:#002b36;color:#839496;&quot; class=&quot;language-scala &quot;&gt;&lt;code class=&quot;language-scala&quot; data-lang=&quot;scala&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;object&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt; Government &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;redeemCampaignPledge&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;()&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Future&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;TaxCut&lt;&#x2F;span&gt;&lt;span&gt;] &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= {
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;val p &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Promise&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;TaxCut&lt;&#x2F;span&gt;&lt;span&gt;]&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;()
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Future &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;      println&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Starting the new legislative period.&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;      &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Thread&lt;&#x2F;span&gt;&lt;span&gt;.sleep&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;2000&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;      p.success&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;TaxCut&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;20&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;))
&lt;&#x2F;span&gt;&lt;span&gt;      println&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;We reduced the taxes! You must reelect us!!!!1111&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;}
&lt;&#x2F;span&gt;&lt;span&gt;    p.future
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;}
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Please don&#x27;t get confused by the usage of the &lt;code&gt;apply&lt;&#x2F;code&gt; method of the &lt;code&gt;Future&lt;&#x2F;code&gt; companion object in this example. I&#x27;m just using it because it is so convenient for executing a block of code asynchronously. I could just as well have implemented the computation of the result (which involves a lot of sleeping) in a &lt;code&gt;Runnable&lt;&#x2F;code&gt; that is executed asynchrously by an &lt;code&gt;ExecutorService&lt;&#x2F;code&gt;, with a lot more boilerplate code. The point is that the &lt;code&gt;Promise&lt;&#x2F;code&gt; is not completed in the caller thread.&lt;&#x2F;p&gt;
&lt;p&gt;Let&#x27;s redeem our campaign pledge then and add an &lt;code&gt;onComplete&lt;&#x2F;code&gt; callback function to our future:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;scala&quot; style=&quot;background-color:#002b36;color:#839496;&quot; class=&quot;language-scala &quot;&gt;&lt;code class=&quot;language-scala&quot; data-lang=&quot;scala&quot;&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;import&lt;&#x2F;span&gt;&lt;span&gt; scala.util.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{&lt;&#x2F;span&gt;&lt;span&gt;Success, Failure&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;}
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;val taxCutF&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Future&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;TaxCut&lt;&#x2F;span&gt;&lt;span&gt;] &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Government&lt;&#x2F;span&gt;&lt;span&gt;.redeemCampaignPledge&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;()
&lt;&#x2F;span&gt;&lt;span&gt;  println&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Now that they&amp;#39;re elected, let&amp;#39;s see if they remember their promises...&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;  taxCutF.onComplete &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;case Success&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;TaxCut&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;reduction&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;=&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;      println&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;s&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;A miracle! They really cut our taxes by &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;$reduction&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt; percentage points!&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;case Failure&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;ex&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;=&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;      println&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;s&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;They broke their promises! Again! Because of a ${&lt;&#x2F;span&gt;&lt;span&gt;ex.getMessage&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;}&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;If you try this out multiple times, you will see that the order of the console output is not deterministic. Eventually, the completion handler will be executed and run into the success case.&lt;&#x2F;p&gt;
&lt;h4 id=&quot;breaking-promises-like-a-sir&quot;&gt;Breaking Promises like a sir&lt;&#x2F;h4&gt;
&lt;p&gt;As a politician, you are pretty much used to not keeping your promises. As a Scala developer, you sometimes have no other choice, either. If that happens, you can still complete your &lt;code&gt;Promise&lt;&#x2F;code&gt; instance gracefully, by calling its &lt;code&gt;failure&lt;&#x2F;code&gt; method and passing it an exception:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;scala&quot; style=&quot;background-color:#002b36;color:#839496;&quot; class=&quot;language-scala &quot;&gt;&lt;code class=&quot;language-scala&quot; data-lang=&quot;scala&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;case class&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt; LameExcuse&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;msg&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;String&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;extends &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;Exception&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;msg&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;object&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt; Government &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;redeemCampaignPledge&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;()&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Future&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;TaxCut&lt;&#x2F;span&gt;&lt;span&gt;] &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= {
&lt;&#x2F;span&gt;&lt;span&gt;       &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;val p &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Promise&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;TaxCut&lt;&#x2F;span&gt;&lt;span&gt;]&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;()
&lt;&#x2F;span&gt;&lt;span&gt;       &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Future &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;         println&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Starting the new legislative period.&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;         &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Thread&lt;&#x2F;span&gt;&lt;span&gt;.sleep&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;2000&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;         p.failure&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;LameExcuse&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;global economy crisis&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;))
&lt;&#x2F;span&gt;&lt;span&gt;         println&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;We didn&amp;#39;t fulfill our promises, but surely they&amp;#39;ll understand.&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;       &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;}
&lt;&#x2F;span&gt;&lt;span&gt;       p.future
&lt;&#x2F;span&gt;&lt;span&gt;     &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;}
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This implementation of the &lt;code&gt;redeemCampaignPledge()&lt;&#x2F;code&gt; method will to lots of broken promises. Once you have completed a &lt;code&gt;Promise&lt;&#x2F;code&gt; with the &lt;code&gt;failure&lt;&#x2F;code&gt; method, it is no longer writable, just as is the case with the &lt;code&gt;success&lt;&#x2F;code&gt; method. The associated &lt;code&gt;Future&lt;&#x2F;code&gt; will now be completed with a &lt;code&gt;Failure&lt;&#x2F;code&gt;, too, so the callback function above would run into the failure case.&lt;&#x2F;p&gt;
&lt;p&gt;If you already have a &lt;code&gt;Try&lt;&#x2F;code&gt;, you can also complete a &lt;code&gt;Promise&lt;&#x2F;code&gt; by calling its &lt;code&gt;complete&lt;&#x2F;code&gt; method. If the &lt;code&gt;Try&lt;&#x2F;code&gt; is a &lt;code&gt;Success&lt;&#x2F;code&gt;, the associated &lt;code&gt;Future&lt;&#x2F;code&gt; will be completed successfully, with the value inside the &lt;code&gt;Success&lt;&#x2F;code&gt;. If it&#x27;s a &lt;code&gt;Failure&lt;&#x2F;code&gt;, the &lt;code&gt;Future&lt;&#x2F;code&gt; will completed with that failure.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;future-based-programming-in-practice&quot;&gt;Future-based programming in practice&lt;&#x2F;h2&gt;
&lt;p&gt;If you want to make use of the future-based paradigm in order to increase the scalability of your application, you have to design your application to be non-blocking from the ground-up, which basically means that the functions in all your application layers are asynchronous and return futures.&lt;&#x2F;p&gt;
&lt;p&gt;A likely use case these days is that of developing a web application. If you are using a modern Scala web framework, it will allow you to return your responses as something like a &lt;code&gt;Future[Response]&lt;&#x2F;code&gt; instead of blocking and then returning your finished &lt;code&gt;Response&lt;&#x2F;code&gt;. This is important since it allows your web server to handle a huge number of open connections with a relatively low number of threads. By always giving your web server &lt;code&gt;Future[Response]&lt;&#x2F;code&gt;, you maximize the utilization of the web server&#x27;s dedicated thread pool.&lt;&#x2F;p&gt;
&lt;p&gt;In the end, a service in your application might make multiple calls to your database layer and&#x2F;or some external web service, receiving multiple futures, and then compose them to return a new &lt;code&gt;Future&lt;&#x2F;code&gt;, all in a very readable for comprehension, such as the one you saw in the previous article. The web layer will turn such a &lt;code&gt;Future&lt;&#x2F;code&gt; into a &lt;code&gt;Future[Response]&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;However, how do you implement this in practice? There are are three different cases you have to consider:&lt;&#x2F;p&gt;
&lt;h3 id=&quot;non-blocking-io&quot;&gt;Non-blocking IO&lt;&#x2F;h3&gt;
&lt;p&gt;Your application will most certainly involve a lot of IO. For example, your web application will have to talk to a database, and it might act as a client that is calling other web services.&lt;&#x2F;p&gt;
&lt;p&gt;If at all possible, make use of libraries that are based on Java&#x27;s non-blocking IO capabilities, either by using Java&#x27;s NIO API directly or through a library like Netty. Such libraries, too, can serve many connections with a reasonably-sized dedicated thread pool.&lt;&#x2F;p&gt;
&lt;p&gt;Developing such a library yourself is one of the few places where directly working with the &lt;code&gt;Promise&lt;&#x2F;code&gt; type makes a lot of sense.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;blocking-io&quot;&gt;Blocking IO&lt;&#x2F;h3&gt;
&lt;p&gt;Sometimes, there is no NIO-based library available. For instance, most database drivers you&#x27;ll find in the Java world nowadays are using blocking IO. If you made a query to your database with such a driver in order to respond to a HTTP request, that call would be made on a web server thread. To avoid that, place all the code talking to the database inside a &lt;code&gt;future&lt;&#x2F;code&gt; block, like so:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;scala&quot; style=&quot;background-color:#002b36;color:#839496;&quot; class=&quot;language-scala &quot;&gt;&lt;code class=&quot;language-scala&quot; data-lang=&quot;scala&quot;&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;&#x2F;&#x2F; get back a Future[ResultSet] or something similar:
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Future &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;  queryDB&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;query&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;So far, we always used the implicitly available global &lt;code&gt;ExecutionContext&lt;&#x2F;code&gt; to execute such future blocks. It&#x27;s probably a good idea to create a dedicated &lt;code&gt;ExecutionContext&lt;&#x2F;code&gt; that you will have in scope in your database layer.&lt;&#x2F;p&gt;
&lt;p&gt;You can create an &lt;code&gt;ExecutionContext&lt;&#x2F;code&gt; from a Java &lt;code&gt;ExecutorService&lt;&#x2F;code&gt;, which means you will be able to tune the thread pool for executing your database calls asynchronously independently from the rest of your application:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;scala&quot; style=&quot;background-color:#002b36;color:#839496;&quot; class=&quot;language-scala &quot;&gt;&lt;code class=&quot;language-scala&quot; data-lang=&quot;scala&quot;&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;import&lt;&#x2F;span&gt;&lt;span&gt; java.util.concurrent.Executors
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;import&lt;&#x2F;span&gt;&lt;span&gt; concurrent.ExecutionContext
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;val executorService &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Executors&lt;&#x2F;span&gt;&lt;span&gt;.newFixedThreadPool&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;4&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;val executionContext &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;ExecutionContext&lt;&#x2F;span&gt;&lt;span&gt;.fromExecutorService&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;executorService&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h3 id=&quot;long-running-computations&quot;&gt;Long-running computations&lt;&#x2F;h3&gt;
&lt;p&gt;Depending on the nature of your application, it will occasionally have to call long-running tasks that don&#x27;t involve any IO at all, which means they are CPU-bound. These, too, should not be executed by a web server thread. Hence, you should turn them into &lt;code&gt;Futures&lt;&#x2F;code&gt;, too:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;scala&quot; style=&quot;background-color:#002b36;color:#839496;&quot; class=&quot;language-scala &quot;&gt;&lt;code class=&quot;language-scala&quot; data-lang=&quot;scala&quot;&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Future &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;  longRunningComputation&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;data, moreData&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Again, if you have long-running computations, having them run in a separate &lt;code&gt;ExecutionContext&lt;&#x2F;code&gt; for CPU-bound tasks is a good idea. How to tune your various thread pools is highly dependent on your individual application and beyond the scope of this article.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;summary&quot;&gt;Summary&lt;&#x2F;h2&gt;
&lt;p&gt;In this article, we looked at promises, the writable part of the future-based concurrency paradigm, and how to use them to complete a &lt;code&gt;Future&lt;&#x2F;code&gt;, followed by some advice on how to put futures to use in practice.&lt;&#x2F;p&gt;
&lt;p&gt;In the next part of this series, we are taking a step back from concurrency issues and examine how functional programming in Scala can help you to make your code more reusable, a claim that has long been associated with object-oriented programming.&lt;&#x2F;p&gt;
</description>
      </item>
      <item>
          <title>The Neophyte&#x27;s Guide to Scala Part 8: Welcome to the Future</title>
          <pubDate>Wed, 09 Jan 2013 00:00:00 +0000</pubDate>
          <author>Unknown</author>
          <link>https://danielwestheide.com/blog/the-neophytes-guide-to-scala-part-8-welcome-to-the-future/</link>
          <guid>https://danielwestheide.com/blog/the-neophytes-guide-to-scala-part-8-welcome-to-the-future/</guid>
          <description xml:base="https://danielwestheide.com/blog/the-neophytes-guide-to-scala-part-8-welcome-to-the-future/">&lt;p&gt;As an aspiring and enthusiastic Scala developer, you will likely have heard of Scala&#x27;s approach at dealing with concurrency – or maybe that was even what attracted you in the first place. Said approach makes reasoning about concurrency and writing well-behaved concurrent programs a lot easier than the rather low-level concurrency APIs you are confronted with in most other languages.&lt;&#x2F;p&gt;
&lt;p&gt;One of the two cornerstones of this approach is the &lt;em&gt;Future&lt;&#x2F;em&gt;, the other being the &lt;em&gt;Actor&lt;&#x2F;em&gt;. The former shall be the subject of this article. I will explain what futures are good for and how you can make use of them in a functional way.&lt;&#x2F;p&gt;
&lt;p&gt;Please make sure that you have version 2.9.3 or later if you want to get your hands dirty and try out the examples yourself. The futures we are discussing here were only incorporated into the Scala core distribution with the 2.10.0 release and later backported to Scala 2.9.3. Originally, with a slightly different API, they were part of the Akka concurrency toolkit.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;why-sequential-code-can-be-bad&quot;&gt;Why sequential code can be bad&lt;&#x2F;h2&gt;
&lt;p&gt;Suppose you want to prepare a cappuccino. You could simply execute the following steps, one after another:&lt;&#x2F;p&gt;
&lt;ol&gt;
&lt;li&gt;Grind the required coffee beans&lt;&#x2F;li&gt;
&lt;li&gt;Heat some water&lt;&#x2F;li&gt;
&lt;li&gt;Brew an espresso using the ground coffee and the heated water&lt;&#x2F;li&gt;
&lt;li&gt;Froth some milk&lt;&#x2F;li&gt;
&lt;li&gt;Combine the espresso and the frothed milk to a cappuccino&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;p&gt;Translated to Scala code, you would do something like this:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;scala&quot; style=&quot;background-color:#002b36;color:#839496;&quot; class=&quot;language-scala &quot;&gt;&lt;code class=&quot;language-scala&quot; data-lang=&quot;scala&quot;&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;import&lt;&#x2F;span&gt;&lt;span&gt; scala.util.Try
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;&#x2F;&#x2F; Some type aliases, just for getting more meaningful method signatures:
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;type &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;CoffeeBeans &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;String
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;type &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;GroundCoffee &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;String
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;case class&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt; Water&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;temperature&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;Int&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;type &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;Milk &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;String
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;type &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;FrothedMilk &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;String
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;type &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;Espresso &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;String
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;type &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;Cappuccino &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;String
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;&#x2F;&#x2F; dummy implementations of the individual steps:
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;grind&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;beans&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;CoffeeBeans&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;GroundCoffee &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;s&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;ground coffee of &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;$beans&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;heatWater&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;water&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Water&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Water &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt; water.copy&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;temperature &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;85&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;frothMilk&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;milk&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Milk&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;FrothedMilk &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;s&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;frothed &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;$milk&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;brew&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;coffee&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;GroundCoffee&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;heatedWater&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Water&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Espresso &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;espresso&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;combine&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;espresso&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Espresso&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;frothedMilk&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;FrothedMilk&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Cappuccino &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;cappuccino&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;&#x2F;&#x2F; some exceptions for things that might go wrong in the individual steps
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;&#x2F;&#x2F; (we&amp;#39;ll need some of them later, use the others when experimenting
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;&#x2F;&#x2F; with the code):
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;case class&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt; GrindingException&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;msg&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;String&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;extends &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;Exception&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;msg&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;case class&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt; FrothingException&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;msg&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;String&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;extends &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;Exception&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;msg&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;case class&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt; WaterBoilingException&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;msg&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;String&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;extends &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;Exception&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;msg&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;case class&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt; BrewingException&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;msg&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;String&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;extends &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;Exception&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;msg&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;&#x2F;&#x2F; going through these steps sequentially:
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;prepareCappuccino&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;()&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Try&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Cappuccino&lt;&#x2F;span&gt;&lt;span&gt;] &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;for &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;ground &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;&amp;lt;- &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Try&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;grind&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;arabica beans&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;))
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;water &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;&amp;lt;- &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Try&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;heatWater&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Water&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;25&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)))
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;espresso &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;&amp;lt;- &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Try&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;brew&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;ground, water&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;))
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;foam &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;&amp;lt;- &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Try&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;frothMilk&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;milk&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;))
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;} &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;yield&lt;&#x2F;span&gt;&lt;span&gt; combine&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;espresso, foam&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Doing it like this has several advantages: You get a very readable step-by-step instruction of what to do. Moreover, you will likely not get confused while preparing the cappuccino this way, since you are avoiding context switches.&lt;&#x2F;p&gt;
&lt;p&gt;On the downside, preparing your cappuccino in such a step-by-step manner means that your brain and body are on wait during large parts of the whole process. While waiting for the ground coffee, you are effectively blocked. Only when that&#x27;s finished, you&#x27;re able to start heating some water, and so on.&lt;&#x2F;p&gt;
&lt;p&gt;This is clearly a waste of valuable resources. It&#x27;s very likely that you would want to initiate multiple steps and have them execute concurrently. Once you see that the water and the ground coffee is ready, you&#x27;d start brewing the espresso, in the meantime already starting the process of frothing the milk.&lt;&#x2F;p&gt;
&lt;p&gt;It&#x27;s really no different when writing a piece of software. A web server only has so many threads for processing requests and creating appropriate responses. You don&#x27;t want to block these valuable threads by waiting for the results of a database query or a call to another HTTP service. Instead, you want an asynchronous programming model and non-blocking IO, so that, while the processing of one request is waiting for the response from a database, the web server thread handling that request can serve the needs of some other request instead of idling along.&lt;&#x2F;p&gt;
&lt;blockquote&gt;
&quot;I heard you like callbacks, so I put a callback in your callback!&quot;
&lt;&#x2F;blockquote&gt;
&lt;p&gt;Of course, you already knew all that - what with Node.js being all the rage among the cool kids for a while now. The approach used by Node.js and some others is to communicate via callbacks, exclusively. Unfortunately, this can very easily lead to a convoluted mess of callbacks within callbacks within callbacks, making your code hard to read and debug.&lt;&#x2F;p&gt;
&lt;p&gt;Scala&#x27;s &lt;code&gt;Future&lt;&#x2F;code&gt; allows callbacks, too, as you will see very shortly, but it provides much better alternatives, so it&#x27;s likely you won&#x27;t need them a lot.&lt;&#x2F;p&gt;
&lt;blockquote&gt;
&quot;I know Futures, and they are completely useless!&quot;
&lt;&#x2F;blockquote&gt;
&lt;p&gt;You might also be familiar with other &lt;code&gt;Future&lt;&#x2F;code&gt; implementations, most notably the one provided by Java. There is not really much you can do with a Java future other than checking if it&#x27;s completed or simply blocking until it is completed. In short, they are nearly useless and definitely not a joy to work with.&lt;&#x2F;p&gt;
&lt;p&gt;If you think that Scala&#x27;s futures are anything like that, get ready for a surprise. Here we go!&lt;&#x2F;p&gt;
&lt;h2 id=&quot;semantics-of-future&quot;&gt;Semantics of Future&lt;&#x2F;h2&gt;
&lt;p&gt;Scala&#x27;s &lt;code&gt;Future[T]&lt;&#x2F;code&gt;, residing in the &lt;code&gt;scala.concurrent&lt;&#x2F;code&gt; package, is a container type, representing a computation that is supposed to &lt;em&gt;eventually&lt;&#x2F;em&gt; result in a value of type &lt;code&gt;T&lt;&#x2F;code&gt;. Alas, the computation might go wrong or time out, so when the future is completed, it may not have been successful after all, in which case it contains an exception instead.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;code&gt;Future&lt;&#x2F;code&gt; is a write-once container – after a future has been completed, it is effectively immutable. Also, the &lt;code&gt;Future&lt;&#x2F;code&gt; type only provides an interface for &lt;em&gt;reading&lt;&#x2F;em&gt; the value to be computed. The task of &lt;em&gt;writing&lt;&#x2F;em&gt; the computed value is achieved via a &lt;code&gt;Promise&lt;&#x2F;code&gt;. Hence, there is a clear separation of concerns in the API design. In this post, we are focussing on the former, postponing the use of the &lt;code&gt;Promise&lt;&#x2F;code&gt; type to the next article in this series.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;working-with-futures&quot;&gt;Working with Futures&lt;&#x2F;h2&gt;
&lt;p&gt;There are several ways you can work with Scala futures, which we are going to examine by rewriting our cappuccino example to make use of the &lt;code&gt;Future&lt;&#x2F;code&gt; type. First, we need to rewrite all of the functions that can be executed concurrently so that they immediately return a &lt;code&gt;Future&lt;&#x2F;code&gt; instead of computing their result in a blocking way:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;scala&quot; style=&quot;background-color:#002b36;color:#839496;&quot; class=&quot;language-scala &quot;&gt;&lt;code class=&quot;language-scala&quot; data-lang=&quot;scala&quot;&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;import&lt;&#x2F;span&gt;&lt;span&gt; scala.concurrent.future
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;import&lt;&#x2F;span&gt;&lt;span&gt; scala.concurrent.Future
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;import&lt;&#x2F;span&gt;&lt;span&gt; scala.concurrent.ExecutionContext.Implicits.global
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;import&lt;&#x2F;span&gt;&lt;span&gt; scala.concurrent.duration.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d33682;&quot;&gt;_
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;import&lt;&#x2F;span&gt;&lt;span&gt; scala.util.Random
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;grind&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;beans&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;CoffeeBeans&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Future&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;GroundCoffee&lt;&#x2F;span&gt;&lt;span&gt;] &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Future &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;  println&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;start grinding...&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Thread&lt;&#x2F;span&gt;&lt;span&gt;.sleep&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Random&lt;&#x2F;span&gt;&lt;span&gt;.nextInt&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;2000&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;))
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;if &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;beans &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;== &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;baked beans&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;throw GrindingException&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;are you joking?&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;  println&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;finished grinding...&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;s&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;ground coffee of &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;$beans&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;}
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;heatWater&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;water&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Water&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Future&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Water&lt;&#x2F;span&gt;&lt;span&gt;] &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Future &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;  println&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;heating the water now&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Thread&lt;&#x2F;span&gt;&lt;span&gt;.sleep&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Random&lt;&#x2F;span&gt;&lt;span&gt;.nextInt&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;2000&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;))
&lt;&#x2F;span&gt;&lt;span&gt;  println&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;hot, it&amp;#39;s hot!&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;  water.copy&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;temperature &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;85&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;}
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;frothMilk&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;milk&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Milk&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Future&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;FrothedMilk&lt;&#x2F;span&gt;&lt;span&gt;] &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Future &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;  println&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;milk frothing system engaged!&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Thread&lt;&#x2F;span&gt;&lt;span&gt;.sleep&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Random&lt;&#x2F;span&gt;&lt;span&gt;.nextInt&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;2000&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;))
&lt;&#x2F;span&gt;&lt;span&gt;	println&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;shutting down milk frothing system&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;s&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;frothed &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;$milk&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;}
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;brew&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;coffee&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;GroundCoffee&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;heatedWater&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Water&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Future&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Espresso&lt;&#x2F;span&gt;&lt;span&gt;] &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Future &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;  println&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;happy brewing :)&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Thread&lt;&#x2F;span&gt;&lt;span&gt;.sleep&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Random&lt;&#x2F;span&gt;&lt;span&gt;.nextInt&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;2000&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;))
&lt;&#x2F;span&gt;&lt;span&gt;	println&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;it&amp;#39;s brewed!&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;  &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;espresso&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;There are several things that require an explanation here.&lt;&#x2F;p&gt;
&lt;p&gt;First off, there is the &lt;code&gt;apply&lt;&#x2F;code&gt; method on the &lt;code&gt;Future&lt;&#x2F;code&gt; companion object, that requires two arguments:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;scala&quot; style=&quot;background-color:#002b36;color:#839496;&quot; class=&quot;language-scala &quot;&gt;&lt;code class=&quot;language-scala&quot; data-lang=&quot;scala&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;object&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt; Future &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;apply&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;T&lt;&#x2F;span&gt;&lt;span&gt;]&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;body&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;=&amp;gt; T&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#93a1a1;&quot;&gt;implicit &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;execctx&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;ExecutionContext&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Future&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;T&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The computation to be computed asynchronously is passed in as the &lt;code&gt;body&lt;&#x2F;code&gt; by-name parameter. The second argument, in its own argument list, is an &lt;em&gt;implicit&lt;&#x2F;em&gt; one, which means we don&#x27;t have to specify one if a matching implicit value is defined somewhere in scope. We make sure this is the case by importing the global execution context.&lt;&#x2F;p&gt;
&lt;p&gt;An &lt;code&gt;ExecutionContext&lt;&#x2F;code&gt; is something that can execute our future, and you can think of it as something like a thread pool. Since the &lt;code&gt;ExecutionContext&lt;&#x2F;code&gt; is available implicitly, we only have a single one-element argument list remaining. Single-argument lists can be enclosed with curly braces instead of parentheses. People often make use of this when calling the &lt;code&gt;future&lt;&#x2F;code&gt; method, making it look a little bit like we are using a feature of the language and not calling an ordinary method. The &lt;code&gt;ExecutionContext&lt;&#x2F;code&gt; is an implicit parameter for virtually all of the &lt;code&gt;Future&lt;&#x2F;code&gt; API.&lt;&#x2F;p&gt;
&lt;p&gt;Furthermore, of course, in this simple example, we don&#x27;t actually compute anything, which is why we are putting in some random sleep, simply for demonstration purposes. We also print to the console before and after our &quot;computation&quot; to make the non-deterministic and concurrent nature of our code clearer when trying it out.&lt;&#x2F;p&gt;
&lt;p&gt;The computation of the value to be returned by a &lt;code&gt;Future&lt;&#x2F;code&gt; will start at some non-deterministic time after that &lt;code&gt;Future&lt;&#x2F;code&gt; instance has been created, by some thread assigned to it by the &lt;code&gt;ExecutionContext&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;callbacks&quot;&gt;Callbacks&lt;&#x2F;h3&gt;
&lt;p&gt;Sometimes, when things are simple, using a callback can be perfectly fine. Callbacks for futures are partial functions. You can pass a callback to the &lt;code&gt;onSuccess&lt;&#x2F;code&gt; method. It will only be called if the &lt;code&gt;Future&lt;&#x2F;code&gt; completes successfully, and if so, it receives the computed value as its input:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;scala&quot; style=&quot;background-color:#002b36;color:#839496;&quot; class=&quot;language-scala &quot;&gt;&lt;code class=&quot;language-scala&quot; data-lang=&quot;scala&quot;&gt;&lt;span&gt;grind&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;arabica beans&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span&gt;.onSuccess &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{ &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;case &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;ground =&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;  println&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;okay, got my ground coffee&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Similarly, you could register a failure callback with the &lt;code&gt;onFailure&lt;&#x2F;code&gt; method. Your callback will receive a &lt;code&gt;Throwable&lt;&#x2F;code&gt;, but it will only be called if the &lt;code&gt;Future&lt;&#x2F;code&gt; did not complete successfully.&lt;&#x2F;p&gt;
&lt;p&gt;Usually, it&#x27;s better to combine these two and register a completion callback that will handle both cases. The input parameter for that callback is a &lt;code&gt;Try&lt;&#x2F;code&gt;:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;scala&quot; style=&quot;background-color:#002b36;color:#839496;&quot; class=&quot;language-scala &quot;&gt;&lt;code class=&quot;language-scala&quot; data-lang=&quot;scala&quot;&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;import&lt;&#x2F;span&gt;&lt;span&gt; scala.util.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{&lt;&#x2F;span&gt;&lt;span&gt;Success, Failure&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;}
&lt;&#x2F;span&gt;&lt;span&gt;grind&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;baked beans&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span&gt;.onComplete &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;	&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;case Success&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;ground&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;=&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; println&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;s&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;got my &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;$ground&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;	&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;case Failure&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;ex&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;=&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; println&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;This grinder needs a replacement, seriously!&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Since we are passing in baked beans, an exception occurs in the &lt;code&gt;grind&lt;&#x2F;code&gt; method, leading to the &lt;code&gt;Future&lt;&#x2F;code&gt; completing with a &lt;code&gt;Failure&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;composing-futures&quot;&gt;Composing futures&lt;&#x2F;h3&gt;
&lt;p&gt;Using callbacks can be quite painful if you have to start nesting callbacks. Thankfully, you don&#x27;t have to do that! The real power of the Scala futures is that they are composable.&lt;&#x2F;p&gt;
&lt;p&gt;If you have followed this series, you will have noticed that all the container types we discussed made it possible for you to map them, flat map them, or use them in for comprehensions and that I mentioned that &lt;code&gt;Future&lt;&#x2F;code&gt; is a container type, too. Hence, the fact that Scala&#x27;s &lt;code&gt;Future&lt;&#x2F;code&gt; type allows you to do all that will not come as a surprise at all.&lt;&#x2F;p&gt;
&lt;p&gt;The real question is: What does it really mean to perform these operations on something that hasn&#x27;t even finished computing yet?&lt;&#x2F;p&gt;
&lt;h4 id=&quot;mapping-the-future&quot;&gt;Mapping the future&lt;&#x2F;h4&gt;
&lt;p&gt;Haven&#x27;t you always wanted to be a traveller in time who sets out to map the future? As a Scala developer you can do exactly that! Suppose that once your water has heated you want to check if its temperature is okay. You can do so by mapping your &lt;code&gt;Future[Water]&lt;&#x2F;code&gt; to a &lt;code&gt;Future[Boolean]&lt;&#x2F;code&gt;:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;scala&quot; style=&quot;background-color:#002b36;color:#839496;&quot; class=&quot;language-scala &quot;&gt;&lt;code class=&quot;language-scala&quot; data-lang=&quot;scala&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;val temperatureOkay&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Future&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;Boolean&lt;&#x2F;span&gt;&lt;span&gt;] &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt; heatWater&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Water&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;25&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;))&lt;&#x2F;span&gt;&lt;span&gt;.map &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{ &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;water =&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;  println&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;we&amp;#39;re in the future!&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;80&lt;&#x2F;span&gt;&lt;span&gt; to &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;85&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span&gt;.contains&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;water.temperature&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The &lt;code&gt;Future[Boolean]&lt;&#x2F;code&gt; assigned to &lt;code&gt;temperatureOkay&lt;&#x2F;code&gt; will eventually contain the successfully computed boolean value. Go change the implementation of &lt;code&gt;heatWater&lt;&#x2F;code&gt; so that it throws an exception (maybe because your water heater explodes or something) and watch how &lt;code&gt;we&#x27;re in the future&lt;&#x2F;code&gt; will never be printed to the console.&lt;&#x2F;p&gt;
&lt;p&gt;When you are writing the function you pass to &lt;code&gt;map&lt;&#x2F;code&gt;, you&#x27;re in the future, or rather in a possible future. That mapping function gets executed as soon as your &lt;code&gt;Future[Water]&lt;&#x2F;code&gt; instance has completed successfully. However, the timeline in which that happens might not be the one you live in. If your instance of &lt;code&gt;Future[Water]&lt;&#x2F;code&gt; fails, what&#x27;s taking place in the function you passed to &lt;code&gt;map&lt;&#x2F;code&gt; will never happen. Instead, the result of calling &lt;code&gt;map&lt;&#x2F;code&gt; will be a &lt;code&gt;Future[Boolean]&lt;&#x2F;code&gt; containing a &lt;code&gt;Failure&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;h4 id=&quot;keeping-the-future-flat&quot;&gt;Keeping the future flat&lt;&#x2F;h4&gt;
&lt;p&gt;If the computation of one &lt;code&gt;Future&lt;&#x2F;code&gt; depends on the result of another, you&#x27;ll likely want to resort to &lt;code&gt;flatMap&lt;&#x2F;code&gt; to avoid a deeply nested structure of futures.&lt;&#x2F;p&gt;
&lt;p&gt;For example, let&#x27;s assume that the process of actually measuring the temperature takes a while, so you want to determine whether the temperature is okay asynchronously, too. You have a function that takes an instance of &lt;code&gt;Water&lt;&#x2F;code&gt; and returns a &lt;code&gt;Future[Boolean]&lt;&#x2F;code&gt;:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;scala&quot; style=&quot;background-color:#002b36;color:#839496;&quot; class=&quot;language-scala &quot;&gt;&lt;code class=&quot;language-scala&quot; data-lang=&quot;scala&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;temperatureOkay&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;water&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Water&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Future&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;Boolean&lt;&#x2F;span&gt;&lt;span&gt;] &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Future &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;80&lt;&#x2F;span&gt;&lt;span&gt; to &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;85&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span&gt;.contains&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;water.temperature&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Use &lt;code&gt;flatMap&lt;&#x2F;code&gt; instead of &lt;code&gt;map&lt;&#x2F;code&gt; in order to get a &lt;code&gt;Future[Boolean]&lt;&#x2F;code&gt; instead of a &lt;code&gt;Future[Future[Boolean]]&lt;&#x2F;code&gt;:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;scala&quot; style=&quot;background-color:#002b36;color:#839496;&quot; class=&quot;language-scala &quot;&gt;&lt;code class=&quot;language-scala&quot; data-lang=&quot;scala&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;val nestedFuture&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Future&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Future&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;Boolean&lt;&#x2F;span&gt;&lt;span&gt;]] &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt; heatWater&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Water&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;25&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;))&lt;&#x2F;span&gt;&lt;span&gt;.map &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;water =&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; temperatureOkay&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;water&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;}
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;val flatFuture&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Future&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;Boolean&lt;&#x2F;span&gt;&lt;span&gt;] &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt; heatWater&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Water&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;25&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;))&lt;&#x2F;span&gt;&lt;span&gt;.flatMap &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;water =&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; temperatureOkay&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;water&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Again, the mapping function is only executed after (and if) the &lt;code&gt;Future[Water]&lt;&#x2F;code&gt; instance has been completed successfully, hopefully with an acceptable temperature.&lt;&#x2F;p&gt;
&lt;h4 id=&quot;for-comprehensions&quot;&gt;For comprehensions&lt;&#x2F;h4&gt;
&lt;p&gt;Instead of calling &lt;code&gt;flatMap&lt;&#x2F;code&gt;, you&#x27;ll usually want to write a for comprehension, which is essentially the same, but reads a lot clearer. Our example above could be rewritten like this:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;scala&quot; style=&quot;background-color:#002b36;color:#839496;&quot; class=&quot;language-scala &quot;&gt;&lt;code class=&quot;language-scala&quot; data-lang=&quot;scala&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;val acceptable&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Future&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;Boolean&lt;&#x2F;span&gt;&lt;span&gt;] &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;for &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;heatedWater &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;&amp;lt;-&lt;&#x2F;span&gt;&lt;span&gt; heatWater&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Water&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;25&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;))
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;okay &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;&amp;lt;-&lt;&#x2F;span&gt;&lt;span&gt; temperatureOkay&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;heatedWater&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;} &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;yield&lt;&#x2F;span&gt;&lt;span&gt; okay
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;If you have multiple computations that can be computed in parallel, you need to take care that you already create the corresponding &lt;code&gt;Future&lt;&#x2F;code&gt; instances outside of the for comprehension.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;scala&quot; style=&quot;background-color:#002b36;color:#839496;&quot; class=&quot;language-scala &quot;&gt;&lt;code class=&quot;language-scala&quot; data-lang=&quot;scala&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;prepareCappuccinoSequentially&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;()&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Future&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Cappuccino&lt;&#x2F;span&gt;&lt;span&gt;] &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= {
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;for &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;ground &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;&amp;lt;-&lt;&#x2F;span&gt;&lt;span&gt; grind&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;arabica beans&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;water &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;&amp;lt;-&lt;&#x2F;span&gt;&lt;span&gt; heatWater&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Water&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;20&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;))
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;foam &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;&amp;lt;-&lt;&#x2F;span&gt;&lt;span&gt; frothMilk&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;milk&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;espresso &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;&amp;lt;-&lt;&#x2F;span&gt;&lt;span&gt; brew&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;ground, water&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;} &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;yield&lt;&#x2F;span&gt;&lt;span&gt; combine&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;espresso, foam&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This reads nicely, but since a for comprehension is just another representation for nested &lt;code&gt;flatMap&lt;&#x2F;code&gt; calls, this means that the &lt;code&gt;Future[Water]&lt;&#x2F;code&gt; created in &lt;code&gt;heatWater&lt;&#x2F;code&gt; is only really instantiated after the &lt;code&gt;Future[GroundCoffee]&lt;&#x2F;code&gt; has completed successfully. You can check this by watching the sequential console output coming from the functions we implemented above.&lt;&#x2F;p&gt;
&lt;p&gt;Hence, make sure to instantiate all your independent futures before the for comprehension:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;scala&quot; style=&quot;background-color:#002b36;color:#839496;&quot; class=&quot;language-scala &quot;&gt;&lt;code class=&quot;language-scala&quot; data-lang=&quot;scala&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;prepareCappuccino&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;()&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Future&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Cappuccino&lt;&#x2F;span&gt;&lt;span&gt;] &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= {
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;val groundCoffee &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt; grind&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;arabica beans&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;val heatedWater &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt; heatWater&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Water&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;20&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;))
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;val frothedMilk &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt; frothMilk&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;milk&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;for &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;ground &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;&amp;lt;-&lt;&#x2F;span&gt;&lt;span&gt; groundCoffee
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;water &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;&amp;lt;-&lt;&#x2F;span&gt;&lt;span&gt; heatedWater
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;foam &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;&amp;lt;-&lt;&#x2F;span&gt;&lt;span&gt; frothedMilk
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;espresso &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;&amp;lt;-&lt;&#x2F;span&gt;&lt;span&gt; brew&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;ground, water&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;} &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;yield&lt;&#x2F;span&gt;&lt;span&gt; combine&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;espresso, foam&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Now, the three futures we create before the for comprehension start being completed immediately and execute concurrently. If you watch the console output, you will see that it&#x27;s non-deterministic. The only thing that&#x27;s certain is that the &lt;code&gt;&quot;happy brewing&quot;&lt;&#x2F;code&gt; output will come last. Since the method in which it is called requires the values coming from two other futures, it is only created inside our for comprehension, i.e. after those futures have completed successfully.&lt;&#x2F;p&gt;
&lt;h4 id=&quot;failure-projections&quot;&gt;Failure projections&lt;&#x2F;h4&gt;
&lt;p&gt;You will have noticed that &lt;code&gt;Future[T]&lt;&#x2F;code&gt; is success-biased, allowing you to use &lt;code&gt;map&lt;&#x2F;code&gt;, &lt;code&gt;flatMap&lt;&#x2F;code&gt;,  &lt;code&gt;filter&lt;&#x2F;code&gt; etc. under the assumption that it will complete successfully. Sometimes, you may want to be able to work in this nice functional way for the timeline in which things go wrong. By calling the &lt;code&gt;failed&lt;&#x2F;code&gt; method on an instance of &lt;code&gt;Future[T]&lt;&#x2F;code&gt;, you get a failure projection of it, which is a &lt;code&gt;Future[Throwable]&lt;&#x2F;code&gt;. Now you can &lt;code&gt;map&lt;&#x2F;code&gt; that &lt;code&gt;Future[Throwable]&lt;&#x2F;code&gt;, for example, and your mapping function will only be executed if the original &lt;code&gt;Future[T]&lt;&#x2F;code&gt; has completed with a failure.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;outlook&quot;&gt;Outlook&lt;&#x2F;h2&gt;
&lt;p&gt;You have seen the &lt;code&gt;Future&lt;&#x2F;code&gt;, and it looks bright! The fact that it&#x27;s just another container type that can be composed and used in a functional way makes working with it very pleasant.&lt;&#x2F;p&gt;
&lt;p&gt;Making blocking code concurrent can be pretty easy by wrapping it in a call to &lt;code&gt;future&lt;&#x2F;code&gt;. However, it&#x27;s better to be non-blocking in the first place. To achieve this, one has to make a &lt;code&gt;Promise&lt;&#x2F;code&gt; to complete a &lt;code&gt;Future&lt;&#x2F;code&gt;. This and using the futures in practice will be the topic of the next part of this series.&lt;&#x2F;p&gt;
</description>
      </item>
      <item>
          <title>The Neophyte&#x27;s Guide to Scala Part 7: The Either type</title>
          <pubDate>Wed, 02 Jan 2013 00:00:00 +0000</pubDate>
          <author>Unknown</author>
          <link>https://danielwestheide.com/blog/the-neophytes-guide-to-scala-part-7-the-either-type/</link>
          <guid>https://danielwestheide.com/blog/the-neophytes-guide-to-scala-part-7-the-either-type/</guid>
          <description xml:base="https://danielwestheide.com/blog/the-neophytes-guide-to-scala-part-7-the-either-type/">&lt;p&gt;In the previous article, I discussed &lt;a href=&quot;http:&#x2F;&#x2F;danielwestheide.com&#x2F;blog&#x2F;2012&#x2F;12&#x2F;26&#x2F;the-neophytes-guide-to-scala-part-6-error-handling-with-try.html&quot;&gt;functional error handling using Try&lt;&#x2F;a&gt;, which was introduced in Scala 2.10. I also mentioned the existence of another, somewhat similar type called &lt;code&gt;Either&lt;&#x2F;code&gt;, which is the subject of this article. You will learn how to use it, when to use it, and what its particular pitfalls are.&lt;&#x2F;p&gt;
&lt;p&gt;Speaking of which, at least at the time of this writing, &lt;code&gt;Either&lt;&#x2F;code&gt; has some serious design flaws you need to be aware of, so much so that one might argue about whether to use it at all. So why then should you learn about &lt;code&gt;Either&lt;&#x2F;code&gt; at all?&lt;&#x2F;p&gt;
&lt;p&gt;For one, people will not all migrate their existing code bases to use &lt;code&gt;Try&lt;&#x2F;code&gt; for dealing with exceptions, so it is good to be able to understand the intricacies of this type, too.&lt;&#x2F;p&gt;
&lt;p&gt;Moreover, &lt;code&gt;Try&lt;&#x2F;code&gt; is not really an all-out replacement for &lt;code&gt;Either&lt;&#x2F;code&gt;, only for one particular usage of it, namely handling exceptions in a functional way. As it stands, &lt;code&gt;Try&lt;&#x2F;code&gt; and &lt;code&gt;Either&lt;&#x2F;code&gt; really complement each other, each covering different use cases. And, as flawed as &lt;code&gt;Either&lt;&#x2F;code&gt; may be, in certain situations it will still be a very good fit.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;the-semantics&quot;&gt;The semantics&lt;&#x2F;h2&gt;
&lt;p&gt;Like &lt;code&gt;Option&lt;&#x2F;code&gt; and &lt;code&gt;Try&lt;&#x2F;code&gt;, &lt;code&gt;Either&lt;&#x2F;code&gt; is a container type. Unlike the aforementioned types, it takes not only one, but two type parameters: An &lt;code&gt;Either[A, B]&lt;&#x2F;code&gt; instance can contain either an instance of &lt;code&gt;A&lt;&#x2F;code&gt;, or an instance of &lt;code&gt;B&lt;&#x2F;code&gt;. This is different from a &lt;code&gt;Tuple2[A, B]&lt;&#x2F;code&gt;, which contains both an &lt;code&gt;A&lt;&#x2F;code&gt; and a &lt;code&gt;B&lt;&#x2F;code&gt; instance.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;code&gt;Either&lt;&#x2F;code&gt; has exactly two sub types, &lt;code&gt;Left&lt;&#x2F;code&gt; and &lt;code&gt;Right&lt;&#x2F;code&gt;. If an &lt;code&gt;Either[A, B]&lt;&#x2F;code&gt; object contains an instance of &lt;code&gt;A&lt;&#x2F;code&gt;, then the &lt;code&gt;Either&lt;&#x2F;code&gt; is a &lt;code&gt;Left&lt;&#x2F;code&gt;. Otherwise it contains an instance of &lt;code&gt;B&lt;&#x2F;code&gt; and is a &lt;code&gt;Right&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;There is nothing in the semantics of this type that specifies one or the other sub type to represent an error or a success, respectively. In fact, &lt;code&gt;Either&lt;&#x2F;code&gt; is a general-purpose type for use whenever you need to deal with situations where the result can be of one of two possible types. Nevertheless, error handling is a popular use case for it, and by convention, when using it that way, the &lt;code&gt;Left&lt;&#x2F;code&gt; represents the error case, whereas the &lt;code&gt;Right&lt;&#x2F;code&gt; contains the success value.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;creating-an-either&quot;&gt;Creating an Either&lt;&#x2F;h2&gt;
&lt;p&gt;Creating an instance of &lt;code&gt;Either&lt;&#x2F;code&gt; is trivial. Both &lt;code&gt;Left&lt;&#x2F;code&gt; and &lt;code&gt;Right&lt;&#x2F;code&gt; are case classes, so if we want to implement a rock-solid internet censorship feature, we can just do the following:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;scala&quot; style=&quot;background-color:#002b36;color:#839496;&quot; class=&quot;language-scala &quot;&gt;&lt;code class=&quot;language-scala&quot; data-lang=&quot;scala&quot;&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;import&lt;&#x2F;span&gt;&lt;span&gt; scala.io.Source
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;import&lt;&#x2F;span&gt;&lt;span&gt; java.net.URL
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;getContent&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;url&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;URL&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Either&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;String&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Source&lt;&#x2F;span&gt;&lt;span&gt;] &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;=
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;if &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;url.getHost.contains&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;google&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;))
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Left&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Requested URL is blocked for the good of the people!&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;else 
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Right&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Source&lt;&#x2F;span&gt;&lt;span&gt;.fromURL&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;url&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;))
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Now, if we call &lt;code&gt;getContent(new URL(&quot;http:&#x2F;&#x2F;danielwestheide.com&quot;))&lt;&#x2F;code&gt;, we will get a &lt;code&gt;scala.io.Source&lt;&#x2F;code&gt; wrapped in a &lt;code&gt;Right&lt;&#x2F;code&gt;. If we pass in &lt;code&gt;new URL(&quot;https:&#x2F;&#x2F;plus.google.com&quot;)&lt;&#x2F;code&gt;, the return value will be a &lt;code&gt;Left&lt;&#x2F;code&gt; containing a &lt;code&gt;String&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;working-with-either-values&quot;&gt;Working with Either values&lt;&#x2F;h2&gt;
&lt;p&gt;Some of the very basic stuff works just as you know from &lt;code&gt;Option&lt;&#x2F;code&gt; or &lt;code&gt;Try&lt;&#x2F;code&gt;: You can ask an instance of &lt;code&gt;Either&lt;&#x2F;code&gt; if it &lt;code&gt;isLeft&lt;&#x2F;code&gt; or &lt;code&gt;isRight&lt;&#x2F;code&gt;. You can also do pattern matching on it, which is one of the most familiar and convenient ways of working with objects of this type:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;scala&quot; style=&quot;background-color:#002b36;color:#839496;&quot; class=&quot;language-scala &quot;&gt;&lt;code class=&quot;language-scala&quot; data-lang=&quot;scala&quot;&gt;&lt;span&gt;getContent&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;new URL&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;http:&#x2F;&#x2F;google.com&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;match &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;case Left&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;msg&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;=&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; println&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;msg&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;case Right&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;source&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;=&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; source.getLines.foreach&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;println&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h3 id=&quot;projections&quot;&gt;Projections&lt;&#x2F;h3&gt;
&lt;p&gt;You &lt;em&gt;cannot&lt;&#x2F;em&gt;, at least not directly, use an &lt;code&gt;Either&lt;&#x2F;code&gt; instance like a collection, the way you are familiar with from &lt;code&gt;Option&lt;&#x2F;code&gt; and &lt;code&gt;Try&lt;&#x2F;code&gt;. This is because &lt;code&gt;Either&lt;&#x2F;code&gt; is designed to be &lt;em&gt;unbiased&lt;&#x2F;em&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;code&gt;Try&lt;&#x2F;code&gt; is &lt;em&gt;success-biased&lt;&#x2F;em&gt;: it offers you &lt;code&gt;map&lt;&#x2F;code&gt;, &lt;code&gt;flatMap&lt;&#x2F;code&gt; and other methods that all work under the assumption that the &lt;code&gt;Try&lt;&#x2F;code&gt; is a &lt;code&gt;Success&lt;&#x2F;code&gt;, and if that&#x27;s not the case, they effectively don&#x27;t do anything, returning the &lt;code&gt;Failure&lt;&#x2F;code&gt; as-is.&lt;&#x2F;p&gt;
&lt;p&gt;The fact that &lt;code&gt;Either&lt;&#x2F;code&gt; is unbiased means that you first have to choose whether you want to work under the assumption that it is a &lt;code&gt;Left&lt;&#x2F;code&gt; or a &lt;code&gt;Right&lt;&#x2F;code&gt;. By calling &lt;code&gt;left&lt;&#x2F;code&gt; or &lt;code&gt;right&lt;&#x2F;code&gt; on an &lt;code&gt;Either&lt;&#x2F;code&gt; value, you get a &lt;code&gt;LeftProjection&lt;&#x2F;code&gt; or &lt;code&gt;RightProjection&lt;&#x2F;code&gt;, respectively, which are basically left- or right-biased wrappers for the &lt;code&gt;Either&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;h4 id=&quot;mapping&quot;&gt;Mapping&lt;&#x2F;h4&gt;
&lt;p&gt;Once you have a projection, you can call &lt;code&gt;map&lt;&#x2F;code&gt; on it:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;scala&quot; style=&quot;background-color:#002b36;color:#839496;&quot; class=&quot;language-scala &quot;&gt;&lt;code class=&quot;language-scala&quot; data-lang=&quot;scala&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;val content&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Either&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;String&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Iterator&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;String&lt;&#x2F;span&gt;&lt;span&gt;]] &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;=
&lt;&#x2F;span&gt;&lt;span&gt;  getContent&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;new URL&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;http:&#x2F;&#x2F;danielwestheide.com&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;))&lt;&#x2F;span&gt;&lt;span&gt;.right.map&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d33682;&quot;&gt;_&lt;&#x2F;span&gt;&lt;span&gt;.getLines&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;())
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;&#x2F;&#x2F; content is a Right containing the lines from the Source returned by getContent
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;val moreContent&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Either&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;String&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Iterator&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;String&lt;&#x2F;span&gt;&lt;span&gt;]] &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= 
&lt;&#x2F;span&gt;&lt;span&gt;  getContent&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;new URL&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;http:&#x2F;&#x2F;google.com&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;))&lt;&#x2F;span&gt;&lt;span&gt;.right.map&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d33682;&quot;&gt;_&lt;&#x2F;span&gt;&lt;span&gt;.getLines&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;&#x2F;&#x2F; moreContent is a Left, as already returned by getContent
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Regardless of whether the &lt;code&gt;Either[String, Source]&lt;&#x2F;code&gt; in this example is a &lt;code&gt;Left&lt;&#x2F;code&gt; or a &lt;code&gt;Right&lt;&#x2F;code&gt;, it will be mapped to an &lt;code&gt;Either[String, Iterator[String]]&lt;&#x2F;code&gt;. If it&#x27;s called on a  &lt;code&gt;Right&lt;&#x2F;code&gt;, the value inside it will be transformed. If it&#x27;s a &lt;code&gt;Left&lt;&#x2F;code&gt;, that will be returned unchanged.&lt;&#x2F;p&gt;
&lt;p&gt;We can do the same with a &lt;code&gt;LeftProjection&lt;&#x2F;code&gt;, of course:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;scala&quot; style=&quot;background-color:#002b36;color:#839496;&quot; class=&quot;language-scala &quot;&gt;&lt;code class=&quot;language-scala&quot; data-lang=&quot;scala&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;val content&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Either&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Iterator&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;String&lt;&#x2F;span&gt;&lt;span&gt;], &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Source&lt;&#x2F;span&gt;&lt;span&gt;] &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;=
&lt;&#x2F;span&gt;&lt;span&gt;  getContent&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;new URL&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;http:&#x2F;&#x2F;danielwestheide.com&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;))&lt;&#x2F;span&gt;&lt;span&gt;.left.map&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Iterator&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d33682;&quot;&gt;_&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;))
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;&#x2F;&#x2F; content is the Right containing a Source, as already returned by getContent
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;val moreContent&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Either&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Iterator&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;String&lt;&#x2F;span&gt;&lt;span&gt;], &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Source&lt;&#x2F;span&gt;&lt;span&gt;] &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;=
&lt;&#x2F;span&gt;&lt;span&gt;  getContent&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;new URL&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;http:&#x2F;&#x2F;google.com&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;))&lt;&#x2F;span&gt;&lt;span&gt;.left.map&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Iterator&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d33682;&quot;&gt;_&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;))
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;&#x2F;&#x2F; moreContent is a Left containing the msg returned by getContent in an Iterator
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Now, if the &lt;code&gt;Either&lt;&#x2F;code&gt; is a &lt;code&gt;Left&lt;&#x2F;code&gt;, its wrapped value is transformed, whereas a &lt;code&gt;Right&lt;&#x2F;code&gt; would be returned unchanged. Either way, the result is of type &lt;code&gt;Either[Iterator[String], Source]&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;Please note that the &lt;code&gt;map&lt;&#x2F;code&gt; method is defined on the projection types, not on &lt;code&gt;Either&lt;&#x2F;code&gt;, but it does return a value of type &lt;code&gt;Either&lt;&#x2F;code&gt;, not a projection. In this, &lt;code&gt;Either&lt;&#x2F;code&gt; deviates from the other container types you know. The reason for this has to do with &lt;code&gt;Either&lt;&#x2F;code&gt; being unbiased, but as you will see, this can lead to some very unpleasant problems in certain cases. It also means that if you want to chain multiple calls to &lt;code&gt;map&lt;&#x2F;code&gt;, &lt;code&gt;flatMap&lt;&#x2F;code&gt; and the like, you always have to ask for your desired projection again in between.&lt;&#x2F;p&gt;
&lt;h4 id=&quot;flat-mapping&quot;&gt;Flat mapping&lt;&#x2F;h4&gt;
&lt;p&gt;Projections also support flat mapping, avoiding the common problem of creating a convoluted structure of multiple inner and outer &lt;code&gt;Either&lt;&#x2F;code&gt; types that you will end up with if you nest multiple calls to &lt;code&gt;map&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;I&#x27;m putting very high requirements on your suspension of disbelief now, coming up with a completely contrived example. Let&#x27;s say we want to calculate the average number of lines of two of my articles. You&#x27;ve always wanted to do that, right? Here&#x27;s how we could solve this challenging problem:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;scala&quot; style=&quot;background-color:#002b36;color:#839496;&quot; class=&quot;language-scala &quot;&gt;&lt;code class=&quot;language-scala&quot; data-lang=&quot;scala&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;val part5 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;new URL&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;http:&#x2F;&#x2F;t.co&#x2F;UR1aalX4&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;val part6 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;new URL&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;http:&#x2F;&#x2F;t.co&#x2F;6wlKwTmu&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;val content &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt; getContent&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;part5&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span&gt;.right.map&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;a =&amp;gt; 
&lt;&#x2F;span&gt;&lt;span&gt;  getContent&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;part6&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span&gt;.right.map&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;b =&amp;gt; 
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;a.getLines&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;()&lt;&#x2F;span&gt;&lt;span&gt;.size + b.getLines&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;()&lt;&#x2F;span&gt;&lt;span&gt;.size&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span&gt; &#x2F; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;2&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;))
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;What we&#x27;ll end up with is an &lt;code&gt;Either[String, Either[String, Int]]&lt;&#x2F;code&gt;. Now, &lt;code&gt;content&lt;&#x2F;code&gt; being a nested structure of &lt;code&gt;Right&lt;&#x2F;code&gt;s, we could flatten it by calling the &lt;code&gt;joinRight&lt;&#x2F;code&gt; method on it (you also have &lt;code&gt;joinLeft&lt;&#x2F;code&gt; available to flatten a nested structure of &lt;code&gt;Left&lt;&#x2F;code&gt;s).&lt;&#x2F;p&gt;
&lt;p&gt;However, we can avoid creating this nested structure altogether. If we &lt;code&gt;flatMap&lt;&#x2F;code&gt; on our outer &lt;code&gt;RightProjection&lt;&#x2F;code&gt;, we get a more pleasant result type, unpacking the &lt;code&gt;Right&lt;&#x2F;code&gt; of the inner &lt;code&gt;Either&lt;&#x2F;code&gt;:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;scala&quot; style=&quot;background-color:#002b36;color:#839496;&quot; class=&quot;language-scala &quot;&gt;&lt;code class=&quot;language-scala&quot; data-lang=&quot;scala&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;val content &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt; getContent&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;part5&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span&gt;.right.flatMap&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;a =&amp;gt; 
&lt;&#x2F;span&gt;&lt;span&gt;  getContent&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;part6&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span&gt;.right.map&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;b =&amp;gt; 
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;a.getLines&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;()&lt;&#x2F;span&gt;&lt;span&gt;.size + b.getLines&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;()&lt;&#x2F;span&gt;&lt;span&gt;.size&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span&gt; &#x2F; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;2&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;))
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Now &lt;code&gt;content&lt;&#x2F;code&gt; is a flat &lt;code&gt;Either[String, Int]&lt;&#x2F;code&gt;, which makes it a lot nicer to work with, for example using pattern matching.&lt;&#x2F;p&gt;
&lt;h4 id=&quot;for-comprehensions&quot;&gt;For comprehensions&lt;&#x2F;h4&gt;
&lt;p&gt;By now, you have probably learned to love working with for comprehensions in a consistent way on various different data types. You can do that, too, with projections of &lt;code&gt;Either&lt;&#x2F;code&gt;, but the sad truth is that it&#x27;s not quite as nice, and there are things that you won&#x27;t be able to do without resorting to ugly workarounds, out of the box.&lt;&#x2F;p&gt;
&lt;p&gt;Let&#x27;s rewrite our &lt;code&gt;flatMap&lt;&#x2F;code&gt; example, making use of for comprehensions instead:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;scala&quot; style=&quot;background-color:#002b36;color:#839496;&quot; class=&quot;language-scala &quot;&gt;&lt;code class=&quot;language-scala&quot; data-lang=&quot;scala&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;averageLineCount&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;url1&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;URL&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;url2&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;URL&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Either&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;String&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;Int&lt;&#x2F;span&gt;&lt;span&gt;] &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= 
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;for &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;source1 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;&amp;lt;-&lt;&#x2F;span&gt;&lt;span&gt; getContent&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;url1&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span&gt;.right
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;source2 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;&amp;lt;-&lt;&#x2F;span&gt;&lt;span&gt; getContent&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;url2&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span&gt;.right
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;} &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;yield &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;source1.getLines&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;()&lt;&#x2F;span&gt;&lt;span&gt;.size + source2.getLines&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;()&lt;&#x2F;span&gt;&lt;span&gt;.size&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span&gt; &#x2F; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;2
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This is not too bad. Note that we have to call &lt;code&gt;right&lt;&#x2F;code&gt; on each &lt;code&gt;Either&lt;&#x2F;code&gt; we use in our generators, of course.&lt;&#x2F;p&gt;
&lt;p&gt;Now, let&#x27;s try to refactor this for comprehension – since the yield expression is a little too involved, we want to extract some parts of it into value definitions inside our for comprehension:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;scala&quot; style=&quot;background-color:#002b36;color:#839496;&quot; class=&quot;language-scala &quot;&gt;&lt;code class=&quot;language-scala&quot; data-lang=&quot;scala&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;averageLineCountWontCompile&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;url1&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;URL&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;url2&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;URL&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Either&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;String&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;Int&lt;&#x2F;span&gt;&lt;span&gt;] &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= 
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;for &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;source1 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;&amp;lt;-&lt;&#x2F;span&gt;&lt;span&gt; getContent&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;url1&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span&gt;.right
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;source2 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;&amp;lt;-&lt;&#x2F;span&gt;&lt;span&gt; getContent&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;url2&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span&gt;.right
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;lines1 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt; source1.getLines&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;()&lt;&#x2F;span&gt;&lt;span&gt;.size
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;lines2 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt; source2.getLines&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;()&lt;&#x2F;span&gt;&lt;span&gt;.size
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;} &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;yield &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;lines1 + lines2&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span&gt; &#x2F; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;2
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This won&#x27;t compile! The reason will become clearer if we examine what this for comprehension corresponds to, if you take away the sugar. It translates to something that is similar to the following, albeit much less readable:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;scala&quot; style=&quot;background-color:#002b36;color:#839496;&quot; class=&quot;language-scala &quot;&gt;&lt;code class=&quot;language-scala&quot; data-lang=&quot;scala&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;averageLineCountDesugaredWontCompile&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;url1&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;URL&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;url2&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;URL&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Either&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;String&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;Int&lt;&#x2F;span&gt;&lt;span&gt;] &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;=
&lt;&#x2F;span&gt;&lt;span&gt;  getContent&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;url1&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span&gt;.right.flatMap &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{ &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;source1 =&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;    getContent&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;url2&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span&gt;.right.map &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{ &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;source2 =&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;      &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;val lines1 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt; source1.getLines&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;()&lt;&#x2F;span&gt;&lt;span&gt;.size
&lt;&#x2F;span&gt;&lt;span&gt;      &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;val lines2 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt; source2.getLines&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;()&lt;&#x2F;span&gt;&lt;span&gt;.size
&lt;&#x2F;span&gt;&lt;span&gt;      &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;lines1, lines2&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;}&lt;&#x2F;span&gt;&lt;span&gt;.map &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{ &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;case &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;x&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;y&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;=&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; x + y &#x2F; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;2 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;}
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The problem is that by including a value definition in our for comprehension, a new call to &lt;code&gt;map&lt;&#x2F;code&gt; is introduced automatically – on the result of the previous call to &lt;code&gt;map&lt;&#x2F;code&gt;, which has returned an &lt;code&gt;Either&lt;&#x2F;code&gt;, not a &lt;code&gt;RightProjection&lt;&#x2F;code&gt;. As you know, &lt;code&gt;Either&lt;&#x2F;code&gt; doesn&#x27;t define a &lt;code&gt;map&lt;&#x2F;code&gt; method, making the compiler a little bit grumpy.&lt;&#x2F;p&gt;
&lt;p&gt;This is where &lt;code&gt;Either&lt;&#x2F;code&gt; shows us its ugly trollface. In this example, the value definitions are not strictly necessary. If they are, you can work around this problem, replacing any value definitions by generators, like this:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;scala&quot; style=&quot;background-color:#002b36;color:#839496;&quot; class=&quot;language-scala &quot;&gt;&lt;code class=&quot;language-scala&quot; data-lang=&quot;scala&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;averageLineCount&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;url1&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;URL&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;url2&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;URL&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Either&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;String&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;Int&lt;&#x2F;span&gt;&lt;span&gt;] &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= 
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;for &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;source1 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;&amp;lt;-&lt;&#x2F;span&gt;&lt;span&gt; getContent&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;url1&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span&gt;.right
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;source2 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;&amp;lt;-&lt;&#x2F;span&gt;&lt;span&gt; getContent&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;url2&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span&gt;.right
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;lines1 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;&amp;lt;- &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Right&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;source1.getLines&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;()&lt;&#x2F;span&gt;&lt;span&gt;.size&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span&gt;.right
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;lines2 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;&amp;lt;- &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Right&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;source2.getLines&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;()&lt;&#x2F;span&gt;&lt;span&gt;.size&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span&gt;.right
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;} &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;yield &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;lines1 + lines2&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span&gt; &#x2F; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;2
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;It&#x27;s important to be aware of these design flaws. They don&#x27;t make &lt;code&gt;Either&lt;&#x2F;code&gt; unusable, but can lead to serious headaches if you don&#x27;t have a clue what&#x27;s going on.&lt;&#x2F;p&gt;
&lt;h4 id=&quot;other-methods&quot;&gt;Other methods&lt;&#x2F;h4&gt;
&lt;p&gt;The projection types have some other useful methods:&lt;&#x2F;p&gt;
&lt;p&gt;You can convert your &lt;code&gt;Either&lt;&#x2F;code&gt; instance to an &lt;code&gt;Option&lt;&#x2F;code&gt; by calling &lt;code&gt;toOption&lt;&#x2F;code&gt; on one of its projections. For example, if you have an &lt;code&gt;e&lt;&#x2F;code&gt; of type &lt;code&gt;Either[A, B]&lt;&#x2F;code&gt;, &lt;code&gt;e.right.toOption&lt;&#x2F;code&gt; will return an &lt;code&gt;Option[B]&lt;&#x2F;code&gt;. If your &lt;code&gt;Either[A, B]&lt;&#x2F;code&gt; instance is a &lt;code&gt;Right&lt;&#x2F;code&gt;, that &lt;code&gt;Option[B]&lt;&#x2F;code&gt; will be a &lt;code&gt;Some&lt;&#x2F;code&gt;. If it&#x27;s a &lt;code&gt;Left&lt;&#x2F;code&gt;, it will be &lt;code&gt;None&lt;&#x2F;code&gt;. The reverse behaviour can be achieved, of course, when calling &lt;code&gt;toOption&lt;&#x2F;code&gt; on the &lt;code&gt;LeftProjection&lt;&#x2F;code&gt; of your &lt;code&gt;Either[A, B]&lt;&#x2F;code&gt;. If you need a sequence of either one value or none, use &lt;code&gt;toSeq&lt;&#x2F;code&gt; instead.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;folding&quot;&gt;Folding&lt;&#x2F;h3&gt;
&lt;p&gt;If you want to transform an &lt;code&gt;Either&lt;&#x2F;code&gt; value regardless of whether it is a &lt;code&gt;Left&lt;&#x2F;code&gt; or a &lt;code&gt;Right&lt;&#x2F;code&gt;, you can do so by means of the &lt;code&gt;fold&lt;&#x2F;code&gt; method that is defined on &lt;code&gt;Either&lt;&#x2F;code&gt;, expecting two transform functions with the same result type, the first one being called if the &lt;code&gt;Either&lt;&#x2F;code&gt; is a &lt;code&gt;Left&lt;&#x2F;code&gt;, the second one if it&#x27;s a &lt;code&gt;Right&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;To demonstrate this, let&#x27;s combine the two mapping operations we implemented on the &lt;code&gt;LeftProjection&lt;&#x2F;code&gt; and the &lt;code&gt;RightProjection&lt;&#x2F;code&gt; above:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;scala&quot; style=&quot;background-color:#002b36;color:#839496;&quot; class=&quot;language-scala &quot;&gt;&lt;code class=&quot;language-scala&quot; data-lang=&quot;scala&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;val content&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Iterator&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;String&lt;&#x2F;span&gt;&lt;span&gt;] &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;=
&lt;&#x2F;span&gt;&lt;span&gt;  getContent&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;new URL&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;http:&#x2F;&#x2F;danielwestheide.com&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;))&lt;&#x2F;span&gt;&lt;span&gt;.fold&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Iterator&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d33682;&quot;&gt;_&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d33682;&quot;&gt;_&lt;&#x2F;span&gt;&lt;span&gt;.getLines&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;())
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;val moreContent&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Iterator&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;String&lt;&#x2F;span&gt;&lt;span&gt;] &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= 
&lt;&#x2F;span&gt;&lt;span&gt;  getContent&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;new URL&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;http:&#x2F;&#x2F;google.com&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;))&lt;&#x2F;span&gt;&lt;span&gt;.fold&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Iterator&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d33682;&quot;&gt;_&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d33682;&quot;&gt;_&lt;&#x2F;span&gt;&lt;span&gt;.getLines&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;())
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;In this example, we are transforming our &lt;code&gt;Either[String, Source]&lt;&#x2F;code&gt; into an &lt;code&gt;Iterator[String]&lt;&#x2F;code&gt;, no matter if it&#x27;s a &lt;code&gt;Left&lt;&#x2F;code&gt; or a &lt;code&gt;Right&lt;&#x2F;code&gt;. You could just as well return a new &lt;code&gt;Either&lt;&#x2F;code&gt; again or execute side-effects and return &lt;code&gt;Unit&lt;&#x2F;code&gt; from your two functions. As such, calling &lt;code&gt;fold&lt;&#x2F;code&gt; provides a nice alternative to pattern matching.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;when-to-use-either&quot;&gt;When to use Either&lt;&#x2F;h2&gt;
&lt;p&gt;Now that you have seen how to work with &lt;code&gt;Either&lt;&#x2F;code&gt; values and what you have to take care of, let&#x27;s move on to some specific use cases.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;error-handling&quot;&gt;Error handling&lt;&#x2F;h3&gt;
&lt;p&gt;You &lt;em&gt;can&lt;&#x2F;em&gt; use &lt;code&gt;Either&lt;&#x2F;code&gt; for exception handling very much like &lt;code&gt;Try&lt;&#x2F;code&gt;. &lt;code&gt;Either&lt;&#x2F;code&gt; has one advantage over &lt;code&gt;Try&lt;&#x2F;code&gt;: you can have more specific error types at compile time, while &lt;code&gt;Try&lt;&#x2F;code&gt; uses &lt;code&gt;Throwable&lt;&#x2F;code&gt; all the time. This means that &lt;code&gt;Either&lt;&#x2F;code&gt; can be a good choice for expected errors.&lt;&#x2F;p&gt;
&lt;p&gt;You&#x27;d have to implement a method like this, delegating to the very useful &lt;code&gt;Exception&lt;&#x2F;code&gt; object from the &lt;code&gt;scala.util.control&lt;&#x2F;code&gt; package:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;scala&quot; style=&quot;background-color:#002b36;color:#839496;&quot; class=&quot;language-scala &quot;&gt;&lt;code class=&quot;language-scala&quot; data-lang=&quot;scala&quot;&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;import&lt;&#x2F;span&gt;&lt;span&gt; scala.util.control.Exception.catching
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;handling&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Ex &amp;lt;: Throwable&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;T&lt;&#x2F;span&gt;&lt;span&gt;]&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;exType&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Class&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Ex&lt;&#x2F;span&gt;&lt;span&gt;]&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;block&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;=&amp;gt; T&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Either&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Ex&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;T&lt;&#x2F;span&gt;&lt;span&gt;] &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;=
&lt;&#x2F;span&gt;&lt;span&gt;  catching&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;exType&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span&gt;.either&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;block&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span&gt;.asInstanceOf[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Either&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Ex&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;T&lt;&#x2F;span&gt;&lt;span&gt;]]
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The reason you might want to do that is because while the methods provided by &lt;code&gt;scala.util.Exception&lt;&#x2F;code&gt; allow you to catch only certain types of exceptions, the resulting compile-time error type is always &lt;code&gt;Throwable&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;With this method at hand, you can pass along expected exceptions in an &lt;code&gt;Either&lt;&#x2F;code&gt;:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;scala&quot; style=&quot;background-color:#002b36;color:#839496;&quot; class=&quot;language-scala &quot;&gt;&lt;code class=&quot;language-scala&quot; data-lang=&quot;scala&quot;&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;import&lt;&#x2F;span&gt;&lt;span&gt; java.net.MalformedURLException
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;parseURL&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;url&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;String&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Either&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;MalformedURLException&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;URL&lt;&#x2F;span&gt;&lt;span&gt;] &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;=
&lt;&#x2F;span&gt;&lt;span&gt;  handling&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;classOf[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;MalformedURLException&lt;&#x2F;span&gt;&lt;span&gt;]&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;new URL&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;url&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;))
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;You will have other expected error conditions, and not all of them result in third-party code throwing an exception you need to handle, as in the example above. In these cases, there is really no need to throw an exception yourself, only to catch it and wrap it in a &lt;code&gt;Left&lt;&#x2F;code&gt;. Instead, simply define your own error type, preferably as a case class, and return a &lt;code&gt;Left&lt;&#x2F;code&gt; wrapping an instance of that error type in your expected error condition occurs.&lt;&#x2F;p&gt;
&lt;p&gt;Here is an example:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;scala&quot; style=&quot;background-color:#002b36;color:#839496;&quot; class=&quot;language-scala &quot;&gt;&lt;code class=&quot;language-scala&quot; data-lang=&quot;scala&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;case class&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt; Customer&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;age&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;Int&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;class&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt; Cigarettes
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;case class&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt; UnderAgeFailure&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;age&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;Int&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;required&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;Int&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;buyCigarettes&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;customer&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Customer&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Either&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;UnderAgeFailure&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Cigarettes&lt;&#x2F;span&gt;&lt;span&gt;] &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;=
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;if &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;customer.age &amp;lt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;16&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Left&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;UnderAgeFailure&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;customer.age, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;16&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;))
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;else Right&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;new Cigarettes&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;You should avoid using &lt;code&gt;Either&lt;&#x2F;code&gt; for wrapping unexpected exceptions. &lt;code&gt;Try&lt;&#x2F;code&gt; does that better, without all the flaws you have to deal with when working with &lt;code&gt;Either&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;processing-collections&quot;&gt;Processing collections&lt;&#x2F;h3&gt;
&lt;p&gt;Generally, &lt;code&gt;Either&lt;&#x2F;code&gt; is a pretty good fit if you want to process a collection, where for some items in that collection, this might result in a condition that is problematic, but should not directly result in an exception, which would result in aborting the processing of the rest of the collection.&lt;&#x2F;p&gt;
&lt;p&gt;Let&#x27;s assume that for our industry-standard web censorship system, we are using some kind of black list:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;scala&quot; style=&quot;background-color:#002b36;color:#839496;&quot; class=&quot;language-scala &quot;&gt;&lt;code class=&quot;language-scala&quot; data-lang=&quot;scala&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;type &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;Citizen &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;String
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;case class&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt; BlackListedResource&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;url&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;URL&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;visitors&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Set&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Citizen&lt;&#x2F;span&gt;&lt;span&gt;]&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;val blacklist &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;List&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;BlackListedResource&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;new URL&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;https:&#x2F;&#x2F;google.com&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Set&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;John Doe&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Johanna Doe&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;))&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;BlackListedResource&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;new URL&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;http:&#x2F;&#x2F;yahoo.com&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Set&lt;&#x2F;span&gt;&lt;span&gt;.empty&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;BlackListedResource&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;new URL&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;https:&#x2F;&#x2F;maps.google.com&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Set&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;John Doe&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;))&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;BlackListedResource&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;new URL&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;http:&#x2F;&#x2F;plus.google.com&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Set&lt;&#x2F;span&gt;&lt;span&gt;.empty&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;A &lt;code&gt;BlackListedResource&lt;&#x2F;code&gt; represents the URL of a black-listed web page plus the citizens who have tried to visit that page.&lt;&#x2F;p&gt;
&lt;p&gt;Now we want to process this black list, where our main purpose is to identify problematic citizens, i.e. those that have tried to visit blocked pages. At the same time, we want to identify suspicous web pages – if not a single citizen has tried to visit a black-listed page, we must assume that our subjects are bypassing our filter somehow, and we need to investigate that.&lt;&#x2F;p&gt;
&lt;p&gt;Here is how we can process our black list:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;scala&quot; style=&quot;background-color:#002b36;color:#839496;&quot; class=&quot;language-scala &quot;&gt;&lt;code class=&quot;language-scala&quot; data-lang=&quot;scala&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;val checkedBlacklist&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;List&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Either&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;URL&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Set&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Citizen&lt;&#x2F;span&gt;&lt;span&gt;]]] &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;=
&lt;&#x2F;span&gt;&lt;span&gt;  blacklist.map&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;resource =&amp;gt; 
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;if &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;resource.visitors.isEmpty&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Left&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;resource.url&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;) 
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;else Right&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;resource.visitors&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;))
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;We have created a sequence of &lt;code&gt;Either&lt;&#x2F;code&gt; values, with the &lt;code&gt;Left&lt;&#x2F;code&gt; instances representing suspicious URLs and the &lt;code&gt;Right&lt;&#x2F;code&gt; ones containing sets of problem citizens. This makes it almost a breeze to identify both our problem citizens and our suspicious web pages:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;scala&quot; style=&quot;background-color:#002b36;color:#839496;&quot; class=&quot;language-scala &quot;&gt;&lt;code class=&quot;language-scala&quot; data-lang=&quot;scala&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;val suspiciousResources &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt; checkedBlacklist.flatMap&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d33682;&quot;&gt;_&lt;&#x2F;span&gt;&lt;span&gt;.left.toOption&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;val problemCitizens &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt; checkedBlacklist.flatMap&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d33682;&quot;&gt;_&lt;&#x2F;span&gt;&lt;span&gt;.right.toOption&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span&gt;.flatten.toSet
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;These more general use cases beyond exception handling are where &lt;code&gt;Either&lt;&#x2F;code&gt; really shines.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;summary&quot;&gt;Summary&lt;&#x2F;h2&gt;
&lt;p&gt;You have learned how to make use of &lt;code&gt;Either&lt;&#x2F;code&gt;, what its pitfalls are, and when to put it to use in your code. It&#x27;s a type that is not without flaws, and whether you want to have to deal with them and incorporate it in your own code is ultimately up to you.&lt;&#x2F;p&gt;
&lt;p&gt;In practice, you will notice that, now that we have &lt;code&gt;Try&lt;&#x2F;code&gt; at our hands, there won&#x27;t be terribly many use cases for it. Nevertheless, it&#x27;s good to know about it, both for those situations where it will be your perfect tool and to understand pre-2.10 Scala code you come across where it&#x27;s used for error handling.&lt;&#x2F;p&gt;
</description>
      </item>
      <item>
          <title>The Neophyte&#x27;s Guide to Scala</title>
          <pubDate>Tue, 01 Jan 2013 00:00:00 +0000</pubDate>
          <author>Unknown</author>
          <link>https://danielwestheide.com/books/the-neophytes-guide-to-scala/</link>
          <guid>https://danielwestheide.com/books/the-neophytes-guide-to-scala/</guid>
          <description xml:base="https://danielwestheide.com/books/the-neophytes-guide-to-scala/">&lt;img src=&quot;&#x2F;images&#x2F;neophyte-cover-small.jpg&quot; alt=&quot;Neophyte&#x27;s Guide Cover&quot;&gt;
&lt;p&gt;From November 2012 to April 2013, I created and published a blog series called &lt;em&gt;The Neophyte&#x27;s Guide to Scala&lt;&#x2F;em&gt;, targeted at aspiring Scala enthusiasts who have already made their first steps with the language and are looking for more detailed explanations.&lt;&#x2F;p&gt;
&lt;p&gt;Later in 2013, I published an ebook version (PDF, epub, Kindle) &lt;a href=&quot;https:&#x2F;&#x2F;leanpub.com&#x2F;theneophytesguidetoscala&quot;&gt;at Leanpub&lt;&#x2F;a&gt; using a pay-as-much-as-you-want price model.&lt;&#x2F;p&gt;
&lt;div class=&quot;button-holder&quot;&gt;
&lt;a class=&quot;button&quot; href=&quot;https:&#x2F;&#x2F;leanpub.com&#x2F;theneophytesguidetoscala&quot;&gt;Get ebook&lt;&#x2F;a&gt;
&lt;&#x2F;div&gt;
&lt;div style=&quot;clear:both;&quot;&gt;
&lt;p&gt;If you prefer an HTML version, here are the links to all its 16 parts:&lt;&#x2F;p&gt;
&lt;div style=&quot;clear:both;&quot;&gt;&lt;&#x2F;div&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;https:&#x2F;&#x2F;danielwestheide.com&#x2F;blog&#x2F;the-neophytes-guide-to-scala-part-1-extractors&#x2F;&quot;&gt;Part 1: Extractors&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
&lt;li&gt;&lt;a href=&quot;https:&#x2F;&#x2F;danielwestheide.com&#x2F;blog&#x2F;the-neophytes-guide-to-scala-part-2-extracting-sequences&#x2F;&quot;&gt;Part 2: Extracting Sequences&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
&lt;li&gt;&lt;a href=&quot;https:&#x2F;&#x2F;danielwestheide.com&#x2F;blog&#x2F;the-neophytes-guide-to-scala-part-3-patterns-everywhere&#x2F;&quot;&gt;Part 3: Patterns Everywhere&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
&lt;li&gt;&lt;a href=&quot;https:&#x2F;&#x2F;danielwestheide.com&#x2F;blog&#x2F;the-neophytes-guide-to-scala-part-4-pattern-matching-anonymous-functions&#x2F;&quot;&gt;Part 4: Pattern Matching Anonymous Functions&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
&lt;li&gt;&lt;a href=&quot;https:&#x2F;&#x2F;danielwestheide.com&#x2F;blog&#x2F;the-neophytes-guide-to-scala-part-5-the-option-type&#x2F;&quot;&gt;Part 5: The Option type&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
&lt;li&gt;&lt;a href=&quot;https:&#x2F;&#x2F;danielwestheide.com&#x2F;blog&#x2F;the-neophytes-guide-to-scala-part-6-error-handling-with-try&#x2F;&quot;&gt;Part 6: Error handling with Try&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
&lt;li&gt;&lt;a href=&quot;https:&#x2F;&#x2F;danielwestheide.com&#x2F;blog&#x2F;the-neophytes-guide-to-scala-part-7-the-either-type&#x2F;&quot;&gt;Part 7: The Either type&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
&lt;li&gt;&lt;a href=&quot;https:&#x2F;&#x2F;danielwestheide.com&#x2F;blog&#x2F;the-neophytes-guide-to-scala-part-8-welcome-to-the-future&#x2F;&quot;&gt;Part 8: Welcome to the Future&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
&lt;li&gt;&lt;a href=&quot;https:&#x2F;&#x2F;danielwestheide.com&#x2F;blog&#x2F;the-neophytes-guide-to-scala-part-9-promises-and-futures-in-practice&#x2F;&quot;&gt;Part 9: Promises and Futures in practice&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
&lt;li&gt;&lt;a href=&quot;https:&#x2F;&#x2F;danielwestheide.com&#x2F;blog&#x2F;the-neophytes-guide-to-scala-part-10-staying-dry-with-higher-order-functions&#x2F;&quot;&gt;Part 10: Staying DRY with higher-order functions&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
&lt;li&gt;&lt;a href=&quot;https:&#x2F;&#x2F;danielwestheide.com&#x2F;blog&#x2F;the-neophytes-guide-to-scala-part-11-currying-and-partially-applied-functions&#x2F;&quot;&gt;Part 11: Currying and partially applied functions&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
&lt;li&gt;&lt;a href=&quot;https:&#x2F;&#x2F;danielwestheide.com&#x2F;blog&#x2F;the-neophytes-guide-to-scala-part-12-type-classes&#x2F;&quot;&gt;Part 12: Type classes&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
&lt;li&gt;&lt;a href=&quot;https:&#x2F;&#x2F;danielwestheide.com&#x2F;blog&#x2F;the-neophytes-guide-to-scala-part-13-path-dependent-types&#x2F;&quot;&gt;Part 13: Path-dependent types&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
&lt;li&gt;&lt;a href=&quot;https:&#x2F;&#x2F;danielwestheide.com&#x2F;blog&#x2F;the-neophytes-guide-to-scala-part-14-the-actor-approach-to-concurrency&#x2F;&quot;&gt;Part 14: The Actor approach to concurrency&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
&lt;li&gt;&lt;a href=&quot;https:&#x2F;&#x2F;danielwestheide.com&#x2F;blog&#x2F;the-neophytes-guide-to-scala-part-15-dealing-with-failure-in-actor-systems&#x2F;&quot;&gt;Part 15: Dealing with Failure in Actor Systems&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
&lt;li&gt;&lt;a href=&quot;https:&#x2F;&#x2F;danielwestheide.com&#x2F;blog&#x2F;the-neophytes-guide-to-scala-part-16-where-to-go-from-here&#x2F;&quot;&gt;Part 16: Where to go from here&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
</description>
      </item>
      <item>
          <title>The Neophyte&#x27;s Guide to Scala Part 6: Error handling with Try</title>
          <pubDate>Wed, 26 Dec 2012 00:00:00 +0000</pubDate>
          <author>Unknown</author>
          <link>https://danielwestheide.com/blog/the-neophytes-guide-to-scala-part-6-error-handling-with-try/</link>
          <guid>https://danielwestheide.com/blog/the-neophytes-guide-to-scala-part-6-error-handling-with-try/</guid>
          <description xml:base="https://danielwestheide.com/blog/the-neophytes-guide-to-scala-part-6-error-handling-with-try/">&lt;p&gt;When just playing around with a new language, you might get away with simply ignoring the fact that something might go wrong. As soon you want to create anything serious, though, you can no longer run away from handling errors and exceptions in your code. The importance of how well a language supports you in doing so is often underestimated, for some reason or another.&lt;&#x2F;p&gt;
&lt;p&gt;Scala, as it turns out, is pretty well positioned when it comes to dealing with error conditions in an elegant way. In this article, I&#x27;m going to present Scala&#x27;s approach to dealing with errors, based on the &lt;code&gt;Try&lt;&#x2F;code&gt; type, and the rationale behind it. I&#x27;m using features introduced with Scala 2.10 and ported back to Scala 2.9.3, so make sure your Scala version in SBT is at 2.9.3 or later.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;throwing-and-catching-exceptions&quot;&gt;Throwing and catching exceptions&lt;&#x2F;h2&gt;
&lt;p&gt;Before going straight to Scala&#x27;s idiomatic approach at error handling, let&#x27;s first have a look at an approach that is more akin to how you are used to working with error conditions if you come from languages like Java or Ruby. Like these languages, Scala allows you to throw an exception:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;scala&quot; style=&quot;background-color:#002b36;color:#839496;&quot; class=&quot;language-scala &quot;&gt;&lt;code class=&quot;language-scala&quot; data-lang=&quot;scala&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;case class&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt; Customer&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;age&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;Int&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;class&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt; Cigarettes
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;case class&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt; UnderAgeException&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;message&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;String&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;extends &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;Exception&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;message&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;buyCigarettes&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;customer&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Customer&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Cigarettes &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;=
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;if &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;customer.age &amp;lt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;16&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;throw UnderAgeException&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;s&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Customer must be older than 16 but was ${&lt;&#x2F;span&gt;&lt;span&gt;customer.age&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;}&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;else new Cigarettes
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Thrown exceptions can be caught and dealt with very similarly to Java, albeit using a partial function to specify the exceptions we want to deal with. Also, Scala&#x27;s &lt;code&gt;try&lt;&#x2F;code&gt;&#x2F;&lt;code&gt;catch&lt;&#x2F;code&gt; is an expression, so the following code returns the message of the exception:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;scala&quot; style=&quot;background-color:#002b36;color:#839496;&quot; class=&quot;language-scala &quot;&gt;&lt;code class=&quot;language-scala&quot; data-lang=&quot;scala&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;val youngCustomer &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Customer&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;15&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;try &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;  buyCigarettes&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;youngCustomer&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;  &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Yo, here are your cancer sticks! Happy smokin&amp;#39;!&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;} &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;catch &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;case UnderAgeException&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;msg&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;=&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; msg
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h2 id=&quot;error-handling-the-functional-way&quot;&gt;Error handling, the functional way&lt;&#x2F;h2&gt;
&lt;p&gt;Now, having this kind of exception handling code all over your code base can become ugly very quickly and doesn&#x27;t really go well with functional programming. It&#x27;s also a rather bad solution for applications with a lot of concurrency. For instance, if you need to deal with an exception thrown by an Actor that is executed on some other thread, you obviously cannot do that by catching that exception – you will want a possibility to receive a message denoting the error condition.&lt;&#x2F;p&gt;
&lt;p&gt;Hence, in Scala, it&#x27;s usually preferred to signify that an error has occurred by returning an appropriate value from your function.&lt;&#x2F;p&gt;
&lt;p&gt;Don&#x27;t worry, we are not going back to C-style error handling, using error codes that we need to check for by convention. Rather, in Scala, we are using a specific type that represents computations that may result in an exception.&lt;&#x2F;p&gt;
&lt;p&gt;In this article, we are confining ourselves to the &lt;code&gt;Try&lt;&#x2F;code&gt; type that was introduced in Scala 2.10 and later backported to Scala 2.9.3. There is also a similar type, called &lt;code&gt;Either&lt;&#x2F;code&gt;, which, even after the introduction of &lt;code&gt;Try&lt;&#x2F;code&gt;, can still be very useful, but is more general.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;the-semantics-of-try&quot;&gt;The semantics of Try&lt;&#x2F;h3&gt;
&lt;p&gt;The semantics of &lt;code&gt;Try&lt;&#x2F;code&gt; are best explained by comparing them to those of the &lt;code&gt;Option&lt;&#x2F;code&gt; type that was the topic of &lt;a href=&quot;http:&#x2F;&#x2F;danielwestheide.com&#x2F;blog&#x2F;2012&#x2F;12&#x2F;19&#x2F;the-neophytes-guide-to-scala-part-5-the-option-type.html&quot;&gt;the previous part of this series&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;Where &lt;code&gt;Option[A]&lt;&#x2F;code&gt; is a container for a value of type &lt;code&gt;A&lt;&#x2F;code&gt; that may be present or not, &lt;code&gt;Try[A]&lt;&#x2F;code&gt; represents a computation that may result in a value of type &lt;code&gt;A&lt;&#x2F;code&gt;, if it is successful, or in some &lt;code&gt;Throwable&lt;&#x2F;code&gt; if something has gone wrong. Instances of such a container type for possible errors can  easily be passed around between concurrently executing parts of your application.&lt;&#x2F;p&gt;
&lt;p&gt;There are two different types of &lt;code&gt;Try&lt;&#x2F;code&gt;: If an instance of &lt;code&gt;Try[A]&lt;&#x2F;code&gt; represents a successful computation, it is an instance of &lt;code&gt;Success[A]&lt;&#x2F;code&gt;, simply wrapping a value of type &lt;code&gt;A&lt;&#x2F;code&gt;. If, on the other hand, it represents a computation in which an error has occurred, it is an instance of &lt;code&gt;Failure[A]&lt;&#x2F;code&gt;, wrapping a &lt;code&gt;Throwable&lt;&#x2F;code&gt;, i.e. an exception or other kind of error.&lt;&#x2F;p&gt;
&lt;p&gt;If we know that a computation may result in an error, we can simply use &lt;code&gt;Try[A]&lt;&#x2F;code&gt; as the return type of our function. This makes the possibility explicit and forces clients of our function to deal with the possibility of an error in some way.&lt;&#x2F;p&gt;
&lt;p&gt;For example, let&#x27;s assume we want to write a simple web page fetcher. The user will be able to enter the URL of the web page they want to fetch. One part of our application will be a function that parses the entered URL and creates a &lt;code&gt;java.net.URL&lt;&#x2F;code&gt; from it:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;scala&quot; style=&quot;background-color:#002b36;color:#839496;&quot; class=&quot;language-scala &quot;&gt;&lt;code class=&quot;language-scala&quot; data-lang=&quot;scala&quot;&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;import&lt;&#x2F;span&gt;&lt;span&gt; scala.util.Try
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;import&lt;&#x2F;span&gt;&lt;span&gt; java.net.URL
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;parseURL&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;url&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;String&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Try&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;URL&lt;&#x2F;span&gt;&lt;span&gt;] &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Try&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;new URL&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;url&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;))
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;As you can see, we return a value of type &lt;code&gt;Try[URL]&lt;&#x2F;code&gt;. If the given &lt;code&gt;url&lt;&#x2F;code&gt; is syntactically correct, this will be a &lt;code&gt;Success[URL]&lt;&#x2F;code&gt;. If the &lt;code&gt;URL&lt;&#x2F;code&gt; constructor throws a &lt;code&gt;MalformedURLException&lt;&#x2F;code&gt;, however, it will be a &lt;code&gt;Failure[URL]&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;To achieve this, we are using the &lt;code&gt;apply&lt;&#x2F;code&gt; factory method on the &lt;code&gt;Try&lt;&#x2F;code&gt; companion object. This method expects a by-name parameter of type &lt;code&gt;A&lt;&#x2F;code&gt; (here, &lt;code&gt;URL&lt;&#x2F;code&gt;). For our example, this means that the &lt;code&gt;new URL(url)&lt;&#x2F;code&gt; is executed inside the &lt;code&gt;apply&lt;&#x2F;code&gt; method of the &lt;code&gt;Try&lt;&#x2F;code&gt; object. Inside that method, non-fatal exceptions are caught, returning a &lt;code&gt;Failure&lt;&#x2F;code&gt; containing the respective exception.&lt;&#x2F;p&gt;
&lt;p&gt;Hence, &lt;code&gt;parseURL(&quot;http:&#x2F;&#x2F;danielwestheide.com&quot;)&lt;&#x2F;code&gt; will result in a &lt;code&gt;Success[URL]&lt;&#x2F;code&gt; containing the created URL, whereas &lt;code&gt;parseURL(&quot;garbage&quot;)&lt;&#x2F;code&gt; will result in a &lt;code&gt;Failure[URL]&lt;&#x2F;code&gt; containing a &lt;code&gt;MalformedURLException&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;working-with-try-values&quot;&gt;Working with Try values&lt;&#x2F;h3&gt;
&lt;p&gt;Working with &lt;code&gt;Try&lt;&#x2F;code&gt; instances is actually very similar to working with &lt;code&gt;Option&lt;&#x2F;code&gt; values, so you won&#x27;t see many surprises here.&lt;&#x2F;p&gt;
&lt;p&gt;You can check if a &lt;code&gt;Try&lt;&#x2F;code&gt; is a success by calling &lt;code&gt;isSuccess&lt;&#x2F;code&gt; on it and then conditionally retrieve the wrapped value by calling &lt;code&gt;get&lt;&#x2F;code&gt; on it. But believe me, there aren&#x27;t many situations where you will want to do that.&lt;&#x2F;p&gt;
&lt;p&gt;It&#x27;s also possible to use &lt;code&gt;getOrElse&lt;&#x2F;code&gt; to pass in a default value to be returned if the &lt;code&gt;Try&lt;&#x2F;code&gt; is a &lt;code&gt;Failure&lt;&#x2F;code&gt;:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;scala&quot; style=&quot;background-color:#002b36;color:#839496;&quot; class=&quot;language-scala &quot;&gt;&lt;code class=&quot;language-scala&quot; data-lang=&quot;scala&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;val url &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt; parseURL&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Console&lt;&#x2F;span&gt;&lt;span&gt;.readLine&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;URL: &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;))&lt;&#x2F;span&gt;&lt;span&gt; getOrElse &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;new URL&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;http:&#x2F;&#x2F;duckduckgo.com&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;If the URL given by the user is malformed, we use the URL of DuckDuckGo as a fallback.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;chaining-operations&quot;&gt;Chaining operations&lt;&#x2F;h3&gt;
&lt;p&gt;One of the most important characteristics of the &lt;code&gt;Try&lt;&#x2F;code&gt; type is that, like &lt;code&gt;Option&lt;&#x2F;code&gt;, it supports all the higher-order methods you know from other types of collections. As you will see in the examples to follow, this allows you to chain operations on &lt;code&gt;Try&lt;&#x2F;code&gt; values and catch any exceptions that might occur, and all that in a very readable manner.&lt;&#x2F;p&gt;
&lt;h4 id=&quot;mapping-and-flat-mapping&quot;&gt;Mapping and flat mapping&lt;&#x2F;h4&gt;
&lt;p&gt;Mapping a &lt;code&gt;Try[A]&lt;&#x2F;code&gt; that is a &lt;code&gt;Success[A]&lt;&#x2F;code&gt; to a &lt;code&gt;Try[B]&lt;&#x2F;code&gt; results in a &lt;code&gt;Success[B]&lt;&#x2F;code&gt;. If it&#x27;s a &lt;code&gt;Failure[A]&lt;&#x2F;code&gt;, the resulting &lt;code&gt;Try[B]&lt;&#x2F;code&gt; will be a &lt;code&gt;Failure[B]&lt;&#x2F;code&gt;, on the other hand, containing the same exception as the &lt;code&gt;Failure[A]&lt;&#x2F;code&gt;:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;scala&quot; style=&quot;background-color:#002b36;color:#839496;&quot; class=&quot;language-scala &quot;&gt;&lt;code class=&quot;language-scala&quot; data-lang=&quot;scala&quot;&gt;&lt;span&gt;parseURL&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;http:&#x2F;&#x2F;danielwestheide.com&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span&gt;.map&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d33682;&quot;&gt;_&lt;&#x2F;span&gt;&lt;span&gt;.getProtocol&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;) 
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;&#x2F;&#x2F; results in Success(&amp;quot;http&amp;quot;)
&lt;&#x2F;span&gt;&lt;span&gt;parseURL&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;garbage&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span&gt;.map&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d33682;&quot;&gt;_&lt;&#x2F;span&gt;&lt;span&gt;.getProtocol&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;&#x2F;&#x2F; results in Failure(java.net.MalformedURLException: no protocol: garbage)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;If you chain multiple &lt;code&gt;map&lt;&#x2F;code&gt; operations, this will result in a nested &lt;code&gt;Try&lt;&#x2F;code&gt; structure, which is usually not what you want. Consider this method that returns an input stream for a given URL:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;scala&quot; style=&quot;background-color:#002b36;color:#839496;&quot; class=&quot;language-scala &quot;&gt;&lt;code class=&quot;language-scala&quot; data-lang=&quot;scala&quot;&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;import&lt;&#x2F;span&gt;&lt;span&gt; java.io.InputStream
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;inputStreamForURL&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;url&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;String&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Try&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Try&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Try&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;InputStream&lt;&#x2F;span&gt;&lt;span&gt;]]] &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt; parseURL&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;url&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span&gt;.map &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{ &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;u =&amp;gt; 
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Try&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;u.openConnection&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;())&lt;&#x2F;span&gt;&lt;span&gt;.map&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;conn =&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Try&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;conn.getInputStream&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;))
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Since the anonymous functions passed to the two &lt;code&gt;map&lt;&#x2F;code&gt; calls each return a &lt;code&gt;Try&lt;&#x2F;code&gt;, the return type is a &lt;code&gt;Try[Try[Try[InputStream]]]&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;This is where the fact that you can &lt;code&gt;flatMap&lt;&#x2F;code&gt; a &lt;code&gt;Try&lt;&#x2F;code&gt; comes in handy. The &lt;code&gt;flatMap&lt;&#x2F;code&gt; method on a &lt;code&gt;Try[A]&lt;&#x2F;code&gt; expects to be passed a function that receives an &lt;code&gt;A&lt;&#x2F;code&gt; and returns a &lt;code&gt;Try[B]&lt;&#x2F;code&gt;. If our &lt;code&gt;Try[A]&lt;&#x2F;code&gt; instance is already a &lt;code&gt;Failure[A]&lt;&#x2F;code&gt;, that failure is returned as a &lt;code&gt;Failure[B]&lt;&#x2F;code&gt;, simply passing along the wrapped exception along the chain. If our &lt;code&gt;Try[A]&lt;&#x2F;code&gt; is a &lt;code&gt;Success[A]&lt;&#x2F;code&gt;, &lt;code&gt;flatMap&lt;&#x2F;code&gt; unpacks the &lt;code&gt;A&lt;&#x2F;code&gt; value in it and maps it to a &lt;code&gt;Try[B]&lt;&#x2F;code&gt; by passing this value to the mapping function.&lt;&#x2F;p&gt;
&lt;p&gt;This means that we can basically create a pipeline of operations that require the values carried over in &lt;code&gt;Success&lt;&#x2F;code&gt; instances by chaining an arbitrary number of &lt;code&gt;flatMap&lt;&#x2F;code&gt; calls. Any exceptions that happen along the way are wrapped in a &lt;code&gt;Failure&lt;&#x2F;code&gt;, which means that the end result of the chain of operations is a &lt;code&gt;Failure&lt;&#x2F;code&gt;, too.&lt;&#x2F;p&gt;
&lt;p&gt;Let&#x27;s rewrite the &lt;code&gt;inputStreamForURL&lt;&#x2F;code&gt; method from the previous example, this time resorting to &lt;code&gt;flatMap&lt;&#x2F;code&gt;:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;scala&quot; style=&quot;background-color:#002b36;color:#839496;&quot; class=&quot;language-scala &quot;&gt;&lt;code class=&quot;language-scala&quot; data-lang=&quot;scala&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;inputStreamForURL&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;url&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;String&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Try&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;InputStream&lt;&#x2F;span&gt;&lt;span&gt;] &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt; parseURL&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;url&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span&gt;.flatMap &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{ &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;u =&amp;gt; 
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Try&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;u.openConnection&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;())&lt;&#x2F;span&gt;&lt;span&gt;.flatMap&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;conn =&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Try&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;conn.getInputStream&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;))
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Now we get a &lt;code&gt;Try[InputStream]&lt;&#x2F;code&gt;, which can be a &lt;code&gt;Failure&lt;&#x2F;code&gt; wrapping an exception from any of the stages in which one may be thrown, or a &lt;code&gt;Success&lt;&#x2F;code&gt; that directly wraps the &lt;code&gt;InputStream&lt;&#x2F;code&gt;, the final result of our chain of operations.&lt;&#x2F;p&gt;
&lt;h4 id=&quot;filter-and-foreach&quot;&gt;Filter and foreach&lt;&#x2F;h4&gt;
&lt;p&gt;Of course, you can also filter a &lt;code&gt;Try&lt;&#x2F;code&gt; or call &lt;code&gt;foreach&lt;&#x2F;code&gt; on it. Both work exactly as you would expect after having learned about &lt;code&gt;Option&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;The &lt;code&gt;filter&lt;&#x2F;code&gt; method returns a &lt;code&gt;Failure&lt;&#x2F;code&gt; if the &lt;code&gt;Try&lt;&#x2F;code&gt; on which it is called is already a &lt;code&gt;Failure&lt;&#x2F;code&gt; or if the predicate passed to it returns &lt;code&gt;false&lt;&#x2F;code&gt; (in which case the wrapped exception is a &lt;code&gt;NoSuchElementException&lt;&#x2F;code&gt;). If the &lt;code&gt;Try&lt;&#x2F;code&gt; on which it is called is a &lt;code&gt;Success&lt;&#x2F;code&gt; and the predicate returns &lt;code&gt;true&lt;&#x2F;code&gt;, that &lt;code&gt;Succcess&lt;&#x2F;code&gt; instance is returned unchanged:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;scala&quot; style=&quot;background-color:#002b36;color:#839496;&quot; class=&quot;language-scala &quot;&gt;&lt;code class=&quot;language-scala&quot; data-lang=&quot;scala&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;parseHttpURL&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;url&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;String&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;) =&lt;&#x2F;span&gt;&lt;span&gt; parseURL&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;url&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span&gt;.filter&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d33682;&quot;&gt;_&lt;&#x2F;span&gt;&lt;span&gt;.getProtocol &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;== &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;http&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;parseHttpURL&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;http:&#x2F;&#x2F;apache.openmirror.de&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;&#x2F;&#x2F; results in a Success[URL]
&lt;&#x2F;span&gt;&lt;span&gt;parseHttpURL&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;ftp:&#x2F;&#x2F;mirror.netcologne.de&#x2F;apache.org&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;&#x2F;&#x2F; results in a Failure[URL]
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The function passed to &lt;code&gt;foreach&lt;&#x2F;code&gt; is executed only if the &lt;code&gt;Try&lt;&#x2F;code&gt; is a &lt;code&gt;Success&lt;&#x2F;code&gt;, which allows you to execute a side-effect. The function passed to &lt;code&gt;foreach&lt;&#x2F;code&gt; is executed exactly once in that case, being passed the value wrapped by the &lt;code&gt;Success&lt;&#x2F;code&gt;:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;scala&quot; style=&quot;background-color:#002b36;color:#839496;&quot; class=&quot;language-scala &quot;&gt;&lt;code class=&quot;language-scala&quot; data-lang=&quot;scala&quot;&gt;&lt;span&gt;parseHttpURL&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;http:&#x2F;&#x2F;danielwestheide.com&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span&gt;.foreach&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;println&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h4 id=&quot;for-comprehensions&quot;&gt;For comprehensions&lt;&#x2F;h4&gt;
&lt;p&gt;The support for &lt;code&gt;flatMap&lt;&#x2F;code&gt;, &lt;code&gt;map&lt;&#x2F;code&gt; and &lt;code&gt;filter&lt;&#x2F;code&gt; means that you can also use for comprehensions in order to chain operations on &lt;code&gt;Try&lt;&#x2F;code&gt; instances. Usually, this results in more readable code. To demonstrate this, let&#x27;s implement a method that returns the content of a web page with a given URL using for comprehensions.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;scala&quot; style=&quot;background-color:#002b36;color:#839496;&quot; class=&quot;language-scala &quot;&gt;&lt;code class=&quot;language-scala&quot; data-lang=&quot;scala&quot;&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;import&lt;&#x2F;span&gt;&lt;span&gt; scala.io.Source
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;getURLContent&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;url&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;String&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Try&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Iterator&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;String&lt;&#x2F;span&gt;&lt;span&gt;]] &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;=
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;for &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;url &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;&amp;lt;-&lt;&#x2F;span&gt;&lt;span&gt; parseURL&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;url&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;connection &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;&amp;lt;- &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Try&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;url.openConnection&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;())
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;is &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;&amp;lt;- &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Try&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;connection.getInputStream&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;source &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Source&lt;&#x2F;span&gt;&lt;span&gt;.fromInputStream&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;is&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;} &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;yield&lt;&#x2F;span&gt;&lt;span&gt; source.getLines&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;()
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;There are three places where things can go wrong, all of them covered by usage of the &lt;code&gt;Try&lt;&#x2F;code&gt; type. First, the already implemented &lt;code&gt;parseURL&lt;&#x2F;code&gt; method returns a &lt;code&gt;Try[URL]&lt;&#x2F;code&gt;. Only if this is a &lt;code&gt;Success[URL]&lt;&#x2F;code&gt;, we will try to open a connection and create a new input stream from it. If opening the connection and creating the input stream succeeds, we continue, finally yielding the lines of the web page. Since we effectively chain multiple &lt;code&gt;flatMap&lt;&#x2F;code&gt; calls in this for comprehension, the result type is a flat &lt;code&gt;Try[Iterator[String]]&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;Please note that this could be simplified using &lt;code&gt;Source#fromURL&lt;&#x2F;code&gt; and that we fail to close our input stream at the end, both of which are due to my decision to keep the example focussed on getting across the subject matter at hand.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;pattern-matching&quot;&gt;Pattern Matching&lt;&#x2F;h3&gt;
&lt;p&gt;At some point in your code, you will often want to know whether a &lt;code&gt;Try&lt;&#x2F;code&gt; instance you have received as the result of some computation represents a success or not and execute different code branches depending on the result. Usually, this is where you will make use of pattern matching. This is easily possible because both &lt;code&gt;Success&lt;&#x2F;code&gt; and &lt;code&gt;Failure&lt;&#x2F;code&gt; are case classes.&lt;&#x2F;p&gt;
&lt;p&gt;We want to render the requested page if it could be retrieved, or print an error message if that was not possible:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;scala&quot; style=&quot;background-color:#002b36;color:#839496;&quot; class=&quot;language-scala &quot;&gt;&lt;code class=&quot;language-scala&quot; data-lang=&quot;scala&quot;&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;import&lt;&#x2F;span&gt;&lt;span&gt; scala.util.Success
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;import&lt;&#x2F;span&gt;&lt;span&gt; scala.util.Failure
&lt;&#x2F;span&gt;&lt;span&gt;getURLContent&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;http:&#x2F;&#x2F;danielwestheide.com&#x2F;foobar&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;match &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;case Success&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;lines&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;=&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; lines.foreach&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;println&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;case Failure&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;ex&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;=&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; println&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;s&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Problem rendering URL content: ${&lt;&#x2F;span&gt;&lt;span&gt;ex.getMessage&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;}&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h3 id=&quot;recovering-from-a-failure&quot;&gt;Recovering from a Failure&lt;&#x2F;h3&gt;
&lt;p&gt;If you want to establish some kind of default behaviour in the case of a &lt;code&gt;Failure&lt;&#x2F;code&gt;, you don&#x27;t have to use &lt;code&gt;getOrElse&lt;&#x2F;code&gt;. An alternative is &lt;code&gt;recover&lt;&#x2F;code&gt;, which expects a partial function and returns another &lt;code&gt;Try&lt;&#x2F;code&gt;. If &lt;code&gt;recover&lt;&#x2F;code&gt; is called on a &lt;code&gt;Success&lt;&#x2F;code&gt; instance, that instance is returned as is. Otherwise, if the partial function is defined for the given &lt;code&gt;Failure&lt;&#x2F;code&gt; instance, its result is returned as a &lt;code&gt;Success&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;Let&#x27;s put this to use in order to print a different message depending on the type of the wrapped exception:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;scala&quot; style=&quot;background-color:#002b36;color:#839496;&quot; class=&quot;language-scala &quot;&gt;&lt;code class=&quot;language-scala&quot; data-lang=&quot;scala&quot;&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;import&lt;&#x2F;span&gt;&lt;span&gt; java.net.MalformedURLException
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;import&lt;&#x2F;span&gt;&lt;span&gt; java.io.FileNotFoundException
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;val content &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt; getURLContent&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;garbage&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span&gt; recover &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;case &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;e&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;FileNotFoundException &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;=&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Iterator&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Requested page does not exist&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;case &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;e&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;MalformedURLException &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;=&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Iterator&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Please make sure to enter a valid URL&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;case &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d33682;&quot;&gt;_ &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;=&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Iterator&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;An unexpected error has occurred. We are so sorry!&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;We could now safely &lt;code&gt;get&lt;&#x2F;code&gt; the wrapped value on the &lt;code&gt;Try[Iterator[String]]&lt;&#x2F;code&gt; that we assigned to &lt;code&gt;content&lt;&#x2F;code&gt;, because we know that it must be a &lt;code&gt;Success&lt;&#x2F;code&gt;. Calling &lt;code&gt;content.get.foreach(println)&lt;&#x2F;code&gt; would result in &lt;code&gt;Please make sure to enter a valid URL&lt;&#x2F;code&gt; being printed to the console.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;conclusion&quot;&gt;Conclusion&lt;&#x2F;h2&gt;
&lt;p&gt;Idiomatic error handling in Scala is quite different from the paradigm known from languages like Java or Ruby. The &lt;code&gt;Try&lt;&#x2F;code&gt; type allows you to encapsulate computations that result in errors in a container and to chain operations on the computed values in a very elegant way. You can transfer what you know from working with collections and with &lt;code&gt;Option&lt;&#x2F;code&gt; values to how you deal with code that may result in errors – all in a uniform way.&lt;&#x2F;p&gt;
&lt;p&gt;To keep this article at a reasonable length, I haven&#x27;t explained all of the methods available on &lt;code&gt;Try&lt;&#x2F;code&gt;. Like &lt;code&gt;Option&lt;&#x2F;code&gt;, &lt;code&gt;Try&lt;&#x2F;code&gt; supports the &lt;code&gt;orElse&lt;&#x2F;code&gt; method. The &lt;code&gt;transform&lt;&#x2F;code&gt; and &lt;code&gt;recoverWith&lt;&#x2F;code&gt; methods are also worth having a look at, and I encourage you to do so.&lt;&#x2F;p&gt;
&lt;p&gt;In the next part we are going to deal with &lt;code&gt;Either&lt;&#x2F;code&gt;, an alternative type for representing computations that may result in errors, but with a wider scope of application that goes beyond error handling.&lt;&#x2F;p&gt;
</description>
      </item>
      <item>
          <title>The Neophyte&#x27;s Guide to Scala Part 5: The Option type</title>
          <pubDate>Wed, 19 Dec 2012 00:00:00 +0000</pubDate>
          <author>Unknown</author>
          <link>https://danielwestheide.com/blog/the-neophytes-guide-to-scala-part-5-the-option-type/</link>
          <guid>https://danielwestheide.com/blog/the-neophytes-guide-to-scala-part-5-the-option-type/</guid>
          <description xml:base="https://danielwestheide.com/blog/the-neophytes-guide-to-scala-part-5-the-option-type/">&lt;p&gt;For the last couple of weeks, we have pressed ahead and covered a lot of ground concerning some rather advanced techniques, particularly ones related to pattern matching and extractors. Time to shift down a gear and look at one of the more fundamental idiosyncrasies of Scala: the &lt;code&gt;Option&lt;&#x2F;code&gt; type.&lt;&#x2F;p&gt;
&lt;p&gt;If you have participated in the Scala course at Coursera, you have already received a brief introduction to this type and seen it in use in the &lt;code&gt;Map&lt;&#x2F;code&gt; API. In this series, we have also used it &lt;a href=&quot;http:&#x2F;&#x2F;danielwestheide.com&#x2F;blog&#x2F;2012&#x2F;11&#x2F;21&#x2F;the-neophytes-guide-to-scala-part-1-extractors.html&quot;&gt;when implementing our own extractors&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;And yet, there is still a lot left to be explained about it. You may have wondered what all the fuss is about, what is so much better about options than other ways of dealing with absent values. You might also be at a loss how to actually work with the &lt;code&gt;Option&lt;&#x2F;code&gt; type in your own code. The goal of this part of the series is to do away with all these question marks and teach you all you really need to know about &lt;code&gt;Option&lt;&#x2F;code&gt; as an aspiring Scala novice.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;the-basic-idea&quot;&gt;The basic idea&lt;&#x2F;h2&gt;
&lt;p&gt;If you have worked with Java at all in the past, it is very likely that you have come across a &lt;code&gt;NullPointerException&lt;&#x2F;code&gt; at some time (other languages will throw similarly named errors in such a case). Usually this happens because some method returns &lt;code&gt;null&lt;&#x2F;code&gt; when you were not expecting it and thus not dealing with that possibility in your client code. A value of &lt;code&gt;null&lt;&#x2F;code&gt; is often abused to represent an absent optional value.&lt;&#x2F;p&gt;
&lt;p&gt;Some languages treat &lt;code&gt;null&lt;&#x2F;code&gt; values in a special way or allow you to work safely with values that might be &lt;code&gt;null&lt;&#x2F;code&gt;. For instance, Groovy has the null-safe operator for accessing properties, so that &lt;code&gt;foo?.bar?.baz&lt;&#x2F;code&gt; will not throw an exception if either &lt;code&gt;foo&lt;&#x2F;code&gt; or its &lt;code&gt;bar&lt;&#x2F;code&gt; property is &lt;code&gt;null&lt;&#x2F;code&gt;, instead directly returning &lt;code&gt;null&lt;&#x2F;code&gt;. However, you are screwed if you forget to use this operator, and nothing forces you to do so.&lt;&#x2F;p&gt;
&lt;p&gt;Clojure basically treats its &lt;code&gt;nil&lt;&#x2F;code&gt; value like an empty thing, i.e. like an empty list if accessed like a list, or like an empty map if accessed like a map. This means that the &lt;code&gt;nil&lt;&#x2F;code&gt; value is bubbling up the call hierarchy. Very often this is okay, but sometimes this just leads to an exception much higher in the call hierchary, where some piece of code isn&#x27;t that nil-friendly after all.&lt;&#x2F;p&gt;
&lt;p&gt;Scala tries to solve the problem by getting rid of &lt;code&gt;null&lt;&#x2F;code&gt; values altogether and providing its own type for representing optional values, i.e. values that may be present or not: the &lt;code&gt;Option[A]&lt;&#x2F;code&gt; trait.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;code&gt;Option[A]&lt;&#x2F;code&gt; is a container for an optional value of type &lt;code&gt;A&lt;&#x2F;code&gt;. If the value of type &lt;code&gt;A&lt;&#x2F;code&gt; is present, the &lt;code&gt;Option[A]&lt;&#x2F;code&gt; is an instance of &lt;code&gt;Some[A]&lt;&#x2F;code&gt;, containing the present value of type &lt;code&gt;A.&lt;&#x2F;code&gt; If the value is absent, the &lt;code&gt;Option[A]&lt;&#x2F;code&gt; is the object &lt;code&gt;None&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;By stating that a value may or may not be present &lt;em&gt;on the type level&lt;&#x2F;em&gt;, you and any other developers who work with your code are forced by the compiler to deal with this possibility. There is no way you may accidentally rely on the presence of a value that is really optional.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;code&gt;Option&lt;&#x2F;code&gt; is mandatory! Do not use &lt;code&gt;null&lt;&#x2F;code&gt; to denote that an optional value is absent.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;creating-an-option&quot;&gt;Creating an option&lt;&#x2F;h2&gt;
&lt;p&gt;Usually, you can simply create an &lt;code&gt;Option[A]&lt;&#x2F;code&gt; for a present value by directly instantiating the &lt;code&gt;Some&lt;&#x2F;code&gt; case class:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;scala&quot; style=&quot;background-color:#002b36;color:#839496;&quot; class=&quot;language-scala &quot;&gt;&lt;code class=&quot;language-scala&quot; data-lang=&quot;scala&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;val greeting&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Option&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;String&lt;&#x2F;span&gt;&lt;span&gt;] &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Some&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Hello world&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Or, if you know that the value is absent, you simply assign or return the &lt;code&gt;None&lt;&#x2F;code&gt; object:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;scala&quot; style=&quot;background-color:#002b36;color:#839496;&quot; class=&quot;language-scala &quot;&gt;&lt;code class=&quot;language-scala&quot; data-lang=&quot;scala&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;val greeting&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Option&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;String&lt;&#x2F;span&gt;&lt;span&gt;] &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;None
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;However, time and again you will need to interoperate with Java libraries or code in other JVM languages that happily make use of &lt;code&gt;null&lt;&#x2F;code&gt; to denote absent values. For this reason, the &lt;code&gt;Option&lt;&#x2F;code&gt; companion object provides a factory method that creates &lt;code&gt;None&lt;&#x2F;code&gt; if the given parameter is &lt;code&gt;null&lt;&#x2F;code&gt;, otherwise the parameter wrapped in a &lt;code&gt;Some&lt;&#x2F;code&gt;:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;scala&quot; style=&quot;background-color:#002b36;color:#839496;&quot; class=&quot;language-scala &quot;&gt;&lt;code class=&quot;language-scala&quot; data-lang=&quot;scala&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;val absentGreeting&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Option&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;String&lt;&#x2F;span&gt;&lt;span&gt;] &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Option&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;null&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;&#x2F;&#x2F; absentGreeting will be None
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;val presentGreeting&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Option&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;String&lt;&#x2F;span&gt;&lt;span&gt;] &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Option&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Hello!&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;&#x2F;&#x2F; presentGreeting will be Some(&amp;quot;Hello!&amp;quot;)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h2 id=&quot;working-with-optional-values&quot;&gt;Working with optional values&lt;&#x2F;h2&gt;
&lt;p&gt;This is all pretty neat, but how do you actually work with optional values? It&#x27;s time for an example. Let&#x27;s do something boring, so we can focus on the important stuff.&lt;&#x2F;p&gt;
&lt;p&gt;Imagine you are working for one of those hipsterrific startups, and one of the first things you need to implement is a repository of users. We need to be able to find a user by their unique id. Sometimes, requests come in with bogus ids. This calls for a return type of &lt;code&gt;Option[User]&lt;&#x2F;code&gt; for our finder method. A dummy implementation of our user repository might look like this:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;scala&quot; style=&quot;background-color:#002b36;color:#839496;&quot; class=&quot;language-scala &quot;&gt;&lt;code class=&quot;language-scala&quot; data-lang=&quot;scala&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;case class&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt; User&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;id&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;Int&lt;&#x2F;span&gt;&lt;span&gt;, 
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;firstName&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;String&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;lastName&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;String&lt;&#x2F;span&gt;&lt;span&gt;, 
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;age&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;Int&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;gender&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Option&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;String&lt;&#x2F;span&gt;&lt;span&gt;]&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;object&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt; UserRepository &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#93a1a1;&quot;&gt;private &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;val users &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Map&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt; -&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;User&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;, &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;John&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Doe&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;32&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Some&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;male&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;))&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;                          &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;2&lt;&#x2F;span&gt;&lt;span&gt; -&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;User&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;2&lt;&#x2F;span&gt;&lt;span&gt;, &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Johanna&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Doe&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;30&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;None&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;))
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;findById&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;id&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;Int&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Option&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;User&lt;&#x2F;span&gt;&lt;span&gt;] &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt; users.get&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;id&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;findAll &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt; users.values
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Now, if you received an instance of &lt;code&gt;Option[User]&lt;&#x2F;code&gt; from the &lt;code&gt;UserRepository&lt;&#x2F;code&gt; and need to do something with it, how do you do that?&lt;&#x2F;p&gt;
&lt;p&gt;One way would be to check if a value is present by means of the &lt;code&gt;isDefined&lt;&#x2F;code&gt; method of your option, and, if that is the case, get that value via its &lt;code&gt;get&lt;&#x2F;code&gt; method:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;scala&quot; style=&quot;background-color:#002b36;color:#839496;&quot; class=&quot;language-scala &quot;&gt;&lt;code class=&quot;language-scala&quot; data-lang=&quot;scala&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;val user1 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;UserRepository&lt;&#x2F;span&gt;&lt;span&gt;.findById&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;if &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;user1.isDefined&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;) {
&lt;&#x2F;span&gt;&lt;span&gt;	println&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;user1.get.firstName&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;} &lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;&#x2F;&#x2F; will print &amp;quot;John&amp;quot;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This is very similar to how the &lt;code&gt;Optional&lt;&#x2F;code&gt; type in the Guava library is used in Java. If you think this is clunky and expect something more elegant from Scala, you&#x27;re on the right track. More importantly, if you use &lt;code&gt;get&lt;&#x2F;code&gt;, you might forget about checking with &lt;code&gt;isDefined&lt;&#x2F;code&gt; before, leading to an exception at runtime, so you haven&#x27;t gained a lot over using &lt;code&gt;null&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;You should stay away from this way of accessing options whenever possible!&lt;&#x2F;p&gt;
&lt;h2 id=&quot;providing-a-default-value&quot;&gt;Providing a default value&lt;&#x2F;h2&gt;
&lt;p&gt;Very often, you want to work with a fallback or default value in case an optional value is absent. This use case is covered pretty well by the &lt;code&gt;getOrElse&lt;&#x2F;code&gt; method defined on &lt;code&gt;Option&lt;&#x2F;code&gt;:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;scala&quot; style=&quot;background-color:#002b36;color:#839496;&quot; class=&quot;language-scala &quot;&gt;&lt;code class=&quot;language-scala&quot; data-lang=&quot;scala&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;val user &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;User&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;2&lt;&#x2F;span&gt;&lt;span&gt;, &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Johanna&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Doe&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;30&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;None&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;println&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Gender: &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; + user.gender.getOrElse&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;not specified&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;&#x2F;&#x2F; will print &amp;quot;not specified&amp;quot;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Please note that the default value you can specify as a parameter to the &lt;code&gt;getOrElse&lt;&#x2F;code&gt; method is a by-name parameter, which means that it is only evaluated if the option on which you invoke &lt;code&gt;getOrElse&lt;&#x2F;code&gt; is indeed &lt;code&gt;None&lt;&#x2F;code&gt;. Hence, there is no need to worry if creating the default value is costly for some reason or another – this will only happen if the default value is actually required.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;pattern-matching&quot;&gt;Pattern matching&lt;&#x2F;h2&gt;
&lt;p&gt;&lt;code&gt;Some&lt;&#x2F;code&gt; is a case class, so it is perfectly possible to use it in a pattern, be it in a regular pattern matching expression or in some other place where patterns are allowed. Let&#x27;s rewrite the example above using pattern matching:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;scala&quot; style=&quot;background-color:#002b36;color:#839496;&quot; class=&quot;language-scala &quot;&gt;&lt;code class=&quot;language-scala&quot; data-lang=&quot;scala&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;val user &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;User&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;2&lt;&#x2F;span&gt;&lt;span&gt;, &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Johanna&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Doe&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;30&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;None&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;user.gender &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;match &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;case Some&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;gender&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;=&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; println&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Gender: &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; + gender&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;case None &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;=&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; println&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Gender: not specified&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Or, if you want to remove the duplicated &lt;code&gt;println&lt;&#x2F;code&gt; statement and make use of the fact that you are working with a pattern matching &lt;em&gt;expression&lt;&#x2F;em&gt;:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;scala&quot; style=&quot;background-color:#002b36;color:#839496;&quot; class=&quot;language-scala &quot;&gt;&lt;code class=&quot;language-scala&quot; data-lang=&quot;scala&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;val user &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;User&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;2&lt;&#x2F;span&gt;&lt;span&gt;, &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Johanna&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Doe&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;30&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;None&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;val gender &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt; user.gender &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;match &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;case Some&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;gender&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;=&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; gender
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;case None &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;=&amp;gt; &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;not specified&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;}
&lt;&#x2F;span&gt;&lt;span&gt;println&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Gender: &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; + gender&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;You will hopefully have noticed that pattern matching on an &lt;code&gt;Option&lt;&#x2F;code&gt; instance is rather verbose, which is also why it is usually not idiomatic to process options this way. So, even if you are all excited about pattern matching, try to use the alternatives when working with options.&lt;&#x2F;p&gt;
&lt;p&gt;There is one quite elegant way of using patterns with options, which you will learn about in the section on for comprehensions, below.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;options-can-be-viewed-as-collections&quot;&gt;Options can be viewed as collections&lt;&#x2F;h2&gt;
&lt;p&gt;So far you haven&#x27;t seen a lot of elegant or idiomatic ways of working with options. We are coming to that now.&lt;&#x2F;p&gt;
&lt;p&gt;I already mentioned that &lt;code&gt;Option[A]&lt;&#x2F;code&gt; is a container for a value of type &lt;code&gt;A&lt;&#x2F;code&gt;. More precisely, you may think of it as some kind of collection — some special snowflake of a collection that contains either zero elements or exactly one element of type &lt;code&gt;A&lt;&#x2F;code&gt;. This is a very powerful idea!&lt;&#x2F;p&gt;
&lt;p&gt;Even though on the type level, &lt;code&gt;Option&lt;&#x2F;code&gt; is not a collection type in Scala, options come with all the goodness you have come to appreciate about Scala collections like &lt;code&gt;List&lt;&#x2F;code&gt;, &lt;code&gt;Set&lt;&#x2F;code&gt; etc — and if you really need to, you can even transform an option into a &lt;code&gt;List&lt;&#x2F;code&gt;, for instance.&lt;&#x2F;p&gt;
&lt;p&gt;So what does this allow you to do?&lt;&#x2F;p&gt;
&lt;h3 id=&quot;performing-a-side-effect-if-a-value-is-present&quot;&gt;Performing a side-effect if a value is present&lt;&#x2F;h3&gt;
&lt;p&gt;If you need to perform some side-effect only if a specific optional value is present, the &lt;code&gt;foreach&lt;&#x2F;code&gt; method you know from Scala&#x27;s collections comes in handy:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;scala&quot; style=&quot;background-color:#002b36;color:#839496;&quot; class=&quot;language-scala &quot;&gt;&lt;code class=&quot;language-scala&quot; data-lang=&quot;scala&quot;&gt;&lt;span style=&quot;color:#859900;&quot;&gt;UserRepository&lt;&#x2F;span&gt;&lt;span&gt;.findById&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;2&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span&gt;.foreach&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;user =&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; println&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;user.firstName&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;&#x2F;&#x2F; prints &amp;quot;Johanna&amp;quot;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The function passed to &lt;code&gt;foreach&lt;&#x2F;code&gt; will be called exactly once, if the &lt;code&gt;Option&lt;&#x2F;code&gt; is a &lt;code&gt;Some&lt;&#x2F;code&gt;, or never, if it is &lt;code&gt;None&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;mapping-an-option&quot;&gt;Mapping an option&lt;&#x2F;h3&gt;
&lt;p&gt;The really good thing about options behaving like a collection is that you can work with them in a very functional way, and the way you do that is exactly the same as for lists, sets etc.&lt;&#x2F;p&gt;
&lt;p&gt;Just as you can map a &lt;code&gt;List[A]&lt;&#x2F;code&gt; to a &lt;code&gt;List[B]&lt;&#x2F;code&gt;, you can map an &lt;code&gt;Option[A]&lt;&#x2F;code&gt; to an &lt;code&gt;Option[B]&lt;&#x2F;code&gt;. This means that if your instance of &lt;code&gt;Option[A]&lt;&#x2F;code&gt; is defined, i.e. it is &lt;code&gt;Some[A]&lt;&#x2F;code&gt;, the result is &lt;code&gt;Some[B]&lt;&#x2F;code&gt;, otherwise it is &lt;code&gt;None&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;If you compare &lt;code&gt;Option&lt;&#x2F;code&gt; to &lt;code&gt;List&lt;&#x2F;code&gt;, &lt;code&gt;None&lt;&#x2F;code&gt; is the equivalent of an empty list: when you map an empty &lt;code&gt;List[A]&lt;&#x2F;code&gt;, you get an empty &lt;code&gt;List[B]&lt;&#x2F;code&gt;, and when you map an &lt;code&gt;Option[A]&lt;&#x2F;code&gt; that is &lt;code&gt;None&lt;&#x2F;code&gt;, you get an &lt;code&gt;Option[B]&lt;&#x2F;code&gt; that is &lt;code&gt;None&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;Let&#x27;s get the age of an optional user:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;scala&quot; style=&quot;background-color:#002b36;color:#839496;&quot; class=&quot;language-scala &quot;&gt;&lt;code class=&quot;language-scala&quot; data-lang=&quot;scala&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;val age &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;UserRepository&lt;&#x2F;span&gt;&lt;span&gt;.findById&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span&gt;.map&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d33682;&quot;&gt;_&lt;&#x2F;span&gt;&lt;span&gt;.age&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;&#x2F;&#x2F; age is Some(32)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h3 id=&quot;flatmap-and-options&quot;&gt;flatMap and options&lt;&#x2F;h3&gt;
&lt;p&gt;Let&#x27;s do the same for the gender:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;scala&quot; style=&quot;background-color:#002b36;color:#839496;&quot; class=&quot;language-scala &quot;&gt;&lt;code class=&quot;language-scala&quot; data-lang=&quot;scala&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;val gender &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;UserRepository&lt;&#x2F;span&gt;&lt;span&gt;.findById&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span&gt;.map&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d33682;&quot;&gt;_&lt;&#x2F;span&gt;&lt;span&gt;.gender&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;&#x2F;&#x2F; gender is an Option[Option[String]]
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The type of the resulting &lt;code&gt;gender&lt;&#x2F;code&gt; is &lt;code&gt;Option[Option[String]]&lt;&#x2F;code&gt;. Why is that?&lt;&#x2F;p&gt;
&lt;p&gt;Think of it like this: You have an &lt;code&gt;Option&lt;&#x2F;code&gt; container for a &lt;code&gt;User&lt;&#x2F;code&gt;, and &lt;em&gt;inside&lt;&#x2F;em&gt; that container you are mapping the &lt;code&gt;User&lt;&#x2F;code&gt; instance to an &lt;code&gt;Option[String]&lt;&#x2F;code&gt;, since that is the type of the &lt;code&gt;gender&lt;&#x2F;code&gt; property on our &lt;code&gt;User&lt;&#x2F;code&gt; class.&lt;&#x2F;p&gt;
&lt;p&gt;These nested options are a nuisance? Why, no problem, like all collections, &lt;code&gt;Option&lt;&#x2F;code&gt; also provides a &lt;code&gt;flatMap&lt;&#x2F;code&gt; method. Just like you can &lt;code&gt;flatMap&lt;&#x2F;code&gt; a &lt;code&gt;List[List[A]]&lt;&#x2F;code&gt; to a &lt;code&gt;List[B]&lt;&#x2F;code&gt;, you can do the same for an &lt;code&gt;Option[Option[A]]&lt;&#x2F;code&gt;:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;scala&quot; style=&quot;background-color:#002b36;color:#839496;&quot; class=&quot;language-scala &quot;&gt;&lt;code class=&quot;language-scala&quot; data-lang=&quot;scala&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;val gender1 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;UserRepository&lt;&#x2F;span&gt;&lt;span&gt;.findById&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span&gt;.flatMap&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d33682;&quot;&gt;_&lt;&#x2F;span&gt;&lt;span&gt;.gender&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;&#x2F;&#x2F; gender is Some(&amp;quot;male&amp;quot;)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;val gender2 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;UserRepository&lt;&#x2F;span&gt;&lt;span&gt;.findById&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;2&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span&gt;.flatMap&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d33682;&quot;&gt;_&lt;&#x2F;span&gt;&lt;span&gt;.gender&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;&#x2F;&#x2F; gender is None
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;val gender3 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;UserRepository&lt;&#x2F;span&gt;&lt;span&gt;.findById&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;3&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span&gt;.flatMap&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d33682;&quot;&gt;_&lt;&#x2F;span&gt;&lt;span&gt;.gender&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;&#x2F;&#x2F; gender is None
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The result type is now &lt;code&gt;Option[String]&lt;&#x2F;code&gt;. If the user is defined &lt;em&gt;and&lt;&#x2F;em&gt; its gender is defined, we get it as a flattened &lt;code&gt;Some&lt;&#x2F;code&gt;. If either the use or its gender is undefined, we get a &lt;code&gt;None&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;To understand how this works, let&#x27;s have a look at what happens when flat mapping a list of lists of strings, always keeping in mind that an &lt;code&gt;Option&lt;&#x2F;code&gt; is just a collection, too, like a &lt;code&gt;List&lt;&#x2F;code&gt;:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;scala&quot; style=&quot;background-color:#002b36;color:#839496;&quot; class=&quot;language-scala &quot;&gt;&lt;code class=&quot;language-scala&quot; data-lang=&quot;scala&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;val names&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;List&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;List&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;String&lt;&#x2F;span&gt;&lt;span&gt;]] &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= 
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;List&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;List&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;John&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Johanna&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Daniel&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;List&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;()&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;List&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Doe&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Westheide&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;))
&lt;&#x2F;span&gt;&lt;span&gt;names.map&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d33682;&quot;&gt;_&lt;&#x2F;span&gt;&lt;span&gt;.map&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d33682;&quot;&gt;_&lt;&#x2F;span&gt;&lt;span&gt;.toUpperCase&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)) 
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;&#x2F;&#x2F; results in List(List(&amp;quot;JOHN&amp;quot;, &amp;quot;JOHANNA&amp;quot;, &amp;quot;DANIEL&amp;quot;), List(), List(&amp;quot;DOE&amp;quot;, &amp;quot;WESTHEIDE&amp;quot;))
&lt;&#x2F;span&gt;&lt;span&gt;names.flatMap&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d33682;&quot;&gt;_&lt;&#x2F;span&gt;&lt;span&gt;.map&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d33682;&quot;&gt;_&lt;&#x2F;span&gt;&lt;span&gt;.toUpperCase&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)) 
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;&#x2F;&#x2F; results in List(&amp;quot;JOHN&amp;quot;, &amp;quot;JOHANNA&amp;quot;, &amp;quot;DANIEL&amp;quot;, &amp;quot;DOE&amp;quot;, &amp;quot;WESTHEIDE&amp;quot;)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;If we use &lt;code&gt;flatMap&lt;&#x2F;code&gt;, the mapped elements of the inner lists are converted into a single flat list of strings. Obviously, nothing will remain of any empty inner lists.&lt;&#x2F;p&gt;
&lt;p&gt;To lead us back to the &lt;code&gt;Option&lt;&#x2F;code&gt; type, consider what happens if you map a list of options of strings:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;scala&quot; style=&quot;background-color:#002b36;color:#839496;&quot; class=&quot;language-scala &quot;&gt;&lt;code class=&quot;language-scala&quot; data-lang=&quot;scala&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;val names&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;List&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Option&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;String&lt;&#x2F;span&gt;&lt;span&gt;]] &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;List&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Some&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Johanna&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;None&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Some&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Daniel&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;))
&lt;&#x2F;span&gt;&lt;span&gt;names.map&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d33682;&quot;&gt;_&lt;&#x2F;span&gt;&lt;span&gt;.map&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d33682;&quot;&gt;_&lt;&#x2F;span&gt;&lt;span&gt;.toUpperCase&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;&#x2F;&#x2F; List(Some(&amp;quot;JOHANNA&amp;quot;), None, Some(&amp;quot;DANIEL&amp;quot;))
&lt;&#x2F;span&gt;&lt;span&gt;names.flatMap&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;xs =&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; xs.map&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d33682;&quot;&gt;_&lt;&#x2F;span&gt;&lt;span&gt;.toUpperCase&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;&#x2F;&#x2F; List(&amp;quot;JOHANNA&amp;quot;, &amp;quot;DANIEL&amp;quot;)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;If you just map over the list of options, the result type stays &lt;code&gt;List[Option[String]]&lt;&#x2F;code&gt;. Using &lt;code&gt;flatMap&lt;&#x2F;code&gt;, all elements of the inner collections are put into a flat list: The one element of any &lt;code&gt;Some[String]&lt;&#x2F;code&gt; in the original list is unwrapped and put into the result list, whereas any &lt;code&gt;None&lt;&#x2F;code&gt; value in the original list does not contain any element to be unwrapped. Hence, &lt;code&gt;None&lt;&#x2F;code&gt; values are effectively filtered out.&lt;&#x2F;p&gt;
&lt;p&gt;With this in mind, have a look again at what &lt;code&gt;flatMap&lt;&#x2F;code&gt; does on the &lt;code&gt;Option&lt;&#x2F;code&gt; type.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;filtering-an-option&quot;&gt;Filtering an option&lt;&#x2F;h3&gt;
&lt;p&gt;You can filter an option just like you can filter a list. If the instance of &lt;code&gt;Option[A]&lt;&#x2F;code&gt; is defined, i.e. it is a &lt;code&gt;Some[A]&lt;&#x2F;code&gt;, &lt;em&gt;and&lt;&#x2F;em&gt; the predicate passed to &lt;code&gt;filter&lt;&#x2F;code&gt; returns &lt;code&gt;true&lt;&#x2F;code&gt; for the wrapped value of type &lt;code&gt;A&lt;&#x2F;code&gt;, the &lt;code&gt;Some&lt;&#x2F;code&gt; instance is returned. If the &lt;code&gt;Option&lt;&#x2F;code&gt; instance is already &lt;code&gt;None&lt;&#x2F;code&gt; or the predicate returns &lt;code&gt;false&lt;&#x2F;code&gt; for the value inside the &lt;code&gt;Some&lt;&#x2F;code&gt;, the result is &lt;code&gt;None&lt;&#x2F;code&gt;:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;scala&quot; style=&quot;background-color:#002b36;color:#839496;&quot; class=&quot;language-scala &quot;&gt;&lt;code class=&quot;language-scala&quot; data-lang=&quot;scala&quot;&gt;&lt;span style=&quot;color:#859900;&quot;&gt;UserRepository&lt;&#x2F;span&gt;&lt;span&gt;.findById&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span&gt;.filter&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d33682;&quot;&gt;_&lt;&#x2F;span&gt;&lt;span&gt;.age &amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;30&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;&#x2F;&#x2F; Some(user), because age is &amp;gt; 30
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;UserRepository&lt;&#x2F;span&gt;&lt;span&gt;.findById&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;2&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span&gt;.filter&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d33682;&quot;&gt;_&lt;&#x2F;span&gt;&lt;span&gt;.age &amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;30&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;&#x2F;&#x2F; None, because age is &amp;lt;= 30
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;UserRepository&lt;&#x2F;span&gt;&lt;span&gt;.findById&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;3&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span&gt;.filter&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d33682;&quot;&gt;_&lt;&#x2F;span&gt;&lt;span&gt;.age &amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;30&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;&#x2F;&#x2F; None, because user is already None
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h2 id=&quot;for-comprehensions&quot;&gt;For comprehensions&lt;&#x2F;h2&gt;
&lt;p&gt;Now that you know that an &lt;code&gt;Option&lt;&#x2F;code&gt; can be treated as a collection and provides &lt;code&gt;map&lt;&#x2F;code&gt;, &lt;code&gt;flatMap&lt;&#x2F;code&gt;, &lt;code&gt;filter&lt;&#x2F;code&gt; and other methods you know from collections, you will probably already suspect that options can be used in &lt;em&gt;for comprehensions&lt;&#x2F;em&gt;. Often, this is the most readable way of working with options, especially if you have to chain a lot of &lt;code&gt;map&lt;&#x2F;code&gt;, &lt;code&gt;flatMap&lt;&#x2F;code&gt; and &lt;code&gt;filter&lt;&#x2F;code&gt; invocations. If it&#x27;s just a single &lt;code&gt;map&lt;&#x2F;code&gt;, that may often be preferrable, as it is a little less verbose.&lt;&#x2F;p&gt;
&lt;p&gt;If we want to get the gender for a single user, we can apply the following for comprehension:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;scala&quot; style=&quot;background-color:#002b36;color:#839496;&quot; class=&quot;language-scala &quot;&gt;&lt;code class=&quot;language-scala&quot; data-lang=&quot;scala&quot;&gt;&lt;span style=&quot;color:#859900;&quot;&gt;for &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;	&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;user &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;&amp;lt;- &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;UserRepository&lt;&#x2F;span&gt;&lt;span&gt;.findById&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;	&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;gender &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;&amp;lt;-&lt;&#x2F;span&gt;&lt;span&gt; user.gender
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;} &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;yield&lt;&#x2F;span&gt;&lt;span&gt; gender &lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;&#x2F;&#x2F; results in Some(&amp;quot;male&amp;quot;)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;As you may know from working with lists, this is equivalent to nested invocations of &lt;code&gt;flatMap&lt;&#x2F;code&gt;. If the &lt;code&gt;UserRepository&lt;&#x2F;code&gt; already returns &lt;code&gt;None&lt;&#x2F;code&gt; or the &lt;code&gt;Gender&lt;&#x2F;code&gt; is &lt;code&gt;None&lt;&#x2F;code&gt;, the result of the for comprehension is &lt;code&gt;None&lt;&#x2F;code&gt;. For the user in the example, a gender is defined, so it is returned in a &lt;code&gt;Some&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;If we wanted to retrieve the genders of all users that have specified it, we could iterate all users, and for each of them yield a gender, if it is defined:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;scala&quot; style=&quot;background-color:#002b36;color:#839496;&quot; class=&quot;language-scala &quot;&gt;&lt;code class=&quot;language-scala&quot; data-lang=&quot;scala&quot;&gt;&lt;span style=&quot;color:#859900;&quot;&gt;for &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;	&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;user &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;&amp;lt;- &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;UserRepository&lt;&#x2F;span&gt;&lt;span&gt;.findAll
&lt;&#x2F;span&gt;&lt;span&gt;	&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;gender &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;&amp;lt;-&lt;&#x2F;span&gt;&lt;span&gt; user.gender
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;} &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;yield&lt;&#x2F;span&gt;&lt;span&gt; gender
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Since we are effectively flat mapping, the result type is &lt;code&gt;List[String]&lt;&#x2F;code&gt;, and the resulting list is &lt;code&gt;List(&quot;male&quot;)&lt;&#x2F;code&gt;, because &lt;code&gt;gender&lt;&#x2F;code&gt; is only defined for the first user.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;usage-in-the-left-side-of-a-generator&quot;&gt;Usage in the left side of a generator&lt;&#x2F;h3&gt;
&lt;p&gt;Maybe you remember from part three of this series that &lt;a href=&quot;http:&#x2F;&#x2F;danielwestheide.com&#x2F;blog&#x2F;2012&#x2F;12&#x2F;05&#x2F;the-neophytes-guide-to-scala-part-3-patterns-everywhere.html&quot;&gt;the left side of a generator in a for comprehension is a pattern&lt;&#x2F;a&gt;. This means that you can also patterns involving options in for comprehensions.&lt;&#x2F;p&gt;
&lt;p&gt;We could rewrite the previous example as follows:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;scala&quot; style=&quot;background-color:#002b36;color:#839496;&quot; class=&quot;language-scala &quot;&gt;&lt;code class=&quot;language-scala&quot; data-lang=&quot;scala&quot;&gt;&lt;span style=&quot;color:#859900;&quot;&gt;for &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;	&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;User&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d33682;&quot;&gt;_&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d33682;&quot;&gt;_&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d33682;&quot;&gt;_&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d33682;&quot;&gt;_&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Some&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;gender&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)) &amp;lt;- &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;UserRepository&lt;&#x2F;span&gt;&lt;span&gt;.findAll
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;} &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;yield&lt;&#x2F;span&gt;&lt;span&gt; gender
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Using a &lt;code&gt;Some&lt;&#x2F;code&gt; pattern in the left side of a generator has the effect of removing all elements from the result collection for which the respective value is &lt;code&gt;None&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;chaining-options&quot;&gt;Chaining options&lt;&#x2F;h2&gt;
&lt;p&gt;Options can also be chained, which is a little similar to chaining partial functions. To do this, you call &lt;code&gt;orElse&lt;&#x2F;code&gt; on an &lt;code&gt;Option&lt;&#x2F;code&gt; instance, and pass in another &lt;code&gt;Option&lt;&#x2F;code&gt; instance as a by-name parameter. If the former is &lt;code&gt;None&lt;&#x2F;code&gt;, &lt;code&gt;orElse&lt;&#x2F;code&gt; returns the option passed to it, otherwise it returns the one on which it was called.&lt;&#x2F;p&gt;
&lt;p&gt;A good use case for this is finding a resource, when you have several different locations to search for it and an order of preference. In our example, we prefer the resource to be found in the config dir, so we call &lt;code&gt;orElse&lt;&#x2F;code&gt; on it, passing in an alternative option:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;scala&quot; style=&quot;background-color:#002b36;color:#839496;&quot; class=&quot;language-scala &quot;&gt;&lt;code class=&quot;language-scala&quot; data-lang=&quot;scala&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;case class&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt; Resource&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;content&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;String&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;val resourceFromConfigDir&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Option&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Resource&lt;&#x2F;span&gt;&lt;span&gt;] &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;None
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;val resourceFromClasspath&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Option&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Resource&lt;&#x2F;span&gt;&lt;span&gt;] &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Some&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Resource&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;I was found on the classpath&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;))
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;val resource &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt; resourceFromConfigDir orElse resourceFromClasspath
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This is usually a good fit if you want to chain more than just two options – if you simply want to provide a default value in case a given option is absent, the &lt;code&gt;getOrElse&lt;&#x2F;code&gt; method may be a better idea.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;summary&quot;&gt;Summary&lt;&#x2F;h2&gt;
&lt;p&gt;In this article, I hope to have given you everything you need to know about the &lt;code&gt;Option&lt;&#x2F;code&gt; type in order to use it for your benefit, to understand other people&#x27;s Scala code and write more readable, functional code. The most important insight to take away from this post is that there is a very basic idea that is common to lists, sets, maps, options, and, as you will see in a future post, other data types, and that there is a uniform way of using these types, which is both elegant and very powerful.&lt;&#x2F;p&gt;
&lt;p&gt;In the following part of this series I am going to deal with idiomatic, functional error handling in Scala.&lt;&#x2F;p&gt;
</description>
      </item>
      <item>
          <title>The Neophyte&#x27;s Guide to Scala Part 4: Pattern Matching Anonymous Functions</title>
          <pubDate>Wed, 12 Dec 2012 00:00:00 +0000</pubDate>
          <author>Unknown</author>
          <link>https://danielwestheide.com/blog/the-neophytes-guide-to-scala-part-4-pattern-matching-anonymous-functions/</link>
          <guid>https://danielwestheide.com/blog/the-neophytes-guide-to-scala-part-4-pattern-matching-anonymous-functions/</guid>
          <description xml:base="https://danielwestheide.com/blog/the-neophytes-guide-to-scala-part-4-pattern-matching-anonymous-functions/">&lt;p&gt;In the &lt;a href=&quot;http:&#x2F;&#x2F;danielwestheide.com&#x2F;blog&#x2F;2012&#x2F;12&#x2F;05&#x2F;the-neophytes-guide-to-scala-part-3-patterns-everywhere.html&quot;&gt;previous part of this series&lt;&#x2F;a&gt;, I gave an overview of the various ways in which patterns can be used in Scala, concluding with a brief mention of anonymous functions as another place in which patterns can be put to use. In this post, we are going to take a detailed look at the possibilities opened up by being able to define anonymous functions in this way.&lt;&#x2F;p&gt;
&lt;p&gt;If you have participated in the &lt;a href=&quot;https:&#x2F;&#x2F;www.coursera.org&#x2F;course&#x2F;progfun&quot;&gt;Scala course at Coursera&lt;&#x2F;a&gt;, or have coded in Scala for a while, you will likely have written anonymous functions on a regular basis. For example, given a list of song titles which you want to transform to lower case for your search index, you might make want to define an anonymous function that you pass to the &lt;code&gt;map&lt;&#x2F;code&gt; method, like this:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;scala&quot; style=&quot;background-color:#002b36;color:#839496;&quot; class=&quot;language-scala &quot;&gt;&lt;code class=&quot;language-scala&quot; data-lang=&quot;scala&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;val songTitles &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;List&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;The White Hare&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Childe the Hunter&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Take no Rogues&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;songTitles.map&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;t =&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; t.toLowerCase&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Or, if you like it even shorter, of course, you will probably write the normalize function like this, making use of Scala&#x27;s placeholder syntax:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;scala&quot; style=&quot;background-color:#002b36;color:#839496;&quot; class=&quot;language-scala &quot;&gt;&lt;code class=&quot;language-scala&quot; data-lang=&quot;scala&quot;&gt;&lt;span&gt;songTitles.map&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d33682;&quot;&gt;_&lt;&#x2F;span&gt;&lt;span&gt;.toLowerCase&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;So far so good. However, let&#x27;s see how this syntax performs for a slightly different example: We have a sequence of pairs, each representing a word and its frequency in some text. Our goal is to filter out those pairs whose frequency is below or above a certain threshold, and then only return the remaining words, without their respective frequencies. We need to write a function &lt;code&gt;wordsWithoutOutliers(wordFrequencies: Seq[(String, Int)]): Seq[String]&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;Our initial solution makes use of the &lt;code&gt;filter&lt;&#x2F;code&gt; and &lt;code&gt;map&lt;&#x2F;code&gt; methods, passing anonymous functions to them using our familiar syntax:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;scala&quot; style=&quot;background-color:#002b36;color:#839496;&quot; class=&quot;language-scala &quot;&gt;&lt;code class=&quot;language-scala&quot; data-lang=&quot;scala&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;val wordFrequencies &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= (&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;habitual&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;6&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span&gt; :: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;and&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;56&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span&gt; :: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;consuetudinary&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;2&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span&gt; :: 
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;additionally&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;27&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span&gt; :: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;homely&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;5&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span&gt; :: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;society&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;13&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span&gt; :: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Nil
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;wordsWithoutOutliers&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;wordFrequencies&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Seq&lt;&#x2F;span&gt;&lt;span&gt;[(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;String&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;Int&lt;&#x2F;span&gt;&lt;span&gt;)]&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Seq&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;String&lt;&#x2F;span&gt;&lt;span&gt;] &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;=
&lt;&#x2F;span&gt;&lt;span&gt;  wordFrequencies.filter&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;wf =&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; wf._2 &amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;3&lt;&#x2F;span&gt;&lt;span&gt; &amp;amp;&amp;amp; wf._2 &amp;lt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;25&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span&gt;.map&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d33682;&quot;&gt;_&lt;&#x2F;span&gt;&lt;span&gt;._1&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;wordsWithoutOutliers&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;wordFrequencies&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;&#x2F;&#x2F; List(&amp;quot;habitual&amp;quot;, &amp;quot;homely&amp;quot;, &amp;quot;society&amp;quot;)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This solution has several problems. The first one is only an aesthetic one – accessing the fields of the tuple looks pretty ugly to me. If only we could destructure the pair, we could make this code a little more pleasant and probably also more readable.&lt;&#x2F;p&gt;
&lt;p&gt;Thankfully, Scala provides an alternative way of writing anonymous functions: A &lt;em&gt;pattern matching anonymous function&lt;&#x2F;em&gt; is an anonymous function that is defined as a block consisting of a sequence of cases, surrounded as usual by curly braces, but without a &lt;code&gt;match&lt;&#x2F;code&gt; keyword before the block. Let&#x27;s rewrite our function, making use of this notation:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;scala&quot; style=&quot;background-color:#002b36;color:#839496;&quot; class=&quot;language-scala &quot;&gt;&lt;code class=&quot;language-scala&quot; data-lang=&quot;scala&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;wordsWithoutOutliers&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;wordFrequencies&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Seq&lt;&#x2F;span&gt;&lt;span&gt;[(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;String&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;Int&lt;&#x2F;span&gt;&lt;span&gt;)]&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Seq&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;String&lt;&#x2F;span&gt;&lt;span&gt;] &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;=
&lt;&#x2F;span&gt;&lt;span&gt;  wordFrequencies.filter &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{ &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;case &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d33682;&quot;&gt;_&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;f&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;=&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; f &amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;3&lt;&#x2F;span&gt;&lt;span&gt; &amp;amp;&amp;amp; f &amp;lt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;25 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;}&lt;&#x2F;span&gt;&lt;span&gt; map &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{ &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;case &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;w&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d33682;&quot;&gt;_&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;=&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; w &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;In this example, we have only used a single case in each of our anonymous functions, because we know that this case always matches – we are simply decomposing a data structure whose type we already know at compile time, so nothing can go wrong here. This is a very common way of using pattern matching anonymous functions.&lt;&#x2F;p&gt;
&lt;p&gt;If you try to assign these anonymous functions to values, you will see that they have the expected type:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;scala&quot; style=&quot;background-color:#002b36;color:#839496;&quot; class=&quot;language-scala &quot;&gt;&lt;code class=&quot;language-scala&quot; data-lang=&quot;scala&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;val predicate&lt;&#x2F;span&gt;&lt;span&gt;: ((&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;String&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;Int&lt;&#x2F;span&gt;&lt;span&gt;)) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;=&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;Boolean &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= { &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;case &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d33682;&quot;&gt;_&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;f&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;=&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; f &amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;3&lt;&#x2F;span&gt;&lt;span&gt; &amp;amp;&amp;amp; f &amp;lt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;25 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;}
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;val transformFn&lt;&#x2F;span&gt;&lt;span&gt;: ((&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;String&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;Int&lt;&#x2F;span&gt;&lt;span&gt;)) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;=&amp;gt; String &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= { &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;case &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;w&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d33682;&quot;&gt;_&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;=&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; w &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Please note that you have to specify the type of the value here, the Scala compiler cannot infer it for pattern matching anonymous functions.&lt;&#x2F;p&gt;
&lt;p&gt;Nothing prevents you from defining a more complex sequence of cases, of course. However, if you define an anonymous function this way and want to pass it to some other function, such as the ones in our example, you have to make sure that for all possible inputs, one of your cases matches so that your anonymous function always returns a value. Otherwise, you will risk a &lt;code&gt;MatchError&lt;&#x2F;code&gt; at runtime.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;partial-functions&quot;&gt;Partial functions&lt;&#x2F;h2&gt;
&lt;p&gt;Sometimes, however, a function that is only defined for specific input values is exactly what you want. In fact, such a function can help us get rid of another problem that we haven&#x27;t solved yet with our current implementation of the &lt;code&gt;wordsWithoutOutliers&lt;&#x2F;code&gt; function: We first filter the given sequence and then map the remaining elements. If we can boil this down to a solution that only has to iterate over the given sequence once, this would not only need fewer CPU cycles but would also make our code shorter and, ultimately, more readable.&lt;&#x2F;p&gt;
&lt;p&gt;If you browse through Scala&#x27;s collections API, you will notice a method called &lt;code&gt;collect&lt;&#x2F;code&gt;, which, for a &lt;code&gt;Seq[A]&lt;&#x2F;code&gt;, has the following signature:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;scala&quot; style=&quot;background-color:#002b36;color:#839496;&quot; class=&quot;language-scala &quot;&gt;&lt;code class=&quot;language-scala&quot; data-lang=&quot;scala&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;collect&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;B&lt;&#x2F;span&gt;&lt;span&gt;]&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;pf&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;PartialFunction&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;A&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;B&lt;&#x2F;span&gt;&lt;span&gt;]&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This method returns a new sequence by applying the given &lt;em&gt;partial function&lt;&#x2F;em&gt; to all of its elements – the partial function both filters and maps the sequence.&lt;&#x2F;p&gt;
&lt;p&gt;So what is a partial function? In short, it&#x27;s a unary function that is known to be defined only for certain input values and that allows clients to check whether it is defined for a specific input value.&lt;&#x2F;p&gt;
&lt;p&gt;To this end, the &lt;code&gt;PartialFunction&lt;&#x2F;code&gt; trait provides an &lt;code&gt;isDefinedAt&lt;&#x2F;code&gt; method. As a matter of fact, the &lt;code&gt;PartialFunction[-A, +B]&lt;&#x2F;code&gt; type extends the type &lt;code&gt;(A) =&amp;gt; B&lt;&#x2F;code&gt; (which can also be written as &lt;code&gt;Function1[A, B]&lt;&#x2F;code&gt;), and a pattern matching anonymous function is always of type &lt;code&gt;PartialFunction&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;Due to this inheritance hierarchy, passing a pattern matching anonymous function to a method that expects a &lt;code&gt;Function1&lt;&#x2F;code&gt;, like &lt;code&gt;map&lt;&#x2F;code&gt; or &lt;code&gt;filter&lt;&#x2F;code&gt;, is perfectly fine, as long as that function is defined for all input values, i.e. there is always a matching case.&lt;&#x2F;p&gt;
&lt;p&gt;The &lt;code&gt;collect&lt;&#x2F;code&gt; method, however, specifically expects a &lt;code&gt;PartialFunction[A, B]&lt;&#x2F;code&gt; that may not be defined for all input values and knows exactly how to deal with that case. For each element in the sequence, it first checks if the partial function is defined for it by calling &lt;code&gt;isDefinedAt&lt;&#x2F;code&gt; on the partial function. If this returns &lt;code&gt;false&lt;&#x2F;code&gt;, the element is ignored. Otherwise, the result of applying the partial function to the element is added to the result sequence.&lt;&#x2F;p&gt;
&lt;p&gt;Let&#x27;s first define a partial function that we want to use for refactoring our &lt;code&gt;wordsWithoutOutliers&lt;&#x2F;code&gt; function to make use of &lt;code&gt;collect&lt;&#x2F;code&gt;:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;scala&quot; style=&quot;background-color:#002b36;color:#839496;&quot; class=&quot;language-scala &quot;&gt;&lt;code class=&quot;language-scala&quot; data-lang=&quot;scala&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;val pf&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;PartialFunction&lt;&#x2F;span&gt;&lt;span&gt;[(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;String&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;Int&lt;&#x2F;span&gt;&lt;span&gt;), &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;String&lt;&#x2F;span&gt;&lt;span&gt;] &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= { 
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;case &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;word&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;freq&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;if&lt;&#x2F;span&gt;&lt;span&gt; freq &amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;3&lt;&#x2F;span&gt;&lt;span&gt; &amp;amp;&amp;amp; freq &amp;lt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;25 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;=&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; word 
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;We added a guard clause to our case, so that this function will not be defined for word&#x2F;frequency pairs whose frequency is not within the required range.&lt;&#x2F;p&gt;
&lt;p&gt;Instead of using the syntax for pattern matching anonymous functions, we could have defined this partial function by explicitly extending the &lt;code&gt;PartialFunction&lt;&#x2F;code&gt; trait:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;scala&quot; style=&quot;background-color:#002b36;color:#839496;&quot; class=&quot;language-scala &quot;&gt;&lt;code class=&quot;language-scala&quot; data-lang=&quot;scala&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;val pf &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;new PartialFunction&lt;&#x2F;span&gt;&lt;span&gt;[(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;String&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;Int&lt;&#x2F;span&gt;&lt;span&gt;), &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;String&lt;&#x2F;span&gt;&lt;span&gt;] &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;apply&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;wordFrequency&lt;&#x2F;span&gt;&lt;span&gt;: (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;String&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;Int&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;) =&lt;&#x2F;span&gt;&lt;span&gt; wordFrequency &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;match &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;case &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;word&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;freq&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;if&lt;&#x2F;span&gt;&lt;span&gt; freq &amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;3&lt;&#x2F;span&gt;&lt;span&gt; &amp;amp;&amp;amp; freq &amp;lt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;25 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;=&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; word 
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;}
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;isDefinedAt&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;wordFrequency&lt;&#x2F;span&gt;&lt;span&gt;: (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;String&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;Int&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;) =&lt;&#x2F;span&gt;&lt;span&gt; wordFrequency &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;match &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;case &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;word&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;freq&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;if&lt;&#x2F;span&gt;&lt;span&gt; freq &amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;3&lt;&#x2F;span&gt;&lt;span&gt; &amp;amp;&amp;amp; freq &amp;lt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;25 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;=&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;true
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;case &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d33682;&quot;&gt;_ &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;=&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;false
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;}
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Usually, however, you will want to use the much more concise anonymous function syntax.&lt;&#x2F;p&gt;
&lt;p&gt;Now, if we passed our partial function to the &lt;code&gt;map&lt;&#x2F;code&gt; method, this would compile just fine, but result in a &lt;code&gt;MatchError&lt;&#x2F;code&gt; at runtime, because our partial function is not defined for all possible input values, thanks to the added guard clause:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;scala&quot; style=&quot;background-color:#002b36;color:#839496;&quot; class=&quot;language-scala &quot;&gt;&lt;code class=&quot;language-scala&quot; data-lang=&quot;scala&quot;&gt;&lt;span&gt;  wordFrequencies.map&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;pf&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;&#x2F;&#x2F; will throw a MatchError
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;However, we can pass this partial function to the &lt;code&gt;collect&lt;&#x2F;code&gt; method, and it will filter and map the sequence as expected:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;scala&quot; style=&quot;background-color:#002b36;color:#839496;&quot; class=&quot;language-scala &quot;&gt;&lt;code class=&quot;language-scala&quot; data-lang=&quot;scala&quot;&gt;&lt;span&gt;  wordFrequencies.collect&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;pf&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;&#x2F;&#x2F; List(&amp;quot;habitual&amp;quot;, &amp;quot;homely&amp;quot;, &amp;quot;society&amp;quot;)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The result of this is the same as that of our current implementation of &lt;code&gt;wordsWithoutOutliers&lt;&#x2F;code&gt; when passing our dummy &lt;code&gt;wordFrequencies&lt;&#x2F;code&gt; sequence to it. So let&#x27;s rewrite that function:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;scala&quot; style=&quot;background-color:#002b36;color:#839496;&quot; class=&quot;language-scala &quot;&gt;&lt;code class=&quot;language-scala&quot; data-lang=&quot;scala&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;wordsWithoutOutliers&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;wordFrequencies&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Seq&lt;&#x2F;span&gt;&lt;span&gt;[(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;String&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;Int&lt;&#x2F;span&gt;&lt;span&gt;)]&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Seq&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;String&lt;&#x2F;span&gt;&lt;span&gt;] &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;=
&lt;&#x2F;span&gt;&lt;span&gt;  wordFrequencies.collect &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{ &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;case &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;word&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;freq&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;if&lt;&#x2F;span&gt;&lt;span&gt; freq &amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;3&lt;&#x2F;span&gt;&lt;span&gt; &amp;amp;&amp;amp; freq &amp;lt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;25 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;=&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; word &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Partial functions have some other very useful properties. For example, they provide the means to be chained, allowing for a neat functional alternative to the &lt;a href=&quot;http:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Chain-of-responsibility_pattern&quot;&gt;chain of responsibility pattern&lt;&#x2F;a&gt; known from object-oriented programming. This, however, will have to be the subject of a future post in this series, when I am going to address the issue of functional composability.&lt;&#x2F;p&gt;
&lt;p&gt;Partial functions are also a crucial element of many Scala libraries and APIs. For example, the way an &lt;a href=&quot;http:&#x2F;&#x2F;akka.io&#x2F;&quot;&gt;Akka&lt;&#x2F;a&gt; actor processes messages sent to it is defined in terms of a partial function. Hence, it&#x27;s quite important to know and understand this concept.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;summary&quot;&gt;Summary&lt;&#x2F;h2&gt;
&lt;p&gt;In this part, we examined an alternative way of defining anonymous functions, namely as a sequence of cases, which opens up some nice destructuring possibilities in a rather concise way. Moreover, we delved into the topic of partial functions, demonstrating their usefulness by means of a simple use case.&lt;&#x2F;p&gt;
&lt;p&gt;In the next article, I am going to dig deeper into the ever-present &lt;code&gt;Option&lt;&#x2F;code&gt; type, explaining the reasoning behind its existence and how best to make use of it.&lt;&#x2F;p&gt;
&lt;p&gt;Please let me know if you have any questions or feedback. Is there any particular topic you would like to see covered in an article?&lt;&#x2F;p&gt;
</description>
      </item>
      <item>
          <title>The Neophyte&#x27;s Guide to Scala Part 3: Patterns Everywhere</title>
          <pubDate>Wed, 05 Dec 2012 00:00:00 +0000</pubDate>
          <author>Unknown</author>
          <link>https://danielwestheide.com/blog/the-neophytes-guide-to-scala-part-3-patterns-everywhere/</link>
          <guid>https://danielwestheide.com/blog/the-neophytes-guide-to-scala-part-3-patterns-everywhere/</guid>
          <description xml:base="https://danielwestheide.com/blog/the-neophytes-guide-to-scala-part-3-patterns-everywhere/">&lt;p&gt;In the first two parts of this series, I spent quite some time explaining what&#x27;s actually happening when you destructure an instance of a case class in a pattern, and how to write your own extractors, allowing you to destructure any types of objects in any way you desire.&lt;&#x2F;p&gt;
&lt;p&gt;Now it is time to get an overview of where patterns can actually be used in your Scala code, because so far you have only seen one of the different possible ways to make use of patterns. Here we go!&lt;&#x2F;p&gt;
&lt;h2 id=&quot;pattern-matching-expressions&quot;&gt;Pattern matching expressions&lt;&#x2F;h2&gt;
&lt;p&gt;One place in which patterns can appear is inside of a &lt;em&gt;pattern matching expression&lt;&#x2F;em&gt;. This way of using patterns should be very familiar to you after attending the Scala course at Coursera and following along in this series. You have some expression &lt;code&gt;e&lt;&#x2F;code&gt;, followed by the &lt;code&gt;match&lt;&#x2F;code&gt; keyword and a block, which can contain any number of cases. A case, in turn, consists of the &lt;code&gt;case&lt;&#x2F;code&gt; keyword followed by a pattern and, optionally, a guard clause on the left side, plus a block on the right side, to be executed if this pattern matches.&lt;&#x2F;p&gt;
&lt;p&gt;Here is a simple example, making use of patterns and, in one of the cases, a guard clause:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;scala&quot; style=&quot;background-color:#002b36;color:#839496;&quot; class=&quot;language-scala &quot;&gt;&lt;code class=&quot;language-scala&quot; data-lang=&quot;scala&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;case class&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt; Player&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;name&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;String&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;score&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;Int&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;printMessage&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;player&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Player&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;) =&lt;&#x2F;span&gt;&lt;span&gt; player &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;match &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;case Player&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d33682;&quot;&gt;_&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;score&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;if&lt;&#x2F;span&gt;&lt;span&gt; score &amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;100000 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;=&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; println&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Get a job, dude!&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;case Player&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;name&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d33682;&quot;&gt;_&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;=&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; println&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Hey &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; + name + &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;, nice to see you again!&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;) 
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The &lt;code&gt;printMessage&lt;&#x2F;code&gt; method has a return type of &lt;code&gt;Unit&lt;&#x2F;code&gt;, its sole purpose is to perform a side effect, namely printing a message. It is important to remember that you don&#x27;t have to use pattern matching as you would use switch statements in languages like Java. What we are using here is called a pattern matching &lt;em&gt;expression&lt;&#x2F;em&gt; for a reason. Their return value is what is returned by the block belonging to the first matched pattern.&lt;&#x2F;p&gt;
&lt;p&gt;Usually, it&#x27;s a good idea to take advantage of this, as it allows you to decouple two things that do not really belong together, making it easier to test your code, too. We could rewrite the example above as follows:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;scala&quot; style=&quot;background-color:#002b36;color:#839496;&quot; class=&quot;language-scala &quot;&gt;&lt;code class=&quot;language-scala&quot; data-lang=&quot;scala&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;message&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;player&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Player&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;) =&lt;&#x2F;span&gt;&lt;span&gt; player &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;match &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;case Player&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d33682;&quot;&gt;_&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;score&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;if&lt;&#x2F;span&gt;&lt;span&gt; score &amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;100000 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;=&amp;gt; &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Get a job, dude!&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;case Player&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;name&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d33682;&quot;&gt;_&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;=&amp;gt; &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Hey &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; + name + &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;, nice to see you again!&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;}
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;printMessage&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;player&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Player&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;) =&lt;&#x2F;span&gt;&lt;span&gt; println&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;message&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;player&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;))
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Now, we have a separate &lt;code&gt;message&lt;&#x2F;code&gt; method whose return type is &lt;code&gt;String&lt;&#x2F;code&gt;. This is essentialy a pure function, returning the result of a pattern matching expression. You could also store the result of such a pattern matching expression as a value or assign it to a variable, of course.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;patterns-in-value-definitions&quot;&gt;Patterns in value definitions&lt;&#x2F;h2&gt;
&lt;p&gt;Another place in which a pattern can occur in Scala is in the left side of a &lt;em&gt;value definition&lt;&#x2F;em&gt; (and in a &lt;em&gt;variable definition&lt;&#x2F;em&gt;, for that matter, but we want write our Scala code in a functional style, so you won&#x27;t see a lot of usage of variables in this series). Let&#x27;s assume we have a method that returns our current player. We will use a dummy implementation that always returns the same player:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;scala&quot; style=&quot;background-color:#002b36;color:#839496;&quot; class=&quot;language-scala &quot;&gt;&lt;code class=&quot;language-scala&quot; data-lang=&quot;scala&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;currentPlayer&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;()&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Player &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Player&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Daniel&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;3500&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Your usual value definition looks like this:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;scala&quot; style=&quot;background-color:#002b36;color:#839496;&quot; class=&quot;language-scala &quot;&gt;&lt;code class=&quot;language-scala&quot; data-lang=&quot;scala&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;val player &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt; currentPlayer&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;()
&lt;&#x2F;span&gt;&lt;span&gt;doSomethingWithTheName&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;player.name&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;If you know Python, you are probably familiar with a feature called &lt;em&gt;sequence unpacking&lt;&#x2F;em&gt;. The fact that you can use any pattern in the left side of a value definition or variable definition lets you write your Scala code in a similar style. We could change our above code and destructure the given current player while assigning it to the left side:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;scala&quot; style=&quot;background-color:#002b36;color:#839496;&quot; class=&quot;language-scala &quot;&gt;&lt;code class=&quot;language-scala&quot; data-lang=&quot;scala&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;val &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Player&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;name&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d33682;&quot;&gt;_&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;) =&lt;&#x2F;span&gt;&lt;span&gt; currentPlayer&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;()
&lt;&#x2F;span&gt;&lt;span&gt;doSomethingWithTheName&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;name&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;You can do this with any pattern, but generally, it is a good idea to make sure that your pattern always matches. Otherwise, you will be the witness of an exception at runtime. For instance, the following code is problematic. &lt;code&gt;scores&lt;&#x2F;code&gt; is a method returning a list of scores. In our code below, this method simply returns an empty list to illustrate the problem.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;scala&quot; style=&quot;background-color:#002b36;color:#839496;&quot; class=&quot;language-scala &quot;&gt;&lt;code class=&quot;language-scala&quot; data-lang=&quot;scala&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;scores&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;List&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;Int&lt;&#x2F;span&gt;&lt;span&gt;] &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;List&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;()
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;val best&lt;&#x2F;span&gt;&lt;span&gt; :: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;rest &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt; scores
&lt;&#x2F;span&gt;&lt;span&gt;println&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;The score of our champion is &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; + best&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Oops, we&#x27;ve got a &lt;code&gt;MatchError&lt;&#x2F;code&gt;. It seems like our game is not that successful after all, having no scores whatsoever.&lt;&#x2F;p&gt;
&lt;p&gt;A safe and very handy way of using patterns in this way is for destructuring case classes whose type you know at compile time. Also, when working with tuples, this makes your code a lot more readable. Let&#x27;s say we have a function that returns the name of a player and their score as a tuple, not using the &lt;code&gt;Player&lt;&#x2F;code&gt; class we have used so far:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;scala&quot; style=&quot;background-color:#002b36;color:#839496;&quot; class=&quot;language-scala &quot;&gt;&lt;code class=&quot;language-scala&quot; data-lang=&quot;scala&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;gameResult&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;()&lt;&#x2F;span&gt;&lt;span&gt;: (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;String&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;Int&lt;&#x2F;span&gt;&lt;span&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= (&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Daniel&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;3500&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Accessing the fields of a tuple always feels very awkward:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;scala&quot; style=&quot;background-color:#002b36;color:#839496;&quot; class=&quot;language-scala &quot;&gt;&lt;code class=&quot;language-scala&quot; data-lang=&quot;scala&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;val result &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt; gameResult&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;()
&lt;&#x2F;span&gt;&lt;span&gt;println&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;result._1 + &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;: &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; + result._2&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;It&#x27;s safe to destructure our tuple in the value definition, as we know we are dealing with a &lt;code&gt;Tuple2&lt;&#x2F;code&gt;:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;scala&quot; style=&quot;background-color:#002b36;color:#839496;&quot; class=&quot;language-scala &quot;&gt;&lt;code class=&quot;language-scala&quot; data-lang=&quot;scala&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;val &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;name&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;score&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;) =&lt;&#x2F;span&gt;&lt;span&gt; gameResult&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;()
&lt;&#x2F;span&gt;&lt;span&gt;println&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;name + &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;: &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; + score&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This is much more readable, isn&#x27;t it?&lt;&#x2F;p&gt;
&lt;h2 id=&quot;patterns-in-for-comprehensions&quot;&gt;Patterns in for comprehensions&lt;&#x2F;h2&gt;
&lt;p&gt;Patterns also have a very valuable place in for comprehensions. For one, a for comprehension can also contain value definitions. And everything you learnt about the usage of patterns in the left side of value definitions holds true for value definitions in for comprehensions. So if we have a collection of results and want to determine the hall of fame, which in our game is simply a collection of the names of players that have trespassed a certain score threshold, we could do that in a very readable way with a for comprehension:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;scala&quot; style=&quot;background-color:#002b36;color:#839496;&quot; class=&quot;language-scala &quot;&gt;&lt;code class=&quot;language-scala&quot; data-lang=&quot;scala&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;gameResults&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;()&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Seq&lt;&#x2F;span&gt;&lt;span&gt;[(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;String&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;Int&lt;&#x2F;span&gt;&lt;span&gt;)] &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= 
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Daniel&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;3500&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span&gt; :: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Melissa&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;13000&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span&gt; :: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;John&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;7000&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span&gt; :: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Nil
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;hallOfFame &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;for &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;	&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;result &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;&amp;lt;-&lt;&#x2F;span&gt;&lt;span&gt; gameResults&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;()
&lt;&#x2F;span&gt;&lt;span&gt;	&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;name&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;score&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;) =&lt;&#x2F;span&gt;&lt;span&gt; result
&lt;&#x2F;span&gt;&lt;span&gt;	&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;if &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;score &amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;5000&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;} &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;yield&lt;&#x2F;span&gt;&lt;span&gt; name
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The result is &lt;code&gt;List(&quot;Melissa&quot;, &quot;John&quot;)&lt;&#x2F;code&gt;, since the first player does not meet the condition of the guard clause.&lt;&#x2F;p&gt;
&lt;p&gt;This can be written even more concisely, because in for comprehensions, the left side of a &lt;em&gt;generator&lt;&#x2F;em&gt; is also a pattern. So, instead of first assigning each game result to &lt;code&gt;result&lt;&#x2F;code&gt;, we can directly destructure the result in the left side of the generator:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;scala&quot; style=&quot;background-color:#002b36;color:#839496;&quot; class=&quot;language-scala &quot;&gt;&lt;code class=&quot;language-scala&quot; data-lang=&quot;scala&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;hallOfFame &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;for &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;	&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;name&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;score&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;) &amp;lt;-&lt;&#x2F;span&gt;&lt;span&gt; gameResults&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;()
&lt;&#x2F;span&gt;&lt;span&gt;	&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;if &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;score &amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;5000&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;} &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;yield&lt;&#x2F;span&gt;&lt;span&gt; name
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;In this example, the pattern &lt;code&gt;(name, score)&lt;&#x2F;code&gt; always matches, so if it were not for the guard clause, &lt;code&gt;if (score &amp;gt; 5000)&lt;&#x2F;code&gt;, the for comprehension would be equivalent to simply mapping from the tuples to the player names, without filtering anything.&lt;&#x2F;p&gt;
&lt;p&gt;It is important to know that patterns in the left side of generators can already be used for filtering purposes – if a pattern on the left side of a generator does not match, the respective element is filtered out.&lt;&#x2F;p&gt;
&lt;p&gt;To illustrate, let&#x27;s say we have a sequence of lists, and we want to return the sizes of all non-empty lists. This means we have to filter out all empty lists and then return the sizes of the ones remaining. Here is one solution:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;scala&quot; style=&quot;background-color:#002b36;color:#839496;&quot; class=&quot;language-scala &quot;&gt;&lt;code class=&quot;language-scala&quot; data-lang=&quot;scala&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;val lists &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;List&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;2&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;3&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span&gt; :: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;List&lt;&#x2F;span&gt;&lt;span&gt;.empty :: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;List&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;5&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;3&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span&gt; :: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Nil
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;for &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;	&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;list &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;@ &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;head&lt;&#x2F;span&gt;&lt;span&gt; :: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d33682;&quot;&gt;_ &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;&amp;lt;-&lt;&#x2F;span&gt;&lt;span&gt; lists
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;} &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;yield&lt;&#x2F;span&gt;&lt;span&gt; list.size
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The pattern on the left side of the generator does not match for empty lists. This will not throw a &lt;code&gt;MatchError&lt;&#x2F;code&gt;, but result in any empty list being removed. Hence, we get back &lt;code&gt;List(3, 2)&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;Patterns and for comprehensions are a very natural and powerful combination, and if you work with Scala for some time, you will see that you&#x27;ll be using them a lot.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;anonymous-functions&quot;&gt;Anonymous functions&lt;&#x2F;h2&gt;
&lt;p&gt;Finally, patterns can be used for defining anonymous functions. If you have ever used a &lt;code&gt;catch&lt;&#x2F;code&gt; block in order to deal with an exception in Scala, then you have made used of this feature. Pattern matching anonymous functions is a subject that warrants its own blog post, because there is a lot to be said about it. Hence, I will refrain from delving into this usage of patterns in this article, instead leaving you with the promise of dealing with it in the next part of the series.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;em&gt;Update:&lt;&#x2F;em&gt; Fixed a mistake in the expected result of the &lt;code&gt;hallOfFame&lt;&#x2F;code&gt; for comprehension. Thanks to Rajiv for pointing it out.&lt;&#x2F;p&gt;
</description>
      </item>
      <item>
          <title>The Neophyte&#x27;s Guide to Scala Part 2: Extracting Sequences</title>
          <pubDate>Wed, 28 Nov 2012 00:00:00 +0000</pubDate>
          <author>Unknown</author>
          <link>https://danielwestheide.com/blog/the-neophytes-guide-to-scala-part-2-extracting-sequences/</link>
          <guid>https://danielwestheide.com/blog/the-neophytes-guide-to-scala-part-2-extracting-sequences/</guid>
          <description xml:base="https://danielwestheide.com/blog/the-neophytes-guide-to-scala-part-2-extracting-sequences/">&lt;p&gt;In the first part of this series, we &lt;a href=&quot;http:&#x2F;&#x2F;danielwestheide.com&#x2F;blog&#x2F;2012&#x2F;11&#x2F;21&#x2F;the-neophytes-guide-to-scala-part-1-extractors.html&quot;&gt;learned how to implement our own extractors&lt;&#x2F;a&gt; and how these extractors can be used for pattern matching. However, we only discussed extractors that allow you to destructure a given object into a fixed number of parameters. Yet, for certain kinds of data structures, Scala allows you to do pattern matching expecting an arbitrary number of extracted parameters.&lt;&#x2F;p&gt;
&lt;p&gt;For example, you can use a pattern that only matches a list of exactly two elements, or a list of exactly three elements:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;scala&quot; style=&quot;background-color:#002b36;color:#839496;&quot; class=&quot;language-scala &quot;&gt;&lt;code class=&quot;language-scala&quot; data-lang=&quot;scala&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;val xs &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;3&lt;&#x2F;span&gt;&lt;span&gt; :: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;6&lt;&#x2F;span&gt;&lt;span&gt; :: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;12&lt;&#x2F;span&gt;&lt;span&gt; :: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Nil
&lt;&#x2F;span&gt;&lt;span&gt;xs &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;match &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;case List&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;a&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;b&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;=&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; a * b
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;case List&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;a&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;b&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;c&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;=&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; a + b + c
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;case &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d33682;&quot;&gt;_ &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;=&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;0
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;What&#x27;s more, if you want to match lists the exact length of which you don&#x27;t care about, you can use a wildcard operator, &lt;code&gt;_*&lt;&#x2F;code&gt;:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;scala&quot; style=&quot;background-color:#002b36;color:#839496;&quot; class=&quot;language-scala &quot;&gt;&lt;code class=&quot;language-scala&quot; data-lang=&quot;scala&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;val xs &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;3&lt;&#x2F;span&gt;&lt;span&gt; :: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;6&lt;&#x2F;span&gt;&lt;span&gt; :: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;12&lt;&#x2F;span&gt;&lt;span&gt; :: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;24&lt;&#x2F;span&gt;&lt;span&gt; :: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Nil
&lt;&#x2F;span&gt;&lt;span&gt;xs &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;match &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;case List&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;a&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;b&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;_*&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;=&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; a * b
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;case &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d33682;&quot;&gt;_ &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;=&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;0
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Here, the first pattern matches, binding the first two elements to the variables &lt;code&gt;a&lt;&#x2F;code&gt; and &lt;code&gt;b&lt;&#x2F;code&gt;, while simply ignoring the rest of the list, regardless how many remaining elements there are.&lt;&#x2F;p&gt;
&lt;p&gt;Clearly, extractors for these kinds of patterns cannot be implemented with the means I introduced in the first article. We need a way to specify that an extractor takes an object of a certain type and destructures it into a sequence of extracted values, where the length of that sequence is unknown at compile time.&lt;&#x2F;p&gt;
&lt;p&gt;Enter &lt;code&gt;unapplySeq&lt;&#x2F;code&gt;, an extractor method that allows for doing exactly that. Let&#x27;s take a look at one of its possible method signatures:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;scala&quot; style=&quot;background-color:#002b36;color:#839496;&quot; class=&quot;language-scala &quot;&gt;&lt;code class=&quot;language-scala&quot; data-lang=&quot;scala&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;unapplySeq&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;object&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;S&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Option&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Seq&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;T&lt;&#x2F;span&gt;&lt;span&gt;]]
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;It expects an object of type &lt;code&gt;S&lt;&#x2F;code&gt; and returns either &lt;code&gt;None&lt;&#x2F;code&gt;, if the object does not match at all, or a sequence of extracted values of type &lt;code&gt;T&lt;&#x2F;code&gt;, wrapped in a &lt;code&gt;Some&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;example-extracting-given-names&quot;&gt;Example: Extracting given names&lt;&#x2F;h2&gt;
&lt;p&gt;Let&#x27;s make use of this kind of extractor method in an admittedly contrived example. Let&#x27;s say that in some piece of our application, we are receiving a person&#x27;s given name as a &lt;code&gt;String&lt;&#x2F;code&gt;. This string can contain the person&#x27;s second or third name, if that person has more than one given name. Hence, possible values could be &lt;code&gt;&quot;Daniel&quot;&lt;&#x2F;code&gt;, or &lt;code&gt;&quot;Catherina Johanna&quot;&lt;&#x2F;code&gt;, or &lt;code&gt;&quot;Matthew John Michael&quot;&lt;&#x2F;code&gt;. We want to be able to match against these names, extracting and binding the individual given names.&lt;&#x2F;p&gt;
&lt;p&gt;Here is a very simple extractor implementation by means of the &lt;code&gt;unapplySeq&lt;&#x2F;code&gt; method that will allow us to do that:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;scala&quot; style=&quot;background-color:#002b36;color:#839496;&quot; class=&quot;language-scala &quot;&gt;&lt;code class=&quot;language-scala&quot; data-lang=&quot;scala&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;object&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt; GivenNames &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;unapplySeq&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;name&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;String&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Option&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Seq&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;String&lt;&#x2F;span&gt;&lt;span&gt;]] &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= {
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;val names &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt; name.trim.split&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;if &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;names.forall&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d33682;&quot;&gt;_&lt;&#x2F;span&gt;&lt;span&gt;.isEmpty&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;None else Some&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;names&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;}
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Given a &lt;code&gt;String&lt;&#x2F;code&gt; containing one or more given names, it will extract those as a sequence. If the input name does not contain at least one given name, this extractor will return &lt;code&gt;None&lt;&#x2F;code&gt;, and thus, a pattern in which this extractor is used will not match such a string.&lt;&#x2F;p&gt;
&lt;p&gt;We can now put our new extractor to test:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;scala&quot; style=&quot;background-color:#002b36;color:#839496;&quot; class=&quot;language-scala &quot;&gt;&lt;code class=&quot;language-scala&quot; data-lang=&quot;scala&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;greetWithFirstName&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;name&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;String&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;) =&lt;&#x2F;span&gt;&lt;span&gt; name &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;match &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;case GivenNames&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;firstName&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;_*&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;=&amp;gt; &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Good morning, &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; + firstName + &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;!&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;case &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d33682;&quot;&gt;_ &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;=&amp;gt; &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Welcome! Please make sure to fill in your name!&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This nifty little method returns a greeting for a given name, ignoring everything but the first name. &lt;code&gt;greetWithFirstName(&quot;Daniel&quot;)&lt;&#x2F;code&gt; will return &lt;code&gt;&quot;Good morning, Daniel!&quot;&lt;&#x2F;code&gt;, while &lt;code&gt;greetWithFirstName(&quot;Catherina Johanna&quot;)&lt;&#x2F;code&gt; will return &lt;code&gt;&quot;Good morning, Catherina!&quot;&lt;&#x2F;code&gt;&lt;&#x2F;p&gt;
&lt;h2 id=&quot;combining-fixed-and-variable-parameter-extraction&quot;&gt;Combining fixed and variable parameter extraction&lt;&#x2F;h2&gt;
&lt;p&gt;Sometimes, you have certain fixed values to be extracted that you know about at compile time, plus an additional optional sequence of values.&lt;&#x2F;p&gt;
&lt;p&gt;Let&#x27;s assume that in our example, the input name contains the person&#x27;s complete name, not only the given name. Possible values might be &lt;code&gt;&quot;John Doe&quot;&lt;&#x2F;code&gt; or &lt;code&gt;&quot;Catherina Johanna Peterson&quot;&lt;&#x2F;code&gt;. We want to be able to match against such strings using a pattern that always binds the person&#x27;s last name to the first variable in the pattern and the first name to the second variable, followed by an arbitrary number of additional given names.&lt;&#x2F;p&gt;
&lt;p&gt;This can be achieved by means of a slight modification of our &lt;code&gt;unapplySeq&lt;&#x2F;code&gt; method, using a different method signature:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;scala&quot; style=&quot;background-color:#002b36;color:#839496;&quot; class=&quot;language-scala &quot;&gt;&lt;code class=&quot;language-scala&quot; data-lang=&quot;scala&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;unapplySeq&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;object&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;S&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Option&lt;&#x2F;span&gt;&lt;span&gt;[(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;T1&lt;&#x2F;span&gt;&lt;span&gt;, .., &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Tn-&lt;&#x2F;span&gt;&lt;span&gt;1, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Seq&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;T&lt;&#x2F;span&gt;&lt;span&gt;])]
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;As you can see, &lt;code&gt;unapplySeq&lt;&#x2F;code&gt; can also return an &lt;code&gt;Option&lt;&#x2F;code&gt; of a &lt;code&gt;TupleN&lt;&#x2F;code&gt;, where the last element of the tuple must be the sequence containing the variable parts of the extracted values. This method signature should be somewhat familiar, as it is similar to one of the possible signatures of the &lt;code&gt;unapply&lt;&#x2F;code&gt; method that I introduced last week.&lt;&#x2F;p&gt;
&lt;p&gt;Here is an extractor making use of this:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;scala&quot; style=&quot;background-color:#002b36;color:#839496;&quot; class=&quot;language-scala &quot;&gt;&lt;code class=&quot;language-scala&quot; data-lang=&quot;scala&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;object&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt; Names &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;unapplySeq&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;name&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;String&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Option&lt;&#x2F;span&gt;&lt;span&gt;[(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;String&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;String&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Seq&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;String&lt;&#x2F;span&gt;&lt;span&gt;])] &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= {
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;val names &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt; name.trim.split&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;if &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;names.size &amp;lt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;2&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;None 
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;else Some&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;((&lt;&#x2F;span&gt;&lt;span&gt;names.last, names.head, names.drop&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span&gt;.dropRight&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)))
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;}
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Have a close look at the return type and the construction of the &lt;code&gt;Some&lt;&#x2F;code&gt;. Our method returns an &lt;code&gt;Option&lt;&#x2F;code&gt; of &lt;code&gt;Tuple3&lt;&#x2F;code&gt;. That tuple is created with Scala&#x27;s syntax for tuple literals by just putting the three elements – the last name, the first name, and the sequence of additional given names – in a pair of parentheses.&lt;&#x2F;p&gt;
&lt;p&gt;If this extractor is used in a pattern, the pattern will only match if at least a first and last name is contained in the given input string. The sequence of additional given names is created by dropping the first and the last element from the sequence of names.&lt;&#x2F;p&gt;
&lt;p&gt;We can use this extractor to implement an alternative greeting method:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;scala&quot; style=&quot;background-color:#002b36;color:#839496;&quot; class=&quot;language-scala &quot;&gt;&lt;code class=&quot;language-scala&quot; data-lang=&quot;scala&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;greet&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fullName&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;String&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;) =&lt;&#x2F;span&gt;&lt;span&gt; fullName &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;match &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;case Names&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;lastName&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;firstName&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;_*&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;=&amp;gt; &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Good morning, &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; + firstName + &amp;quot; &amp;quot; + lastName + &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;!&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;case &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d33682;&quot;&gt;_ &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;=&amp;gt; &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Welcome! Please make sure to fill in your name!&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Feel free to play around with this in the REPL or a worksheet.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;summary&quot;&gt;Summary&lt;&#x2F;h2&gt;
&lt;p&gt;In this article, we learned how to implement and use extractors that return variable-length sequences of extracted values. Extractors are a pretty powerful mechanism. They can often be re-used in flexible ways and provide a powerful way to extend the kinds of patterns you can match against.&lt;&#x2F;p&gt;
&lt;p&gt;We will revisit extractors in a case study towards the end of this series. In the next part, however, I will give an overview of the different ways in which patterns can be applied in Scala code – there is more to it than just the pattern matching you have seen in the examples so far.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;em&gt;Update, 24.01.2013: I updated the code example implementing the GivenNames extractor. Thanks to Christophe Bliard for pointing out a mistake in there.&lt;&#x2F;em&gt;&lt;&#x2F;p&gt;
</description>
      </item>
      <item>
          <title>The Neophyte&#x27;s Guide to Scala Part 1: Extractors</title>
          <pubDate>Wed, 21 Nov 2012 00:00:00 +0000</pubDate>
          <author>Unknown</author>
          <link>https://danielwestheide.com/blog/the-neophytes-guide-to-scala-part-1-extractors/</link>
          <guid>https://danielwestheide.com/blog/the-neophytes-guide-to-scala-part-1-extractors/</guid>
          <description xml:base="https://danielwestheide.com/blog/the-neophytes-guide-to-scala-part-1-extractors/">&lt;p&gt;More than 50,000 people signed up for Martin Odersky&#x27;s course &lt;a href=&quot;https:&#x2F;&#x2F;www.coursera.org&#x2F;course&#x2F;progfun&quot;&gt;&quot;Functional Programming Principles in Scala&quot;&lt;&#x2F;a&gt; at Coursera. That&#x27;s a huge number of developers for whom this might have been the first contact with Scala, functional programming, or both.&lt;&#x2F;p&gt;
&lt;p&gt;If you are reading this, maybe you are one of them, or maybe you have started to learn Scala by some other means. In any case, if you have started to learn Scala, you are excited to delve deeper into this beautiful language, but it all still feels a little exotic or foggy to you, then the series of articles that is beginning with this one is for you.&lt;&#x2F;p&gt;
&lt;p&gt;Even though the Coursera course covered quite a lot of what you need to know about Scala, the given time constraints made it impossible to explain everything in detail. As a result, some Scala features might seem like magic to you if you are new to the language. You are able to use them somehow, but you haven&#x27;t fully grasped how they work and, more importantly, why they work as they do.&lt;&#x2F;p&gt;
&lt;p&gt;In this article and the ones following in the coming weeks, I would like to clear things up and remove those question marks. I will also explain some of the features of the Scala language and library that I had trouble with when I started learning the language, partially because I didn&#x27;t find any good explanations for them, but instead just stumbled upon them in the wild. Where appropriate, I will also try to give guidance on how to use these features in an idiomatic™ way.&lt;&#x2F;p&gt;
&lt;p&gt;Enough of the introductions. Before I begin, keep in mind that, while having attended the Coursera course is not a prerequisite for following this series, having roughly the knowledge of Scala as can be acquired in that course is definitely helpful, and I will sometimes refer to the course.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;so-how-does-this-pattern-matching-thingie-actually-work&quot;&gt;So how does this pattern matching thingie actually work?&lt;&#x2F;h2&gt;
&lt;p&gt;In the Coursera course, you came across one very powerful language feature of Scala: &lt;a href=&quot;http:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Pattern_matching&quot;&gt;Pattern matching&lt;&#x2F;a&gt;. It allows you to decompose a given data structure, binding the values it was constructed from to variables. It&#x27;s not an idea that is unique to Scala, though. Other prominent languages in which pattern matching plays an important role are Haskell and Erlang, for instance.&lt;&#x2F;p&gt;
&lt;p&gt;If you followed the video lectures, you saw that you can decompose various kinds of data structures using pattern matching, among them lists, streams, and any instances of case classes. So is this list of data structures that can be destructured fixed, or can you extend it somehow? And first of all, how does this actually work? Is there some kind of magic involved that allows you to write things like the following?&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;scala&quot; style=&quot;background-color:#002b36;color:#839496;&quot; class=&quot;language-scala &quot;&gt;&lt;code class=&quot;language-scala&quot; data-lang=&quot;scala&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;case class&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt; User&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;firstName&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;String&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;lastName&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;String&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;score&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;Int&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;advance&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;xs&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;List&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;User&lt;&#x2F;span&gt;&lt;span&gt;]&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;) =&lt;&#x2F;span&gt;&lt;span&gt; xs &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;match &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;case User&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d33682;&quot;&gt;_&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d33682;&quot;&gt;_&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;score1&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span&gt; :: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;User&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d33682;&quot;&gt;_&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d33682;&quot;&gt;_&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;score2&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span&gt; :: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d33682;&quot;&gt;_ &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;=&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; score1 - score2
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;case &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d33682;&quot;&gt;_ &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;=&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;0
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;As it turns out, there isn&#x27;t. At least not much. The reason why you are able to write the above code (no matter how little sense this particular example makes) is the existence of so-called &lt;a href=&quot;http:&#x2F;&#x2F;www.scala-lang.org&#x2F;node&#x2F;112&quot;&gt;extractors&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;In its most widely applied form, an extractor has the opposite role of a constructor: While the latter creates an object from a given list of parameters, an extractor extracts the parameters from which an object passed to it was created.&lt;&#x2F;p&gt;
&lt;p&gt;The Scala library contains some predefined extractors, and we will have a look at one of them shortly. Case classes are special because Scala automatically creates a companion object for them: a singleton object that contains not only an &lt;code&gt;apply&lt;&#x2F;code&gt; method for creating new instances of the case class, but also an &lt;code&gt;unapply&lt;&#x2F;code&gt; method – the method that needs to be implemented by an object in order for it to be an extractor.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;our-first-extractor-yay&quot;&gt;Our first extractor, yay!&lt;&#x2F;h3&gt;
&lt;p&gt;There is more than one possible signature for a valid &lt;code&gt;unapply&lt;&#x2F;code&gt; method, but we will start with the ones that are most widely used. Let&#x27;s pretend that our &lt;code&gt;User&lt;&#x2F;code&gt; class is not a case class after all, but instead a trait, with two classes extending it, and for the moment, it only contains a single field:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;scala&quot; style=&quot;background-color:#002b36;color:#839496;&quot; class=&quot;language-scala &quot;&gt;&lt;code class=&quot;language-scala&quot; data-lang=&quot;scala&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;trait&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt; User &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;name&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;String
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;}
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;class&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt; FreeUser&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;val name&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;String&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;extends &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;User
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;class&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt; PremiumUser&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;val name&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;String&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;extends &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;User
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;We want to implement extractors for the &lt;code&gt;FreeUser&lt;&#x2F;code&gt; and &lt;code&gt;PremiumUser&lt;&#x2F;code&gt; classes in respective companion objects, just as Scala would have done were these case classes. If your extractor is supposed to only extract a single parameter from a given object, the signature of an &lt;code&gt;unapply&lt;&#x2F;code&gt; method looks like this:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;scala&quot; style=&quot;background-color:#002b36;color:#839496;&quot; class=&quot;language-scala &quot;&gt;&lt;code class=&quot;language-scala&quot; data-lang=&quot;scala&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;unapply&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;object&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;S&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Option&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;T&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The method expects some object of type &lt;code&gt;S&lt;&#x2F;code&gt; and returns an &lt;code&gt;Option&lt;&#x2F;code&gt; of type &lt;code&gt;T&lt;&#x2F;code&gt;, which is the type of the parameter it extracts. Remember that &lt;code&gt;Option&lt;&#x2F;code&gt; is Scala&#x27;s safe alternative to the existence of &lt;code&gt;null&lt;&#x2F;code&gt; values. There will be a separate article about it, but for now, it&#x27;s enough to know that the &lt;code&gt;unapply&lt;&#x2F;code&gt; method returns either &lt;code&gt;Some[T]&lt;&#x2F;code&gt; (if it could successfully extract the parameter from the given object) or &lt;code&gt;None&lt;&#x2F;code&gt;, which means that the parameters could not be extracted, as per the rules determined by the extractor implementation.&lt;&#x2F;p&gt;
&lt;p&gt;Here are our extractors:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;scala&quot; style=&quot;background-color:#002b36;color:#839496;&quot; class=&quot;language-scala &quot;&gt;&lt;code class=&quot;language-scala&quot; data-lang=&quot;scala&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;trait&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt; User &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;name&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;String
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;}
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;class&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt; FreeUser&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;val name&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;String&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;extends &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;User
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;class&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt; PremiumUser&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;val name&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;String&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;extends &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;User
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;object&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt; FreeUser &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;unapply&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;user&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;FreeUser&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Option&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;String&lt;&#x2F;span&gt;&lt;span&gt;] &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Some&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;user.name&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;}
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;object&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt; PremiumUser &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;unapply&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;user&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;PremiumUser&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Option&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;String&lt;&#x2F;span&gt;&lt;span&gt;] &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Some&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;user.name&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;We can now use this in the REPL:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;scala&quot; style=&quot;background-color:#002b36;color:#839496;&quot; class=&quot;language-scala &quot;&gt;&lt;code class=&quot;language-scala&quot; data-lang=&quot;scala&quot;&gt;&lt;span&gt;scala&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;FreeUser&lt;&#x2F;span&gt;&lt;span&gt;.unapply&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;new FreeUser&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Daniel&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;))
&lt;&#x2F;span&gt;&lt;span&gt;res0: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Option&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;String&lt;&#x2F;span&gt;&lt;span&gt;] &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Some&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Daniel&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;But you wouldn&#x27;t usually call this method directly. Scala calls an extractor&#x27;s &lt;code&gt;unapply&lt;&#x2F;code&gt; method if the extractor is used as an &lt;em&gt;extractor pattern&lt;&#x2F;em&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;If the result of calling &lt;code&gt;unapply&lt;&#x2F;code&gt; is &lt;code&gt;Some[T]&lt;&#x2F;code&gt;, this means that the pattern matches, and the extracted value is bound to the variable declared in the pattern. If it is &lt;code&gt;None&lt;&#x2F;code&gt;, this means that the pattern doesn&#x27;t match and the next case statement is tested.&lt;&#x2F;p&gt;
&lt;p&gt;Let&#x27;s use our extractors for pattern matching:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;scala&quot; style=&quot;background-color:#002b36;color:#839496;&quot; class=&quot;language-scala &quot;&gt;&lt;code class=&quot;language-scala&quot; data-lang=&quot;scala&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;val user&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;User &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;new PremiumUser&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Daniel&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;user &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;match &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;case FreeUser&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;name&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;=&amp;gt; &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Hello &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; + name
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;case PremiumUser&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;name&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;=&amp;gt; &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Welcome back, dear &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; + name
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;As you will already have noticed, our two extractors never return &lt;code&gt;None&lt;&#x2F;code&gt;. The example shows that this makes more sense than it might seem at first. If you have an object that could be of some type or another, you can check its type and destructure it at the same time.&lt;&#x2F;p&gt;
&lt;p&gt;In the example, the &lt;code&gt;FreeUser&lt;&#x2F;code&gt; pattern will not match because it expects an object of a different type than we pass it. Since it wants an object of type &lt;code&gt;FreeUser&lt;&#x2F;code&gt;, not one of type &lt;code&gt;PremiumUser&lt;&#x2F;code&gt;, this extractor is never even called. Hence, the &lt;code&gt;user&lt;&#x2F;code&gt; value is now passed to the &lt;code&gt;unapply&lt;&#x2F;code&gt; method of the &lt;code&gt;PremiumUser&lt;&#x2F;code&gt; companion object, as that extractor is used in the second pattern. This pattern will match, and the returned value is bound to the &lt;code&gt;name&lt;&#x2F;code&gt; parameter.&lt;&#x2F;p&gt;
&lt;p&gt;Later in this article, we will see an example of an extractor that does not always return &lt;code&gt;Some[T]&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;extracting-several-values&quot;&gt;Extracting several values&lt;&#x2F;h3&gt;
&lt;p&gt;Now, let&#x27;s assume that our classes against which we want to match have some more fields:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;scala&quot; style=&quot;background-color:#002b36;color:#839496;&quot; class=&quot;language-scala &quot;&gt;&lt;code class=&quot;language-scala&quot; data-lang=&quot;scala&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;trait&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt; User &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;name&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;String
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;score&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;Int
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;}
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;class&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt; FreeUser&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;val name&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;String&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;val score&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;Int&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;val upgradeProbability&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;Double&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;) 
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;extends &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;User
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;class&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt; PremiumUser&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;val name&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;String&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;val score&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;Int&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;extends &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;User
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;If an extractor pattern is supposed to decompose a given data structure into more than one parameter, the signature of the extractor&#x27;s &lt;code&gt;unapply&lt;&#x2F;code&gt; method looks like this:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;scala&quot; style=&quot;background-color:#002b36;color:#839496;&quot; class=&quot;language-scala &quot;&gt;&lt;code class=&quot;language-scala&quot; data-lang=&quot;scala&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;unapply&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;object&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;S&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Option&lt;&#x2F;span&gt;&lt;span&gt;[(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;T1&lt;&#x2F;span&gt;&lt;span&gt;, ..., &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Tn&lt;&#x2F;span&gt;&lt;span&gt;)]
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The method expects some object of type &lt;code&gt;S&lt;&#x2F;code&gt; and returns an &lt;code&gt;Option&lt;&#x2F;code&gt; of type &lt;code&gt;TupleN&lt;&#x2F;code&gt;, where &lt;code&gt;N&lt;&#x2F;code&gt; is the number of parameters to extract.&lt;&#x2F;p&gt;
&lt;p&gt;Let&#x27;s adapt our extractors to the modified classes:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;scala&quot; style=&quot;background-color:#002b36;color:#839496;&quot; class=&quot;language-scala &quot;&gt;&lt;code class=&quot;language-scala&quot; data-lang=&quot;scala&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;trait&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt; User &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;name&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;String
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;score&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;Int
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;}
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;class&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt; FreeUser&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;val name&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;String&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;val score&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;Int&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;val upgradeProbability&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;Double&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;) 
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;extends &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;User
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;class&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt; PremiumUser&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;val name&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;String&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;val score&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;Int&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;extends &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;User
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;object&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt; FreeUser &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;unapply&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;user&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;FreeUser&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Option&lt;&#x2F;span&gt;&lt;span&gt;[(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;String&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;Int&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;Double&lt;&#x2F;span&gt;&lt;span&gt;)] &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= 
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Some&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;((&lt;&#x2F;span&gt;&lt;span&gt;user.name, user.score, user.upgradeProbability&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;))
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;}
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;object&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt; PremiumUser &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;unapply&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;user&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;PremiumUser&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Option&lt;&#x2F;span&gt;&lt;span&gt;[(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;String&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;Int&lt;&#x2F;span&gt;&lt;span&gt;)] &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Some&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;((&lt;&#x2F;span&gt;&lt;span&gt;user.name, user.score&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;))
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;We can now use this extractor for pattern matching, just like we did with the previous version:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;scala&quot; style=&quot;background-color:#002b36;color:#839496;&quot; class=&quot;language-scala &quot;&gt;&lt;code class=&quot;language-scala&quot; data-lang=&quot;scala&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;val user&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;User &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;new FreeUser&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Daniel&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;3000&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;0.7&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;d&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;user &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;match &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;case FreeUser&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;name&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d33682;&quot;&gt;_&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;p&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;=&amp;gt; 
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;if &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;p &amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;0.75&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span&gt; name + &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;, what can we do for you today?&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;else &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Hello &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; + name
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;case PremiumUser&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;name&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d33682;&quot;&gt;_&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;=&amp;gt; &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Welcome back, dear &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; + name
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h3 id=&quot;a-boolean-extractor&quot;&gt;A Boolean extractor&lt;&#x2F;h3&gt;
&lt;p&gt;Sometimes, you don&#x27;t really have the need to extract parameters from a data structure against which you want to match – instead, you just want to do a simple boolean check. In this case, the third and last of the available &lt;code&gt;unapply&lt;&#x2F;code&gt; method signatures comes in handy, which expects a value of type &lt;code&gt;S&lt;&#x2F;code&gt; and returns a &lt;code&gt;Boolean&lt;&#x2F;code&gt;:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;scala&quot; style=&quot;background-color:#002b36;color:#839496;&quot; class=&quot;language-scala &quot;&gt;&lt;code class=&quot;language-scala&quot; data-lang=&quot;scala&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;unapply&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;object&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;S&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;Boolean
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Used in a pattern, the pattern will match if the extractor returns &lt;code&gt;true&lt;&#x2F;code&gt;. Otherwise the next case, if available, is tried.&lt;&#x2F;p&gt;
&lt;p&gt;In the previous example, we had some logic that checks whether a free user is likely to be susceptible to being persuaded to upgrade their account. Let&#x27;s place this logic in its own boolean extractor:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;scala&quot; style=&quot;background-color:#002b36;color:#839496;&quot; class=&quot;language-scala &quot;&gt;&lt;code class=&quot;language-scala&quot; data-lang=&quot;scala&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;object&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt; premiumCandidate &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;	&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;unapply&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;user&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;FreeUser&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;Boolean &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt; user.upgradeProbability &amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;0.75
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;As you can see here, it is not necessary for an extractor to reside in the companion object of the class for which it is applicable. Using such a boolean extractor is as simple as this:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;scala&quot; style=&quot;background-color:#002b36;color:#839496;&quot; class=&quot;language-scala &quot;&gt;&lt;code class=&quot;language-scala&quot; data-lang=&quot;scala&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;val user&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;User &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;new FreeUser&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Daniel&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;2500&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;0.8&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;d&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;user &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;match &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;	&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;case &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;freeUser &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;@ premiumCandidate&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;() &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;=&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; initiateSpamProgram&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;freeUser&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;	&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;case &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d33682;&quot;&gt;_ &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;=&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; sendRegularNewsletter&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;user&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This example shows that a boolean extractor is used by just passing it an empty parameter list, which makes sense because it doesn&#x27;t really extract any parameters to be bound to variables.&lt;&#x2F;p&gt;
&lt;p&gt;There is one other peculiarity in this example: I am pretending that our fictional &lt;code&gt;initiateSpamProgram&lt;&#x2F;code&gt; function expects an instance of &lt;code&gt;FreeUser&lt;&#x2F;code&gt; because premium users are never to be spammed. Our pattern matching is against any type of &lt;code&gt;User&lt;&#x2F;code&gt;, though, so I cannot pass &lt;code&gt;user&lt;&#x2F;code&gt; to the &lt;code&gt;initiateSpamProgram&lt;&#x2F;code&gt; function – not without ugly type casting anyway.&lt;&#x2F;p&gt;
&lt;p&gt;Luckily, Scala&#x27;s pattern matching allows to bind the value that is matched to a variable, too, using the type that the used extractor expects. This is done using the &lt;code&gt;@&lt;&#x2F;code&gt; operator. Since our &lt;code&gt;premiumCandidate&lt;&#x2F;code&gt; extractor expects an instance of &lt;code&gt;FreeUser&lt;&#x2F;code&gt;, we have therefore bound the matched value to a variable &lt;code&gt;freeUser&lt;&#x2F;code&gt; of type &lt;code&gt;FreeUser&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;Personally, I haven&#x27;t used boolean extractors that much, but it&#x27;s good to know they exist, as sooner or later you will probably find yourself in a situation where they come in handy.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;infix-operation-patterns&quot;&gt;Infix operation patterns&lt;&#x2F;h3&gt;
&lt;p&gt;If you followed the Scala course at Coursera, you learned that you can destructure lists and streams in a way that is akin to one of the ways you can create them, using the cons operator, &lt;code&gt;::&lt;&#x2F;code&gt; or &lt;code&gt;#::&lt;&#x2F;code&gt;, respectively:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;scala&quot; style=&quot;background-color:#002b36;color:#839496;&quot; class=&quot;language-scala &quot;&gt;&lt;code class=&quot;language-scala&quot; data-lang=&quot;scala&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;val xs &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;58&lt;&#x2F;span&gt;&lt;span&gt; #:: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;43&lt;&#x2F;span&gt;&lt;span&gt; #:: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;93&lt;&#x2F;span&gt;&lt;span&gt; #:: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Stream&lt;&#x2F;span&gt;&lt;span&gt;.empty
&lt;&#x2F;span&gt;&lt;span&gt;xs &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;match &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;case &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;first&lt;&#x2F;span&gt;&lt;span&gt; #:: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;second&lt;&#x2F;span&gt;&lt;span&gt; #:: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d33682;&quot;&gt;_ &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;=&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; first - second
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;case &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d33682;&quot;&gt;_ &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;=&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; -&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;1
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Maybe you have wondered why that is possible. The answer is that as an alternative to the extractor pattern notation we have seen so far, Scala also allows extractors to be used in an infix notation. So, instead of writing &lt;code&gt;e(p1, p2)&lt;&#x2F;code&gt;, where &lt;code&gt;e&lt;&#x2F;code&gt; is the extractor and &lt;code&gt;p1&lt;&#x2F;code&gt; and &lt;code&gt;p2&lt;&#x2F;code&gt; are the parameters to be extracted from a given data structure, it&#x27;s always possible to write &lt;code&gt;p1 e p2&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;Hence, the &lt;em&gt;infix operation pattern&lt;&#x2F;em&gt; &lt;code&gt;head #:: tail&lt;&#x2F;code&gt; could also be written as &lt;code&gt;#::(head, tail)&lt;&#x2F;code&gt;, and our &lt;code&gt;PremiumUser&lt;&#x2F;code&gt; extractor could also be used in a pattern that reads &lt;code&gt;name PremiumUser score&lt;&#x2F;code&gt;. However, this is not something you would do in practice. Usage of infix operation patterns is only recommended for extractors that indeed are supposed to read like operators, which is true for the cons operators of &lt;code&gt;List&lt;&#x2F;code&gt; and &lt;code&gt;Stream&lt;&#x2F;code&gt;, but certainly not for our &lt;code&gt;PremiumUser&lt;&#x2F;code&gt; extractor.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;a-closer-look-at-the-stream-extractor&quot;&gt;A closer look at the Stream extractor&lt;&#x2F;h3&gt;
&lt;p&gt;Even though there is nothing special about how the &lt;code&gt;#::&lt;&#x2F;code&gt; extractor can be used in pattern matching, let&#x27;s take a look at it, to better understand what is going on in our pattern matching code above. Also, this is a good example of an extractor that, depending on the state of the passed in data structure, may return &lt;code&gt;None&lt;&#x2F;code&gt; and thus not match.&lt;&#x2F;p&gt;
&lt;p&gt;Here is the complete extractor, taken from the sources of Scala 2.9.2:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;scala&quot; style=&quot;background-color:#002b36;color:#839496;&quot; class=&quot;language-scala &quot;&gt;&lt;code class=&quot;language-scala&quot; data-lang=&quot;scala&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;object&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt; #:: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;unapply&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;A&lt;&#x2F;span&gt;&lt;span&gt;]&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;xs&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Stream&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;A&lt;&#x2F;span&gt;&lt;span&gt;]&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Option&lt;&#x2F;span&gt;&lt;span&gt;[(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;A&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Stream&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;A&lt;&#x2F;span&gt;&lt;span&gt;])] &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;=
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;if &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;xs.isEmpty&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;None
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;else Some&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;((&lt;&#x2F;span&gt;&lt;span&gt;xs.head, xs.tail&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;))
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;If the given &lt;code&gt;Stream&lt;&#x2F;code&gt; instance is empty, it just returns &lt;code&gt;None&lt;&#x2F;code&gt;. Thus, &lt;code&gt;case head #:: tail&lt;&#x2F;code&gt; will not match for an empty stream. Otherwise, a &lt;code&gt;Tuple2&lt;&#x2F;code&gt; is returned, the first element of which is the head of the stream, while the second element of the tuple is the tail, which is itself a &lt;code&gt;Stream&lt;&#x2F;code&gt; again. Hence, &lt;code&gt;case head #:: tail&lt;&#x2F;code&gt; will match for a stream of one or more elements. If it has only one element, &lt;code&gt;tail&lt;&#x2F;code&gt; will be bound to the empty stream.&lt;&#x2F;p&gt;
&lt;p&gt;To understand how this extractor works for our pattern matching example, let&#x27;s rewrite that example, going from infix operation patterns to the usual extractor pattern notation:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;scala&quot; style=&quot;background-color:#002b36;color:#839496;&quot; class=&quot;language-scala &quot;&gt;&lt;code class=&quot;language-scala&quot; data-lang=&quot;scala&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;val xs &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;58&lt;&#x2F;span&gt;&lt;span&gt; #:: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;43&lt;&#x2F;span&gt;&lt;span&gt; #:: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;93&lt;&#x2F;span&gt;&lt;span&gt; #:: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Stream&lt;&#x2F;span&gt;&lt;span&gt;.empty
&lt;&#x2F;span&gt;&lt;span&gt;xs &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;match &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;case&lt;&#x2F;span&gt;&lt;span&gt; #::&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;first&lt;&#x2F;span&gt;&lt;span&gt;, #::&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;second&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d33682;&quot;&gt;_&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;=&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; first - second
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;case &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d33682;&quot;&gt;_ &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;=&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; -&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;1
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;First, the extractor is called for the intitial stream &lt;code&gt;xs&lt;&#x2F;code&gt; that is passed to the pattern matching block. The extractor returns &lt;code&gt;Some((xs.head, xs.tail))&lt;&#x2F;code&gt;, so &lt;code&gt;first&lt;&#x2F;code&gt; is bound to &lt;code&gt;58&lt;&#x2F;code&gt;, while the tail of &lt;code&gt;xs&lt;&#x2F;code&gt; is passed to the extractor again, which is used again inside of the first one. Again, it returns the head and and tail as a &lt;code&gt;Tuple2&lt;&#x2F;code&gt; wrapped in a &lt;code&gt;Some&lt;&#x2F;code&gt;, so that &lt;code&gt;second&lt;&#x2F;code&gt; is bound to the value &lt;code&gt;43&lt;&#x2F;code&gt;, while the tail is bound to the wildcard &lt;code&gt;_&lt;&#x2F;code&gt; and thus thrown away.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;using-extractors&quot;&gt;Using extractors&lt;&#x2F;h3&gt;
&lt;p&gt;So when and how should you actually make use of custom extractors, especially considering that you can get some useful extractors for free if you make use of case classes?&lt;&#x2F;p&gt;
&lt;p&gt;While some people point out that using case classes and pattern matching against them breaks encapsulation, coupling the way you match against data with its concrete representation, this criticism usually stems from an object-oriented point of view. It&#x27;s a good idea, if you want to do functional programming in Scala, to use case classes as &lt;a href=&quot;http:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Algebraic_data_type&quot;&gt;algebraic data types (ADTs)&lt;&#x2F;a&gt; that contain pure data and no behaviour whatsoever.&lt;&#x2F;p&gt;
&lt;p&gt;Usually, implementing your own extractors is only necessary if you want to extract something from a type you have no control over, or if you need additional ways of pattern matching against certain data. For example, a common usage of extractors is to extract meaningful values from some string. As an exercise, think about how you would implement and use a &lt;code&gt;URLExtractor&lt;&#x2F;code&gt; that takes &lt;code&gt;String&lt;&#x2F;code&gt; representations of URLs.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;conclusion&quot;&gt;Conclusion&lt;&#x2F;h3&gt;
&lt;p&gt;In this first part of the series, we have examined extractors, the workhorse behind pattern matching in Scala. You have learned how to implement your own extractors and how the implementation of an extractor relates to its usage in a pattern.&lt;&#x2F;p&gt;
&lt;p&gt;We haven&#x27;t covered all there is to say about extractors, because this article is already long enough as it is. In the next part of this series, I am going to revisit extractors, covering how to implement them if you you want to bind a variable number of extracted parameters in a pattern.&lt;&#x2F;p&gt;
&lt;p&gt;Please do let me know if this article was helpful to you or if something is not clear to you.&lt;&#x2F;p&gt;
</description>
      </item>
      <item>
          <title>Functional Programming Principles in scala: A Wrap-up</title>
          <pubDate>Sun, 04 Nov 2012 00:00:00 +0000</pubDate>
          <author>Unknown</author>
          <link>https://danielwestheide.com/blog/functional-programming-principles-in-scala-a-wrap-up/</link>
          <guid>https://danielwestheide.com/blog/functional-programming-principles-in-scala-a-wrap-up/</guid>
          <description xml:base="https://danielwestheide.com/blog/functional-programming-principles-in-scala-a-wrap-up/">&lt;p&gt;For the past seven weeks, I have been participating in the online class &lt;a href=&quot;https:&#x2F;&#x2F;www.coursera.org&#x2F;course&#x2F;progfun&quot;&gt;&quot;Functional Programming Principles in Scala&quot;&lt;&#x2F;a&gt; provided by Martin Odersky, the EPFL, and Coursera. This has not only reignited my more or less dormant passion for the Scala language, but has also been tremendously fun and instructive at the same time. Having just finished the final assignment, let me tell you why.&lt;&#x2F;p&gt;
&lt;p&gt;First of all, of course, the chance of attending a class on a programming language and paradigm that is conducted by the very designer of that language, a distinguished expert in their field, is all kinds of amazing in itself. It&#x27;s great to live in a time in which attending such a class online and for free is possible for thousands of interested students who would otherwise not have had the opportunity at all, or who otherwise have to make do with the often times mediocre lecturers at their own university.&lt;&#x2F;p&gt;
&lt;p&gt;This was my first ever online class, and I must say I didn&#x27;t really miss the physical presence of a meatspace university class at all. In each of the seven weeks, the programme consisted of numerous video lectures, sessions of five to 25 minutes, altogether between one and two hours a week. These video lectures were accompanied by weekly (or sometimes biweekly) programming assignments in which students had to transfer and apply what they learned to a new problem. Usually, this meant to implement several pre-defined functions, with the groundwork already laid for you.&lt;&#x2F;p&gt;
&lt;p&gt;During the first couple of weeks, the course was focused a lot on the basic principles of functional programming, while keeping the amount of Scala syntax and library code used to an absolute minimum. This changed in the second half, when a bunch of important Scala concepts were introduced, like for comprehensions and pattern matching.&lt;&#x2F;p&gt;
&lt;p&gt;I found the course to be challenging at just the right level, and the assignments had just about the right mixture of highly abstract problems and ones with interesting practical relevance, like analyzing a feed of tweets or implementing a solver for an interesting Flash game. I have already had my share of experience with functional programming in general, and Scala in particular, so some small parts of assignments were just a finger exercise, and I assume I haven&#x27;t had to think about getting my Scala code syntactically right or more idiomatic as much as students new to the language.&lt;&#x2F;p&gt;
&lt;p&gt;Thankfully, such experience didn&#x27;t make solving the assignments a cakewalk altogether. I noticed that I wasn&#x27;t used to thinking in the abstract any more as much as I had thought, so having to reflect on these problems to come up with a solution was not always easy and took a while, but was also one of most important profits I drew from the class. I literally felt like a student again, back in the days, and enjoyed this very much.&lt;&#x2F;p&gt;
&lt;p&gt;The Coursera Scala course is highly recommended if you want to delve into the world of functional programming or you want to learn Scala. I don&#x27;t know any other up-and-coming language with such high-quality, educational and fun learning material, so I can imagine Scala gaining some traction from it.&lt;&#x2F;p&gt;
&lt;p&gt;Have you attended the course, too? If so, tell me about your experience!&lt;&#x2F;p&gt;
</description>
      </item>
      <item>
          <title>Bonn Agile August 2012: CQRS-based Architectures</title>
          <pubDate>Sun, 26 Aug 2012 00:00:00 +0000</pubDate>
          <author>Unknown</author>
          <link>https://danielwestheide.com/blog/bonn-agile-august-2012-cqrs-based-architectures/</link>
          <guid>https://danielwestheide.com/blog/bonn-agile-august-2012-cqrs-based-architectures/</guid>
          <description xml:base="https://danielwestheide.com/blog/bonn-agile-august-2012-cqrs-based-architectures/">&lt;p&gt;Last Monday, at the August meetup of &lt;a href=&quot;https:&#x2F;&#x2F;sites.google.com&#x2F;site&#x2F;bonnagile&#x2F;&quot;&gt;Bonn Agile&lt;&#x2F;a&gt;, I did a talk about architectures based on the CQRS pattern. I presented various opportunities enabled by this pattern, specifically asymmetrical scaling, separating the models for reading and writing, eventual consistency, and event sourcing. I also put up &lt;a href=&quot;https:&#x2F;&#x2F;github.com&#x2F;dwestheide&#x2F;cqrs-bonnagile-demo&quot;&gt;some very crude demo code in Scala&lt;&#x2F;a&gt; to show how one might implement a command handler and a write model handler, and how easy it is to build the current state of an aggregate from the events stored for it.&lt;&#x2F;p&gt;
&lt;p&gt;Although the pattern and the architectural decisions it enables haven&#x27;t made their way into the mainstream yet, a couple of people had at least heard of them. Since I was speaking to an audience of agilists, I tried to bring two questions into the focus of the discussion:&lt;&#x2F;p&gt;
&lt;ol&gt;
&lt;li&gt;How can such an architecture help us be more agile?&lt;&#x2F;li&gt;
&lt;li&gt;When does it rather hinder us from being agile?&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;p&gt;I might put my thoughts on these topics into a blog post if there is sufficient interest in that.&lt;&#x2F;p&gt;
&lt;p&gt;What I really found interesting about the evolving discussion at the end of my talk was the fact that some people have built applications in which events are actually an almost natural part of the domain model. Apparently, they implemented some of the ideas behind event sourcing, but without really knowing about it, so events did not play the first class role that they have in an event-sourced architecture. This suggests that currently, CQRS-based architectures, and especially event sourcing, are indeed under-used.&lt;&#x2F;p&gt;
&lt;p&gt;Link: &lt;a href=&quot;https:&#x2F;&#x2F;github.com&#x2F;dwestheide&#x2F;cqrs-bonnagile&#x2F;&quot;&gt;Slides&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
</description>
      </item>
      <item>
          <title>Book Review: Team Geek</title>
          <pubDate>Mon, 13 Aug 2012 00:00:00 +0000</pubDate>
          <author>Unknown</author>
          <link>https://danielwestheide.com/blog/book-review-team-geek/</link>
          <guid>https://danielwestheide.com/blog/book-review-team-geek/</guid>
          <description xml:base="https://danielwestheide.com/blog/book-review-team-geek/">&lt;p&gt;&lt;img src=&quot;&#x2F;images&#x2F;content&#x2F;teamgeek.jpg&quot; alt=&quot;Cover image of the book &amp;#39;Team Geek&amp;#39;&quot; &#x2F;&gt;
Many books have been published about the technical aspects of software development, and, granted, a lot has been written about how to successfully manage IT projects and about why and how to apply certain agile methodologies. Yet, you hardly find any literature dealing with the human factors of software development, neither does it play a significant role on any of the computer science curricula that I know of.&lt;&#x2F;p&gt;
&lt;p&gt;This is is especially strange because, based on my own experience and my conversations with other developers, human problems among team members as well as with other stakeholders, be it managers or users, seem to be very common and can have more serious consequences for the success of a project than is apparent at first glance.&lt;&#x2F;p&gt;
&lt;p&gt;Behold, though, fellow developers, you no longer need to shed lonely tears! &lt;a href=&quot;http:&#x2F;&#x2F;shop.oreilly.com&#x2F;product&#x2F;0636920018025.do&quot;&gt;&quot;Team Geek&quot;&lt;&#x2F;a&gt; is a completely non-technical book by Google&#x27;s Brian Fitzpatrick and Ben Collins-Sussman that, while also containing some crucial lessons for management people, is targeted explicitly at software developers.&lt;&#x2F;p&gt;
&lt;p&gt;As a developer, you are nearly always in the position of being a member of a team, and all of a sudden, sooner than you know, you may even find yourself leading a team, even if it&#x27;s only temporary. This book teaches you a couple of important things:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;How to be a great team member&lt;&#x2F;li&gt;
&lt;li&gt;How to be a good team leader&lt;&#x2F;li&gt;
&lt;li&gt;How to recognize and deal with poisonous team members of various categories&lt;&#x2F;li&gt;
&lt;li&gt;How to deal with poisonous people from outside your team&lt;&#x2F;li&gt;
&lt;li&gt;How to exert influence in your company or other organization&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;In each of the chapters of the book, Fitzpatrick and Collins-Sussman describe certain problems that are likely to occur in software development teams or projects sooner or later, often anecdotally, sometimes also as a collection of anti-patterns. Most of the problems can occur both in a company environment as well as in medium-sized or big open source projects. However, some of them are very specific to open-source projects and stem directly from the authors&#x27; experience with the Subversion project.&lt;&#x2F;p&gt;
&lt;p&gt;All this is written in a very entertaining and engaging way, and unless you are an extremely lucky person or someone who is only about to start their career as a software developer, you will often nod in agreement, hopefully smiling, thinking of specific instances of the described problems that you have experienced first-hand. The text is far from being dry, and so are the humourous illustrations that clarify the authors&#x27; respective messages.&lt;&#x2F;p&gt;
&lt;p&gt;While the price of the book is worth paying for the entertainment value of the described problems alone, what makes this book really valuable, of course, are the proposed ways of dealing with each of the described problems, which are based on many years of experience by the two authors.&lt;&#x2F;p&gt;
&lt;p&gt;In the end, Fitzpatrick and Collins-Sussman always come back to the trinity of humility, respect, and trust (HRT) as the core of nearly all their problem solutions, and to building and keeping intact a specific team culture. Of course, there are more specific recommendations than that, but they are mostly based on one or more of those three principles.&lt;&#x2F;p&gt;
&lt;p&gt;While some of the solutions are rather obvious, and some very few I do not wholeheartedly agree with, all in all I found this book to be a real treasure, giving me new insight, new perspectives, and also more confidence in dealing with the addressed human problems of software development. An especially inspirational chapter was the one on &quot;The Art of Organizational Manipulation&quot;. It demonstrates that fighting the ways an organization works, no matter how pointless its rules are, only brings you so far. Instead, understand the rules, play by them, and the chances are usually much better that you will be able to change it to the better, thereby essentially hacking the organization.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;strong&gt;TL;DR:&lt;&#x2F;strong&gt; Being a great developer also comprises being a great team player. &quot;Team Geek&quot; teaches you exactly that and should be required reading for prospective and experienced software developers alike. Even if you don&#x27;t have any of the covered problems in your current team, you will want to be prepared for it.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;em&gt;Team Geek: A Software Developer&#x27;s Guide to Working Well with Others&lt;br &#x2F;&gt;
By Brian W. Fitzpatrick and Ben Collins-Sussman&lt;br &#x2F;&gt;
Publisher: O&#x27;Reilly&lt;br &#x2F;&gt;
Released: July 2012&lt;br &#x2F;&gt;
Pages: 194&lt;&#x2F;em&gt;&lt;&#x2F;p&gt;
&lt;p&gt;Link: &lt;a href=&quot;http:&#x2F;&#x2F;shop.oreilly.com&#x2F;product&#x2F;0636920018025.do&quot;&gt;&quot;Team Geek&quot; at the O&#x27;Reilly shop&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
</description>
      </item>
      <item>
          <title>Advance Hackathon 2012: A Retrospective</title>
          <pubDate>Tue, 01 May 2012 00:00:00 +0000</pubDate>
          <author>Unknown</author>
          <link>https://danielwestheide.com/blog/advance-hackathon-2012-a-retrospective/</link>
          <guid>https://danielwestheide.com/blog/advance-hackathon-2012-a-retrospective/</guid>
          <description xml:base="https://danielwestheide.com/blog/advance-hackathon-2012-a-retrospective/">&lt;p&gt;Last weekend, I attended the &lt;em&gt;Advance Hackathon 2012&lt;&#x2F;em&gt;, which was hosted at the Coworking Space Gasmotorenfabrik in Cologne. Even though I had been intending to participate at such an event for a long time, this was indeed my first hackathon.&lt;&#x2F;p&gt;
&lt;p&gt;There were 80-90 people working on 20 projects from Saturday, 10am to Sunday 4pm. All of the projects were very interesting, and most of them managed to make a lot of progress in the course of the weekend.&lt;&#x2F;p&gt;
&lt;p&gt;The project I was involved in is called &lt;a href=&quot;https:&#x2F;&#x2F;github.com&#x2F;lunjure&#x2F;&quot;&gt;lunjure&lt;&#x2F;a&gt;. Our goal was to create a lunch planner as a single-page HTML5 web application that is written in pure Clojure and ClojureScript, making use of web sockets for most of the communication between the client and the server. While we didn&#x27;t finish this project in the short time of the hackathon, we managed to implement all of the core functionality and will probably continue working on it. If nothing else, it is certainly a nice demo project showcasing the usage of the aforementioned technologies.&lt;&#x2F;p&gt;
&lt;p&gt;The only minor complaint I have to make is this: Everybody who had submitted a project idea was to pitch it on Saturday morning in order to find interested developers. However, my impression was that least 80 percent of the participants had already formed teams before the hackathon, and that for the most part, people who already knew each other worked together in a team. This is okay, of course (and maybe my impression is completely wrong anyway), but it made the whole pitching process somewhat redundant.&lt;&#x2F;p&gt;
&lt;p&gt;Nevertheless, it was an amazing weekend, and all the people involved in the organization did an outstanding and highly professional job, and what little need for improvisation arose once in a while was handled very gracefully. Also, the whole atmosphere was very friendly and cooperative. I would definitely like to see an Advance Hackathon 2013.&lt;&#x2F;p&gt;
</description>
      </item>
      <item>
          <title>Mission Statement</title>
          <pubDate>Fri, 01 Jul 2011 00:00:00 +0000</pubDate>
          <author>Unknown</author>
          <link>https://danielwestheide.com/blog/mission-statement/</link>
          <guid>https://danielwestheide.com/blog/mission-statement/</guid>
          <description xml:base="https://danielwestheide.com/blog/mission-statement/">&lt;p&gt;I have been intending to start blogging again for a long time. Now I have finally managed to put this into practice. I am a maker of software, and my plan is to primarily blog about my experience as well as opinions on various aspects of the software development process. But from time to time, I will certainly put on the user hat and write about the intricacies of human computer interaction from that very different perspective.&lt;&#x2F;p&gt;
&lt;p&gt;Why do I do all that? I am humble enough to be convinced that the majority of the world will not care about what I write here, will not even read it. But I like to write, it helps me to get a clearer understanding by forcing me to organize my thoughts. Also, I have found other people&#x27;s blogs to be not only interesting, but also very useful in certain situations, and I do think that my contributions might be helpful to the one or the other person, too, or at least entertaining.&lt;&#x2F;p&gt;
&lt;p&gt;For those interested, I decided to stay away from hosted blogging systems and instead make use of &lt;a href=&quot;http:&#x2F;&#x2F;jekyllrb.com&quot;&gt;Jekyll&lt;&#x2F;a&gt;, a very nice, blog-aware static-site generator written in Ruby.&lt;&#x2F;p&gt;
</description>
      </item>
    </channel>
</rss>
