{"id":277,"date":"2022-05-29T14:40:49","date_gmt":"2022-05-29T14:40:49","guid":{"rendered":"https:\/\/www.kindsonthegenius.com\/scala\/?p=277"},"modified":"2022-05-29T14:41:18","modified_gmt":"2022-05-29T14:41:18","slug":"scala-algebraic-data-types-adt","status":"publish","type":"post","link":"https:\/\/www.kindsonthegenius.com\/scala\/scala-algebraic-data-types-adt\/","title":{"rendered":"Scala &#8211; Algebraic Data Types (ADT)"},"content":{"rendered":"<p>In this tutorial, you will learn about the very important concept of Algebraic Data Types (ADT) in Scala.<\/p>\n<p>Let&#8217;s begin with what we already know, <em>enum<\/em> or <em>enumeration<\/em> types.<\/p>\n<ol>\n<li><a href=\"#t1\">Enumerations Types<\/a><\/li>\n<li><a href=\"#t2\">What are ADTS<\/a><\/li>\n<li><a href=\"#t3\">Sum Types (Union Types)<\/a><\/li>\n<li><a href=\"#t4\">Pattern Matching on ADTs<\/a><\/li>\n<\/ol>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t1\">1. Enumeration Types<\/strong><\/h4>\n<p>An enumeration is used to define a data type that consists of a set of named values. All the value in an enumeration share a unique type known as Value type member of the enumeration.<\/p>\n<p><strong>Creating Enums in Scala<\/strong><\/p>\n<p>To create a enum type in Scala, we need to create an object that extends the Enumeration type. This is because Scala does not provide an <em>enum<\/em> keyword.<\/p>\n<p>The code below creates an enum type names Size with three values: Small, Medium and Large<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #008800; font-weight: bold;\">object<\/span> <span style=\"color: #bb0066; font-weight: bold;\">Sizes<\/span> <span style=\"color: #008800; font-weight: bold;\">extends<\/span> <span style=\"color: #bb0066; font-weight: bold;\">Enumeration<\/span> <span style=\"color: #333333;\">{<\/span>\r\n  <span style=\"color: #008800; font-weight: bold;\">type<\/span> <span style=\"color: #333399; font-weight: bold;\">Sizes<\/span> <span style=\"color: #333333;\">=<\/span> <span style=\"color: #bb0066; font-weight: bold;\">Value<\/span>\r\n  <span style=\"color: #008800; font-weight: bold;\">val<\/span> <span style=\"color: #bb0066; font-weight: bold;\">Small<\/span><span style=\"color: #333333;\">,<\/span> <span style=\"color: #bb0066; font-weight: bold;\">Medium<\/span><span style=\"color: #333333;\">,<\/span> <span style=\"color: #bb0066; font-weight: bold;\">Large<\/span> <span style=\"color: #008800; font-weight: bold;\">=<\/span> <span style=\"color: #bb0066; font-weight: bold;\">Value<\/span>\r\n<span style=\"color: #333333;\">}<\/span>\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>You can print the string value of enumeration using\u00a0 the code below:<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<pre style=\"margin: 0; line-height: 125%;\">println<span style=\"color: #333333;\">(<\/span>s<span style=\"background-color: #fff0f0;\">\"My size is ${Size.Medium}\"<\/span><span style=\"color: #333333;\">)<\/span>\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>Pattern Matching on Enum Values<\/strong><\/p>\n<p>Although we&#8217;ll talk about pattern matching in a different tutorial, the code below shows how to perform some operation base on pattern matching on values of an enum.<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #bb0066; font-weight: bold;\">Size<\/span><span style=\"color: #333333;\">.<\/span>values<span style=\"color: #333333;\">.<\/span>foreach <span style=\"color: #333333;\">{<\/span>\r\n  <span style=\"color: #008800; font-weight: bold;\">case<\/span> s <span style=\"color: #008800; font-weight: bold;\">if<\/span> <span style=\"color: #333333;\">(<\/span>s <span style=\"color: #333333;\">==<\/span> <span style=\"color: #bb0066; font-weight: bold;\">Size<\/span><span style=\"color: #333333;\">.<\/span><span style=\"color: #bb0066; font-weight: bold;\">Small<\/span> <span style=\"color: #333333;\">)<\/span> <span style=\"color: #008800; font-weight: bold;\">=&gt;<\/span> <span style=\"color: #333333;\">{<\/span>\r\n    println<span style=\"color: #333333;\">(<\/span>s<span style=\"color: #333333;\">)<\/span>\r\n  <span style=\"color: #333333;\">}<\/span>\r\n  <span style=\"color: #008800; font-weight: bold;\">case<\/span> s <span style=\"color: #008800; font-weight: bold;\">if<\/span> <span style=\"color: #333333;\">(<\/span>s <span style=\"color: #333333;\">==<\/span> <span style=\"color: #bb0066; font-weight: bold;\">Size<\/span><span style=\"color: #333333;\">.<\/span><span style=\"color: #bb0066; font-weight: bold;\">Medium<\/span><span style=\"color: #333333;\">)<\/span> <span style=\"color: #008800; font-weight: bold;\">=&gt;<\/span> <span style=\"color: #333333;\">{<\/span>\r\n    println<span style=\"color: #333333;\">(<\/span>s<span style=\"color: #333333;\">)<\/span>\r\n  <span style=\"color: #333333;\">}<\/span>\r\n  <span style=\"color: #008800; font-weight: bold;\">case<\/span> s <span style=\"color: #008800; font-weight: bold;\">if<\/span> <span style=\"color: #333333;\">(<\/span>s <span style=\"color: #333333;\">==<\/span> <span style=\"color: #bb0066; font-weight: bold;\">Size<\/span><span style=\"color: #333333;\">.<\/span><span style=\"color: #bb0066; font-weight: bold;\">Large<\/span><span style=\"color: #333333;\">)<\/span> <span style=\"color: #008800; font-weight: bold;\">=&gt;<\/span> <span style=\"color: #333333;\">{<\/span>\r\n    println<span style=\"color: #333333;\">(<\/span>s<span style=\"color: #333333;\">)<\/span>\r\n  <span style=\"color: #333333;\">}<\/span>\r\n<span style=\"color: #333333;\">}<\/span>\r\n<\/pre>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t2\">2. Algebraic Data Types<\/strong><\/h4>\n<p>In theory, algebra can be viewed as:<\/p>\n<ul>\n<li>a set of objects<\/li>\n<li>as set of operations that can be applied to those objects to create new objects<\/li>\n<\/ul>\n<p>For example, we can apply a set of arithmetic operations (+, -, * and \/) to a set of numbers to produce new numbers.<\/p>\n<p>In Scala, we can apply certain operation to a set of types to produce\u00a0 new type(s).<\/p>\n<p>Let&#8217;s create a new type called Weather from a set of objects: Windy, Cloudy, Rainy and Overcast<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #008800; font-weight: bold;\">sealed<\/span>  <span style=\"color: #008800; font-weight: bold;\">trait<\/span> <span style=\"color: #bb0066; font-weight: bold;\">Weather<\/span>\r\n\r\n<span style=\"color: #008800; font-weight: bold;\">case<\/span> <span style=\"color: #008800; font-weight: bold;\">object<\/span> <span style=\"color: #bb0066; font-weight: bold;\">Rainy<\/span> <span style=\"color: #008800; font-weight: bold;\">extends<\/span> <span style=\"color: #bb0066; font-weight: bold;\">Weather<\/span>\r\n<span style=\"color: #008800; font-weight: bold;\">case<\/span> <span style=\"color: #008800; font-weight: bold;\">object<\/span> <span style=\"color: #bb0066; font-weight: bold;\">Windy<\/span> <span style=\"color: #008800; font-weight: bold;\">extends<\/span> <span style=\"color: #bb0066; font-weight: bold;\">Weather<\/span>\r\n<span style=\"color: #008800; font-weight: bold;\">case<\/span> <span style=\"color: #008800; font-weight: bold;\">object<\/span> <span style=\"color: #bb0066; font-weight: bold;\">Cloudy<\/span> <span style=\"color: #008800; font-weight: bold;\">extends<\/span> <span style=\"color: #bb0066; font-weight: bold;\">Weather<\/span>\r\n<span style=\"color: #008800; font-weight: bold;\">case<\/span> <span style=\"color: #008800; font-weight: bold;\">object<\/span> <span style=\"color: #bb0066; font-weight: bold;\">Overcast<\/span> <span style=\"color: #008800; font-weight: bold;\">extends<\/span> <span style=\"color: #bb0066; font-weight: bold;\">Weather<\/span>\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>The <em>sealed trait<\/em> means that only these four values defined here can extend the Weather type. Let&#8217;s now talk about a specific algebraic data type: the sum type<\/p>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t3\">3. The Sum Type (Union Type)<\/strong><\/h4>\n<p>The Sum type helps us define a type that can have certain values. With the sum type, we enumerate all the possible instances of a type. The following are important to know about the sum type:<\/p>\n<ul>\n<li>they are create with a sealed trait as\u00a0 the base type<\/li>\n<li>the individual instances are created as case objects<\/li>\n<li>the &#8220;is-a&#8221; relationship is used for describing the sum type<\/li>\n<li>instances of the sum type can be parameterized<\/li>\n<\/ul>\n<p>In the code below, we define an Employee type which could be either a Doctor, Driver or Intern. This is represented below:<\/p>\n<ul>\n<li>Doctor has two parameter: name: String and specialty: String<\/li>\n<li>Driver has two parameter: name: String and assignedCar: String<\/li>\n<li>Intern has one parameter: age<\/li>\n<\/ul>\n<p>The equivalent Scala code is given below:<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #008800; font-weight: bold;\">sealed<\/span> <span style=\"color: #008800; font-weight: bold;\">trait<\/span> <span style=\"color: #bb0066; font-weight: bold;\">Employee<\/span>\r\n  <span style=\"color: #008800; font-weight: bold;\">case<\/span> <span style=\"color: #008800; font-weight: bold;\">class<\/span> <span style=\"color: #bb0066; font-weight: bold;\">Doctor<\/span><span style=\"color: #333333;\">(<\/span>name<span style=\"color: #008800; font-weight: bold;\">:<\/span><span style=\"color: #333399; font-weight: bold;\">String<\/span><span style=\"color: #333333;\">,<\/span> specialty<span style=\"color: #008800; font-weight: bold;\">:<\/span> <span style=\"color: #333399; font-weight: bold;\">String<\/span><span style=\"color: #333333;\">)<\/span> <span style=\"color: #008800; font-weight: bold;\">extends<\/span> <span style=\"color: #bb0066; font-weight: bold;\">Employee<\/span>\r\n  <span style=\"color: #008800; font-weight: bold;\">case<\/span> <span style=\"color: #008800; font-weight: bold;\">class<\/span> <span style=\"color: #bb0066; font-weight: bold;\">Driver<\/span><span style=\"color: #333333;\">(<\/span>name<span style=\"color: #008800; font-weight: bold;\">:<\/span><span style=\"color: #333399; font-weight: bold;\">String<\/span><span style=\"color: #333333;\">,<\/span> assignedCar<span style=\"color: #008800; font-weight: bold;\">:<\/span> <span style=\"color: #333399; font-weight: bold;\">String<\/span><span style=\"color: #333333;\">)<\/span> <span style=\"color: #008800; font-weight: bold;\">extends<\/span> <span style=\"color: #bb0066; font-weight: bold;\">Employee<\/span>\r\n  <span style=\"color: #008800; font-weight: bold;\">case<\/span> <span style=\"color: #008800; font-weight: bold;\">class<\/span> <span style=\"color: #bb0066; font-weight: bold;\">Intern<\/span><span style=\"color: #333333;\">(<\/span>name<span style=\"color: #008800; font-weight: bold;\">:<\/span><span style=\"color: #333399; font-weight: bold;\">String<\/span><span style=\"color: #333333;\">,<\/span> age<span style=\"color: #008800; font-weight: bold;\">:<\/span> <span style=\"color: #333399; font-weight: bold;\">Int<\/span><span style=\"color: #333333;\">)<span style=\"color: #008800; font-weight: bold;\">extends<\/span> <span style=\"color: #bb0066; font-weight: bold;\">Employee<\/span><\/span>\r\n<\/pre>\n<p>Now you that you understand ADTs, you can create a couple more just to get used to it.<\/p>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t4\">4. Pattern Matching With ADTs<\/strong><\/h4>\n<p>Pattern matching allows us to decompose a specified ADT using the extractor method. With it, we can extract the fields of an ADT. This means that the case classed would have to implement unapply method to be an extractor.<\/p>\n<p>Let&#8217;s define a pattern match for our Employee ADT<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #008800; font-weight: bold;\">def<\/span> isEmployee<span style=\"color: #333333;\">(<\/span>e<span style=\"color: #008800; font-weight: bold;\">:<\/span> <span style=\"color: #333399; font-weight: bold;\">Employee<\/span><span style=\"color: #333333;\">)<\/span> <span style=\"color: #008800; font-weight: bold;\">:<\/span> <span style=\"color: #333399; font-weight: bold;\">Boolean<\/span> <span style=\"color: #333333;\">=<\/span> e <span style=\"color: #008800; font-weight: bold;\">match<\/span> <span style=\"color: #333333;\">{<\/span>\r\n  <span style=\"color: #008800; font-weight: bold;\">case<\/span> <span style=\"color: #bb0066; font-weight: bold;\">Doctor<\/span><span style=\"color: #333333;\">(<\/span>name<span style=\"color: #333333;\">,<\/span> specialty<span style=\"color: #333333;\">)<\/span> <span style=\"color: #008800; font-weight: bold;\">=&gt;<\/span> <span style=\"color: #008800; font-weight: bold;\">true<\/span>\r\n  <span style=\"color: #008800; font-weight: bold;\">case<\/span> <span style=\"color: #bb0066; font-weight: bold;\">Driver<\/span><span style=\"color: #333333;\">(<\/span>name<span style=\"color: #333333;\">,<\/span> assignedCar<span style=\"color: #333333;\">)<\/span> <span style=\"color: #008800; font-weight: bold;\">=&gt;true<\/span>\r\n  <span style=\"color: #008800; font-weight: bold;\">case<\/span> <span style=\"color: #bb0066; font-weight: bold;\">Intern<\/span><span style=\"color: #333333;\">(<\/span>name<span style=\"color: #333333;\">,<\/span> age<span style=\"color: #333333;\">)<\/span> <span style=\"color: #008800; font-weight: bold;\">=&gt;<\/span> <span style=\"color: #008800; font-weight: bold;\">false<\/span>\r\n<span style=\"color: #333333;\">}<\/span>\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>In the case of pattern matching code above, we define a function that takes an Employee object and returns a boolean depending on the particular instance.<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, you will learn about the very important concept of Algebraic Data Types (ADT) in Scala. Let&#8217;s begin with what we already know, &hellip; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[52,53,54,55],"class_list":["post-277","post","type-post","status-publish","format-standard","hentry","category-scala-programming","tag-adt","tag-algebraic-data-types","tag-pattern-matching","tag-sum-type"],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/www.kindsonthegenius.com\/scala\/wp-json\/wp\/v2\/posts\/277","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.kindsonthegenius.com\/scala\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.kindsonthegenius.com\/scala\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/scala\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/scala\/wp-json\/wp\/v2\/comments?post=277"}],"version-history":[{"count":3,"href":"https:\/\/www.kindsonthegenius.com\/scala\/wp-json\/wp\/v2\/posts\/277\/revisions"}],"predecessor-version":[{"id":280,"href":"https:\/\/www.kindsonthegenius.com\/scala\/wp-json\/wp\/v2\/posts\/277\/revisions\/280"}],"wp:attachment":[{"href":"https:\/\/www.kindsonthegenius.com\/scala\/wp-json\/wp\/v2\/media?parent=277"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/scala\/wp-json\/wp\/v2\/categories?post=277"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/scala\/wp-json\/wp\/v2\/tags?post=277"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}