<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Menestret Martin</title>
    <description>The latest articles on DEV Community by Menestret Martin (@mmenestret).</description>
    <link>https://dev.to/mmenestret</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F138466%2F038416bd-8c8f-43ac-ad9c-dd5f9fca84a3.jpg</url>
      <title>DEV Community: Menestret Martin</title>
      <link>https://dev.to/mmenestret</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/mmenestret"/>
    <language>en</language>
    <item>
      <title>Anatomy of semigroups and monoids</title>
      <dc:creator>Menestret Martin</dc:creator>
      <pubDate>Thu, 21 Feb 2019 10:24:52 +0000</pubDate>
      <link>https://dev.to/mmenestret/anatomy-of-semigroups-and-monoids-22i8</link>
      <guid>https://dev.to/mmenestret/anatomy-of-semigroups-and-monoids-22i8</guid>
      <description>

&lt;p&gt;[Check my articles on my blog &lt;a href="http://geekocephale.com/blog/"&gt;here&lt;/a&gt;]&lt;/p&gt;

&lt;p&gt;I will try to group here, in an anatomy atlas, basic notions of functional programming that I find myself explaining often lately into a series of articles.&lt;/p&gt;

&lt;p&gt;The idea here is to have a place to point people needing explanations and to increase my own understanding of these subjects by trying to explain them the best I can.&lt;br&gt;
I'll try to focus more on making the reader feel an intuition, a feeling about the concepts rather than on the perfect, strict correctness of my explanations.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Part 1: &lt;a href="https://dev.to/mmenestret/anatomy-of-functional-programming-1bpg"&gt;Anatomy of functional programming&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Part 2: &lt;a href="https://dev.to/mmenestret/anatomy-of-an-algebra-3cd9"&gt;Anatomy of an algebra&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Part 3: &lt;a href="https://dev.to/mmenestret/anatomy-of-a-type-class-440j"&gt;Anatomy of a type class&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Part 4: &lt;a href="https://dev.to/mmenestret/anatomy-of-semigroups-and-monoids-22i8"&gt;Anatomy of semi groups and monoids&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Part 5: &lt;a href="https://dev.to/mmenestret/anatomy-of-functors-and-category-theory-2gf0"&gt;Anatomy of functors and category theory&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Part 6: Anatomy of the tagless final encoding - Yet to come !&lt;/li&gt;
&lt;/ul&gt;

&lt;h1&gt;
  
  
  What is a &lt;em&gt;semigroup&lt;/em&gt; ?
&lt;/h1&gt;

&lt;h2&gt;
  
  
  General definition
&lt;/h2&gt;

&lt;p&gt;&lt;em&gt;Semigroup&lt;/em&gt; (and &lt;em&gt;monoid&lt;/em&gt;, you'll see later) is a complicated word for a &lt;strong&gt;really&lt;/strong&gt; simple concept.&lt;br&gt;
We'll cover quickly &lt;em&gt;semigroups&lt;/em&gt; and we'll explain longer &lt;em&gt;monoids&lt;/em&gt; since they are strongly related.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Wikipedia's&lt;/em&gt; definition is:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;In mathematics, a &lt;em&gt;semigroup&lt;/em&gt; is an algebraic structure consisting of a set together with an associative binary operation.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Ok, that sounds a bit abstract, let's try to re-phrase it with programming terms:&lt;/p&gt;

&lt;p&gt;In the context of programming, a &lt;em&gt;semigroup&lt;/em&gt; is composed of two things:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;A type &lt;code&gt;A&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;An associative operation combining two values of type &lt;code&gt;A&lt;/code&gt; into a value of type &lt;code&gt;A&lt;/code&gt;, let's call it &lt;code&gt;combine&lt;/code&gt;

&lt;ul&gt;
&lt;li&gt;That would be a &lt;em&gt;function&lt;/em&gt; with a type signature: &lt;code&gt;(A, A) =&amp;gt; A&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Which is associative, meaning that the order in which you combine elements together (where you decide to put your parenthesis) does not matter

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;combine(combine(a1, a2), a3) == combine(a1, combine(a2, a3))&lt;/code&gt; with &lt;code&gt;a1&lt;/code&gt;, &lt;code&gt;a2&lt;/code&gt;, &lt;code&gt;a3&lt;/code&gt; values of type &lt;code&gt;A&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Then it is said that &lt;strong&gt;&lt;code&gt;A&lt;/code&gt; forms a &lt;em&gt;semigroup&lt;/em&gt; under &lt;code&gt;combine&lt;/code&gt;&lt;/strong&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Some examples
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Integer under addition
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;type: &lt;code&gt;Int&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;operation: &lt;code&gt;+&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Indeed,&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;+&lt;/code&gt; type here is: &lt;code&gt;(Int, Int) =&amp;gt; Int&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;+&lt;/code&gt; is associative &lt;code&gt;(20 + 20) + 2 == 20 + (20 + 2)&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Integers form a &lt;em&gt;semigroup&lt;/em&gt; under addition.&lt;/p&gt;

&lt;h3&gt;
  
  
  Boolean under OR
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;type: &lt;code&gt;Boolean&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;operation: &lt;code&gt;||&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Indeed,&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;||&lt;/code&gt; type here is: &lt;code&gt;(Boolean, Boolean) =&amp;gt; Boolean&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;||&lt;/code&gt; is associative &lt;code&gt;(true || false) || true == true || (false || true)&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Booleans form a &lt;em&gt;semigroup&lt;/em&gt; under OR.&lt;/p&gt;

&lt;h3&gt;
  
  
  List under list concatenation
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;type: &lt;code&gt;List[A]&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;operation: &lt;code&gt;++&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Indeed,&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;++&lt;/code&gt; type here is: &lt;code&gt;(List[A], List[A]) =&amp;gt; List[A]&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;++&lt;/code&gt; is associative &lt;code&gt;(List(1, 2) ++ List(3, 4)) ++ List(5, 6) == List(1, 2) ++ (List(3, 4) ++ List(5, 6))&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  More examples !
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Integers under multiplication&lt;/li&gt;
&lt;li&gt;Booleans under AND&lt;/li&gt;
&lt;li&gt;String under concatenation&lt;/li&gt;
&lt;li&gt;A LOT more.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;We'll now explore &lt;em&gt;monoids&lt;/em&gt; since they are a "upgraded" version of &lt;em&gt;semigroups&lt;/em&gt;.&lt;/p&gt;

&lt;h1&gt;
  
  
  What is a &lt;em&gt;monoid&lt;/em&gt; ?
&lt;/h1&gt;

&lt;h2&gt;
  
  
  General definition
&lt;/h2&gt;

&lt;p&gt;Given the definition of a &lt;em&gt;semigroup&lt;/em&gt;, the definition of a &lt;em&gt;monoid&lt;/em&gt; is pretty straight forward:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;In mathematics, a &lt;em&gt;monoid&lt;/em&gt; is an algebraic structure consisting of a set together with an associative binary operation and an identity element.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Which means that a &lt;em&gt;monoid&lt;/em&gt; is a &lt;em&gt;semigroup&lt;/em&gt; plus an identity element.&lt;/p&gt;

&lt;p&gt;In our programming terms:&lt;/p&gt;

&lt;p&gt;In the context of programming, a &lt;em&gt;monoid&lt;/em&gt; is composed of two things:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;A &lt;em&gt;semigroup&lt;/em&gt;:

&lt;ul&gt;
&lt;li&gt;A type &lt;code&gt;A&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;An associative operation combining two values of type &lt;code&gt;A&lt;/code&gt; into a value of type &lt;code&gt;A&lt;/code&gt;, let's call it &lt;code&gt;combine&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;An identity element of type &lt;code&gt;A&lt;/code&gt;, let's call it &lt;code&gt;id&lt;/code&gt;, that has to obey the following laws:

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;combine(a, id) == a&lt;/code&gt; with &lt;code&gt;a&lt;/code&gt; a value of type &lt;code&gt;A&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;combine(id, a) == a&lt;/code&gt; with &lt;code&gt;a&lt;/code&gt; a value of type &lt;code&gt;A&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Then it is said that &lt;strong&gt;&lt;code&gt;A&lt;/code&gt; forms a &lt;em&gt;monoid&lt;/em&gt; under &lt;code&gt;combine&lt;/code&gt; with identity element &lt;code&gt;id&lt;/code&gt;&lt;/strong&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Some examples
&lt;/h2&gt;

&lt;p&gt;We could take our &lt;em&gt;semigroups&lt;/em&gt; examples here and add their respective identity elements:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Integer under addition

&lt;ul&gt;
&lt;li&gt;With identity element &lt;code&gt;0&lt;/code&gt;:

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;42 + 0 = 42&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;0 + 42 = 42&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Boolean under OR

&lt;ul&gt;
&lt;li&gt;With identity element &lt;code&gt;false&lt;/code&gt;:

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;true || false == true&lt;/code&gt;, &lt;code&gt;false || true == true&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;&lt;code&gt;false || false == false&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;List under list concatenation

&lt;ul&gt;
&lt;li&gt;With identity element &lt;code&gt;Nil&lt;/code&gt; (empty List):

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;List(1, 2, 3) ++ Nil == List(1, 2, 3)&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;Nil ++ List(1, 2, 3) == List(1, 2, 3)&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Whenever you have an identity element for your &lt;em&gt;semigroup&lt;/em&gt;'s type and &lt;code&gt;combine&lt;/code&gt; operation that holds the identity laws, then you have a &lt;em&gt;monoid&lt;/em&gt; for it.&lt;/p&gt;

&lt;p&gt;But be careful, there are some &lt;em&gt;semigroups&lt;/em&gt; which are not &lt;em&gt;monoids&lt;/em&gt;:&lt;/p&gt;

&lt;p&gt;Tuples form a &lt;em&gt;semigroup&lt;/em&gt; under first (which gives back the tuple's first element).&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;type: &lt;code&gt;Tuple2[A, A]&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;operation: &lt;code&gt;first&lt;/code&gt; (&lt;code&gt;def first[A](t: Tuple2[A, A]): A = t._1&lt;/code&gt;)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Indeed,&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;first&lt;/code&gt; type here is: &lt;code&gt;Tuple2[A, A] =&amp;gt; A&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;first&lt;/code&gt; is associative &lt;code&gt;first(Tuple2(first(Tuple2(a1, a2)), a3)) == first(Tuple2(a1, first(Tuple2(a2, a3))))&lt;/code&gt; with &lt;code&gt;a1&lt;/code&gt;, &lt;code&gt;a2&lt;/code&gt;, &lt;code&gt;a3&lt;/code&gt; values of type &lt;code&gt;A&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;But there is no way to provide an identity element &lt;code&gt;id&lt;/code&gt; of type &lt;code&gt;A&lt;/code&gt; so that:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;first(Tuple2(id, a)) == a&lt;/code&gt; and &lt;code&gt;first(Tuple2(a, id)) == a&lt;/code&gt; with &lt;code&gt;a&lt;/code&gt; a value of type &lt;code&gt;A&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h1&gt;
  
  
  What the hell is it for ?
&lt;/h1&gt;

&lt;p&gt;&lt;em&gt;Monoid&lt;/em&gt; is a functional programming constructs that &lt;strong&gt;embodies the notion of combining "things" together&lt;/strong&gt;, often in order to reduce "things" into one "thing". Given that the combining operation is associative, it can be &lt;strong&gt;parallelized&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;And that's a &lt;strong&gt;BIG&lt;/strong&gt; deal.&lt;/p&gt;

&lt;p&gt;As a simple illustration, this is what you can do, absolutely fearlessly when you know your type &lt;code&gt;A&lt;/code&gt; forms a &lt;em&gt;monoid&lt;/em&gt; under &lt;code&gt;combine&lt;/code&gt; with identity &lt;code&gt;id&lt;/code&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;You have a huge, large, massive list of &lt;code&gt;A&lt;/code&gt;s that you want to reduce into a single &lt;code&gt;A&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;You have a cluster of N nodes and a master node&lt;/li&gt;
&lt;li&gt;You split your huge, large, massive list of &lt;code&gt;A&lt;/code&gt;s in N sub lists&lt;/li&gt;
&lt;li&gt;You distribute each sub list to a node of your cluster&lt;/li&gt;
&lt;li&gt;Each node reduce its own sub list by &lt;code&gt;combining&lt;/code&gt; its elements 2 by 2 down to 1 final element&lt;/li&gt;
&lt;li&gt;They send back their results to the master node&lt;/li&gt;
&lt;li&gt;The master node only has N intermediary results to &lt;code&gt;combine&lt;/code&gt; down (in the same order as the sub lists these intermediary results were produced from, remember, associativity !) to a final result&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;You successfully, without any fear of messing things up, parallelized, almost for free, a reduction process on a huge list thanks to &lt;em&gt;monoids&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;Does it sound familiar ? That's naively how fork-join operations works on Spark ! Thank you &lt;em&gt;monoids&lt;/em&gt; !&lt;/p&gt;

&lt;h1&gt;
  
  
  How can we encode them in Scala ?
&lt;/h1&gt;

&lt;p&gt;&lt;em&gt;Semigroups&lt;/em&gt; and &lt;em&gt;monoids&lt;/em&gt; are encoded as &lt;a href="https://dev.to/mmenestret/anatomy-of-a-type-class-440j"&gt;type classes&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;We are gonna go through a simple implementation example, you should never have to do it by hand like that since everything we'll do is provided by awesome FP libraries like &lt;a href="https://github.com/typelevel/cats"&gt;Cats&lt;/a&gt; or &lt;a href="https://github.com/scalaz/scalaz"&gt;Scalaz&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Here are our two type classes:&lt;/p&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight scala"&gt;&lt;code&gt;&lt;span class="k"&gt;trait&lt;/span&gt; &lt;span class="nc"&gt;Semigroup&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;S&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="n"&gt;combine&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;s1&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;S&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;s2&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;S&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;S&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;trait&lt;/span&gt; &lt;span class="nc"&gt;Monoid&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;M&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="nc"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;Semigroup&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;M&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;val&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;M&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;And here is my business domain modeling:&lt;/p&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight scala"&gt;&lt;code&gt;&lt;span class="k"&gt;type&lt;/span&gt; &lt;span class="kt"&gt;ItemId&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Int&lt;/span&gt;
&lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Sale&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;items&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;List&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;ItemId&lt;/span&gt;&lt;span class="o"&gt;],&lt;/span&gt; &lt;span class="n"&gt;totalPrice&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Double&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;I want to be able to combine all my year's sales into one big, consolidated, sale.&lt;/p&gt;

&lt;p&gt;Let's define a &lt;em&gt;monoid&lt;/em&gt; type class instance for &lt;code&gt;Sale&lt;/code&gt; by defining:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;id&lt;/code&gt; being an empty &lt;code&gt;Sale&lt;/code&gt; which contains no item ids, and 0 as &lt;code&gt;totalPrice&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;combine&lt;/code&gt; as concatenation of item id lists and addition of &lt;code&gt;totalPrice&lt;/code&gt;s&lt;/li&gt;
&lt;/ul&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight scala"&gt;&lt;code&gt;&lt;span class="k"&gt;implicit&lt;/span&gt; &lt;span class="k"&gt;val&lt;/span&gt; &lt;span class="n"&gt;saleMonoid&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Monoid&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;Sale&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="k"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Monoid&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;Sale&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;override&lt;/span&gt; &lt;span class="k"&gt;val&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Sale&lt;/span&gt;                          &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Sale&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;List&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;empty&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;ItemId&lt;/span&gt;&lt;span class="o"&gt;],&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;override&lt;/span&gt; &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="n"&gt;combine&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;s1&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Sale&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;s2&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Sale&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Sale&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Sale&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;s1&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;items&lt;/span&gt; &lt;span class="o"&gt;++&lt;/span&gt; &lt;span class="n"&gt;s2&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;items&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;s1&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;totalPrice&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;s2&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;totalPrice&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Then I can use a lot of existing tooling, generic functions, leveraging the fact that the types they are working on are instances of &lt;em&gt;monoid&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;combineAll&lt;/code&gt; (which is also provided by &lt;em&gt;Cats&lt;/em&gt; or &lt;em&gt;Scalaz&lt;/em&gt;) is one of them and permit to, generically, combine all my sales together for free !&lt;/p&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight scala"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="n"&gt;combineAll&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;A&lt;/span&gt;&lt;span class="o"&gt;](&lt;/span&gt;&lt;span class="n"&gt;as&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;List&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;A&lt;/span&gt;&lt;span class="o"&gt;])(&lt;/span&gt;&lt;span class="k"&gt;implicit&lt;/span&gt; &lt;span class="n"&gt;M&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Monoid&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;A&lt;/span&gt;&lt;span class="o"&gt;])&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;A&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="n"&gt;accumulate&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;accumulator&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;A&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;remaining&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;List&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;A&lt;/span&gt;&lt;span class="o"&gt;])&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;A&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;remaining&lt;/span&gt; &lt;span class="k"&gt;match&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="nc"&gt;Nil&lt;/span&gt;          &lt;span class="k"&gt;⇒&lt;/span&gt; &lt;span class="n"&gt;accumulator&lt;/span&gt;
        &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="n"&gt;head&lt;/span&gt; &lt;span class="o"&gt;::&lt;/span&gt; &lt;span class="n"&gt;tail&lt;/span&gt; &lt;span class="k"&gt;⇒&lt;/span&gt; &lt;span class="n"&gt;accumulate&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;M&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;combine&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;accumulator&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;head&lt;/span&gt;&lt;span class="o"&gt;),&lt;/span&gt; &lt;span class="n"&gt;tail&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
    &lt;span class="n"&gt;accumulate&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;M&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;as&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;val&lt;/span&gt; &lt;span class="n"&gt;sales2018&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;List&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;Sale&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="k"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;List&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Sale&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;List&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;),&lt;/span&gt; &lt;span class="mi"&gt;32&lt;/span&gt;&lt;span class="o"&gt;),&lt;/span&gt; &lt;span class="nc"&gt;Sale&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;List&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;),&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="o"&gt;))&lt;/span&gt;
&lt;span class="k"&gt;val&lt;/span&gt; &lt;span class="n"&gt;totalSale&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Sale&lt;/span&gt;       &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;combineAll&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sales2018&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// Sale(List(0, 1),42)
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Nota bene:&lt;/strong&gt; Here, for sake of simplicity, I did not implement &lt;code&gt;combineAll&lt;/code&gt; with &lt;code&gt;foldLeft&lt;/code&gt; so I don't have to explain &lt;code&gt;foldLeft&lt;/code&gt;, but you should know that my &lt;code&gt;accumulate&lt;/code&gt; inner function &lt;strong&gt;is&lt;/strong&gt; &lt;code&gt;foldLeft&lt;/code&gt; and that &lt;code&gt;combineAll&lt;/code&gt; should in fact be implemented like that:&lt;/p&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight scala"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="n"&gt;combineAll&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;A&lt;/span&gt;&lt;span class="o"&gt;](&lt;/span&gt;&lt;span class="n"&gt;as&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;List&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;A&lt;/span&gt;&lt;span class="o"&gt;])(&lt;/span&gt;&lt;span class="k"&gt;implicit&lt;/span&gt; &lt;span class="n"&gt;M&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Monoid&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;A&lt;/span&gt;&lt;span class="o"&gt;])&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;A&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;as&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;foldLeft&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;M&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="o"&gt;)(&lt;/span&gt;&lt;span class="n"&gt;M&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;combine&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Voilà !&lt;/p&gt;

&lt;h1&gt;
  
  
  More material
&lt;/h1&gt;

&lt;p&gt;If you want to keep diving deeper, some interesting stuff can be found on my &lt;a href="https://github.com/mmenestret/fp-ressources"&gt;FP resources list&lt;/a&gt; and in particular:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://underscore.io/books/scala-with-cats/"&gt;Scala with Cats - Semigroup and monoid chapters&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://parkergordon.io/2017/04/03/why-spark-cant-foldleft/"&gt;Why Spark can’t foldLeft: Monoids and Associativity&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://typelevel.org/cats/typeclasses/semigroup.html"&gt;Cats documentation&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Let me know if you need more&lt;/li&gt;
&lt;/ul&gt;

&lt;h1&gt;
  
  
  Conclusion
&lt;/h1&gt;

&lt;p&gt;To sum up, we saw:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;How simple &lt;strong&gt;semigroups&lt;/strong&gt; and &lt;strong&gt;monoids&lt;/strong&gt; are and how closely related they are&lt;/li&gt;
&lt;li&gt;We saw examples of &lt;em&gt;semigroups&lt;/em&gt; and &lt;em&gt;monoids&lt;/em&gt;
&lt;/li&gt;
&lt;li&gt;We had an insight about how useful these FP constructs can be in real life&lt;/li&gt;
&lt;li&gt;And finally we showed how they are encoded in &lt;em&gt;Scala&lt;/em&gt; and had a glimpse on what you can do with it thanks to major FP librairies&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I'll try to keep that blog post updated.&lt;br&gt;
If there are any additions, imprecision or mistakes that I should correct or if you need more explanations, feel free to contact me on Twitter or by mail !&lt;/p&gt;


</description>
      <category>scala</category>
      <category>fp</category>
      <category>functional</category>
    </item>
    <item>
      <title>Anatomy of an algebra</title>
      <dc:creator>Menestret Martin</dc:creator>
      <pubDate>Thu, 21 Feb 2019 10:16:37 +0000</pubDate>
      <link>https://dev.to/mmenestret/anatomy-of-an-algebra-3cd9</link>
      <guid>https://dev.to/mmenestret/anatomy-of-an-algebra-3cd9</guid>
      <description>&lt;p&gt;[Check my articles on my blog &lt;a href="http://geekocephale.com/blog/"&gt;here&lt;/a&gt;]&lt;/p&gt;

&lt;p&gt;I will try to group here, in an anatomy atlas, basic notions of functional programming that I find myself explaining often lately into a series of articles.&lt;/p&gt;

&lt;p&gt;The idea here is to have a place to point people needing explanations and to increase my own understanding of these subjects by trying to explain them the best I can.&lt;br&gt;
I'll try to focus more on making the reader feel an intuition, a feeling about the concepts rather than on the perfect, strict correctness of my explanations.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Part 1: &lt;a href="https://dev.to/mmenestret/anatomy-of-functional-programming-1bpg"&gt;Anatomy of functional programming&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Part 2: &lt;a href="https://dev.to/mmenestret/anatomy-of-an-algebra-3cd9"&gt;Anatomy of an algebra&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Part 3: &lt;a href="https://dev.to/mmenestret/anatomy-of-a-type-class-440j"&gt;Anatomy of a type class&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Part 4: &lt;a href="https://dev.to/mmenestret/anatomy-of-semigroups-and-monoids-22i8"&gt;Anatomy of semi groups and monoids&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Part 5: &lt;a href="https://dev.to/mmenestret/anatomy-of-functors-and-category-theory-2gf0"&gt;Anatomy of functors and category theory&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Part 6: Anatomy of the tagless final encoding - Yet to come !&lt;/li&gt;
&lt;/ul&gt;
&lt;h1&gt;
  
  
  What is an algebra ?
&lt;/h1&gt;

&lt;p&gt;Functional programmers tend to talk a lot about &lt;em&gt;algebras&lt;/em&gt;, so a good starting point would be to understand what an &lt;em&gt;algebra&lt;/em&gt; is.&lt;br&gt;
A simple definition would be (from &lt;em&gt;Wikipedia&lt;/em&gt;):&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;In its most general form, an &lt;em&gt;algebra&lt;/em&gt; is the study of mathematical symbols and the rules for manipulating these symbols&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Then to rephrase it in simpler terms, it is a system formed by:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Symbols&lt;/strong&gt;, items or "things"&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Operations&lt;/strong&gt; on thoses "things"&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Properties and rules&lt;/strong&gt; about those operations&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;A simple &lt;em&gt;algebra&lt;/em&gt; example would be:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Symbols: the integer numbers&lt;/li&gt;
&lt;li&gt;Operations: addition and multiplication&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://en.wikipedia.org/wiki/Integer#Algebraic_properties"&gt;Properties and rules&lt;/a&gt;:

&lt;ul&gt;
&lt;li&gt;Closure: the result of the two operations is itself in integer number&lt;/li&gt;
&lt;li&gt;Associativity: &lt;code&gt;a + (b + c) = (a + b) + c&lt;/code&gt; and &lt;code&gt;a × (b × c) = (a × b) × c&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Commutativity: &lt;code&gt;a + b = b + a&lt;/code&gt; and &lt;code&gt;a × b = b × a&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Identity elements: &lt;code&gt;a + 0 = a&lt;/code&gt; and &lt;code&gt;a * 1 = a&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;And so on (check the link if you want to see the rest)&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;h1&gt;
  
  
  How does it relates to FP ?
&lt;/h1&gt;
&lt;h2&gt;
  
  
  Domain modeling
&lt;/h2&gt;

&lt;p&gt;When modeling a business domain, in functional programming, &lt;a href="https://dev.to/mmenestret/anatomy-of-a-type-class-440j"&gt;we separate strongly data from behaviors&lt;/a&gt; (see the &lt;em&gt;Data/behavior relationship&lt;/em&gt; section).&lt;/p&gt;

&lt;p&gt;To do that:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;We create types on one side&lt;/li&gt;
&lt;li&gt;Pure functions manipulating those types&lt;/li&gt;
&lt;li&gt;And these functions have to respect some strict business logic&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Doesn't that ring any bell ?&lt;/p&gt;

&lt;p&gt;We manipulate &lt;em&gt;algebras&lt;/em&gt; !&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Symbols&lt;/strong&gt;: our business types&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Operations&lt;/strong&gt;: our business functions acting on those types&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Properties and rules&lt;/strong&gt;: our business logic implemented in these functions, and our &lt;em&gt;property based tests&lt;/em&gt;

&lt;ul&gt;
&lt;li&gt;We won't cover those here, but to give you an idea, these are not unit tests, but tests based on function properties that they must &lt;em&gt;always&lt;/em&gt; verify (such as: "A combination of several sales cannot result in a negative price") while tested against a wide range of more or less random values generated by a test framework&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;
  
  
  A concrete example
&lt;/h3&gt;

&lt;p&gt;To make it clearer, let's take a dummy example.&lt;/p&gt;

&lt;p&gt;Say we have to produce sales reports, we would probably model our domain like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight scala"&gt;&lt;code&gt;&lt;span class="k"&gt;type&lt;/span&gt; &lt;span class="kt"&gt;Amount&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Int&lt;/span&gt;
&lt;span class="k"&gt;type&lt;/span&gt; &lt;span class="kt"&gt;Price&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Int&lt;/span&gt;

&lt;span class="k"&gt;final&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;ID&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="k"&gt;val&lt;/span&gt; &lt;span class="nv"&gt;value&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Int&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="k"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;AnyVal&lt;/span&gt;
&lt;span class="k"&gt;final&lt;/span&gt; &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Item&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;ID&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;price&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Price&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;final&lt;/span&gt; &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Sales&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;items&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;List&lt;/span&gt;&lt;span class="o"&gt;[(&lt;/span&gt;&lt;span class="kt"&gt;ID&lt;/span&gt;, &lt;span class="kt"&gt;Amount&lt;/span&gt;&lt;span class="o"&gt;)],&lt;/span&gt; &lt;span class="n"&gt;totalPrice&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Price&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;trait&lt;/span&gt; &lt;span class="nc"&gt;SalesOperations&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;combineAll&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sales&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;List&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;Sales&lt;/span&gt;&lt;span class="o"&gt;])&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Sales&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;generateReport&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sales&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Sales&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;In that case:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Symbols&lt;/strong&gt;: &lt;code&gt;Amount&lt;/code&gt;, &lt;code&gt;Price&lt;/code&gt;, &lt;code&gt;ID&lt;/code&gt;, &lt;code&gt;Item&lt;/code&gt;, &lt;code&gt;Sales&lt;/code&gt;, etc. (our business types)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Operations&lt;/strong&gt;: &lt;code&gt;combineAll&lt;/code&gt;, &lt;code&gt;generateReport&lt;/code&gt; (operations on those types)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Properties and rules&lt;/strong&gt;: the business logic and the tests we haven't implemented here&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;That's pretty much it !&lt;/p&gt;

&lt;p&gt;It happens frequently to have shared behaviors that are common to several types.&lt;br&gt;
We want to be able to design functions that can be run on those different types and have the expected behavior depending on their input types. Those functions are said then said to be &lt;em&gt;polymorphic&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;That's a problem we solve with &lt;a href="https://dev.to/mmenestret/anatomy-of-a-type-class-440j"&gt;&lt;em&gt;type classes&lt;/em&gt;&lt;/a&gt;.&lt;/p&gt;
&lt;h2&gt;
  
  
  What is an ADT ?
&lt;/h2&gt;

&lt;p&gt;&lt;em&gt;ADT&lt;/em&gt; stands for &lt;em&gt;algebraic data type&lt;/em&gt;. Well, we talked a bit about &lt;em&gt;algebras&lt;/em&gt; but we didn't talk about &lt;em&gt;types&lt;/em&gt; yet.&lt;/p&gt;
&lt;h3&gt;
  
  
  What is a type ?
&lt;/h3&gt;

&lt;p&gt;A &lt;em&gt;type&lt;/em&gt; or a &lt;em&gt;data type&lt;/em&gt;, is just a classification of data, a &lt;em&gt;set of values&lt;/em&gt; or &lt;em&gt;inhabitants&lt;/em&gt;, that we use to tell the compiler how we intend to use data so it can check that we respect the rules (that's the &lt;em&gt;type checking&lt;/em&gt;).&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;Boolean&lt;/code&gt; is a &lt;em&gt;type&lt;/em&gt; that classifies these two values: &lt;code&gt;true&lt;/code&gt;, &lt;code&gt;false&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;String&lt;/code&gt; is a &lt;em&gt;type&lt;/em&gt; that reprents an infinity of inhabitants, all the possible characters combinations&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;code&gt;Option[A]&lt;/code&gt; is a &lt;em&gt;type&lt;/em&gt; that represents all the values of type &lt;code&gt;A&lt;/code&gt; (wrapped in a &lt;code&gt;Some&lt;/code&gt;) + the value &lt;code&gt;None&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;Option&lt;/code&gt; has a number of inhabitants equals to the number of inhabitants of the type &lt;code&gt;A&lt;/code&gt; + 1 (the &lt;code&gt;None&lt;/code&gt; value)&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Option[Boolean]&lt;/code&gt; has 3 inhabitants: &lt;code&gt;Some(true)&lt;/code&gt;, &lt;code&gt;Some(false)&lt;/code&gt;, &lt;code&gt;None&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;Unit&lt;/code&gt; is a &lt;em&gt;type&lt;/em&gt; that has only 1 inhabitant, the value: &lt;code&gt;()&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;Noting&lt;/code&gt; is a &lt;em&gt;type&lt;/em&gt; that has no member (there is no way to create a value whose type is &lt;code&gt;Nothing&lt;/code&gt;)&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;
  
  
  What is an algebraic data type then ?
&lt;/h3&gt;

&lt;p&gt;Here is &lt;em&gt;Wikipedia&lt;/em&gt;'s definition of an &lt;em&gt;algebraic data type&lt;/em&gt;:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;In computer programming, especially functional programming and type theory, an &lt;em&gt;algebraic data type&lt;/em&gt; is a kind of composite type, i.e., a type formed by combining other types.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;We call these new types &lt;em&gt;ADTs&lt;/em&gt; because we create them by combining existing types... well, guess what ? We use an &lt;em&gt;algebra&lt;/em&gt; again !&lt;/p&gt;

&lt;p&gt;This is algebra is the following:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Symbols&lt;/strong&gt;: existing types: primitive types, other existing ADTs, ... (&lt;code&gt;Sales&lt;/code&gt;, &lt;code&gt;Boolean&lt;/code&gt;, &lt;code&gt;String&lt;/code&gt;, &lt;code&gt;Int&lt;/code&gt;, ...)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Operations&lt;/strong&gt;: &lt;em&gt;sum&lt;/em&gt; and &lt;em&gt;product&lt;/em&gt; (we'll see what they mean)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Properties and rules&lt;/strong&gt;: some properties and laws about these &lt;em&gt;sum&lt;/em&gt; and &lt;em&gt;product&lt;/em&gt; operations (we'll also see what they mean)&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;
  
  
  Sum
&lt;/h3&gt;

&lt;p&gt;&lt;em&gt;sum&lt;/em&gt; is the action of combining by "summing" their respective values together.&lt;/p&gt;

&lt;p&gt;You can see it as a way to define that: &lt;strong&gt;values of a &lt;em&gt;sum type&lt;/em&gt; can only be either a value of this OR that type&lt;/strong&gt; (the &lt;strong&gt;OR&lt;/strong&gt; is the key intuition here).&lt;/p&gt;

&lt;p&gt;It is done, in &lt;em&gt;Scala&lt;/em&gt;, usually by using:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;em&gt;sealed traits&lt;/em&gt; and their instances as &lt;em&gt;case classes&lt;/em&gt; or &lt;em&gt;case objects&lt;/em&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Or less often:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The &lt;code&gt;Either[A, B]&lt;/code&gt; type&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;That way, when you declare:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight scala"&gt;&lt;code&gt;&lt;span class="k"&gt;sealed&lt;/span&gt; &lt;span class="k"&gt;trait&lt;/span&gt; &lt;span class="nc"&gt;CoinSide&lt;/span&gt;
&lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="k"&gt;object&lt;/span&gt; &lt;span class="nc"&gt;Heads&lt;/span&gt; &lt;span class="k"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;CoinSide&lt;/span&gt;
&lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="k"&gt;object&lt;/span&gt; &lt;span class="nc"&gt;Tails&lt;/span&gt; &lt;span class="k"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;CoinSide&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;You are creating an &lt;em&gt;ADT&lt;/em&gt; &lt;code&gt;CoinSide&lt;/code&gt; which is a &lt;em&gt;sum type&lt;/em&gt; (created with a &lt;em&gt;sum&lt;/em&gt; operation) which has two and only two possible values (or inhabitants), &lt;code&gt;Heads&lt;/code&gt; or &lt;code&gt;Tails&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The same would be achieved with:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight scala"&gt;&lt;code&gt;&lt;span class="k"&gt;type&lt;/span&gt; &lt;span class="kt"&gt;CoinSide&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Either&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;Heads&lt;/span&gt;, &lt;span class="kt"&gt;Tails&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Whose possible values would then be, &lt;code&gt;Left(Heads)&lt;/code&gt; or &lt;code&gt;Right(Tails)&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;These are just two different encodings for the same thing.&lt;/p&gt;

&lt;h3&gt;
  
  
  Sum properties
&lt;/h3&gt;

&lt;p&gt;The &lt;em&gt;sum&lt;/em&gt; operation also have properties.&lt;/p&gt;

&lt;p&gt;I won't go into full details here but (these examples would equally be true with &lt;em&gt;sealed traits&lt;/em&gt; + their implementations instead of &lt;code&gt;Either&lt;/code&gt; type):&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;The number of values of a &lt;em&gt;sum type&lt;/em&gt; is the &lt;em&gt;sum&lt;/em&gt; of the values of its composing types members (just as you would assume for addition on integers)&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;Boolean&lt;/code&gt; has &lt;strong&gt;two&lt;/strong&gt; inhabitants: &lt;code&gt;true&lt;/code&gt; and &lt;code&gt;false&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;code&gt;Either[Boolean, Boolean]&lt;/code&gt; which is a &lt;em&gt;sum type&lt;/em&gt; of two types &lt;code&gt;Boolean&lt;/code&gt; has &lt;strong&gt;four inhabitants&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;Left(true)&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;Left(false)&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;Right(true)&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;Right(false)&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;

&lt;p&gt;&lt;code&gt;Either[Boolean, Either[Boolean, Boolean]]]&lt;/code&gt; is a &lt;em&gt;sum type&lt;/em&gt; between a &lt;code&gt;Boolean&lt;/code&gt; and another &lt;em&gt;sum type&lt;/em&gt; of a &lt;code&gt;Boolean&lt;/code&gt; and a &lt;code&gt;Boolean&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Well guess what ? It has 2 + (2 + 2) values !&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;

&lt;p&gt;It has an &lt;em&gt;identity&lt;/em&gt; element which is the &lt;code&gt;Nothing&lt;/code&gt; type which has no values at all&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;code&gt;Either[Boolean, Nothing]&lt;/code&gt; is a &lt;em&gt;sum type&lt;/em&gt; of a &lt;code&gt;Boolean&lt;/code&gt; with the &lt;em&gt;sum&lt;/em&gt; identity. Because there is no way to create a value of type &lt;code&gt;Nothing&lt;/code&gt;, it does not exist, so there is no way to construct a &lt;code&gt;Right&lt;/code&gt;, it has only &lt;strong&gt;two values&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;Left(true)&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;Left(false)&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;It is associative, &lt;code&gt;Either[Boolean, Either[Boolean, Boolean]]]&lt;/code&gt; is the same as &lt;code&gt;Either[Either[Boolean, Boolean]],Boolean]&lt;/code&gt;, you can enumerate the values, you'll see (well &lt;em&gt;isomorphic&lt;/em&gt;, we have the same &lt;em&gt;"expressive power"&lt;/em&gt; with both representations, but let's say they are the same) !&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;And so on (I'll give you more material at the end if you want to keep diving)&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Product
&lt;/h3&gt;

&lt;p&gt;&lt;em&gt;product&lt;/em&gt; is the action of combining two or more types together by "multiplying" their respective values together.&lt;/p&gt;

&lt;p&gt;You can see it as a way to define that &lt;strong&gt;values of a &lt;em&gt;product type&lt;/em&gt; are the combination of values of this AND that type&lt;/strong&gt; (the &lt;strong&gt;AND&lt;/strong&gt; is the key intuition here).&lt;/p&gt;

&lt;p&gt;It is done, in &lt;em&gt;Scala&lt;/em&gt; usually by using:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;em&gt;case classes&lt;/em&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Or less often&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;em&gt;tuples&lt;/em&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;That way, when you declare:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight scala"&gt;&lt;code&gt;&lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;TwoCoinsAndABoolean&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;fst&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;CoinSide&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;snd&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;CoinSide&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Boolean&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;// or&lt;/span&gt;
&lt;span class="k"&gt;type&lt;/span&gt; &lt;span class="kt"&gt;TwoCoinsAndABoolean&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;CoinSide&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;CoinSide&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;Boolean&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;These are just two different encodings for the same thing.&lt;/p&gt;

&lt;p&gt;You are creating an &lt;em&gt;ADT&lt;/em&gt; &lt;code&gt;TwoCoinsAndABoolean&lt;/code&gt; which is a &lt;em&gt;product types&lt;/em&gt; (created with a &lt;em&gt;product&lt;/em&gt; operation) which has the number of values of its members multiplied.&lt;/p&gt;

&lt;p&gt;In our case 8 values (2 * 2 * 2):&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;TwoCoinsAndABoolean(Heads, Heads, true)&lt;/code&gt; or &lt;code&gt;(Heads, Heads, true)&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;TwoCoinsAndABoolean(Heads, Tails, true)&lt;/code&gt; or &lt;code&gt;(Heads, Tails, true)&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;TwoCoinsAndABoolean(Tails, Heads, true)&lt;/code&gt; or &lt;code&gt;(Tails, Heads, true)&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;TwoCoinsAndABoolean(Tails, Tails, true)&lt;/code&gt; or &lt;code&gt;(Tails, Tails, true)&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;TwoCoinsAndABoolean(Heads, Heads, false)&lt;/code&gt; or &lt;code&gt;(Heads, Heads, false)&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;TwoCoinsAndABoolean(Heads, Tails, false)&lt;/code&gt; or &lt;code&gt;(Heads, Tails, false)&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;TwoCoinsAndABoolean(Tails, Heads, false)&lt;/code&gt; or &lt;code&gt;(Tails, Heads, false)&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;TwoCoinsAndABoolean(Tails, Tails, false)&lt;/code&gt; or &lt;code&gt;(Tails, Tails, false)&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;You can observe that &lt;em&gt;product types&lt;/em&gt; values are the cartesian product of their &lt;em&gt;composing types&lt;/em&gt; values !&lt;/p&gt;

&lt;h3&gt;
  
  
  Product properties
&lt;/h3&gt;

&lt;p&gt;The &lt;em&gt;product&lt;/em&gt; operation also have properties.&lt;/p&gt;

&lt;p&gt;I won't go into full details here but (these example would equally be true with &lt;em&gt;case classes&lt;/em&gt; instead of &lt;em&gt;tuples&lt;/em&gt;):&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;The number of values of a &lt;em&gt;product types&lt;/em&gt; is the &lt;em&gt;product&lt;/em&gt; of the number of the values of its combining members (as you would assume for multiplication on integers)&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;Boolean&lt;/code&gt; has &lt;strong&gt;two&lt;/strong&gt; inhabitants: &lt;code&gt;true&lt;/code&gt; and &lt;code&gt;false&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;code&gt;(Boolean, Boolean)&lt;/code&gt; which is a &lt;em&gt;product type&lt;/em&gt; of two types &lt;code&gt;Boolean&lt;/code&gt; has &lt;strong&gt;four values&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;(true, true)&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;(true, false)&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;(false, true)&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;(false, false)&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;

&lt;p&gt;&lt;code&gt;(Boolean, (Boolean, Boolean)&lt;/code&gt; is a &lt;em&gt;product type&lt;/em&gt; between a &lt;code&gt;Boolean&lt;/code&gt; and another &lt;em&gt;product type&lt;/em&gt; of a &lt;code&gt;Boolean&lt;/code&gt; and a &lt;code&gt;Boolean&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Well guess what ? It has 2 * (2 * 2) values !&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;

&lt;p&gt;It has an &lt;em&gt;identity&lt;/em&gt; which is the famous &lt;code&gt;Unit&lt;/code&gt; type which has only one inhabitant, the value &lt;code&gt;()&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;code&gt;(Boolean, Unit)&lt;/code&gt; is a &lt;em&gt;product type&lt;/em&gt; of a &lt;code&gt;Boolean&lt;/code&gt; with the &lt;em&gt;product&lt;/em&gt; identity. It has &lt;strong&gt;two values&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;(true, ())&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;(false, ())&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;It is associative, &lt;code&gt;(Boolean, (Boolean, Boolean)&lt;/code&gt; is the same as &lt;code&gt;((Boolean, Boolean),Boolean)&lt;/code&gt;, you can enumerate the values, you'll see (well &lt;em&gt;isomorphic&lt;/em&gt;, we have the same &lt;em&gt;"expressive power"&lt;/em&gt; with both representations, but let's say they are the same) !&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;And so on (I'll give you more material at the end if you want to keep diving)&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Mixed types
&lt;/h3&gt;

&lt;p&gt;Of course, as we saw in some of the examples, &lt;em&gt;ADTs&lt;/em&gt; can be combinations of other &lt;em&gt;ADTs&lt;/em&gt;, such as &lt;em&gt;sum of products&lt;/em&gt;, &lt;em&gt;products of sums&lt;/em&gt;, &lt;em&gt;product of products&lt;/em&gt; and so on.&lt;/p&gt;

&lt;h1&gt;
  
  
  More material
&lt;/h1&gt;

&lt;p&gt;If you want to keep diving deeper, some interesting stuff can be found on my &lt;a href="https://github.com/mmenestret/fp-ressources"&gt;FP resources list&lt;/a&gt; and in particular:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;a href="https://www.manning.com/books/functional-and-reactive-domain-modeling"&gt;Functional and Reactive Domain Modeling&lt;/a&gt; (An awesome book about functional domain modeling using &lt;em&gt;algebras&lt;/em&gt;)&lt;/li&gt;
&lt;li&gt;&lt;a href="https://kubuszok.com/compiled/kinds-of-types-in-scala/"&gt;Kinds of types in Scala&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.youtube.com/watch?v=s2ay9nEW3ak"&gt;Why do Functional Programmers always talk about Algebra(s)?&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h1&gt;
  
  
  Conclusion
&lt;/h1&gt;

&lt;p&gt;All that long digression was only meant to show you that, &lt;strong&gt;creating new composite data types the way we do in pure FP is done by using an algebra&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;That's why these composite types are called &lt;em&gt;Algebraic Data Types&lt;/em&gt; :).&lt;/p&gt;

&lt;p&gt;To sum up, we learnt:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;What was an &lt;em&gt;algebra&lt;/em&gt;
&lt;/li&gt;
&lt;li&gt;How algebras were used to model domains in a neat way and how it was adequate to the pure functional programming approach&lt;/li&gt;
&lt;li&gt;What was a &lt;em&gt;type&lt;/em&gt;
&lt;/li&gt;
&lt;li&gt;How creating new &lt;em&gt;data types&lt;/em&gt; was done by using an &lt;em&gt;algebra&lt;/em&gt; composed by &lt;em&gt;types&lt;/em&gt; and operations on them and thus why the types created that were called &lt;em&gt;algebraic data types&lt;/em&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I'll try to keep that blog post updated.&lt;br&gt;
If there are any additions, imprecision or mistakes that I should correct or if you need more explanations, feel free to contact me on Twitter or by mail !&lt;/p&gt;

</description>
      <category>scala</category>
      <category>functional</category>
    </item>
    <item>
      <title>Anatomy of a type class</title>
      <dc:creator>Menestret Martin</dc:creator>
      <pubDate>Thu, 21 Feb 2019 10:12:12 +0000</pubDate>
      <link>https://dev.to/mmenestret/anatomy-of-a-type-class-440j</link>
      <guid>https://dev.to/mmenestret/anatomy-of-a-type-class-440j</guid>
      <description>&lt;p&gt;[Check my articles on my blog &lt;a href="http://geekocephale.com/blog/"&gt;here&lt;/a&gt;]&lt;/p&gt;

&lt;p&gt;I will try to group here, in an anatomy atlas, basic notions of functional programming that I find myself explaining often lately into a series of articles.&lt;/p&gt;

&lt;p&gt;The idea here is to have a place to point people needing explanations and to increase my own understanding of these subjects by trying to explain them the best I can.&lt;br&gt;
I'll try to focus more on making the reader feel an intuition, a feeling about the concepts rather than on the perfect, strict correctness of my explanations.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Part 1: &lt;a href="https://dev.to/mmenestret/anatomy-of-functional-programming-1bpg"&gt;Anatomy of functional programming&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Part 2: &lt;a href="https://dev.to/mmenestret/anatomy-of-an-algebra-3cd9"&gt;Anatomy of an algebra&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Part 3: &lt;a href="https://dev.to/mmenestret/anatomy-of-a-type-class-440j"&gt;Anatomy of a type class&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Part 4: &lt;a href="https://dev.to/mmenestret/anatomy-of-semigroups-and-monoids-22i8"&gt;Anatomy of semi groups and monoids&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Part 5: &lt;a href="https://dev.to/mmenestret/anatomy-of-functors-and-category-theory-2gf0"&gt;Anatomy of functors and category theory&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Part 6: Anatomy of the tagless final encoding - Yet to come !&lt;/li&gt;
&lt;/ul&gt;
&lt;h1&gt;
  
  
  Motivation
&lt;/h1&gt;
&lt;h2&gt;
  
  
  Data / behavior relationship
&lt;/h2&gt;

&lt;p&gt;&lt;em&gt;OOP&lt;/em&gt; and &lt;em&gt;FP&lt;/em&gt; have two different approaches when it comes to data/behavior relationship:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;em&gt;Object oriented programming&lt;/em&gt; often &lt;strong&gt;combines data and behavior&lt;/strong&gt; by mixing them into classes that:

&lt;ul&gt;
&lt;li&gt;Store data as an internal state&lt;/li&gt;
&lt;li&gt;Expose methods that act on it and may mutate it&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;Functional programming&lt;/em&gt; aims to completely &lt;strong&gt;separate data from behavior&lt;/strong&gt; by:

&lt;ul&gt;
&lt;li&gt;Defining types (&lt;a href="https://dev.to/mmenestret/anatomy-of-an-algebra-3cd9"&gt;ADT&lt;/a&gt; on one side that expose no behavior and only holds data&lt;/li&gt;
&lt;li&gt;Functions taking values of some of these types as inputs, acting on them and outputting values of some of those types (leaving the input values unchanged)&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;You can check &lt;a href="https://dev.to/mmenestret/anatomy-of-functional-programming-1bpg"&gt;Anatomy of functional programming&lt;/a&gt; for examples.&lt;/p&gt;
&lt;h2&gt;
  
  
  Polymorphism
&lt;/h2&gt;

&lt;p&gt;The general idea of &lt;em&gt;polymorphism&lt;/em&gt; is to increase code re-use by using a more generic code.&lt;/p&gt;

&lt;p&gt;The are several kinds of polymorphisms but the one adressed by &lt;em&gt;type classes&lt;/em&gt; is &lt;em&gt;ad hoc polymorphism&lt;/em&gt;, so we are going to focus on that one.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Ad hoc polymorphism&lt;/em&gt; is defined on &lt;em&gt;Wikipedia&lt;/em&gt; by:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Ad hoc polymorphism is a kind of polymorphism in which polymorphic functions can be applied to arguments of different types, because a polymorphic function can denote a number of distinct and potentially heterogeneous implementations depending on the type of argument(s) to which it is applied&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;It is a mechanism allowing a function to be defined in such a way that the actual &lt;em&gt;function behavior&lt;/em&gt; that is going to depends on the types of the parameters over which it is applied to.&lt;/p&gt;

&lt;p&gt;To make it clearer, &lt;em&gt;Ad hoc polymorphism&lt;/em&gt; avoids you from having to define &lt;code&gt;printInt&lt;/code&gt;, &lt;code&gt;printDouble&lt;/code&gt;, &lt;code&gt;printString&lt;/code&gt; functions, a &lt;code&gt;print&lt;/code&gt; function is enough.&lt;/p&gt;

&lt;p&gt;Our &lt;code&gt;print&lt;/code&gt; will rely on &lt;em&gt;ad hoc polymorphism&lt;/em&gt; to behave differently based on the type of the the parameter they are applied to.&lt;/p&gt;

&lt;p&gt;The purpose of that is mainly to program by manipulating &lt;em&gt;interfaces&lt;/em&gt; (as a concept, the layer allowing elements to communicate, not as in a &lt;em&gt;Java Interface&lt;/em&gt; even their purpose is to encode that concept) exposing shared, common, behaviors or properties and use these behaviors instead of writing different function implementations for each of the concrete types abstracted by these interfaces.&lt;/p&gt;

&lt;p&gt;Your &lt;code&gt;print&lt;/code&gt; function might somehow require a &lt;code&gt;Printable&lt;/code&gt; interface, abstracting for its argument the ability to print themselves.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;em&gt;Object oriented programming&lt;/em&gt; often use &lt;strong&gt;interface subtyping&lt;/strong&gt; to permit polymorphism by making concrete classes inherit interfaces exposing the needed shared behaviors&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;Functionnal programming&lt;/em&gt;, willing to strongly separate data and behavior &lt;strong&gt;favours type classes&lt;/strong&gt; which allows to add behaviors to existing types without having to modify them or having to plan they will need these functionnalities beforehand.&lt;/li&gt;
&lt;/ul&gt;
&lt;h1&gt;
  
  
  What's a type class ?
&lt;/h1&gt;

&lt;p&gt;A &lt;em&gt;type class&lt;/em&gt; can be described literally as a class of types, a grouping of types, that shares common capabilities.&lt;/p&gt;

&lt;p&gt;It represents an abstraction of something that a grouping of types would have in common, just as &lt;em&gt;"Things that can say Hi"&lt;/em&gt; abstracts over every concrete types that have the ability to greet or as &lt;em&gt;"Things that have petals"&lt;/em&gt; might abstract over flowers in the real world.&lt;/p&gt;

&lt;p&gt;It plays the same role as an interface in OOP but it is more than that:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;It allows to add behavior to existing types (even type that are out of our codebase scope)&lt;/li&gt;
&lt;li&gt;It permits conditionnal interfacing, by that I mean, we can encode that &lt;code&gt;A&lt;/code&gt; is a member of the type class &lt;code&gt;T1&lt;/code&gt; if &lt;code&gt;A&lt;/code&gt; is also a member of type class &lt;code&gt;T2&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;h1&gt;
  
  
  How can it be done in Scala ?
&lt;/h1&gt;

&lt;p&gt;&lt;em&gt;Type classes&lt;/em&gt; are not specific to &lt;em&gt;Scala&lt;/em&gt; and can be found in many functional programming languages.&lt;br&gt;
They are not a first class construct in &lt;em&gt;Scala&lt;/em&gt;, as it can be in &lt;em&gt;Haskell&lt;/em&gt; but it still can be done quit easily.&lt;/p&gt;

&lt;p&gt;In &lt;em&gt;Scala&lt;/em&gt; it is encoded by:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;A &lt;em&gt;trait&lt;/em&gt; which exposes the &lt;em&gt;"contract"&lt;/em&gt; of the &lt;em&gt;type class&lt;/em&gt;, what the &lt;em&gt;type class&lt;/em&gt; is going to abstract over, &lt;/li&gt;
&lt;li&gt;The &lt;em&gt;concrete implementations&lt;/em&gt; of that trait for every types that we want to be instances of that &lt;em&gt;type classes&lt;/em&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Our &lt;em&gt;type class&lt;/em&gt; for &lt;em&gt;"types that can say Hi"&lt;/em&gt; is going to be:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight scala"&gt;&lt;code&gt;&lt;span class="k"&gt;trait&lt;/span&gt; &lt;span class="nc"&gt;CanGreet&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;T&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;sayHi&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;T&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Representing all the types &lt;code&gt;T&lt;/code&gt; which have the capability to &lt;code&gt;sayHi&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Given a:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight scala"&gt;&lt;code&gt;&lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Player&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;nickname&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;level&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Int&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;val&lt;/span&gt; &lt;span class="nv"&gt;geekocephale&lt;/span&gt; &lt;span class="k"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Player&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Geekocephale"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;42&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;We now create a &lt;code&gt;Player&lt;/code&gt; instance of the &lt;code&gt;CanGreet&lt;/code&gt; &lt;em&gt;trait&lt;/em&gt; for it to be an instance of our &lt;em&gt;type class&lt;/em&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight scala"&gt;&lt;code&gt;&lt;span class="k"&gt;val&lt;/span&gt; &lt;span class="nv"&gt;playerGreeter&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;CanGreet&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;Player&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="k"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;CanGreet&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;Player&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;sayHi&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Player&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="s"&gt;"Hi, I'm player ${t.nickname}, I'm lvl ${t.level} !"&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Thanks to what we did, we can now define generic functions such as:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight scala"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;greet&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;T&lt;/span&gt;&lt;span class="o"&gt;](&lt;/span&gt;&lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;T&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;greeter&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;CanGreet&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;T&lt;/span&gt;&lt;span class="o"&gt;])&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;greeter&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="py"&gt;sayHi&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;code&gt;greet&lt;/code&gt; is polymorphic in the sense that it will work on any &lt;code&gt;T&lt;/code&gt; as long as it gets an instance of &lt;code&gt;CanGreet&lt;/code&gt; for that type.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight scala"&gt;&lt;code&gt;&lt;span class="nf"&gt;greet&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;geekocephale&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;playerGreeter&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;However, that is a bit cumbersome to use, and we can leverage &lt;em&gt;Scala&lt;/em&gt;'s &lt;em&gt;implicits&lt;/em&gt; power to make our life easier (and to get closer to what's done in other languages' &lt;em&gt;type class&lt;/em&gt; machinery).&lt;/p&gt;

&lt;p&gt;Let's redifine our &lt;code&gt;greet&lt;/code&gt; function, and make our &lt;code&gt;CanGreet&lt;/code&gt; instance &lt;em&gt;implicit&lt;/em&gt; as well.&lt;/p&gt;

&lt;p&gt;Some hygiene guidelines comming from &lt;em&gt;Haskell&lt;/em&gt; &lt;em&gt;type classes&lt;/em&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Make your &lt;em&gt;type class&lt;/em&gt; instances live

&lt;ul&gt;
&lt;li&gt;Either in the &lt;em&gt;type class&lt;/em&gt; companion object&lt;/li&gt;
&lt;li&gt;Or in the type's &lt;em&gt;companion object&lt;/em&gt; of your type &lt;code&gt;T&lt;/code&gt;, here &lt;code&gt;Player&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;Have no more than &lt;strong&gt;one&lt;/strong&gt; instance per type for a given &lt;em&gt;type class&lt;/em&gt; (especially true in &lt;em&gt;Scala&lt;/em&gt;)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;em&gt;Type class&lt;/em&gt; and type companion objects are nice places for &lt;em&gt;type class&lt;/em&gt; instances because both of their scopes are checked when a function requires an implicit of type &lt;code&gt;TC[T]&lt;/code&gt; where &lt;code&gt;TC&lt;/code&gt; is your &lt;em&gt;type class trait&lt;/em&gt; and &lt;code&gt;T&lt;/code&gt; is the type for which you look for a &lt;em&gt;type class&lt;/em&gt; instance.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight scala"&gt;&lt;code&gt;&lt;span class="k"&gt;object&lt;/span&gt; &lt;span class="nc"&gt;Player&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;implicit&lt;/span&gt; &lt;span class="k"&gt;val&lt;/span&gt; &lt;span class="nv"&gt;playerGreeter&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;CanGreet&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;Player&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="k"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;CanGreet&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;Player&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;sayHi&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Player&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="s"&gt;"Hi, I'm player ${t.nickname}, I'm lvl ${t.level} !"&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;greet&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;T&lt;/span&gt;&lt;span class="o"&gt;](&lt;/span&gt;&lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;T&lt;/span&gt;&lt;span class="o"&gt;)(&lt;/span&gt;&lt;span class="k"&gt;implicit&lt;/span&gt; &lt;span class="n"&gt;greeter&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;CanGreet&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;T&lt;/span&gt;&lt;span class="o"&gt;])&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;greeter&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="py"&gt;sayHi&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Now, we can call our &lt;code&gt;greet&lt;/code&gt; function without explicitly passing the &lt;code&gt;CanGreet&lt;/code&gt; instance as long as we have an &lt;em&gt;implicit&lt;/em&gt; instance for the type &lt;code&gt;T&lt;/code&gt; we are using in scope (which we have, &lt;code&gt;playerGreeter&lt;/code&gt;) !&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight scala"&gt;&lt;code&gt;&lt;span class="nf"&gt;greet&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;geekocephale&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;To sum up, here's all the code that we wrote so far:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight scala"&gt;&lt;code&gt;&lt;span class="k"&gt;trait&lt;/span&gt; &lt;span class="nc"&gt;CanGreet&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;T&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;sayHi&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;T&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Player&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;nickname&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;level&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Int&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;object&lt;/span&gt; &lt;span class="nc"&gt;Player&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;implicit&lt;/span&gt; &lt;span class="k"&gt;val&lt;/span&gt; &lt;span class="nv"&gt;playerGreeter&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;CanGreet&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;Player&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="k"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;CanGreet&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;Player&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;sayHi&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Player&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="s"&gt;"Hi, I'm player ${t.nickname}, I'm lvl ${t.level} !"&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;greet&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;T&lt;/span&gt;&lt;span class="o"&gt;](&lt;/span&gt;&lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;T&lt;/span&gt;&lt;span class="o"&gt;)(&lt;/span&gt;&lt;span class="k"&gt;implicit&lt;/span&gt; &lt;span class="n"&gt;greeter&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;CanGreet&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;T&lt;/span&gt;&lt;span class="o"&gt;])&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;greeter&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="py"&gt;sayHi&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h1&gt;
  
  
  Optionnal cosmetics
&lt;/h1&gt;

&lt;p&gt;That part is absolutely not mandatory to understand how &lt;em&gt;type classes&lt;/em&gt; work, it is just about common syntax additions to what we saw before, mostly for convenience, and it can be skipped to go directly to conclusion.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight scala"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;greet&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;T&lt;/span&gt;&lt;span class="o"&gt;](&lt;/span&gt;&lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;T&lt;/span&gt;&lt;span class="o"&gt;)(&lt;/span&gt;&lt;span class="k"&gt;implicit&lt;/span&gt; &lt;span class="n"&gt;greeter&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;CanGreet&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;T&lt;/span&gt;&lt;span class="o"&gt;])&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Is strictly the same thing and can be refactored as:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight scala"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;greet&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;T:&lt;/span&gt; &lt;span class="kt"&gt;CanGreet&lt;/span&gt;&lt;span class="o"&gt;](&lt;/span&gt;&lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;T&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The function signature looks nicer, and I think it expresses better the &lt;em&gt;"need"&lt;/em&gt; for &lt;code&gt;T&lt;/code&gt; to &lt;em&gt;"be"&lt;/em&gt; an instance of &lt;code&gt;CanGreet&lt;/code&gt;, but there is a drawback: we lost the possibility to refer to our &lt;code&gt;CanGreet&lt;/code&gt; implicit instance by a name.&lt;br&gt;
To do so, in order to summon our instance from the implicit scope, we can use the &lt;code&gt;implicitly&lt;/code&gt; function:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight scala"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;greet&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;T:&lt;/span&gt; &lt;span class="kt"&gt;CanGreet&lt;/span&gt;&lt;span class="o"&gt;](&lt;/span&gt;&lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;T&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;val&lt;/span&gt; &lt;span class="nv"&gt;greeter&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;CanGreet&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;T&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="k"&gt;=&lt;/span&gt; &lt;span class="n"&gt;implicitly&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;CanGreet&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;T&lt;/span&gt;&lt;span class="o"&gt;]]&lt;/span&gt;
    &lt;span class="nv"&gt;greeter&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="py"&gt;sayHi&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;To make it less cumbersome, you'll commonly see a companion object for &lt;em&gt;type classes&lt;/em&gt; traits with an &lt;code&gt;apply&lt;/code&gt; method:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight scala"&gt;&lt;code&gt;&lt;span class="k"&gt;object&lt;/span&gt; &lt;span class="nc"&gt;CanGreet&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;apply&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;T&lt;/span&gt;&lt;span class="o"&gt;](&lt;/span&gt;&lt;span class="k"&gt;implicit&lt;/span&gt; &lt;span class="n"&gt;C&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;CanGreet&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;T&lt;/span&gt;&lt;span class="o"&gt;])&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;CanGreet&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;T&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="k"&gt;=&lt;/span&gt; &lt;span class="n"&gt;C&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;It does exactly what we did in our last &lt;code&gt;greet&lt;/code&gt; function implementation, allowing us to now re-write our &lt;code&gt;greet&lt;/code&gt; function as follows:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight scala"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;greet&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;T:&lt;/span&gt; &lt;span class="kt"&gt;CanGreet&lt;/span&gt;&lt;span class="o"&gt;](&lt;/span&gt;&lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;T&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;CanGreet&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;T&lt;/span&gt;&lt;span class="o"&gt;].&lt;/span&gt;&lt;span class="py"&gt;sayHi&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;code&gt;CanGreet[T]&lt;/code&gt; is calling the companion object &lt;code&gt;apply&lt;/code&gt; function (&lt;code&gt;CanGreet[T]&lt;/code&gt; is in fact desugarized as &lt;code&gt;CanGreet.apply[T]()&lt;/code&gt; with the implicit instance in scope passed to &lt;code&gt;apply&lt;/code&gt;) to summon &lt;code&gt;T&lt;/code&gt;'s &lt;code&gt;CanGreet&lt;/code&gt; instance from implicit scope and we can immediately use it in our &lt;code&gt;greet&lt;/code&gt; function by calling &lt;code&gt;.sayHi(t)&lt;/code&gt; on it.&lt;/p&gt;

&lt;p&gt;Finally, you'll also probably see &lt;em&gt;implicit classes&lt;/em&gt;, called &lt;em&gt;syntax&lt;/em&gt; for our &lt;em&gt;type class&lt;/em&gt; that holds the operation our &lt;em&gt;type class&lt;/em&gt; permits:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight scala"&gt;&lt;code&gt;&lt;span class="k"&gt;implicit&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;CanGreetSyntax&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;T:&lt;/span&gt; &lt;span class="kt"&gt;CanGreet&lt;/span&gt;&lt;span class="o"&gt;](&lt;/span&gt;&lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;T&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;greet&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;CanGreet&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;T&lt;/span&gt;&lt;span class="o"&gt;].&lt;/span&gt;&lt;span class="py"&gt;sayHi&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Allowing our &lt;code&gt;greet&lt;/code&gt; function to be called in a more convenient, OOP method way:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight scala"&gt;&lt;code&gt;&lt;span class="nv"&gt;geekocephale&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="py"&gt;greet&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;To sum up, here's all the code we wrote with these upgrades:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight scala"&gt;&lt;code&gt;&lt;span class="k"&gt;trait&lt;/span&gt; &lt;span class="nc"&gt;CanGreet&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;T&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;sayHi&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;T&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;object&lt;/span&gt; &lt;span class="nc"&gt;CanGreet&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;apply&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;T&lt;/span&gt;&lt;span class="o"&gt;](&lt;/span&gt;&lt;span class="k"&gt;implicit&lt;/span&gt; &lt;span class="n"&gt;C&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;CanGreet&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;T&lt;/span&gt;&lt;span class="o"&gt;])&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;CanGreet&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;T&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="k"&gt;=&lt;/span&gt; &lt;span class="n"&gt;C&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;implicit&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;CanGreetSyntax&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;T:&lt;/span&gt; &lt;span class="kt"&gt;CanGreet&lt;/span&gt;&lt;span class="o"&gt;](&lt;/span&gt;&lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;T&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;greet&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;CanGreet&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;T&lt;/span&gt;&lt;span class="o"&gt;].&lt;/span&gt;&lt;span class="py"&gt;sayHi&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Player&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;nickname&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;level&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Int&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;object&lt;/span&gt; &lt;span class="nc"&gt;Player&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;implicit&lt;/span&gt; &lt;span class="k"&gt;val&lt;/span&gt; &lt;span class="nv"&gt;playerGreeter&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;CanGreet&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;Player&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="k"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;CanGreet&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;Player&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;sayHi&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Player&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="s"&gt;"Hi, I'm player ${t.nickname}, I'm lvl ${t.level} !"&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h1&gt;
  
  
  Type classes bonus
&lt;/h1&gt;

&lt;h2&gt;
  
  
  A posteriori subtyping
&lt;/h2&gt;

&lt;p&gt;&lt;em&gt;Type classes&lt;/em&gt; have more to offer than classical &lt;em&gt;OOP&lt;/em&gt; &lt;em&gt;subtyping&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Type classes&lt;/em&gt; permit to add behavior to &lt;strong&gt;existing types&lt;/strong&gt; (including types that are not yours):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight scala"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;java.net.URL&lt;/span&gt;

&lt;span class="k"&gt;implicit&lt;/span&gt; &lt;span class="k"&gt;val&lt;/span&gt; &lt;span class="nv"&gt;urlGreeter&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;CanGreet&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;URL&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="k"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;CanGreet&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;URL&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;override&lt;/span&gt; &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;sayHi&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;URL&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="s"&gt;"Hi, I'm an URL pointing at ${t.getPath}"&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;We just added to &lt;code&gt;java.net.URL&lt;/code&gt; the property of being able to say Hi !&lt;/p&gt;

&lt;h2&gt;
  
  
  Conditionnal interfacing
&lt;/h2&gt;

&lt;p&gt;You can define conditionnal &lt;em&gt;type class&lt;/em&gt; instances:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight scala"&gt;&lt;code&gt;&lt;span class="k"&gt;implicit&lt;/span&gt; &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;listGreeter&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;A:&lt;/span&gt; &lt;span class="kt"&gt;CanGreet&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;CanGreet&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;List&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;A&lt;/span&gt;&lt;span class="o"&gt;]]&lt;/span&gt; &lt;span class="k"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;CanGreet&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;List&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;A&lt;/span&gt;&lt;span class="o"&gt;]]&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;override&lt;/span&gt; &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;sayHi&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;List&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;A&lt;/span&gt;&lt;span class="o"&gt;])&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="s"&gt;"Hi, I'm an List : [${t.map(CanGreet[A].sayHi).mkString("&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;&lt;span class="s"&gt;")}]"&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;By requiring &lt;code&gt;[A: CanGreet]&lt;/code&gt;, we just stated that &lt;code&gt;List[A]&lt;/code&gt; in an instance of the &lt;code&gt;CanGreet&lt;/code&gt; &lt;em&gt;type class&lt;/em&gt; if and only if &lt;code&gt;A&lt;/code&gt; is an instance of &lt;code&gt;CanGreet&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Just to show you that we can push that conditional behavior further, we could have done something complety useless like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight scala"&gt;&lt;code&gt;&lt;span class="k"&gt;implicit&lt;/span&gt; &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;listGreeter&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;A:&lt;/span&gt; &lt;span class="kt"&gt;CanGreet:&lt;/span&gt; &lt;span class="kt"&gt;MySndTypeClass&lt;/span&gt;&lt;span class="o"&gt;](&lt;/span&gt;&lt;span class="k"&gt;implicit&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;MyThirdTypeClass&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="o"&gt;])&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;CanGreet&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;List&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;A&lt;/span&gt;&lt;span class="o"&gt;]]&lt;/span&gt; &lt;span class="k"&gt;=&lt;/span&gt; &lt;span class="o"&gt;???&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Here we request, for &lt;code&gt;List[A]&lt;/code&gt; to be an instance of &lt;code&gt;CanGreet&lt;/code&gt;, that:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;A&lt;/code&gt; is instance of

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;CanGreet&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;MySndTypeClass&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;String&lt;/code&gt; is an instance of &lt;code&gt;MyThirdTypeClass&lt;/code&gt; (which is, I have to admit, absolutly stupid).&lt;/li&gt;
&lt;/ul&gt;

&lt;h1&gt;
  
  
  Tooling
&lt;/h1&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;a href="https://github.com/mpilquist/simulacrum"&gt;Simulacrum&lt;/a&gt; is an awesome library that helps tremendously reducing the boilerplate by using annotations that will generate the &lt;em&gt;type classe&lt;/em&gt; and syntax stuff for you at compile time&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://github.com/propensive/magnolia"&gt;Magnolia&lt;/a&gt; is a great library as well to automaticly derive your &lt;em&gt;type classes&lt;/em&gt; instances&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;We did not talk about &lt;em&gt;type classes&lt;/em&gt; derivation which is a bit more advanced topic, but the basic idea being that, if your types &lt;code&gt;A&lt;/code&gt; and &lt;code&gt;B&lt;/code&gt; are instances of a &lt;em&gt;type class&lt;/em&gt;, and if you have a type C formed by combining &lt;code&gt;A&lt;/code&gt; and &lt;code&gt;B&lt;/code&gt;, such as:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight scala"&gt;&lt;code&gt;&lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;C&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;A&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;B&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;or&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight scala"&gt;&lt;code&gt;&lt;span class="k"&gt;sealed&lt;/span&gt; &lt;span class="k"&gt;trait&lt;/span&gt; &lt;span class="nc"&gt;C&lt;/span&gt;
&lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;A&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="k"&gt;extends&lt;/span&gt; &lt;span class="n"&gt;C&lt;/span&gt;
&lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;B&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="k"&gt;extends&lt;/span&gt; &lt;span class="n"&gt;C&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;It makes the type &lt;code&gt;C&lt;/code&gt; automatically an instance of your &lt;em&gt;type class&lt;/em&gt; !&lt;/p&gt;

&lt;h1&gt;
  
  
  More material
&lt;/h1&gt;

&lt;p&gt;If you want to keep diving deeper, some interesting stuff can be found on my &lt;a href="https://github.com/mmenestret/fp-ressources"&gt;FP resources list&lt;/a&gt; and in particular:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://www.youtube.com/watch?v=Nm4OIhjjA2o"&gt;Mastering Typeclass Induction&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.youtube.com/watch?v=2EdQFCP5mZ8"&gt;Type class, ultimate ad hoc&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://blog.scalac.io/2017/04/19/typeclasses-in-scala.html"&gt;Type classes in Scala&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://kubuszok.com/compiled/implicits-type-classes-and-extension-methods/"&gt;Implicits, type classes, and extension methods&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h1&gt;
  
  
  Conclusion
&lt;/h1&gt;

&lt;p&gt;So to conclude here, we saw why &lt;em&gt;type classes&lt;/em&gt; are useful, what they are, how they are encoded in &lt;em&gt;Scala&lt;/em&gt; and some cosmetics and tooling that might help you to work with them.&lt;/p&gt;

&lt;p&gt;We went through:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;How to create a &lt;em&gt;class&lt;/em&gt; of types that provides shared behaviors&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Without &lt;strong&gt;having to know it beforehand&lt;/strong&gt; (we don't extend our types nor modify them)&lt;/li&gt;
&lt;li&gt;Without &lt;strong&gt;having to mix that behavior to the data&lt;/strong&gt; (the case class itself)&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;&lt;p&gt;How to create new instances of our &lt;em&gt;type class&lt;/em&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;How to leverage polymorphism by designing &lt;strong&gt;polymorphic functions&lt;/strong&gt; defined and working for any type &lt;code&gt;T&lt;/code&gt; as long as an (implicit) instance of it's required &lt;em&gt;type class&lt;/em&gt; is provided !&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I'll try to keep that blog post updated.&lt;br&gt;
If there are any additions, imprecision or mistakes that I should correct or if you need more explanations, feel free to contact me on Twitter or by mail !&lt;/p&gt;




&lt;p&gt;Edit: Thanks &lt;a href="https://twitter.com/guizmaii"&gt;Jules Ivanic&lt;/a&gt; for the review :).&lt;/p&gt;

</description>
      <category>scala</category>
      <category>functional</category>
    </item>
    <item>
      <title>Anatomy of functional programming</title>
      <dc:creator>Menestret Martin</dc:creator>
      <pubDate>Thu, 21 Feb 2019 10:05:48 +0000</pubDate>
      <link>https://dev.to/mmenestret/anatomy-of-functional-programming-1bpg</link>
      <guid>https://dev.to/mmenestret/anatomy-of-functional-programming-1bpg</guid>
      <description>&lt;p&gt;[Check my articles on my blog &lt;a href="http://geekocephale.com/blog/"&gt;here&lt;/a&gt;]&lt;/p&gt;

&lt;p&gt;I will try to group here, in an anatomy atlas, basic notions of functional programming that I find myself explaining often lately into a series of articles.&lt;/p&gt;

&lt;p&gt;The idea here is to have a place to point people needing explanations and to increase my own understanding of these subjects by trying to explain them the best I can.&lt;br&gt;
I'll try to focus more on making the reader feel an intuition, a feeling about the concepts rather than on the perfect, strict correctness of my explanations.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Part 1: &lt;a href="https://dev.to/mmenestret/anatomy-of-functional-programming-1bpg"&gt;Anatomy of functional programming&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Part 2: &lt;a href="https://dev.to/mmenestret/anatomy-of-an-algebra-3cd9"&gt;Anatomy of an algebra&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Part 3: &lt;a href="https://dev.to/mmenestret/anatomy-of-a-type-class-440j"&gt;Anatomy of a type class&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Part 4: &lt;a href="https://dev.to/mmenestret/anatomy-of-semigroups-and-monoids-22i8"&gt;Anatomy of semi groups and monoids&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Part 5: &lt;a href="https://dev.to/mmenestret/anatomy-of-functors-and-category-theory-2gf0"&gt;Anatomy of functors and category theory&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Part 6: Anatomy of the tagless final encoding - Yet to come !&lt;/li&gt;
&lt;/ul&gt;
&lt;h1&gt;
  
  
  What is functional programming ?
&lt;/h1&gt;
&lt;h2&gt;
  
  
  A general definition
&lt;/h2&gt;

&lt;p&gt;To begin our &lt;em&gt;anatomy atlas of functional programming&lt;/em&gt;, the first question would be: &lt;em&gt;what is functional programming ?&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;I would define it that way:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;A style of programming which aims to avoid &lt;em&gt;side effects&lt;/em&gt; by using, as much as possible, &lt;em&gt;pure functions&lt;/em&gt; that manipulate &lt;em&gt;immutable&lt;/em&gt; data.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;em&gt;As much as possible&lt;/em&gt;, here, means everywhere except, if needed, in your &lt;em&gt;main function&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;That way, almost your entire code base is said to be "&lt;em&gt;pure&lt;/em&gt;" and has the nice properties we'll see after.&lt;/p&gt;

&lt;p&gt;Your &lt;em&gt;main&lt;/em&gt; is the only "&lt;em&gt;impure&lt;/em&gt;" part but you drastically reduced the portion of your code you have to be extra careful with.&lt;/p&gt;

&lt;p&gt;The definition we gave for &lt;em&gt;functional programming&lt;/em&gt; introduced the notions of &lt;em&gt;side effects&lt;/em&gt;, &lt;em&gt;pure functions&lt;/em&gt; and &lt;em&gt;immutable data&lt;/em&gt; that we are going to explain.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;A general purpose of functional programming would be to reduce, as much as possible, the moving parts of your software.&lt;/strong&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Side effects
&lt;/h2&gt;

&lt;p&gt;A function should have only one effect: the effect of computing its return value.&lt;/p&gt;

&lt;p&gt;Any other effects triggered by that function are &lt;em&gt;side effects&lt;/em&gt; (&lt;em&gt;logging, printing, inputs and outputs of any kinds&lt;/em&gt;, and so on).&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Functional programming&lt;/em&gt; does not forbid to do &lt;em&gt;IO&lt;/em&gt; or &lt;em&gt;logging&lt;/em&gt;, but it encourages to do it &lt;em&gt;explicitly&lt;/em&gt; rather than in secret, hidden in functions, without publicly declaring it.&lt;/p&gt;
&lt;h2&gt;
  
  
  Pure functions
&lt;/h2&gt;

&lt;p&gt;&lt;em&gt;Pure functions&lt;/em&gt; are somehow a computational analogy of mathematical function.&lt;/p&gt;
&lt;h3&gt;
  
  
  Purity rules
&lt;/h3&gt;

&lt;p&gt;Pure functions have to respect a set of simple rules:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;em&gt;Determinism&lt;/em&gt;: &lt;em&gt;pure functions&lt;/em&gt; will &lt;strong&gt;always return the same output when given the same inputs&lt;/strong&gt;. Consequently, they cannot use non-local variables, global mutable states, perform IO, and so on&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;def add(a: Int, b: Int): Int = a + b&lt;/code&gt; is deterministic, it will always return the same output value when given the same input values&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;def rand(a: Int): Int = Random.nextInt(a)&lt;/code&gt; is not, each call may return different values&lt;/li&gt;
&lt;li&gt;A function returning &lt;code&gt;Unit&lt;/code&gt; should be a &lt;strong&gt;huge code smell&lt;/strong&gt;, as it is does nothing else than &lt;em&gt;side effecting&lt;/em&gt; (execept if it only returns Unit and nothing else, but I doubt you would want to do that...) ! It a determinism's nemesis !&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;em&gt;Totality&lt;/em&gt;: &lt;em&gt;pure functions&lt;/em&gt; from type &lt;code&gt;A =&amp;gt; B&lt;/code&gt; (&lt;code&gt;A&lt;/code&gt; is called the &lt;em&gt;domain&lt;/em&gt; and &lt;code&gt;B&lt;/code&gt; the &lt;em&gt;co-domain&lt;/em&gt;), &lt;strong&gt;have to be defined for every value of its domain&lt;/strong&gt;, here for every value of type &lt;code&gt;A&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;def divide(a: Int, b: Int): Int = a / b&lt;/code&gt; is not total, it will crash if &lt;code&gt;b == 0&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;def safeDivide(a: Int, b: Int): Option[Int] = Try(a / b).toOption&lt;/code&gt; is total by handling undefined case with &lt;code&gt;None&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;em&gt;Referential transparency&lt;/em&gt;: &lt;em&gt;pure functions&lt;/em&gt; calls &lt;strong&gt;should be replacable by their results&lt;/strong&gt; anywhere they are used &lt;strong&gt;without altering the rest of the program&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight scala"&gt;&lt;code&gt;    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;launchTheMissiles&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Unit&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;???&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;formattingMyStuff&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;launchTheMissiles&lt;/span&gt;
        &lt;span class="nv"&gt;s&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="py"&gt;toUpperCase&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;Here we cannot replace any calls to &lt;code&gt;formattingMyStuff("hi")&lt;/code&gt; by &lt;code&gt;"HI"&lt;/code&gt; without altering the rest of the program, because &lt;code&gt;formattingMyStuff&lt;/code&gt; performs a side effect, &lt;code&gt;launchTheMissiles&lt;/code&gt;. We know that by replacing &lt;code&gt;formattingMyStuff("hi")&lt;/code&gt; by &lt;code&gt;"HI"&lt;/code&gt;, the missiles won't be launched.&lt;/p&gt;

&lt;p&gt;With &lt;code&gt;def purelyFormattingMyStuff(s: String): String = s.toUpperCase&lt;/code&gt; howerver, any call to it could be replaced directly by the uppercased argument and we know that the program would work the same way it did before.&lt;/p&gt;

&lt;p&gt;To go even further, a &lt;em&gt;referentialy transparent&lt;/em&gt; function should be replaceable by a lookup table associating directly its inputs to its output.&lt;/p&gt;

&lt;p&gt;Since &lt;code&gt;def boolToString(b: Boolean): String = if (b) "That's true !" else "That's wrong..."&lt;/code&gt; is referencially transparent, it is replaceable by the following lookup table without altering the rest program:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Input&lt;/th&gt;
&lt;th&gt;Output&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;true&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;"That's true !"&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;false&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;"That's wrong..."&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Now you can tell how functions that respect those rules limit the moving parts of your software.&lt;/p&gt;

&lt;p&gt;They do what their signatures tell they do, and they only do that. &lt;em&gt;They don't move other stuff around&lt;/em&gt;.&lt;/p&gt;
&lt;h3&gt;
  
  
  How to do "real world" stuff then ?
&lt;/h3&gt;

&lt;p&gt;You tell me that the one and only function effect should only be to allocate memory and to use some processor power to compute its result ? And nothing else ?&lt;/p&gt;

&lt;p&gt;Without the ability to do &lt;em&gt;IO&lt;/em&gt;, to encode &lt;em&gt;randomness&lt;/em&gt; or to &lt;em&gt;fail&lt;/em&gt;, it might be pretty hard to do any real world work...&lt;/p&gt;

&lt;p&gt;Of course, &lt;em&gt;functional programming&lt;/em&gt; lets you do all that but it asks you do to it &lt;strong&gt;explicitly&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Here are some examples:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A function returning an &lt;code&gt;Option[Int]&lt;/code&gt; is in fact just returning an &lt;code&gt;Int&lt;/code&gt; but it adds explicitly to that &lt;code&gt;Int&lt;/code&gt; the &lt;strong&gt;effect&lt;/strong&gt; of being able to fail by wrapping it in an &lt;code&gt;Option&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;A function returning an &lt;code&gt;Either[String, Int]&lt;/code&gt; is in fact just returning an &lt;code&gt;Int&lt;/code&gt; but it adds explicitly to that it the &lt;strong&gt;effect&lt;/strong&gt; of potentialy returning a &lt;code&gt;String&lt;/code&gt; instead (which is often used to describe a failure with its reason), by wrapping it in an &lt;code&gt;Either[String, Int]&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;A &lt;code&gt;Task[Int]&lt;/code&gt; or &lt;code&gt;IO[Int]&lt;/code&gt; (these are more or less the same thing), returns a &lt;strong&gt;computation that is not run yet but that will produce an &lt;code&gt;Int&lt;/code&gt; or fail, when executed&lt;/strong&gt;, which is often used to explicitly represent a value that is obtained by doing an &lt;em&gt;IO&lt;/em&gt;. It is the description of the things to do to produce that &lt;code&gt;Int&lt;/code&gt;, but it hasn't started yet.&lt;/li&gt;
&lt;li&gt;A lot of other effects can be encoded that way but going in details is some other blog post material (you can find a lot of related resources &lt;a href="https://github.com/mmenestret/fp-ressources/blob/master/README.md"&gt;here&lt;/a&gt;)&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  Data philosophy
&lt;/h2&gt;
&lt;h3&gt;
  
  
  Data / behavior relationship
&lt;/h3&gt;

&lt;p&gt;&lt;em&gt;OOP&lt;/em&gt; and &lt;em&gt;FP&lt;/em&gt; have two different approaches when it comes to data/behavior relationship:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;em&gt;Object oriented programming&lt;/em&gt; often &lt;strong&gt;combines data and behavior&lt;/strong&gt; by mixing them into classes that:

&lt;ul&gt;
&lt;li&gt;Store data as an internal state&lt;/li&gt;
&lt;li&gt;Expose methods that act on it and may mutate it
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight scala"&gt;&lt;code&gt;&lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Player&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;nickname&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="k"&gt;var&lt;/span&gt; &lt;span class="n"&gt;level&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Int&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;levelUp&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Unit&lt;/span&gt;          &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt; &lt;span class="n"&gt;level&lt;/span&gt; &lt;span class="k"&gt;=&lt;/span&gt; &lt;span class="n"&gt;level&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;sayHi&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;          &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="s"&gt;"Hi, I'm player $nickname, I'm lvl $level !"&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;


&lt;ul&gt;
&lt;li&gt;
&lt;em&gt;Functional programming&lt;/em&gt; aims to completely &lt;strong&gt;separate data from behavior&lt;/strong&gt; by:

&lt;ul&gt;
&lt;li&gt;Defining types (&lt;a href="https://dev.to/mmenestret/anatomy-of-an-algebra-3cd9"&gt;ADT&lt;/a&gt; on one side that expose no behavior and only holds data&lt;/li&gt;
&lt;li&gt;Functions taking values of some of these types as inputs, acting on them and outputting values of some of those types (leaving the input values unchanged)
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight scala"&gt;&lt;code&gt;&lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Player&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;nickname&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;level&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Int&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;object&lt;/span&gt; &lt;span class="nc"&gt;PlayerOperations&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;levelUp&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Player&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Player&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;p&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="py"&gt;copy&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;level&lt;/span&gt; &lt;span class="k"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;p&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="py"&gt;level&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;sayHi&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Player&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;   &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="s"&gt;"Hi, I'm player ${p.nickname}, I'm lvl ${p.level} !"&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;h3&gt;
  
  
  Expression problem
&lt;/h3&gt;

&lt;p&gt;The expression problem is about how a language behaves when it comes to add to an existing code base:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;New cases to existing types&lt;/li&gt;
&lt;li&gt;New behaviors (functions) over existing types&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;And if they manage to do that without having to modify the existing code.&lt;/p&gt;

&lt;p&gt;The idea behind the expression problem is to compare how languages and programming paradigms tackle these problems.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;OOP&lt;/em&gt; and &lt;em&gt;FP&lt;/em&gt; have both a different answer to that problem.&lt;/p&gt;
&lt;h4&gt;
  
  
  &lt;em&gt;Object oriented programming paradigm&lt;/em&gt;
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;👍 : Adding new cases to an existing &lt;em&gt;data type&lt;/em&gt;

&lt;ul&gt;
&lt;li&gt;A new &lt;em&gt;class&lt;/em&gt; &lt;em&gt;extending&lt;/em&gt; the existing &lt;em&gt;class / interface&lt;/em&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;👎 : Adding a new behavior over an existing &lt;em&gt;data type&lt;/em&gt;

&lt;ul&gt;
&lt;li&gt;A new &lt;em&gt;method&lt;/em&gt; on the appropriate &lt;em&gt;super class / interface&lt;/em&gt; which impacts every single &lt;em&gt;sub classes&lt;/em&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Base case:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight scala"&gt;&lt;code&gt;&lt;span class="k"&gt;trait&lt;/span&gt; &lt;span class="nc"&gt;MyType&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt; &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;behavior&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt; &lt;span class="o"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;final&lt;/span&gt; &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;A&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="k"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;MyType&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt; &lt;span class="k"&gt;override&lt;/span&gt; &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;behavior&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"I'm A"&lt;/span&gt; &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="k"&gt;final&lt;/span&gt; &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;B&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="k"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;MyType&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt; &lt;span class="k"&gt;override&lt;/span&gt; &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;behavior&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"I'm B"&lt;/span&gt; &lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Adding a new case to an existing &lt;em&gt;data type&lt;/em&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight scala"&gt;&lt;code&gt;&lt;span class="k"&gt;final&lt;/span&gt; &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;C&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="k"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;MyType&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt; &lt;span class="k"&gt;override&lt;/span&gt; &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;behavior&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"I'm C"&lt;/span&gt; &lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Adding a new behavior over an existing &lt;em&gt;data type&lt;/em&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight scala"&gt;&lt;code&gt;&lt;span class="k"&gt;trait&lt;/span&gt; &lt;span class="nc"&gt;MyType&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt; 
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;behavior&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;newBehavior&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Now you have to get back to every single &lt;em&gt;classes&lt;/em&gt; extending &lt;code&gt;MyType&lt;/code&gt; to implement &lt;code&gt;newBehavior&lt;/code&gt;.&lt;/p&gt;

&lt;h4&gt;
  
  
  &lt;em&gt;Functional programming paradigm&lt;/em&gt;
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;👎 : Adding new cases to an existing &lt;em&gt;data type&lt;/em&gt;

&lt;ul&gt;
&lt;li&gt;A new &lt;em&gt;type&lt;/em&gt; to your existing &lt;em&gt;sum type&lt;/em&gt; (cf: &lt;a href="https://dev.to/mmenestret/anatomy-of-an-algebra-3cd9"&gt;Anatomy of an algebra&lt;/a&gt; which impacts every &lt;em&gt;functions&lt;/em&gt; over that &lt;em&gt;data type&lt;/em&gt; (you'll have to handle that new case)&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;👍 : Adding a new behavior over an existing &lt;em&gt;data type&lt;/em&gt;

&lt;ul&gt;
&lt;li&gt;A new &lt;em&gt;function&lt;/em&gt;, nothing more&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Base case:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight scala"&gt;&lt;code&gt;&lt;span class="k"&gt;sealed&lt;/span&gt; &lt;span class="k"&gt;trait&lt;/span&gt; &lt;span class="nc"&gt;MyType&lt;/span&gt;
&lt;span class="k"&gt;final&lt;/span&gt; &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;A&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="k"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;MyType&lt;/span&gt;
&lt;span class="k"&gt;final&lt;/span&gt; &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;B&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="k"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;MyType&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;behavior&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;m&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;MyType&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;m&lt;/span&gt; &lt;span class="k"&gt;match&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="nf"&gt;A&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="k"&gt;⇒&lt;/span&gt; &lt;span class="s"&gt;"I'm A"&lt;/span&gt;
    &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="nf"&gt;B&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="k"&gt;⇒&lt;/span&gt; &lt;span class="s"&gt;"I'm B"&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Adding a new case to an existing &lt;em&gt;data type&lt;/em&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight scala"&gt;&lt;code&gt;&lt;span class="k"&gt;final&lt;/span&gt; &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;C&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="k"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;MyType&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Now you have to get back to every &lt;em&gt;functions&lt;/em&gt; over &lt;em&gt;MyType&lt;/em&gt; to pattern match on the new case.&lt;/p&gt;

&lt;p&gt;Adding a new behavior over an existing &lt;em&gt;data type&lt;/em&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight scala"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;newBehavior&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;m&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;MyType&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;m&lt;/span&gt; &lt;span class="k"&gt;match&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="nf"&gt;A&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="k"&gt;⇒&lt;/span&gt; &lt;span class="o"&gt;???&lt;/span&gt;
    &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="nf"&gt;B&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="k"&gt;⇒&lt;/span&gt; &lt;span class="o"&gt;???&lt;/span&gt;
  &lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h3&gt;
  
  
  Immutable Data
&lt;/h3&gt;

&lt;p&gt;That one is simple: a &lt;em&gt;value&lt;/em&gt; is said to be &lt;em&gt;immutable&lt;/em&gt; if, once evaluated, there is no way to change it.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;That is &lt;em&gt;mutability&lt;/em&gt;, and thus, should be avoided in &lt;em&gt;functional programming&lt;/em&gt;:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight scala"&gt;&lt;code&gt;&lt;span class="k"&gt;var&lt;/span&gt; &lt;span class="n"&gt;meaningOfLife&lt;/span&gt; &lt;span class="k"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;41&lt;/span&gt;
&lt;span class="n"&gt;meaningOfLife&lt;/span&gt; &lt;span class="k"&gt;=&lt;/span&gt; &lt;span class="n"&gt;meaningOfLife&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;That piece of data is &lt;em&gt;immutable&lt;/em&gt;:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight scala"&gt;&lt;code&gt;&lt;span class="k"&gt;val&lt;/span&gt; &lt;span class="nv"&gt;meaningOfLife&lt;/span&gt; &lt;span class="k"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;42&lt;/span&gt;
&lt;span class="n"&gt;meaningOfLife&lt;/span&gt; &lt;span class="k"&gt;=&lt;/span&gt; &lt;span class="n"&gt;meaningOfLife&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;
&lt;span class="c1"&gt;//&amp;lt;console&amp;gt;:12: error: reassignment to val&lt;/span&gt;
&lt;span class="c1"&gt;//       meaningOfLife = meaningOfLife + 0&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;If you really have to use mutability, say for performance reasons, I encourage you to do it with caution, by isolating and encapsulating the mutation in an &lt;em&gt;immutable&lt;/em&gt; construct such as:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight scala"&gt;&lt;code&gt;&lt;span class="k"&gt;val&lt;/span&gt; &lt;span class="nv"&gt;magic&lt;/span&gt; &lt;span class="k"&gt;=&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;var&lt;/span&gt; &lt;span class="n"&gt;mutableMagic&lt;/span&gt; &lt;span class="k"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;
    &lt;span class="n"&gt;mutableMagic&lt;/span&gt; &lt;span class="k"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;42&lt;/span&gt;
    &lt;span class="n"&gt;mutableMagic&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;That way you know that mutability won't spread and your &lt;em&gt;moving part&lt;/em&gt; is contained in a &lt;em&gt;non moving&lt;/em&gt; one.&lt;/p&gt;

&lt;h1&gt;
  
  
  Benefits of FP
&lt;/h1&gt;

&lt;p&gt;For now, what we saw about &lt;em&gt;FP&lt;/em&gt; was more or less only constraints.&lt;/p&gt;

&lt;p&gt;But &lt;em&gt;functional programming&lt;/em&gt; shouldn't be only seen a set of constraints, as &lt;a href="https://www.youtube.com/watch?v=GqmsQeSzMdw"&gt;Runar Bjarnason&lt;/a&gt; would say, constraints buy you a lot of freedom.&lt;/p&gt;

&lt;p&gt;That may not seem obvious but I'll try to explain why.&lt;/p&gt;

&lt;h2&gt;
  
  
  Equationnal reasoning
&lt;/h2&gt;

&lt;p&gt;&lt;em&gt;Pure functions&lt;/em&gt;, while being restrictive allow you to reason about your program in a way that would not be possible otherwise, it is called &lt;em&gt;equational reasoning&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;It means that, once you figure out that &lt;code&gt;f(x) = something&lt;/code&gt;, then everywhere &lt;code&gt;f(x)&lt;/code&gt; appears, you can simply replace it by &lt;code&gt;something&lt;/code&gt; and keep reducing your problematic complexity thay way as far as you need.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Equationnal reasoning&lt;/em&gt; allows you to follow the logic of your program by replacing your function calls by their results, exactly how you'd do when trying to solve a mathematical equation:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;If you had these two equations:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    2x - 3y  = 12
    y + 2x   = 180
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Then you could isolate &lt;code&gt;x&lt;/code&gt; the first one:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    2x - 3y = 12
    2x      = 12 + 3y
    x       = (12 + 3y ) / 2
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;And then simply &lt;strong&gt;replace&lt;/strong&gt; &lt;code&gt;x&lt;/code&gt; in the rest of your reasoning by its value:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    y + 2x                  = 180
    y + 2 * (12 + 3y) / 2   = 180
    y + 12 + 3y             = 180
    4y                      = 168
    y                       = 42
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;That's a powerful way to reason about complex problems.&lt;/p&gt;

&lt;p&gt;Without pure functions, you would have to analyze every single function calls, check if those functions do anything more than returning their results, keep a mental track of that other things they do, and keep analyzing while trying not to forget what you just witnessed.&lt;/p&gt;

&lt;p&gt;That's a good example about how constraints on one side give you a lot of freedom on other sides.&lt;/p&gt;

&lt;h2&gt;
  
  
  Predictable data
&lt;/h2&gt;

&lt;p&gt;As your data is &lt;em&gt;immutable&lt;/em&gt;, it is easier to keep track of its state, since &lt;strong&gt;its state doesn't change&lt;/strong&gt;.&lt;br&gt;
The only way to create data is by using its constructor (in a broad sense) and that, also, &lt;em&gt;reduces a lot your software's moving parts&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;You know for sure that, when using a piece of data, it has not been modified in the meantime by something else, you can rely on it.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;If you isolate the places where changes occur by severely restricting mutation, you create a much smaller space for errors to occur and have fewer places to test.&lt;br&gt;
(&lt;a href="https://www.ibm.com/developerworks/library/j-ft4/index.html"&gt;Neal Ford&lt;/a&gt;)&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Morever, it gives you &lt;em&gt;thread safety by design&lt;/em&gt;, your data will never be in an unknown or undesirable state, which is huge in our more and more concurrent applications.&lt;/p&gt;

&lt;h2&gt;
  
  
  Playing with Lego
&lt;/h2&gt;

&lt;p&gt;In addition to &lt;em&gt;equational reasoning and immutability&lt;/em&gt;, I'll try to show you what else &lt;em&gt;FP&lt;/em&gt; brings to you your code with an analogy.&lt;/p&gt;

&lt;p&gt;Do you remember how it was to play with Lego ? Well &lt;em&gt;FP&lt;/em&gt; lets you play with functions the way you did with Lego pieces.&lt;/p&gt;

&lt;p&gt;You had plenty of pieces that do or don't fit together, each pieces being solid, &lt;em&gt;immutable&lt;/em&gt;, and doing one single, simple thing.&lt;/p&gt;

&lt;p&gt;Just as pure functions do.&lt;/p&gt;

&lt;p&gt;It gives you:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;Trivial refactoring&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;You know that you can change this red brick by that blue one if it respects the same contract without affecting at all the rest of your construct for obscure reasons (side effects, mutability, ...)&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;

&lt;p&gt;Composability&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;You can build a construct on one side, another on the other side and plug them together to make a new, more complex, construct safely that behaves as you would expect&lt;/li&gt;
&lt;li&gt;That's exactly what you'll do with your pure functions, you know they take inputs, compute and return outputs and &lt;strong&gt;nothing&lt;/strong&gt; else, thus you can safely compose them and build up more and more complexity fearlessly&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;

&lt;p&gt;Easier testability&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;You can easily test if a piece does what it is expected to since it does nothing more than being, well, a simple, independent, side effect free, piece !&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Crystallizing design patterns
&lt;/h2&gt;

&lt;p&gt;To end with &lt;em&gt;FP&lt;/em&gt; benefits, there is this curious thing called &lt;a href="https://en.wikipedia.org/wiki/Curry%E2%80%93Howard_correspondence"&gt;&lt;em&gt;Curry–Howard correspondence&lt;/em&gt;&lt;/a&gt; which is a direct analogy between &lt;em&gt;mathematical concepts&lt;/em&gt; and &lt;em&gt;computational calculus&lt;/em&gt; (which is what we do, programmers).&lt;/p&gt;

&lt;p&gt;This correspondence means that a lot of useful stuff discovered and proven for decades in &lt;em&gt;Math&lt;/em&gt; can then be transposed to programming, opening a way for a lot of extremely robust constructs for free.&lt;/p&gt;

&lt;p&gt;In OOP, &lt;em&gt;Design patterns&lt;/em&gt; are used a lot and could be defined as &lt;em&gt;idiomatic ways to solve a given problems, in specific contexts&lt;/em&gt; but their existences won't save you from having to apply and write them again and again each time you encounter the problems they solve.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Functional programming&lt;/em&gt; constructs, some directly coming from &lt;em&gt;category theory&lt;/em&gt; (mathematics), solve directly what you would have tried to solve with design patterns.&lt;/p&gt;

&lt;p&gt;The classical &lt;em&gt;functional programming&lt;/em&gt; toolbox gives you constructs to handle, almost for free:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Global states&lt;/li&gt;
&lt;li&gt;Concurrency&lt;/li&gt;
&lt;li&gt;Computation parallelization&lt;/li&gt;
&lt;li&gt;Computation failure&lt;/li&gt;
&lt;li&gt;Validation accumulation&lt;/li&gt;
&lt;li&gt;Asynchronism&lt;/li&gt;
&lt;li&gt;Sequentiallity&lt;/li&gt;
&lt;li&gt;...&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;You can find here a list of how &lt;em&gt;FP&lt;/em&gt; constructs corresponds to &lt;em&gt;OOP&lt;/em&gt; classical design patterns: &lt;a href="https://github.com/thma/LtuPatternFactory"&gt;Lambda, the ultimate pattern factory&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Pretty convenient !&lt;/p&gt;

&lt;h1&gt;
  
  
  More material
&lt;/h1&gt;

&lt;p&gt;If you want to keep diving deeper, some interesting stuff can be found on my &lt;a href="https://github.com/mmenestret/fp-ressources"&gt;FP resources list&lt;/a&gt; and in particular:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://www.youtube.com/watch?v=X-cEGEJMx_4&amp;amp;feature=youtu.be&amp;amp;t=228"&gt;What Referential Transparency can do for you - Luka Jacobowitz&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.youtube.com/watch?v=GqmsQeSzMdw"&gt;Constraints Liberate, Liberties Constrain — Runar Bjarnason&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.youtube.com/watch?v=srQt1NAHYC0"&gt;Functional Design Patterns - Scott Wlaschin&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.youtube.com/watch?v=IOiZatlZtGU"&gt;Propositions as Types - Philip Wadler&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h1&gt;
  
  
  Conclusion
&lt;/h1&gt;

&lt;p&gt;To sum up, we saw:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;What actually &lt;em&gt;is&lt;/em&gt; &lt;strong&gt;functional programming&lt;/strong&gt; and that is what about manipulating &lt;strong&gt;immutable data&lt;/strong&gt; with &lt;strong&gt;pure functions&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Then we saw what were &lt;em&gt;side effects&lt;/em&gt;, &lt;em&gt;pure functions&lt;/em&gt; and &lt;em&gt;immutability&lt;/em&gt;
&lt;/li&gt;
&lt;li&gt;The liberties that &lt;em&gt;FP&lt;/em&gt; constraints gives you (equationnal reasoning, predictability, composability, easier refactoring, easier testability, etc.)&lt;/li&gt;
&lt;li&gt;That it exists a bridge between the world of &lt;em&gt;Mathematical logic&lt;/em&gt; and &lt;em&gt;computation&lt;/em&gt; and how that bridge is used to give us powerful constructs&lt;/li&gt;
&lt;li&gt;How some of these constructs can address real world problems you would have solved with cumbersome, hand crafted, design patterns otherwise&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I'll try to keep that blog post updated.&lt;br&gt;
If there are any additions, imprecision or mistakes that I should correct or if you need more explanations, feel free to contact me on Twitter or by mail !&lt;/p&gt;

</description>
      <category>scala</category>
      <category>functional</category>
    </item>
    <item>
      <title>Anatomy of functors and category theory</title>
      <dc:creator>Menestret Martin</dc:creator>
      <pubDate>Thu, 21 Feb 2019 08:51:28 +0000</pubDate>
      <link>https://dev.to/mmenestret/anatomy-of-functors-and-category-theory-2gf0</link>
      <guid>https://dev.to/mmenestret/anatomy-of-functors-and-category-theory-2gf0</guid>
      <description>&lt;p&gt;[Check my articles on my blog &lt;a href="http://geekocephale.com/blog/" rel="noopener noreferrer"&gt;here&lt;/a&gt;]&lt;/p&gt;

&lt;p&gt;I will try to group here, in an anatomy atlas, basic notions of functional programming that I find myself explaining often lately into a series of articles.&lt;/p&gt;

&lt;p&gt;The idea here is to have a place to point people needing explanations and to increase my own understanding of these subjects by trying to explain them the best I can.&lt;br&gt;
I'll try to focus more on making the reader feel an intuition, a feeling about the concepts rather than on the perfect, strict correctness of my explanations.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Part 1: &lt;a href="https://dev.to/mmenestret/anatomy-of-functional-programming-1bpg"&gt;Anatomy of functional programming&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Part 2: &lt;a href="https://dev.to/mmenestret/anatomy-of-an-algebra-3cd9"&gt;Anatomy of an algebra&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Part 3: &lt;a href="https://dev.to/mmenestret/anatomy-of-a-type-class-440j"&gt;Anatomy of a type class&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Part 4: &lt;a href="https://dev.to/mmenestret/anatomy-of-semigroups-and-monoids-22i8"&gt;Anatomy of semi groups and monoids&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Part 5: &lt;a href="https://dev.to/mmenestret/anatomy-of-functors-and-category-theory-2gf0"&gt;Anatomy of functors and category theory&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Part 6: Anatomy of the tagless final encoding - Yet to come !&lt;/li&gt;
&lt;/ul&gt;
&lt;h1&gt;
  
  
  What is a &lt;em&gt;functor&lt;/em&gt; ?
&lt;/h1&gt;

&lt;p&gt;There's a nice answer &lt;a href="https://www.quora.com/Functional-Programming-What-is-a-functor" rel="noopener noreferrer"&gt;by Bartosz Milewski on &lt;em&gt;Quora&lt;/em&gt;&lt;/a&gt; from which I'll keep some parts:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;I like to think of a &lt;em&gt;functor&lt;/em&gt; as a generalization of a container. A regular container contains zero or more values of some type. A &lt;em&gt;functor&lt;/em&gt; may or may not contain a value or values of some type (...) .&lt;/p&gt;

&lt;p&gt;So what can you do with such a container? You might think that, at the minimum, you should be able to retrieve values. But each container has its own interface for accessing values. If you try to specify that interface, you're Balkanizing containers. You're splitting them into stacks, queues, smart pointers,  futures, etc. So value retrieval is too specific.&lt;/p&gt;

&lt;p&gt;It turns out that the most general way of interacting with a container is by modifying its contents using a function.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h2&gt;
  
  
  Let's try to rephrase that
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;em&gt;Functors&lt;/em&gt; represent containers&lt;/li&gt;
&lt;li&gt;For now, we won't care about their particularities, all we need to know is that, at some point, they will maybe hold a value or values "inside" (but keep in mind that every container have particularities, I'll refer to that at the end)&lt;/li&gt;
&lt;li&gt;Defining an generic interface about how to access values inside a container does not make any sense since some containers' values would be accessed by index (&lt;em&gt;arrays&lt;/em&gt; for example), others only by taking the first element (&lt;em&gt;stacks&lt;/em&gt; for example), other by taking the value only if it exists (&lt;em&gt;optionals&lt;/em&gt;), etc.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;However&lt;/strong&gt;, we can define an interface defining how the value(s) inside containers is modified by a function despite being in a container&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;So, to summarize, a &lt;em&gt;functor&lt;/em&gt; is a kind of container that can be mapped over by a function.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;But &lt;em&gt;functors&lt;/em&gt; have to respect some rules, called &lt;em&gt;functor&lt;/em&gt;'s laws...&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Identity&lt;/strong&gt;: A &lt;em&gt;functor&lt;/em&gt; mapped over by the identity function (the function returning its parameter unchanged) is the same as the original &lt;em&gt;functor&lt;/em&gt; (the container and its content remain unchanged)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Composition&lt;/strong&gt;: A &lt;em&gt;functor&lt;/em&gt; mapped over the composition of two functions is the same as the &lt;em&gt;functor&lt;/em&gt; mapped over the first function and then mapped over the second one&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;A quick note about &lt;em&gt;functor&lt;/em&gt; / &lt;em&gt;container&lt;/em&gt; parallel: the analogy is convenient to get the intuition, but not all &lt;em&gt;functors&lt;/em&gt; will not fit into that model, keep it in a corner of you mind so that you're not taken off guard.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h1&gt;
  
  
  How does it look like in practice
&lt;/h1&gt;

&lt;p&gt;Along the next sections, the examples and code snippets I'll provide will be in &lt;em&gt;Scala&lt;/em&gt;. &lt;/p&gt;
&lt;h2&gt;
  
  
  Let explore some examples
&lt;/h2&gt;

&lt;p&gt;We're going to play with concrete containers of &lt;code&gt;Int&lt;/code&gt; values to try to grasp the concept.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight scala"&gt;&lt;code&gt;&lt;span class="k"&gt;val&lt;/span&gt; &lt;span class="nv"&gt;halve&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Int&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;Float&lt;/span&gt; &lt;span class="k"&gt;=&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="k"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nv"&gt;x&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="py"&gt;toFloat&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here we defined the function from &lt;code&gt;Int&lt;/code&gt; to &lt;code&gt;Float&lt;/code&gt; that we are going to use to map over our containers&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Our first guinea pig is &lt;code&gt;Option[Int]&lt;/code&gt;, which is a container of (0 or 1) &lt;code&gt;Int&lt;/code&gt;.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight scala"&gt;&lt;code&gt;&lt;span class="k"&gt;val&lt;/span&gt; &lt;span class="nv"&gt;intOpt&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Option&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;Int&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="k"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Some&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;99&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;val&lt;/span&gt; &lt;span class="nv"&gt;mappedResult1&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Option&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;Float&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="k"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;intOpt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="py"&gt;map&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;halve&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We can see that an &lt;code&gt;Option[Int]&lt;/code&gt; turns into an &lt;code&gt;Option[Float]&lt;/code&gt;, the inner value of the container being modified from &lt;code&gt;Int&lt;/code&gt; to &lt;code&gt;Float&lt;/code&gt; when mapped over with a function from &lt;code&gt;Int&lt;/code&gt; to &lt;code&gt;Float&lt;/code&gt;... &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Our second guinea pig is &lt;code&gt;List[Int]&lt;/code&gt;, which is a container of (0 or more) &lt;code&gt;Int&lt;/code&gt;.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight scala"&gt;&lt;code&gt;&lt;span class="k"&gt;val&lt;/span&gt; &lt;span class="nv"&gt;intList&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;List&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;Int&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="k"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;List&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;val&lt;/span&gt; &lt;span class="nv"&gt;mappedResult2&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;List&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;Float&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="k"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;intList&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="py"&gt;map&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;halve&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We can see that an &lt;code&gt;List[Int]&lt;/code&gt; turns into a &lt;code&gt;List[Float]&lt;/code&gt;, the inner values of the container are modified from &lt;code&gt;Int&lt;/code&gt; to &lt;code&gt;Float&lt;/code&gt; when mapped over with a function from &lt;code&gt;Int&lt;/code&gt; to &lt;code&gt;Float&lt;/code&gt;... &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Our third is a hand made &lt;code&gt;UselessContainer[Int]&lt;/code&gt;, which is a container of exactly 1 &lt;code&gt;Int&lt;/code&gt;.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight scala"&gt;&lt;code&gt;&lt;span class="k"&gt;final&lt;/span&gt; &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;UselessContainer&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;A&lt;/span&gt;&lt;span class="o"&gt;](&lt;/span&gt;&lt;span class="n"&gt;innerValue&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;A&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;val&lt;/span&gt; &lt;span class="nv"&gt;intContainer&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;UselessContainer&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;Int&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="k"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;UselessContainer&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;99&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;val&lt;/span&gt; &lt;span class="nv"&gt;mappedResult3&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;UselessContainer&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;Float&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="k"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;intContainer&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="py"&gt;map&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;halve&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We can see that an &lt;code&gt;UselessContainer[Int]&lt;/code&gt; turns into an &lt;code&gt;UselessContainer[Float]&lt;/code&gt;, the inner value of the container being modified from &lt;code&gt;Int&lt;/code&gt; to &lt;code&gt;Float&lt;/code&gt; when mapped over with a function from &lt;code&gt;Int&lt;/code&gt; to &lt;code&gt;Float&lt;/code&gt;... (I've deliberately hidden an implementation detail here for clarity, I'll cover it later)&lt;/p&gt;

&lt;p&gt;So we can observe that pattern we described earlier: &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;A &lt;em&gt;functor&lt;/em&gt;, let's call it &lt;code&gt;F[A]&lt;/code&gt;, is a structure containing a value of type &lt;code&gt;A&lt;/code&gt; and which can be mapped over by a function of type &lt;code&gt;A =&amp;gt; B&lt;/code&gt;, getting back a &lt;em&gt;functor&lt;/em&gt; &lt;code&gt;F[B]&lt;/code&gt; containing a value of type &lt;code&gt;B&lt;/code&gt;.&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  How do we abstract and encode that ability ?
&lt;/h2&gt;

&lt;p&gt;&lt;em&gt;Functors&lt;/em&gt; are usually represented by a &lt;em&gt;type class&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;As a reminder, a &lt;em&gt;type class&lt;/em&gt; is a group of types that all provide the same abilities (interface), which make them part of the same class (group, "club") of same abilities providing types (see my article about &lt;em&gt;type classes&lt;/em&gt; &lt;a href="http://geekocephale.com/blog/2018/10/05/typeclasses" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;This is the &lt;em&gt;functor type class&lt;/em&gt; implementation:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight scala"&gt;&lt;code&gt;&lt;span class="k"&gt;trait&lt;/span&gt; &lt;span class="nc"&gt;Functor&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;F&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="k"&gt;_&lt;/span&gt;&lt;span class="o"&gt;]]{&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;map&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;A&lt;/span&gt;, &lt;span class="kt"&gt;B&lt;/span&gt;&lt;span class="o"&gt;](&lt;/span&gt;&lt;span class="n"&gt;fa&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;F&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;A&lt;/span&gt;&lt;span class="o"&gt;],&lt;/span&gt; &lt;span class="n"&gt;func&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;A&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;B&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;F&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;B&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;The types our &lt;em&gt;functor&lt;/em&gt; &lt;em&gt;type class&lt;/em&gt; abstract over are &lt;em&gt;type constructors&lt;/em&gt; (&lt;code&gt;F[_]&lt;/code&gt;, our container types)&lt;/li&gt;
&lt;li&gt;The &lt;em&gt;type class&lt;/em&gt; exposes a &lt;code&gt;map&lt;/code&gt; function taking a container &lt;code&gt;F[A]&lt;/code&gt; of values of type &lt;code&gt;A&lt;/code&gt;, a function of type &lt;code&gt;A =&amp;gt; B&lt;/code&gt; and return a &lt;code&gt;F[B]&lt;/code&gt;, a container of values of type &lt;code&gt;B&lt;/code&gt;: the pattern we just described.&lt;/li&gt;
&lt;/ol&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;A note about &lt;em&gt;type constructors&lt;/em&gt;&lt;/strong&gt;: A &lt;em&gt;type constructor&lt;/em&gt; is a &lt;em&gt;type&lt;/em&gt; to which you have to supply an other &lt;em&gt;type&lt;/em&gt; to get back a new &lt;em&gt;type&lt;/em&gt;. You can think of it just as functions that take values to produce values. And that makes sense, since we have to supply to our container type the type of values it will "hold" ! &lt;/p&gt;

&lt;p&gt;Most used concrete &lt;em&gt;type constructors&lt;/em&gt; are &lt;code&gt;List[_]&lt;/code&gt;, &lt;code&gt;Option[_]&lt;/code&gt;, &lt;code&gt;Either[_,_]&lt;/code&gt;, &lt;code&gt;Map[_, _]&lt;/code&gt; and so on. &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;To illustrate what it means in your &lt;em&gt;Scala&lt;/em&gt; code let's make our &lt;code&gt;UselessContainer&lt;/code&gt; a &lt;em&gt;functor&lt;/em&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight scala"&gt;&lt;code&gt;&lt;span class="k"&gt;implicit&lt;/span&gt; &lt;span class="k"&gt;val&lt;/span&gt; &lt;span class="nv"&gt;ucFunctor&lt;/span&gt; &lt;span class="k"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Functor&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;UselessContainer&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;override&lt;/span&gt; &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;map&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;A&lt;/span&gt;, &lt;span class="kt"&gt;B&lt;/span&gt;&lt;span class="o"&gt;](&lt;/span&gt;&lt;span class="n"&gt;fa&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;UselessContainer&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;A&lt;/span&gt;&lt;span class="o"&gt;],&lt;/span&gt;
                           &lt;span class="n"&gt;func&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;A&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;B&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;UselessContainer&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;B&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="k"&gt;=&lt;/span&gt;
      &lt;span class="nc"&gt;UselessContainer&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;func&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;fa&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="py"&gt;innerValue&lt;/span&gt;&lt;span class="o"&gt;))&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Be careful, if you attempt to create your own &lt;em&gt;functor&lt;/em&gt;, it is not enough. &lt;strong&gt;You have to prove that your &lt;em&gt;functor&lt;/em&gt; instance respects the &lt;em&gt;functor&lt;/em&gt;'s laws we stated earlier&lt;/strong&gt; (usually via property based tests), hence that:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;For all values &lt;code&gt;uc&lt;/code&gt; of type &lt;code&gt;UselessContainer&lt;/code&gt;:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight scala"&gt;&lt;code&gt;&lt;span class="nv"&gt;ucFunctor&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="py"&gt;map&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;uc&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;identity&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;uc&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;For all values &lt;code&gt;uc&lt;/code&gt; of type &lt;code&gt;UselessContainer&lt;/code&gt; and for any two functions &lt;code&gt;f&lt;/code&gt; of type &lt;code&gt;A =&amp;gt; B&lt;/code&gt; and &lt;code&gt;g&lt;/code&gt; of type &lt;code&gt;B =&amp;gt; C&lt;/code&gt;:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight scala"&gt;&lt;code&gt;&lt;span class="nv"&gt;ucFunctor&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="py"&gt;map&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;uc&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;g&lt;/span&gt; &lt;span class="n"&gt;compose&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="nv"&gt;ucFunctor&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="py"&gt;map&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;ucFunctor&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="py"&gt;map&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;uc&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="o"&gt;),&lt;/span&gt; &lt;span class="n"&gt;g&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;However, you can safely use &lt;em&gt;functor&lt;/em&gt; instances brought to you by &lt;em&gt;Cats&lt;/em&gt; or &lt;em&gt;Scalaz&lt;/em&gt; because their implementations &lt;strong&gt;lawfulness&lt;/strong&gt; are tested for you.&lt;/p&gt;

&lt;p&gt;(You can find the &lt;em&gt;Cats&lt;/em&gt; &lt;em&gt;functor&lt;/em&gt; laws &lt;a href="https://github.com/typelevel/cats/blob/master/laws/src/main/scala/cats/laws/FunctorLaws.scala" rel="noopener noreferrer"&gt;here&lt;/a&gt; and their tests &lt;a href="https://github.com/typelevel/cats/blob/master/laws/src/main/scala/cats/laws/discipline/FunctorTests.scala" rel="noopener noreferrer"&gt;here&lt;/a&gt;. They are tested with &lt;a href="https://typelevel.org/cats/typeclasses/lawtesting.html" rel="noopener noreferrer"&gt;discipline&lt;/a&gt;.)&lt;/p&gt;

&lt;p&gt;Now that you know what a &lt;em&gt;functor&lt;/em&gt; is and how it's implemented in Scala, let's talk a bit about category theory !&lt;/p&gt;

&lt;h1&gt;
  
  
  An insight about the theory behind &lt;em&gt;functors&lt;/em&gt;
&lt;/h1&gt;

&lt;p&gt;During this article, we only talked about the most widely known kind of &lt;em&gt;functors&lt;/em&gt;, the &lt;em&gt;co-variant endofunctors&lt;/em&gt;. Don't mind the complicated name, they are all you need to know to begin having fun in functional programming.&lt;/p&gt;

&lt;p&gt;However if you'd like to have a grasp a little bit of theory behind &lt;em&gt;functors&lt;/em&gt;, keep on reading.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Functors&lt;/em&gt; are structure-preserving mappings between categories.&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Tiny crash course into category theory
&lt;/h2&gt;

&lt;p&gt;Category theory is the mathematical field that study how things relate to each others in general and how their relations compose.&lt;/p&gt;

&lt;p&gt;A category is composed of:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Objects&lt;/strong&gt; (view it as something purely abstract, absolutely anything, points for example)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Arrows&lt;/strong&gt; or &lt;strong&gt;morphisms&lt;/strong&gt; (which are the ways to go from one object to another)&lt;/li&gt;
&lt;li&gt;And two fundamental properties:

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Composition&lt;/strong&gt;: A way to compose these arrows associatively. It means that if it exists an arrow from an object &lt;code&gt;a&lt;/code&gt; to an object &lt;code&gt;b&lt;/code&gt; and an arrow from the object &lt;code&gt;b&lt;/code&gt; to an object &lt;code&gt;c&lt;/code&gt;, it exists an arrow that goes from &lt;code&gt;a&lt;/code&gt; to &lt;code&gt;c&lt;/code&gt; and the order of composition does not matter (given 3 morphisms that are composable &lt;code&gt;f&lt;/code&gt;, &lt;code&gt;g&lt;/code&gt;, &lt;code&gt;h&lt;/code&gt; then (&lt;code&gt;h&lt;/code&gt; . &lt;code&gt;g&lt;/code&gt;) . &lt;code&gt;f&lt;/code&gt;) == &lt;code&gt;h&lt;/code&gt; . (&lt;code&gt;g&lt;/code&gt; . &lt;code&gt;f&lt;/code&gt;))&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Identity&lt;/strong&gt;: There is an identity arrow for every object in the category which is the arrow which goes from that object to itself&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fupload.wikimedia.org%2Fwikipedia%2Fcommons%2Fthumb%2Ff%2Fff%2FCategory_SVG.svg%2F1024px-Category_SVG.svg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fupload.wikimedia.org%2Fwikipedia%2Fcommons%2Fthumb%2Ff%2Fff%2FCategory_SVG.svg%2F1024px-Category_SVG.svg.png" alt="category"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;A&lt;/code&gt;, &lt;code&gt;B&lt;/code&gt;, &lt;code&gt;C&lt;/code&gt; are this category's &lt;strong&gt;objects&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;f&lt;/code&gt; and &lt;code&gt;g&lt;/code&gt; are its &lt;strong&gt;arrows&lt;/strong&gt; or &lt;strong&gt;morphisms&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;g . f&lt;/code&gt; is &lt;code&gt;f&lt;/code&gt; and &lt;code&gt;g&lt;/code&gt; composition since &lt;code&gt;f&lt;/code&gt; goes from &lt;code&gt;A&lt;/code&gt; to &lt;code&gt;B&lt;/code&gt; and &lt;code&gt;g&lt;/code&gt; goes from &lt;code&gt;B&lt;/code&gt; to &lt;code&gt;C&lt;/code&gt; (and it &lt;strong&gt;MUST&lt;/strong&gt; exist to satisfy composition law, since &lt;code&gt;f&lt;/code&gt; and &lt;code&gt;g&lt;/code&gt; exist)&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;1A&lt;/code&gt;, &lt;code&gt;1B&lt;/code&gt; and &lt;code&gt;1C&lt;/code&gt; are the identity arrows of &lt;code&gt;A&lt;/code&gt;, &lt;code&gt;B&lt;/code&gt; and &lt;code&gt;C&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Back to &lt;em&gt;Scala&lt;/em&gt;
&lt;/h2&gt;

&lt;p&gt;In the context of purely functional programming in &lt;em&gt;Scala&lt;/em&gt;, we can consider that we work in a particular category that we are going to call it &lt;code&gt;S&lt;/code&gt; (I won't go into theoretical compromises implied by that parallel, but there are some !):&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;S&lt;/code&gt; &lt;strong&gt;objects&lt;/strong&gt; are &lt;em&gt;Scala&lt;/em&gt;'s &lt;strong&gt;types&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;S&lt;/code&gt; &lt;strong&gt;morphisms&lt;/strong&gt; are &lt;em&gt;Scala&lt;/em&gt;'s &lt;strong&gt;functions&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Composition&lt;/strong&gt; between morphisms is then &lt;strong&gt;function composition&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Identity&lt;/strong&gt; morphisms for &lt;code&gt;S&lt;/code&gt; objects is &lt;strong&gt;the identity function&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;p&gt;Indeed, if we consider the object &lt;code&gt;a&lt;/code&gt; (the type &lt;code&gt;A&lt;/code&gt;) and the object &lt;code&gt;b&lt;/code&gt; (the type &lt;code&gt;B&lt;/code&gt;), &lt;em&gt;Scala&lt;/em&gt; functions &lt;code&gt;A =&amp;gt; B&lt;/code&gt; are morphisms between &lt;code&gt;a&lt;/code&gt; and &lt;code&gt;b&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Given our morphism from &lt;code&gt;a&lt;/code&gt; to &lt;code&gt;b&lt;/code&gt;, if it exists an object &lt;code&gt;c&lt;/code&gt; (the type &lt;code&gt;C&lt;/code&gt;) and a morphism between &lt;code&gt;b&lt;/code&gt; and &lt;code&gt;c&lt;/code&gt; exists (a function &lt;code&gt;B =&amp;gt; C&lt;/code&gt;):&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Then it must exist a morphism from &lt;code&gt;a&lt;/code&gt; to &lt;code&gt;c&lt;/code&gt; which is the composition of the two. And it does ! It is (pseudo code):

&lt;ul&gt;
&lt;li&gt;For &lt;code&gt;g: B =&amp;gt; C&lt;/code&gt; and &lt;code&gt;f: A =&amp;gt; B&lt;/code&gt;, &lt;code&gt;g compose f&lt;/code&gt; &lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;And that composition is associative:

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;(h compose g) compose f&lt;/code&gt; is the same as &lt;code&gt;h compose (g compose f)&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;p&gt;Moreover for every object (every type) it exists an identity morphism, the identity function, which is the type parametric function: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;def id[A](a: A) = a&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;We can now grasp how category theory and purely functional programming can relate !&lt;/p&gt;

&lt;h2&gt;
  
  
  And then back to our &lt;em&gt;functors&lt;/em&gt;
&lt;/h2&gt;

&lt;p&gt;Now that you know what a category is, and that you know about the category &lt;code&gt;S&lt;/code&gt; we work in when doing functional programming in &lt;em&gt;Scala&lt;/em&gt;, re-think about it. &lt;/p&gt;

&lt;p&gt;A &lt;em&gt;functor&lt;/em&gt; &lt;code&gt;F&lt;/code&gt; being a structure-preserving mapping between two categories means that it maps objects from category &lt;code&gt;A&lt;/code&gt; to objects from category &lt;code&gt;F(A)&lt;/code&gt; (the category which &lt;code&gt;A&lt;/code&gt; is mapped to by the &lt;em&gt;functor&lt;/em&gt; &lt;code&gt;F&lt;/code&gt;) and morphisms from &lt;code&gt;A&lt;/code&gt; to morphisms of &lt;code&gt;F(A)&lt;/code&gt; while preserving their relations.&lt;/p&gt;

&lt;p&gt;Since we always work with types and with functions between types in Scala, a &lt;em&gt;functor&lt;/em&gt; in that context is a mapping from and to the &lt;strong&gt;same category&lt;/strong&gt;, between &lt;code&gt;S&lt;/code&gt; and &lt;code&gt;S&lt;/code&gt;, and that particular kind of &lt;em&gt;functor&lt;/em&gt; is called an &lt;strong&gt;endofunctor&lt;/strong&gt;. &lt;/p&gt;

&lt;p&gt;Let's explore how &lt;code&gt;Option&lt;/code&gt; behaves (but we could have replaced &lt;code&gt;Option&lt;/code&gt; by any &lt;em&gt;functor&lt;/em&gt; &lt;code&gt;F&lt;/code&gt;):&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Objects&lt;/strong&gt;&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Objects in &lt;code&gt;S&lt;/code&gt; (types)&lt;/th&gt;
&lt;th&gt;Objects in &lt;code&gt;F(S)&lt;/code&gt;
&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;A&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;Option[A]&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;Int&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;Option[Int]&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;String&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;Option[String]&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;So &lt;code&gt;Option&lt;/code&gt; type construtor maps objects (types) in &lt;code&gt;S&lt;/code&gt; to other objects (types) in &lt;code&gt;S&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Morphisms&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Let's use our previously defined:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt; &lt;code&gt;def map[A, B](fa: F[A], func: A =&amp;gt; B): F[B]&lt;/code&gt;. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If we partially apply &lt;code&gt;map&lt;/code&gt; with a function &lt;code&gt;f&lt;/code&gt; of type &lt;code&gt;A =&amp;gt; B&lt;/code&gt; like so (pseudo-code): &lt;code&gt;map(_, f)&lt;/code&gt;, then we are left with a new function of type &lt;code&gt;F[A] =&amp;gt; F[B]&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;Using &lt;code&gt;map&lt;/code&gt; that way, let's see how morphisms behave:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Morphisms in &lt;code&gt;S&lt;/code&gt; (function between types)&lt;/th&gt;
&lt;th&gt;Morphisms in &lt;code&gt;F(S)&lt;/code&gt;
&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;code&gt;A =&amp;gt; A&lt;/code&gt; (identity)&lt;/td&gt;
&lt;td&gt;&lt;code&gt;Option[A] =&amp;gt; Option[A]&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;A =&amp;gt; B&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;Option[A] =&amp;gt; Option[B]&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;Int =&amp;gt; Float&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;Option[Int] =&amp;gt; Option[Float]&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;String =&amp;gt; String&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;Option[String] =&amp;gt; Option[String]&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;So &lt;code&gt;Option&lt;/code&gt;'s &lt;code&gt;map&lt;/code&gt; maps morphisms (functions from type to type) in &lt;code&gt;S&lt;/code&gt; to other morphisms (functions from type to type) in &lt;code&gt;S&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;We won't go into details but we could have shown how &lt;code&gt;Option&lt;/code&gt; &lt;em&gt;functor&lt;/em&gt; respects morphism composition and identity laws.&lt;/p&gt;

&lt;h2&gt;
  
  
  What does it buy us ?
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;em&gt;Functors&lt;/em&gt; are mappings between two categories&lt;/li&gt;
&lt;li&gt;A &lt;em&gt;functor&lt;/em&gt;, due to its theorical nature, preserves the &lt;em&gt;morphisms&lt;/em&gt; and their relations between the two categories it maps&lt;/li&gt;
&lt;li&gt;When programming in pure FP, we are in &lt;code&gt;S&lt;/code&gt;, the category of &lt;em&gt;Scala&lt;/em&gt; types, functions and under function composition. The &lt;em&gt;functors&lt;/em&gt; we use are then &lt;em&gt;endofunctors&lt;/em&gt; (from &lt;code&gt;S&lt;/code&gt; to &lt;code&gt;S&lt;/code&gt;) because they map &lt;em&gt;Scala&lt;/em&gt; types and functions between them to other &lt;em&gt;Scala&lt;/em&gt; types and functions between them&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In programming terms, &lt;em&gt;(endo)functors&lt;/em&gt; in &lt;em&gt;Scala&lt;/em&gt; allow us to move from origin types (&lt;code&gt;A&lt;/code&gt;, &lt;code&gt;B&lt;/code&gt;, ...), to new target types (&lt;code&gt;F[A]&lt;/code&gt;, &lt;code&gt;F[B]&lt;/code&gt;, ...) while safely allowing us to re-use the origin functions and their compositions on the target types.&lt;/p&gt;

&lt;p&gt;To continue with our &lt;code&gt;Option&lt;/code&gt; example, &lt;code&gt;Option&lt;/code&gt; type constructor "map" our types &lt;code&gt;A&lt;/code&gt; and &lt;code&gt;B&lt;/code&gt; into &lt;code&gt;Option[A]&lt;/code&gt; and &lt;code&gt;Option[B]&lt;/code&gt; types while allowing us to re-use functions of type &lt;code&gt;A =&amp;gt; B&lt;/code&gt; thanks to &lt;code&gt;Options&lt;/code&gt;' &lt;code&gt;map&lt;/code&gt;, turning them into &lt;code&gt;Option[A] =&amp;gt; Option[B]&lt;/code&gt; and preserving their compositions. &lt;/p&gt;

&lt;p&gt;But that is not over ! Let's leave abstraction world we all love so much and head back to concrete world.&lt;/p&gt;

&lt;p&gt;Concrete &lt;em&gt;functors&lt;/em&gt;  instances enhance our origin types with new capacities. &lt;strong&gt;Indeed, &lt;em&gt;functor&lt;/em&gt; instances are concrete data structures&lt;/strong&gt; with particularities (the one we said we did not care about at the beginning of that article), the abilty to represent empty value for &lt;code&gt;Option&lt;/code&gt;, the ability to suspend an effectful computation for &lt;code&gt;IO&lt;/code&gt;, the ability to hold multiple values for &lt;code&gt;List&lt;/code&gt; and so on !&lt;/p&gt;

&lt;p&gt;Ok, so, to sum up, why &lt;em&gt;functors&lt;/em&gt; are awesome ? The two main reasons I can think of are:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Abstraction, abstraction, abstraction... Code using &lt;em&gt;functors&lt;/em&gt; allows you to only care about the fact that what you manipulate is mappable.

&lt;ul&gt;
&lt;li&gt;It increases code reuse since a piece of code using &lt;em&gt;functors&lt;/em&gt; can be called with any concrete &lt;em&gt;functor&lt;/em&gt; instance &lt;/li&gt;
&lt;li&gt;And it reduces a lot error risks since you &lt;em&gt;have to&lt;/em&gt; deal with less particularities of the concrete, final, data structure your functions will be called with&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;They add functionnalities to existing types, while allowing to still use functions on them (you would not want to re-write them for every &lt;em&gt;functor&lt;/em&gt; instances), and that's a big deal:

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;Option&lt;/code&gt; allow you to bring &lt;code&gt;null&lt;/code&gt; into a concrete value, making code a lot healthier (and functions purer)&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Either&lt;/code&gt; allow you to bring computation errors into concrete values, making dealing with computation errors a lot healthier (and functions purer)&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;IO&lt;/code&gt; allow you to turn a computations into a values, allowing better compositionality and referential transparency&lt;/li&gt;
&lt;li&gt;And so on...&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;I hope I made a bit clearer what &lt;em&gt;functors&lt;/em&gt; are in the context of category theory and how that translates to pure FP in &lt;em&gt;Scala&lt;/em&gt; !&lt;/p&gt;

&lt;h1&gt;
  
  
  More material
&lt;/h1&gt;

&lt;p&gt;If you want to keep diving deeper, some interesting stuff can be found on my &lt;a href="https://github.com/mmenestret/fp-ressources" rel="noopener noreferrer"&gt;FP resources list&lt;/a&gt; and in particular:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://underscore.io/books/scala-with-cats/" rel="noopener noreferrer"&gt;Scala with Cats - Functor chapter&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://adit.io/posts/2013-04-17-functors,_applicatives,_and_monads_in_pictures.html#functors" rel="noopener noreferrer"&gt;Functors' section of the awesome Functors, Applicatives, And Monads In Pictures article&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://yogsototh.github.io/Category-Theory-Presentation/categories.html" rel="noopener noreferrer"&gt;Yann Esposito's great "Category theory and programming"&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Let me know if you need more&lt;/li&gt;
&lt;/ul&gt;

&lt;h1&gt;
  
  
  Conclusion
&lt;/h1&gt;

&lt;p&gt;To sum up, we saw:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;That a &lt;em&gt;functor&lt;/em&gt; is a kind of container that can be mapped over by a function and the laws it has to respect&lt;/li&gt;
&lt;li&gt;Some examples and identified a common pattern&lt;/li&gt;
&lt;li&gt;How we abstract over and encode that pattern in &lt;em&gt;Scala&lt;/em&gt; as a &lt;em&gt;type class&lt;/em&gt; of &lt;em&gt;type constructors&lt;/em&gt;
&lt;/li&gt;
&lt;li&gt;We had a modest overview about category theory, what &lt;em&gt;functors&lt;/em&gt; are in category theory, and how both relates to pure FP in &lt;em&gt;Scala&lt;/em&gt;
&lt;/li&gt;
&lt;li&gt;We concluded by how great &lt;em&gt;functors&lt;/em&gt; are and for what practical reasons&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I'll try to keep that blog post updated.&lt;br&gt;
If there are any additions, imprecision or mistakes that I should correct or if you need more explanations, feel free to contact me on Twitter or by mail !&lt;/p&gt;




&lt;p&gt;Edit: Thanks &lt;a href="https://twitter.com/guizmaii" rel="noopener noreferrer"&gt;Jules Ivanic&lt;/a&gt; for the review :).&lt;/p&gt;

</description>
      <category>scala</category>
      <category>functional</category>
    </item>
  </channel>
</rss>
