<?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: kamya</title>
    <description>The latest articles on DEV Community by kamya (@shethiakamya).</description>
    <link>https://dev.to/shethiakamya</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%2F213623%2Fbb347470-744a-42da-bdcd-17e11aa68414.jpg</url>
      <title>DEV Community: kamya</title>
      <link>https://dev.to/shethiakamya</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/shethiakamya"/>
    <language>en</language>
    <item>
      <title>SQL for the Potterhead: Outer Joins</title>
      <dc:creator>kamya</dc:creator>
      <pubDate>Wed, 26 Feb 2020 04:07:00 +0000</pubDate>
      <link>https://dev.to/shethiakamya/sql-for-the-potterhead-outer-joins-134l</link>
      <guid>https://dev.to/shethiakamya/sql-for-the-potterhead-outer-joins-134l</guid>
      <description>&lt;p&gt;In this article we will cover the Left Outer Join as well as the Right Outer Join. Let's dive in! &lt;/p&gt;

&lt;h4&gt;
  
  
  Hagrid has some extra pets, and he's thinking about giving some away as Christmas gifts. To do this, he wants to find the names of all the students who don't have pets. Can we help him?
&lt;/h4&gt;

&lt;p&gt;We want all the students who don't have pets. Let's start by visualizing this: &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--mVP1NDiK--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/gp6c5cth823be0u118yg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--mVP1NDiK--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/gp6c5cth823be0u118yg.png" alt="Venn diagram for join"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Looking at the diagram, this looks very similar to the one for our &lt;a href="https://dev.to/shethiakamya/sql-for-the-potterhead-left-joins-2p1g"&gt;left join&lt;/a&gt;, except that we now want to exclude data that is present in both the tables. &lt;/p&gt;

&lt;p&gt;Let's start writing the query. &lt;/p&gt;

&lt;p&gt;1.First, we will write a left join. This will give us all the wizards, and attach their pets if they have them:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;postgres&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="c"&gt;#&lt;/span&gt;
SELECT &lt;span class="k"&gt;*&lt;/span&gt; FROM wizard LEFT JOIN pet ON wizard.id &lt;span class="o"&gt;=&lt;/span&gt; pet.owner_id&lt;span class="p"&gt;;&lt;/span&gt;
 &lt;span class="nb"&gt;id&lt;/span&gt; |        name        |   house    | &lt;span class="nb"&gt;id&lt;/span&gt; |    name     | species | owner_id 
&lt;span class="nt"&gt;----&lt;/span&gt;+--------------------+------------+----+-------------+---------+----------
  1 | Neville Longbottom | Gryffindor |  2 | Trevor      | toad    |        1
  2 | Ronald Weasley     | Gryffindor |  1 | Scabbers    | rat     |        2
  3 | Harry Potter       | Gryffindor |  3 | Hedwig      | owl     |        3
  5 | Seamus Finnigan    | Gryffindor |    |             |         |         
  6 | Hermione Granger   | Gryffindor |  4 | Crookshanks | &lt;span class="nb"&gt;cat&lt;/span&gt;     |        6
  4 | Draco Malfoy       | Slytherin  |  5 | unknown      | owl     |        4
&lt;span class="o"&gt;(&lt;/span&gt;6 rows&lt;span class="o"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;All 6 rows in our wizards table are present. If a wizard has a pet, they have been added.&lt;/p&gt;

&lt;p&gt;2.Looking at the data above, you might notice that the &lt;code&gt;owner_id&lt;/code&gt; is blank for the wizards that did not have pets. Let's use that to filter down to just the wizards that have no pets:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;SELECT &lt;span class="k"&gt;*&lt;/span&gt; FROM wizard LEFT JOIN pet ON wizard.id &lt;span class="o"&gt;=&lt;/span&gt; pet.owner_id WHERE pet.owner_id IS NULL&lt;span class="p"&gt;;&lt;/span&gt;
 &lt;span class="nb"&gt;id&lt;/span&gt; |      name       |   house    | &lt;span class="nb"&gt;id&lt;/span&gt; | name | species | owner_id 
&lt;span class="nt"&gt;----&lt;/span&gt;+-----------------+------------+----+------+---------+----------
  5 | Seamus Finnigan | Gryffindor |    |      |         |         
&lt;span class="o"&gt;(&lt;/span&gt;1 row&lt;span class="o"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;3.Let's break down this query to make sure we understand it &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;code&gt;SELECT *&lt;/code&gt;: We want to select all the fields from the tables &lt;/li&gt;
&lt;li&gt;
&lt;code&gt;FROM wizard LEFT JOIN pet&lt;/code&gt;: We want to JOIN the &lt;code&gt;wizard&lt;/code&gt; and &lt;code&gt;pet&lt;/code&gt; table. We are using a left join, where the wizards table is to the left hand side, and the pets table is to the right hand side &lt;/li&gt;
&lt;li&gt;
&lt;code&gt;ON wizard.id = pet.owner_id&lt;/code&gt;: This is the join condition, which tells our query how to join the results&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;WHERE pet.owner_id IS NULL;&lt;/code&gt; We only want to select the rows where the pet's owner_id is &lt;code&gt;NULL&lt;/code&gt; &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;According to our dataset, Seamus Finnigan is the only wizard without a pet, which should match our understanding of the Harry Potter Universe. &lt;/p&gt;

&lt;h2&gt;
  
  
  3.2 Right Outer Join:
&lt;/h2&gt;

&lt;h4&gt;
  
  
  Hagrid is very worried about abandoned pets, and wants to make sure that all pets without owners are being fed. Can we help him find all the pets that don't have owners listed?
&lt;/h4&gt;

&lt;p&gt;Let's start by visualizing the data we need, using a set diagram: &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Q2wlqj4U--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/1a380lnk5phg1ze8f1u9.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Q2wlqj4U--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/1a380lnk5phg1ze8f1u9.png" alt="Venn diagram for join"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This is very similar to the example we just discussed with the &lt;code&gt;LEFT OUTER JOIN&lt;/code&gt; above. Let's try and write some SQl. &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Let's write a RIGHT JOIN, that will give us all the pets, and attach their owners if they have them:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;postgres&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="c"&gt;#                                                                                                     &lt;/span&gt;
SELECT &lt;span class="k"&gt;*&lt;/span&gt; FROM wizard RIGHT JOIN pet ON wizard.id &lt;span class="o"&gt;=&lt;/span&gt; pet.owner_id&lt;span class="p"&gt;;&lt;/span&gt;
 &lt;span class="nb"&gt;id&lt;/span&gt; |        name        |   house    | &lt;span class="nb"&gt;id&lt;/span&gt; |    name     | species | owner_id 
&lt;span class="nt"&gt;----&lt;/span&gt;+--------------------+------------+----+-------------+---------+----------
  1 | Neville Longbottom | Gryffindor |  2 | Trevor      | toad    |        1
  2 | Ronald Weasley     | Gryffindor |  1 | Scabbers    | rat     |        2
  3 | Harry Potter       | Gryffindor |  3 | Hedwig      | owl     |        3
  6 | Hermione Granger   | Gryffindor |  4 | Crookshanks | &lt;span class="nb"&gt;cat&lt;/span&gt;     |        6
  4 | Draco Malfoy       | Slytherin  |  5 | unknown      | owl     |        4
    |                    |            |  7 | Brodwin     | owl     |       10
    |                    |            |  6 | Norbert     | Dragon  |      100
&lt;span class="o"&gt;(&lt;/span&gt;7 rows&lt;span class="o"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;The pet's without owners have the &lt;code&gt;wizard.id&lt;/code&gt; field blank. Let's use this to just return the pet's with no listed owners:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;postgres&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="c"&gt;# &lt;/span&gt;
SELECT pet.name FROM wizard RIGHT JOIN pet ON wizard.id &lt;span class="o"&gt;=&lt;/span&gt; pet.owner_id WHERE wizard.name IS null&lt;span class="p"&gt;;&lt;/span&gt;
  name   
&lt;span class="nt"&gt;---------&lt;/span&gt;
 Brodwin
 Norbert
&lt;span class="o"&gt;(&lt;/span&gt;2 rows&lt;span class="o"&gt;)&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;That looks good! Hedwig, Scabbers, Crookshanks and Malfoy's nameless owl are absent from this list! &lt;/p&gt;

&lt;p&gt;Let's examine the SQL:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;code&gt;SELECT pet.name&lt;/code&gt; : Select only the pet's name
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;FROM wizard&lt;/code&gt; : the table on the left &lt;/li&gt;
&lt;li&gt;
&lt;code&gt;RIGHT JOIN pet&lt;/code&gt; : the table on the right &lt;/li&gt;
&lt;li&gt;
&lt;code&gt;ON wizard.id = pet.owner_id&lt;/code&gt; : the join clause, which specifies how we want the rows from the two tables to be joined&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;WHERE wizard.name IS null&lt;/code&gt;; : We want to filter out all the rows that are present in both tables. This will leave behind only the rows that are exclusive to the &lt;code&gt;pet&lt;/code&gt; table&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;We have learnt about left and a right outer joins! &lt;/p&gt;




&lt;ol&gt;
&lt;li&gt;Like always, you can view this on &lt;a href="https://github.com/kamyashethia/SQLForThePotterhead/blob/master/Joins.md"&gt;github&lt;/a&gt;
&lt;/li&gt;
&lt;/ol&gt;

</description>
    </item>
    <item>
      <title>SQL for the Potterhead: Inner Joins</title>
      <dc:creator>kamya</dc:creator>
      <pubDate>Wed, 19 Feb 2020 05:01:35 +0000</pubDate>
      <link>https://dev.to/shethiakamya/sql-for-the-potterhead-inner-joins-76n</link>
      <guid>https://dev.to/shethiakamya/sql-for-the-potterhead-inner-joins-76n</guid>
      <description>&lt;h4&gt;
  
  
  Luna Lovegood wants to send a letter but none of the school owls are available. Can we write a query that will return the name of all the students who have owls, so she can find one to borrow??
&lt;/h4&gt;

&lt;p&gt;Let's start by thinking of the data we need from each table: &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;From the &lt;code&gt;pet&lt;/code&gt; table, we want all the pets that are owls &lt;/li&gt;
&lt;li&gt;From the &lt;code&gt;wizard&lt;/code&gt; table, we want all the wizards who have pets that are owls&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Drawing a quick venn diagram, the relationship might look something like this: &lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--ey0SApCb--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/louk9qngsnoc9mi65xpg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ey0SApCb--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/louk9qngsnoc9mi65xpg.png" alt="Venn diagram for inner join"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Since we are only interested in data present in both tables, we will write an inner join. Let's write some SQL: &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;First, let's get all pets that are owls:
&lt;/li&gt;
&lt;/ol&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;
&lt;span class="nv"&gt;postgres&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="c"&gt;# &lt;/span&gt;
SELECT &lt;span class="k"&gt;*&lt;/span&gt; FROM pet WHERE species &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'owl'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
 &lt;span class="nb"&gt;id&lt;/span&gt; |  name   | species | owner_id 
&lt;span class="nt"&gt;----&lt;/span&gt;+---------+---------+----------
  3 | Hedwig  | owl     |        3
  5 | unknown  | owl     |        4
  7 | Brodwin | owl     |       10
&lt;span class="o"&gt;(&lt;/span&gt;3 rows&lt;span class="o"&gt;)&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;Our query returned 3 owls. We can't just send someone's owl on an errand, so we will have to join this with the owners table &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Let's write a JOIN:
&lt;/li&gt;
&lt;/ol&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;postgres&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="c"&gt;# &lt;/span&gt;
SELECT &lt;span class="k"&gt;*&lt;/span&gt; FROM pet JOIN wizard ON wizard.id &lt;span class="o"&gt;=&lt;/span&gt; pet.owner_id WHERE pet.species &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'owl'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
 &lt;span class="nb"&gt;id&lt;/span&gt; |  name  | species | owner_id | &lt;span class="nb"&gt;id&lt;/span&gt; |     name     |   house    
&lt;span class="nt"&gt;----&lt;/span&gt;+--------+---------+----------+----+--------------+------------
  3 | Hedwig | owl     |        3 |  3 | Harry Potter | Gryffindor
  5 | unknown | owl     |        4 |  4 | Draco Malfoy | Slytherin
&lt;span class="o"&gt;(&lt;/span&gt;2 rows&lt;span class="o"&gt;)&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;Nice, it looks like we only have 2 rows. There is no wizard with an &lt;code&gt;owner_id&lt;/code&gt; of 10 in the wizards table, so it's appropriate that Brodwin the owl is not present in our data.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Let's now just filter down to the columns we need:
&lt;/li&gt;
&lt;/ol&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;postgres&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="c"&gt;#&lt;/span&gt;
SELECT wizard.name, pet.name FROM pet JOIN wizard ON wizard.id &lt;span class="o"&gt;=&lt;/span&gt; pet.owner_id WHERE pet.species &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'owl'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
     name     |  name  
&lt;span class="nt"&gt;--------------&lt;/span&gt;+--------
 Harry Potter | Hedwig
 Draco Malfoy | unknown
&lt;span class="o"&gt;(&lt;/span&gt;2 rows&lt;span class="o"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;We have the data we need -- let's step through this query.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;code&gt;SELECT wizard.name, pet.name&lt;/code&gt; : We want to select just two fields; the name of the pet, and the name of the wizard. &lt;/li&gt;
&lt;li&gt;
&lt;code&gt;FROM pet&lt;/code&gt; : pet is our first table, or the table to the left &lt;/li&gt;
&lt;li&gt;
&lt;code&gt;JOIN wizard&lt;/code&gt; :  When we don't specify a type of join, it is assumed we want to run an inner join &lt;/li&gt;
&lt;li&gt;
&lt;code&gt;ON wizard.id = pet.owner_id&lt;/code&gt; : The ON clause&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;WHERE pet.species = 'owl';&lt;/code&gt; : We filter down to only pets that are owls &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The &lt;code&gt;INNER JOIN&lt;/code&gt; clause also comes with alternate syntax, which has slightly fewer words to type. &lt;br&gt;
You could run:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;wizard&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;pet&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;pet&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;wizard&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;wizard&lt;/span&gt;&lt;span class="p"&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;pet&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;owner_id&lt;/span&gt;
&lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="n"&gt;pet&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;species&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'owl'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Notice that the &lt;code&gt;JOIN .. ON&lt;/code&gt; statement is absent. Instead, we specify both the tables in the &lt;code&gt;FROM&lt;/code&gt; clause, and specify the condition with &lt;code&gt;WHERE&lt;/code&gt;. Let's see this in action&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;postgres&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="c"&gt;#&lt;/span&gt;
SELECT wizard.name, pet.name FROM pet, wizard WHERE wizard.id &lt;span class="o"&gt;=&lt;/span&gt; pet.owner_id AND pet.species &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'owl'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
     name     |  name  
&lt;span class="nt"&gt;--------------&lt;/span&gt;+--------
 Harry Potter | Hedwig
 Draco Malfoy | unknown
&lt;span class="o"&gt;(&lt;/span&gt;2 rows&lt;span class="o"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Nice, this returned the same data. We've learnt about the &lt;code&gt;INNER JOIN&lt;/code&gt;! &lt;/p&gt;




&lt;ol&gt;
&lt;li&gt;You can find this github &lt;a href="https://github.com/kamyashethia/SQLForThePotterhead/blob/master/Joins.md"&gt;here&lt;/a&gt; &lt;/li&gt;
&lt;li&gt;A cool article about the owls of harry potter can be found &lt;a href="https://harrypotter.fandom.com/wiki/Owl"&gt;here&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;The schema for the &lt;code&gt;pet&lt;/code&gt; and &lt;code&gt;wizard&lt;/code&gt; tables can be found in the introduction to the series &lt;a href="https://dev.to/shethiakamya/sql-for-the-potterhead-joins-introduction-49h1"&gt;here&lt;/a&gt;. You should be able to follow along without any prior reading! &lt;/li&gt;
&lt;/ol&gt;

</description>
      <category>sql</category>
    </item>
    <item>
      <title>SQL for the Potterhead: Left Joins</title>
      <dc:creator>kamya</dc:creator>
      <pubDate>Tue, 18 Feb 2020 04:06:27 +0000</pubDate>
      <link>https://dev.to/shethiakamya/sql-for-the-potterhead-left-joins-2p1g</link>
      <guid>https://dev.to/shethiakamya/sql-for-the-potterhead-left-joins-2p1g</guid>
      <description>&lt;h4&gt;
  
  
  The Fat Lady's portrait is updating her knowledge to make sure only authorized students have access to the Gryffindor dorm. Can we write a query that will inform us of all the students and pets that are allowed in the Gryffindor dormitory?
&lt;/h4&gt;

&lt;p&gt;Let's break down the requirements. We need:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;All the students who are allowed in the Gryffindor dormitory&lt;/li&gt;
&lt;li&gt;Any pets that belong to students from Gryffindor&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Let's start by using a set diagram to understand what data we need: &lt;br&gt;
  &lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--7wCz5PX7--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/ww6wv7vkwo9mzik3227n.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--7wCz5PX7--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/ww6wv7vkwo9mzik3227n.png" alt="Venn diagram for join"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;It looks like we need &lt;em&gt;all the data in the left hand side table, along with any common data&lt;/em&gt;. Thinking about the join's available to us, this sounds a lot like a &lt;code&gt;LEFT JOIN&lt;/code&gt;. Let's write some SQL. &lt;/p&gt;

&lt;p&gt;1.Start by extracting the data we need from the &lt;code&gt;wizards&lt;/code&gt; table. We want to filter the data to the students who are in the Gryffindor house. We can do that with a &lt;code&gt;WHERE&lt;/code&gt; clause.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
postgres=# `SELECT * FROM wizard WHERE house = 'Gryffindor'` 

id |        name        |   house    
----|--------------------|------------
  1 | Neville Longbottom | Gryffindor
  2 | Ronald Weasley     | Gryffindor
  3 | Harry Potter       | Gryffindor
  5 | Seamus Finnigan    | Gryffindor
  6 | Hermione Granger   | Gryffindor

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The query returned every student in the table except Malfoy, who is in Slytherin. &lt;/p&gt;

&lt;p&gt;2.Now, let's write a JOIN to combine this data with that of the &lt;code&gt;pet&lt;/code&gt; table:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;
&lt;span class="nv"&gt;postgres&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="c"&gt;#&lt;/span&gt;
SELECT &lt;span class="k"&gt;*&lt;/span&gt; FROM wizard LEFT JOIN pet ON wizard.id &lt;span class="o"&gt;=&lt;/span&gt; pet.owner_id WHERE house &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'Gryffindor'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 
 &lt;span class="nb"&gt;id&lt;/span&gt; |        name        |   house    | &lt;span class="nb"&gt;id&lt;/span&gt; |    name     | species | owner_id 
&lt;span class="nt"&gt;----&lt;/span&gt;+--------------------+------------+----+-------------+---------+----------
  1 | Neville Longbottom | Gryffindor |  2 | Trevor      | toad    |        1
  2 | Ronald Weasley     | Gryffindor |  1 | Scabbers    | rat     |        2
  3 | Harry Potter       | Gryffindor |  3 | Hedwig      | owl     |        3
  6 | Hermione Granger   | Gryffindor |  4 | Crookshanks | &lt;span class="nb"&gt;cat&lt;/span&gt;     |        6
  5 | Seamus Finnigan    | Gryffindor |    |             |         |         
&lt;span class="o"&gt;(&lt;/span&gt;5 rows&lt;span class="o"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;I see all the student's in Gryffindor, and all the appropriate pets. But, we have too many columns.&lt;/p&gt;

&lt;p&gt;3.Let's filter down to only select the data we need.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;
&lt;span class="nv"&gt;postgres&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="c"&gt;#&lt;/span&gt;
SELECT wizard.name, pet.name FROM wizard LEFT JOIN pet ON wizard.id &lt;span class="o"&gt;=&lt;/span&gt; pet.owner_id WHERE house &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'Gryffindor'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        name        |    name     
&lt;span class="nt"&gt;--------------------&lt;/span&gt;+-------------
 Neville Longbottom | Trevor
 Ronald Weasley     | Scabbers
 Harry Potter       | Hedwig
 Hermione Granger   | Crookshanks
 Seamus Finnigan    | 
&lt;span class="o"&gt;(&lt;/span&gt;5 rows&lt;span class="o"&gt;)&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;This looks good, let's step through this query.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;code&gt;SELECT wizard.name, pet.name&lt;/code&gt;: The SELECT clause specifies the fields we want to see in our results. We're only interested in the wizard's name, and the pet's name&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;FROM wizard&lt;/code&gt; : The first table, or the table on the left for our join &lt;/li&gt;
&lt;li&gt;
&lt;code&gt;LEFT JOIN pet&lt;/code&gt; : the LEFT JOIN clause &lt;/li&gt;
&lt;li&gt;
&lt;code&gt;ON wizard.id = pet.owner_id&lt;/code&gt; :  We specify what we are joining ON. This is generally a field present in both tables, that has a foreign-key-esque relationship &lt;/li&gt;
&lt;li&gt;
&lt;code&gt;WHERE house = 'Gryffindor';&lt;/code&gt; : We filter down our results to only include rows where the wizard's house is in Gryffindor. We don't need to specify &lt;code&gt;wizard.house&lt;/code&gt;, because the &lt;code&gt;pet&lt;/code&gt; table does not include that column. &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Our table has very few rows, so we probably won't run into any performance issues. However, I generally like looking at the query planner and look at the execution plan.  We can use postgres's &lt;code&gt;EXPLAIN ANALYZE&lt;/code&gt; clause for this.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;postgres&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="c"&gt;#&lt;/span&gt;
EXPLAIN ANALYZE SELECT wizard.name, pet.name FROM wizard
LEFT JOIN pet ON wizard.id &lt;span class="o"&gt;=&lt;/span&gt; pet.owner_id WHERE wizard.house &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'Gryffindor'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
                                                  QUERY PLAN                                                   
&lt;span class="nt"&gt;---------------------------------------------------------------------------------------------------------------&lt;/span&gt;
 1. Hash Right Join  &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;cost&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;14.90..29.05 &lt;span class="nv"&gt;rows&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;3 &lt;span class="nv"&gt;width&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;236&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;actual &lt;span class="nb"&gt;time&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;0.045..0.056 &lt;span class="nv"&gt;rows&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;5 &lt;span class="nv"&gt;loops&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;1&lt;span class="o"&gt;)&lt;/span&gt;
 2.   Hash Cond: &lt;span class="o"&gt;(&lt;/span&gt;pet.owner_id &lt;span class="o"&gt;=&lt;/span&gt; wizard.id&lt;span class="o"&gt;)&lt;/span&gt;
 3.   -&amp;gt;  Seq Scan on pet  &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;cost&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;0.00..13.00 &lt;span class="nv"&gt;rows&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;300 &lt;span class="nv"&gt;width&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;122&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;actual &lt;span class="nb"&gt;time&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;0.006..0.008 &lt;span class="nv"&gt;rows&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;6 &lt;span class="nv"&gt;loops&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;1&lt;span class="o"&gt;)&lt;/span&gt;
 4.   -&amp;gt;  Hash  &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;cost&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;14.88..14.88 &lt;span class="nv"&gt;rows&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;2 &lt;span class="nv"&gt;width&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;122&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;actual &lt;span class="nb"&gt;time&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;0.030..0.030 &lt;span class="nv"&gt;rows&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;5 &lt;span class="nv"&gt;loops&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;1&lt;span class="o"&gt;)&lt;/span&gt;
 5.         Buckets: 1024  Batches: 1  Memory Usage: 9kB
 6.         -&amp;gt;  Seq Scan on wizard  &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;cost&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;0.00..14.88 &lt;span class="nv"&gt;rows&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;2 &lt;span class="nv"&gt;width&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;122&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;actual &lt;span class="nb"&gt;time&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;0.020..0.023 &lt;span class="nv"&gt;rows&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;5 &lt;span class="nv"&gt;loops&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;1&lt;span class="o"&gt;)&lt;/span&gt;
 7.              Filter: &lt;span class="o"&gt;((&lt;/span&gt;house&lt;span class="o"&gt;)&lt;/span&gt;::text &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'Gryffindor'&lt;/span&gt;::text&lt;span class="o"&gt;)&lt;/span&gt;
 8.              Rows Removed by Filter: 1
 Planning Time: 0.122 ms
 Execution Time: 0.091 ms
&lt;span class="o"&gt;(&lt;/span&gt;10 rows&lt;span class="o"&gt;)&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;In general, the most indented piece of the &lt;code&gt;EXPLAIN&lt;/code&gt; output is executed first. Towards the bottom of the output, we see that we performed a scan on the wizards table, and filtered out all rows whose owners were not in Gryffindor. This means that we won't be scanning and joining unnecessary data. &lt;/p&gt;

&lt;p&gt;You might see the &lt;code&gt;Hash Right Join&lt;/code&gt; at the start output, and be confused about the discrepency between our SQL syntax, and how the query planner wants to operate.&lt;/p&gt;

&lt;p&gt;The answer can be found in Postgres's documentation. &lt;/p&gt;

&lt;p&gt;&lt;em&gt;hash join: the right relation is first scanned and loaded into a hash table, using its join attributes as hash keys. Next the left relation is scanned and the appropriate values of every row found are used as hash keys to locate the matching rows in the table.&lt;/em&gt; &lt;/p&gt;

&lt;p&gt;This doesn't impact the data we will see, just how we will get it. Lines 1-4 specify the type of join Postgres is running (a hash join), and lists how the query planner will access the data. There is timing information in the output, which we will ignore for now. &lt;/p&gt;




&lt;ol&gt;
&lt;li&gt;View this on &lt;a href="https://github.com/kamyashethia/SQLForThePotterhead/blob/master/Joins.md"&gt;github&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;1. Postgres docs for &lt;a href="https://www.postgresql.org/docs/12/planner-optimizer.html"&gt;planner optimizer&lt;/a&gt; &lt;/li&gt;
&lt;/ol&gt;

</description>
    </item>
    <item>
      <title>SQL for the Potterhead: Joins Introduction</title>
      <dc:creator>kamya</dc:creator>
      <pubDate>Tue, 18 Feb 2020 03:51:16 +0000</pubDate>
      <link>https://dev.to/shethiakamya/sql-for-the-potterhead-joins-introduction-49h1</link>
      <guid>https://dev.to/shethiakamya/sql-for-the-potterhead-joins-introduction-49h1</guid>
      <description>&lt;p&gt;Querying data with SQL can feel magical, and JOINS are one of the things that feel the most magical to me. In the following article, I'm going to explain the magic behind joins. &lt;/p&gt;

&lt;p&gt;We will cover: &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Left Joins &lt;/li&gt;
&lt;li&gt;Inner Joins &lt;/li&gt;
&lt;li&gt;Outer Joins&lt;/li&gt;
&lt;/ol&gt;




&lt;p&gt;Let's start with some setup and talk about the pets of the wizarding world. Students at Hogwarts are allowed to bring with them an owl OR a cat OR a toad. &lt;/p&gt;

&lt;p&gt;Let's consider a table to hold some of the students at Hogwarts. The table will have the following schema:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;code&gt;id&lt;/code&gt; : A unique id used to identify a wizarding student &lt;/li&gt;
&lt;li&gt;
&lt;code&gt;name&lt;/code&gt; : The student's full name &lt;/li&gt;
&lt;li&gt;
&lt;code&gt;house&lt;/code&gt; : Their Hogwarts house &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Here's the table, populated with some students: &lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;id&lt;/th&gt;
&lt;th&gt;name&lt;/th&gt;
&lt;th&gt;house&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;Neville Longbottom&lt;/td&gt;
&lt;td&gt;Gryffindor&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;td&gt;Ronald Weasley&lt;/td&gt;
&lt;td&gt;Gryffindor&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;td&gt;Harry Potter&lt;/td&gt;
&lt;td&gt;Gryffindor&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;4&lt;/td&gt;
&lt;td&gt;Draco Malfoy&lt;/td&gt;
&lt;td&gt;Slytherin&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;5&lt;/td&gt;
&lt;td&gt;Seamus Finnigan&lt;/td&gt;
&lt;td&gt;Gryffindor&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;6&lt;/td&gt;
&lt;td&gt;Hermione Granger&lt;/td&gt;
&lt;td&gt;Gryffindor&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Now, let's create a table to hold information about the student's pets. The table will have the following schema: &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;code&gt;id&lt;/code&gt;: Unique id used to identify the pet &lt;/li&gt;
&lt;li&gt;
&lt;code&gt;name&lt;/code&gt; : The name of the pet &lt;/li&gt;
&lt;li&gt;
&lt;code&gt;species&lt;/code&gt;: The species of the pet. &lt;/li&gt;
&lt;li&gt;
&lt;code&gt;owner_id&lt;/code&gt;: The id of the owner of the pet. In general, &lt;code&gt;pet.owner_id&lt;/code&gt; equals &lt;code&gt;wizard.id&lt;/code&gt;. In database-ey terms, we think of this as a foreign key. (We will not be explicitly specifying a foreign key relation here) &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Here is a table with some pets.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;id&lt;/th&gt;
&lt;th&gt;name&lt;/th&gt;
&lt;th&gt;species&lt;/th&gt;
&lt;th&gt;owner_id&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;td&gt;Trevor&lt;/td&gt;
&lt;td&gt;toad&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;Scabbers&lt;/td&gt;
&lt;td&gt;rat&lt;/td&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;td&gt;Hedwig&lt;/td&gt;
&lt;td&gt;owl&lt;/td&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;4&lt;/td&gt;
&lt;td&gt;Crookshanks&lt;/td&gt;
&lt;td&gt;cat&lt;/td&gt;
&lt;td&gt;6&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;5&lt;/td&gt;
&lt;td&gt;unknown&lt;/td&gt;
&lt;td&gt;owl&lt;/td&gt;
&lt;td&gt;4&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;6&lt;/td&gt;
&lt;td&gt;Norbert&lt;/td&gt;
&lt;td&gt;Dragon&lt;/td&gt;
&lt;td&gt;100&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;7&lt;/td&gt;
&lt;td&gt;Brodwin&lt;/td&gt;
&lt;td&gt;owl&lt;/td&gt;
&lt;td&gt;10&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Let's confirm our understanding of this table is correct, by looking at the pet with id = 3. The pet's name is Hedwig, and it's owner_id is 3. Looking at the &lt;code&gt;wizard&lt;/code&gt; table, Harry Potter has an id of 3. The data indicates that Harry Potter owns Hedwig, which is what we would expect. &lt;/p&gt;

&lt;p&gt;Now that we have table schemas ready, let's learn about joins! &lt;/p&gt;




&lt;ol&gt;
&lt;li&gt;View this on &lt;a href="https://github.com/kamyashethia/SQLForThePotterhead/blob/master/Joins.md"&gt;github&lt;/a&gt; &lt;/li&gt;
&lt;li&gt;
&lt;a href="https://www.wizardingworld.com/features/pottermore-guide-to-wizarding-world-pets"&gt;Pottermore's guide to wizarding world pets&lt;/a&gt; &lt;/li&gt;
&lt;/ol&gt;

</description>
    </item>
    <item>
      <title>Scala for the Potterhead: Pattern Matching </title>
      <dc:creator>kamya</dc:creator>
      <pubDate>Wed, 22 Jan 2020 04:14:56 +0000</pubDate>
      <link>https://dev.to/shethiakamya/scala-for-the-potterhead-pattern-matching-26n9</link>
      <guid>https://dev.to/shethiakamya/scala-for-the-potterhead-pattern-matching-26n9</guid>
      <description>&lt;p&gt;Pattern Matching might seem a bit puzzling. But imagine having to solve a puzzle to prevent Voldemort from getting his hands on the Sorceror's Stone! The Potions Puzzle was one of the challenges that protected the Sorcerers Stone in the very first Harry Potter book. As we all know, this stone could be used to create the elixer of life and grant it's owner immortality. &lt;/p&gt;

&lt;p&gt;There are seven potion bottles in the puzzle. Drinking the wrong bottle could kill you -- but one bottle contained a potion that would allow you to progress. Let's write some scala code to represent our potion bottles.&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;Potion&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;content&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;bottle1&lt;/span&gt; &lt;span class="k"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Potion&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"poison"&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;bottle2&lt;/span&gt; &lt;span class="k"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Potion&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"nettle wine"&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;bottle3&lt;/span&gt; &lt;span class="k"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Potion&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"move ahead"&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;bottle4&lt;/span&gt; &lt;span class="k"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Potion&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"poison"&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;bottle5&lt;/span&gt; &lt;span class="k"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Potion&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"poison"&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;bottle6&lt;/span&gt; &lt;span class="k"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Potion&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"nettle wine"&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;bottle7&lt;/span&gt; &lt;span class="k"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Potion&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"go back"&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 have 7 bottles ready to go. Next, we want to write a function that will tell us what will happen if a wizard chooses a bottle. &lt;br&gt;
We could represent this logic with a &lt;code&gt;if-elseif&lt;/code&gt; block. Here's a function that does that:&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;checkBottle&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;bottle&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt;&lt;span class="kt"&gt;Potion&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="nf"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;bottle&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="py"&gt;content&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="s"&gt;"poison"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
     &lt;span class="s"&gt;"you are dead"&lt;/span&gt;
   &lt;span class="o"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="nf"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;bottle&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="py"&gt;content&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="s"&gt;"move ahead"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
     &lt;span class="s"&gt;"go ahead and find the mirror"&lt;/span&gt;
   &lt;span class="o"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="nf"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;bottle&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="py"&gt;content&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="s"&gt;"nettle wine"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
     &lt;span class="s"&gt;"Well I guess you're drunk now"&lt;/span&gt;
   &lt;span class="o"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="nf"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;bottle&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="py"&gt;content&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="s"&gt;"go back"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
     &lt;span class="s"&gt;"go and get Dumbledore -- quick!"&lt;/span&gt;
   &lt;span class="o"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
     &lt;span class="s"&gt;"this bottle has an unknown substance"&lt;/span&gt;
   &lt;span class="o"&gt;}&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;If we pass in our bottles to this function, it will accurately tell us the effect the potion had on the wizard. &lt;br&gt;
If the wizard drank the potion in bottle 6, our function will accurately inform the wizard of their intoxicated state.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;scala&amp;gt; checkBottle&lt;span class="o"&gt;(&lt;/span&gt;bottle6&lt;span class="o"&gt;)&lt;/span&gt;
res4: String &lt;span class="o"&gt;=&lt;/span&gt; Well I guess you&lt;span class="s1"&gt;'re drunk now
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;We can simplify this function by using pattern matching. If you are familiar with the &lt;code&gt;switch&lt;/code&gt; statement found in many programming languages, the following code block might look familiar.&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;checkBottle&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;bottle&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt;&lt;span class="kt"&gt;Potion&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;bottle&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="py"&gt;content&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="s"&gt;"poison"&lt;/span&gt; &lt;span class="k"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s"&gt;"you are dead"&lt;/span&gt;
    &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="s"&gt;"move ahead"&lt;/span&gt; &lt;span class="k"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s"&gt;"go ahead and find the mirror"&lt;/span&gt;
    &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="s"&gt;"nettle wine"&lt;/span&gt; &lt;span class="k"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s"&gt;"Well I guess you're drunk now"&lt;/span&gt;
    &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="s"&gt;"go back"&lt;/span&gt; &lt;span class="k"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s"&gt;"go and get Dumbledore -- quick!"&lt;/span&gt;
    &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="k"&gt;_&lt;/span&gt; &lt;span class="k"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s"&gt;"this bottle has an unknown substance"&lt;/span&gt;
  &lt;span class="o"&gt;}&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The above code block looks at &lt;code&gt;potion.content&lt;/code&gt; and tries to find a 'pattern' to 'match' it with.&lt;br&gt;
It specifies four strings explicitly to match on.  The last case, &lt;code&gt;case _&lt;/code&gt; is a catch-all. You can think of it as a default block, or a match-anything. &lt;/p&gt;

&lt;p&gt;Let's test this function:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;
scala&amp;gt; checkBottle&lt;span class="o"&gt;(&lt;/span&gt;bottle6&lt;span class="o"&gt;)&lt;/span&gt;
res6: String &lt;span class="o"&gt;=&lt;/span&gt; Well I guess you&lt;span class="s1"&gt;'re drunk now

&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;This seems correct! Now let's see what happens if we tried to pass in a bottle containing Amortentia, the strongest love potion.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;
scala&amp;gt; val bottle8 &lt;span class="o"&gt;=&lt;/span&gt; Potion&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Amortentia"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
bottle8: Potion &lt;span class="o"&gt;=&lt;/span&gt; Potion&lt;span class="o"&gt;(&lt;/span&gt;Amortentia&lt;span class="o"&gt;)&lt;/span&gt;

scala&amp;gt; checkBottle&lt;span class="o"&gt;(&lt;/span&gt;bottle8&lt;span class="o"&gt;)&lt;/span&gt;
res7: String &lt;span class="o"&gt;=&lt;/span&gt; this bottle has an unknown substance

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;As expected, Amortentia matched on our default case statement. &lt;/p&gt;

&lt;p&gt;I hope you learnt a little bit about the basics of Pattern Matching in Scala. We will cover a more complex use case soon!&lt;/p&gt;




&lt;p&gt;Here are some links that you might find interesting: &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;a href="https://github.com/kamyashethia/ScalaForThePotterhead/blob/master/PatternMatching1.md"&gt;View this article on github&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://docs.scala-lang.org/tour/pattern-matching.html"&gt;Scaladocs on Pattern Matching &lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://pottermore.fandom.com/wiki/The_Potion_Puzzle"&gt;The Potion Puzzle on Pottermore&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;

</description>
      <category>scala</category>
    </item>
    <item>
      <title>Scala for the Potterhead: Options</title>
      <dc:creator>kamya</dc:creator>
      <pubDate>Sun, 13 Oct 2019 23:35:29 +0000</pubDate>
      <link>https://dev.to/shethiakamya/scala-for-the-potterhead-options-ibj</link>
      <guid>https://dev.to/shethiakamya/scala-for-the-potterhead-options-ibj</guid>
      <description>&lt;p&gt;Before we dive into Options, let's talk about the spell Prior Incantato. Prior Incantato is an incantation that reveals the last spell cast by a wand. &lt;/p&gt;

&lt;p&gt;Let's represent a wand class that can cast spells.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight scala"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Collections available to us in Scala are immutable by default. If we want to store a value that can change, we explicitly declare it's mutability&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;scala.collection.mutable&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;Wand&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;

  &lt;span class="c1"&gt;// A mutable collection that stores each spell cast by the wand.&lt;/span&gt;
  &lt;span class="k"&gt;val&lt;/span&gt; &lt;span class="nv"&gt;spellsCast&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;mutable.ListBuffer&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;Spell&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="nv"&gt;mutable&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="py"&gt;ListBuffer&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;Spell&lt;/span&gt;&lt;span class="o"&gt;]()&lt;/span&gt;

  &lt;span class="c1"&gt;// A function that allows a wizard to cast a spell&lt;/span&gt;
  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;castSpell&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;spell&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt;&lt;span class="kt"&gt;Spell&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="c1"&gt;// Store the spell&lt;/span&gt;
    &lt;span class="nv"&gt;spellsCast&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="py"&gt;addOne&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;spell&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
    &lt;span class="nf"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Cast spell!"&lt;/span&gt;&lt;span class="o"&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;p&gt;If we needed to cast the Prior Incantato spell on a wand, we might think of implementing a function like this:&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;PriorIncantato&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;wand&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt;&lt;span class="kt"&gt;Wand&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Spell&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
  &lt;span class="nv"&gt;wand&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="py"&gt;spellsCast&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="py"&gt;last&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;But wait, what would happen if we called &lt;code&gt;priorIncantato&lt;/code&gt; on a wand that has never cast a spell.&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;wand&lt;/span&gt; &lt;span class="k"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Wand&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; 
&lt;span class="nf"&gt;priorIncantato&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;wand&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;We would probably expect something to go wrong if we tried this. Sure enough, we see a a runtime error:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;scala&amp;gt; java.util.NoSuchElementException: last of empty ListBuffer
  at scala.collection.mutable.ListBuffer.last&lt;span class="o"&gt;(&lt;/span&gt;ListBuffer.scala:343&lt;span class="o"&gt;)&lt;/span&gt;
  at .priorIncantato&lt;span class="o"&gt;(&lt;/span&gt;&amp;lt;console&amp;gt;:2&lt;span class="o"&gt;)&lt;/span&gt;
  ... 28 elided

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;We tried to access the last element of an empty list, and the Scala compiler was unhappy. Now there might be some temptation to just use null to fix this problem.&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;priorIncantato&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;wand&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt;&lt;span class="kt"&gt;Wand&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Spell&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;if&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt; &lt;span class="nv"&gt;wand&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="py"&gt;spellsCast&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="py"&gt;isEmpty&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; 
    &lt;span class="kc"&gt;null&lt;/span&gt; 
  &lt;span class="k"&gt;else&lt;/span&gt; 
    &lt;span class="nv"&gt;wand&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="py"&gt;spellsCast&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="py"&gt;last&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The code above would work just fine!&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt; scala&amp;gt; val spell:Spell &lt;span class="o"&gt;=&lt;/span&gt; priorIncantato&lt;span class="o"&gt;(&lt;/span&gt;wand&lt;span class="o"&gt;)&lt;/span&gt;
spell: Spell &lt;span class="o"&gt;=&lt;/span&gt; null
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;But now, we have to remember that the &lt;code&gt;val spell&lt;/code&gt; could potentially be null. If we tried to use it, we might encounter a frustrating &lt;code&gt;NullPointerException&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt; scala&amp;gt; spell.getClass
java.lang.NullPointerException
  ... 28 elided

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Scala's &lt;code&gt;Option&lt;/code&gt; provides us with an abstraction for dealing with values that have the potential to be non-existent. Let's refactor the &lt;code&gt;priorIncantato&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="c1"&gt;// We explicitly state that the function optionally returns a spell&lt;/span&gt;
&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;priorIncantato&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;wand&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt;&lt;span class="kt"&gt;Wand&lt;/span&gt;&lt;span class="o"&gt;)&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;Spell&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="c1"&gt;// The lastOption function can be called on the ListBuffer. Instead of throwing a `NoSuchElementException`, the function will return None if the list is empty&lt;/span&gt;
  &lt;span class="nv"&gt;wand&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="py"&gt;spellsCast&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="py"&gt;lastOption&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Let's see what happens when we run this!&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;//call the priorIncantato &lt;span class="k"&gt;function &lt;/span&gt;on a wand that has not cast any spells
scala&amp;gt; val spell &lt;span class="o"&gt;=&lt;/span&gt; priorIncantato&lt;span class="o"&gt;(&lt;/span&gt;wand&lt;span class="o"&gt;)&lt;/span&gt;
spell: Option[Spell] &lt;span class="o"&gt;=&lt;/span&gt; None

//try to find the name of the spell
scala&amp;gt; spell.getClass
res8: Class[_ &amp;lt;: Option[Spell]] &lt;span class="o"&gt;=&lt;/span&gt; class scala.None&lt;span class="err"&gt;$&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Yay -- we don't have a runtime error anymore! We simply get back a &lt;code&gt;class scala.None&lt;/code&gt;, which represents to &lt;code&gt;None&lt;/code&gt; class.&lt;/p&gt;

&lt;p&gt;Let's cast a spell with our wand, and then call the &lt;code&gt;priorIncantato&lt;/code&gt; function:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;scala&amp;gt; wand.castSpell&lt;span class="o"&gt;(&lt;/span&gt;expelliarmus&lt;span class="o"&gt;)&lt;/span&gt;
Cast spell!

scala&amp;gt; val spell &lt;span class="o"&gt;=&lt;/span&gt; priorIncantato&lt;span class="o"&gt;(&lt;/span&gt;wand&lt;span class="o"&gt;)&lt;/span&gt;
spell: Option[Spell] &lt;span class="o"&gt;=&lt;/span&gt; Some&lt;span class="o"&gt;(&lt;/span&gt;Expelliarmus&lt;span class="o"&gt;())&lt;/span&gt;

scala&amp;gt; spell.getClass
res7: Class[_ &amp;lt;: Option[Spell]] &lt;span class="o"&gt;=&lt;/span&gt; class scala.Some

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;This time, the function returns &lt;code&gt;Some(Expelliarmus())&lt;/code&gt;. The value stored in &lt;code&gt;spell&lt;/code&gt; is an Instance of the &lt;code&gt;Some&lt;/code&gt; class in scala.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Option&lt;/code&gt; is an abstract class in Scala, with two possible subclasses, &lt;code&gt;Some&lt;/code&gt; or &lt;code&gt;None&lt;/code&gt;.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;This means that the &lt;code&gt;priorIncantaton(wand:Wand): Option[Spell]&lt;/code&gt; function will return either &lt;code&gt;Some(Spell)&lt;/code&gt; or &lt;code&gt;None&lt;/code&gt;. &lt;/li&gt;
&lt;li&gt;In general, an &lt;code&gt;Option[A]&lt;/code&gt; can either be &lt;code&gt;Some(A)&lt;/code&gt; or &lt;code&gt;None&lt;/code&gt;. &lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;Options provide us with a handy way of representing missing values. Here are some other links that you might find interesting: &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;a href="https://github.com/kamyashethia/ScalaForThePotterhead/blob/master/Options.scala"&gt;Code on github&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.scala-lang.org/api/2.12.8/scala/Option.html"&gt;Scaladocs on the Option abstract class&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://pottermore.fandom.com/wiki/Prior_Incantato"&gt;Pottermore on Prior Incantato&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.infoq.com/presentations/Null-References-The-Billion-Dollar-Mistake-Tony-Hoare/"&gt;Null References - The Billion Dollar Mistake&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;

</description>
      <category>scala</category>
    </item>
    <item>
      <title>Scala for the Potterhead: For Comprehensions</title>
      <dc:creator>kamya</dc:creator>
      <pubDate>Sat, 24 Aug 2019 20:28:01 +0000</pubDate>
      <link>https://dev.to/shethiakamya/scala-for-the-potterhead-for-comprehensions-2lma</link>
      <guid>https://dev.to/shethiakamya/scala-for-the-potterhead-for-comprehensions-2lma</guid>
      <description>&lt;p&gt;We'll cover For comprehensions, generator expressions, guards and yielding in this tutorial. Before we dive in, let's talk about wands.&lt;/p&gt;

&lt;p&gt;Everyone knows that good wands are crafted from the finest wood, and imbued with a token from a magical creature. Here is a wand class represented in Scala:&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;class&lt;/span&gt; &lt;span class="nc"&gt;wand&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;typeOfWood&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;lengthOfWand&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Long&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;magicalMaterial&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;wood&lt;/span&gt; &lt;span class="k"&gt;=&lt;/span&gt; &lt;span class="n"&gt;typeOfWood&lt;/span&gt;
  &lt;span class="k"&gt;val&lt;/span&gt; &lt;span class="nv"&gt;length&lt;/span&gt; &lt;span class="k"&gt;=&lt;/span&gt; &lt;span class="n"&gt;lengthOfWand&lt;/span&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="n"&gt;magicalMaterial&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;toString&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;wood&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s"&gt;" wand of length "&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;length&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s"&gt;" inches with "&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;magic&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;Notice that we override the &lt;code&gt;toString&lt;/code&gt; method. Whenever we call the print function on an object, Scala asks the object's &lt;code&gt;toString&lt;/code&gt; method how it should be displayed to the console. Let's see that in action:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;scala&amp;gt; val wandA &lt;span class="o"&gt;=&lt;/span&gt; new wand&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"english wood"&lt;/span&gt;, 6, &lt;span class="s2"&gt;"wampus cat hair"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
wandA: wand &lt;span class="o"&gt;=&lt;/span&gt; english wood wand of length 6 inches with wampus &lt;span class="nb"&gt;cat &lt;/span&gt;hair
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;That looks good! Now let's create a catalog of more wands in Olivander's shop. We will store them in a &lt;code&gt;List&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="k"&gt;val&lt;/span&gt; &lt;span class="nv"&gt;wandA&lt;/span&gt; &lt;span class="k"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;wand&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"english wood"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"wampus cat hair"&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;wandB&lt;/span&gt; &lt;span class="k"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;wand&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"hazel"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;9&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"veela hair"&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;wandC&lt;/span&gt; &lt;span class="k"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;wand&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Walnut"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;11&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"troll whisker"&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;wandD&lt;/span&gt; &lt;span class="k"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;wand&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Cypress"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;9&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"kelpie hair"&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;wandE&lt;/span&gt; &lt;span class="k"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;wand&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"ash"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;7&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"phoenix feather"&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;wandF&lt;/span&gt; &lt;span class="k"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;wand&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"birch"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"unicorn tail hair"&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;wands&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="n"&gt;wandA&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;wandB&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;wandC&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;wandD&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;wandE&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;wandF&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 want to find a wand for a particularly tall wizard. We know that he will require a wand that is greater than 7 inches in length. Let's look at some code that does that:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight scala"&gt;&lt;code&gt;&lt;span class="c1"&gt;//For comprehension that prints all wands with length &amp;gt; 7 inches&lt;/span&gt;
&lt;span class="k"&gt;val&lt;/span&gt; &lt;span class="nv"&gt;wandsLongerThan7Inches&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;wand&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="k"&gt;=&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
&lt;span class="c1"&gt;//Generator expression&lt;/span&gt;
&lt;span class="n"&gt;wand&lt;/span&gt; &lt;span class="k"&gt;&amp;lt;-&lt;/span&gt; &lt;span class="n"&gt;wands&lt;/span&gt;
&lt;span class="c1"&gt;//Guard&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="nv"&gt;wand&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="py"&gt;length&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;7&lt;/span&gt;
&lt;span class="c1"&gt;//Yielding&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt; &lt;span class="k"&gt;yield&lt;/span&gt; &lt;span class="n"&gt;wand&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Let's talk about each of the pieces: &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Generator expression (&lt;code&gt;wand &amp;lt;- wands&lt;/code&gt;): &lt;br&gt;
This gives us access to individual elements of the &lt;code&gt;wands&lt;/code&gt; list. In python we would write this as &lt;code&gt;for wand in wands&lt;/code&gt;. The &lt;code&gt;&amp;lt;-&lt;/code&gt; operator is called a left arrow operator, and it's Scala's way of iterating through elements in a &lt;code&gt;List&lt;/code&gt; &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Guard (&lt;code&gt;if wand.length &amp;gt; 7&lt;/code&gt;):&lt;br&gt;
A guard is just an &lt;code&gt;if&lt;/code&gt; expression that helps us filter elements in the foor loop. In this case, we only want to keep wands that are greater than 7 inches.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Yielding (&lt;code&gt;yield wand&lt;/code&gt;):&lt;br&gt;&lt;br&gt;
Yielding is a way to return values from the For comprehension. In this case, every time a wand passes the guard statement, we &lt;code&gt;yield&lt;/code&gt; it to the &lt;code&gt;wandsLongerThan7Inches&lt;/code&gt; List. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;For comprehension: &lt;br&gt;
This entire block is called a For comprehension. It's similar to a for loop in other programming languages, but we call it a comprehension because it can have side effects. In this case, the side effect is populating the &lt;code&gt;wandsLongerThan7Inches&lt;/code&gt; List. &lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Let's see what the &lt;code&gt;wandsLongerThan7Inches&lt;/code&gt; List looks like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;
scala&amp;gt; wandsLongerThan7Inches
res30: List[wand] &lt;span class="o"&gt;=&lt;/span&gt; List&lt;span class="o"&gt;(&lt;/span&gt;hazelwood wand of length 9 inches with veela hair, Walnutwood wand of length 11 inches with troll whisker, Cypresswood wand of length 9 inches with kelpie hair&lt;span class="o"&gt;)&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Yay! Our new list only contains wands that are greater than 7 inches in length. But we're not done yet! We think that the wizard will do very well with a wand composed with the hair of a magical creature. Let's add that to our Guard condition:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight scala"&gt;&lt;code&gt;&lt;span class="n"&gt;scala&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;val&lt;/span&gt; &lt;span class="nv"&gt;wandsLongerThan7InchesWithMagicalCreatureHair&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;wand&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="k"&gt;=&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;wand&lt;/span&gt; &lt;span class="k"&gt;&amp;lt;-&lt;/span&gt; &lt;span class="n"&gt;wands&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="nv"&gt;wand&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="py"&gt;length&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;7&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="nv"&gt;wand&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="py"&gt;magic&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="py"&gt;contains&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"hair"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt; &lt;span class="k"&gt;yield&lt;/span&gt; &lt;span class="n"&gt;wand&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;We can add multiple if statements in our guard if we choose to do so. In this case, we also have the option of simplifying the code and grouping the conditions:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight scala"&gt;&lt;code&gt;&lt;span class="n"&gt;scala&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;val&lt;/span&gt; &lt;span class="nv"&gt;wandsLongerThan7InchesWithMagicalCreatureHair&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;wand&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="k"&gt;=&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;wand&lt;/span&gt; &lt;span class="k"&gt;&amp;lt;-&lt;/span&gt; &lt;span class="n"&gt;wands&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="nv"&gt;wand&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="py"&gt;length&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;7&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nv"&gt;wand&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="py"&gt;magic&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="py"&gt;contains&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"hair"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt; &lt;span class="k"&gt;yield&lt;/span&gt; &lt;span class="n"&gt;wand&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;This looks good, let's take a look at the result:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;scala&amp;gt; wandsLongerThan7InchesWithMagicalCreatureHair
res1: List[wand] &lt;span class="o"&gt;=&lt;/span&gt; List&lt;span class="o"&gt;(&lt;/span&gt;hazel wand of length 9 inches with veela hair, Cypress wand of length 9 inches with kelpie hair&lt;span class="o"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Nice! The wizard only has to 'swish and flick' two wands, and wait for one of them to pick him! &lt;/p&gt;

&lt;p&gt;You can find links below to learn more about for comprehensions or magical wands. &lt;/p&gt;

&lt;p&gt;Thanks for reading!&lt;/p&gt;




&lt;ol&gt;
&lt;li&gt;All the example code on &lt;a href="https://github.com/kamyashethia/ScalaForThePotterhead/blob/master/ForComprehension.scala"&gt;github&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://www.pottermore.com/features/the-great-wand-o-graphic"&gt;This&lt;/a&gt; neat wand-o-graphic from pottermore&lt;/li&gt;
&lt;li&gt;Official documentation about &lt;a href="https://docs.scala-lang.org/tour/for-comprehensions.html"&gt;for comprehensions&lt;/a&gt; &lt;/li&gt;
&lt;li&gt;
&lt;a href="https://alvinalexander.com/scala/scala-for-loop-yield-examples-yield-tutorial"&gt;These&lt;/a&gt; examples of for loops from the author of the scala cookbook&lt;/li&gt;
&lt;/ol&gt;

</description>
      <category>scala</category>
    </item>
    <item>
      <title>Scala for the Potterhead: Generics</title>
      <dc:creator>kamya</dc:creator>
      <pubDate>Sun, 18 Aug 2019 02:22:54 +0000</pubDate>
      <link>https://dev.to/shethiakamya/generics-scala-for-the-potterhead-30d8</link>
      <guid>https://dev.to/shethiakamya/generics-scala-for-the-potterhead-30d8</guid>
      <description>&lt;h1&gt;
  
  
  Generics
&lt;/h1&gt;

&lt;p&gt;Before diving into Generics, let's talk about something a bit more benign ... like Horcruxes.&lt;/p&gt;

&lt;p&gt;A Horcrux is a magical object used to store a piece of a person's soul.&lt;br&gt;
The darkest of all magic is needed to create one, so ofcourse Voldemort created 7 of them. &lt;br&gt;
Not all magical objects are nefarious, like the Remembrall, a small orb which turns red when the owner has forgotten something. &lt;/p&gt;

&lt;p&gt;Let's represent some magical objects in Scala:&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;class&lt;/span&gt; &lt;span class="nc"&gt;MagicalObject&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;isDangerous&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="kc"&gt;false&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;//When all 3 hallows are owned by one wizard, the owner is said to have mastery over death&lt;/span&gt;
&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;DeathlyHallows&lt;/span&gt; &lt;span class="k"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;MagicalObject&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="nv"&gt;isDangerous&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="kc"&gt;true&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="c1"&gt;//The smoke contained inside the remembrall turns red if it's owner has forgotten something&lt;/span&gt;
&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Remembrall&lt;/span&gt; &lt;span class="k"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;MagicalObject&lt;/span&gt;
&lt;span class="c1"&gt;//An object containing a piece of a human soul&lt;/span&gt;
&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Horcrux&lt;/span&gt; &lt;span class="k"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;MagicalObject&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;isDestroyed&lt;/span&gt; &lt;span class="k"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;
  &lt;span class="k"&gt;override&lt;/span&gt; &lt;span class="k"&gt;val&lt;/span&gt; &lt;span class="nv"&gt;isDangerous&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="kc"&gt;true&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;//A magical map of Hogwarts&lt;/span&gt;
&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;MaraudersMap&lt;/span&gt; &lt;span class="k"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;MagicalObject&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;An old wizarding story, The tale of the Three Brothers talks about the Deathly Hallows. There is an Unbeatable Wand, a Stone that can bring the dead back to life, and an invisibility cloak! As the legend goes, if a single wizard comes to possess all three of these things, he or she will have complete mastery over death.&lt;br&gt;&lt;br&gt;
Let's represent this in Scala&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;class&lt;/span&gt; &lt;span class="nc"&gt;InvisibilityCloak&lt;/span&gt; &lt;span class="k"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;DeathlyHallows&lt;/span&gt;
&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;ResurrectionStone&lt;/span&gt; &lt;span class="k"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;DeathlyHallows&lt;/span&gt;
&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;ElderWand&lt;/span&gt; &lt;span class="k"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;DeathlyHallows&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;owner&lt;/span&gt; &lt;span class="k"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"Antioch Peverell"&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;You will never hear a Hogwarts professor mention Horcruxes, and any references to them have been banned from the library. Voldemort turned several items of significance into hosts for his soul.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight scala"&gt;&lt;code&gt;&lt;span class="c1"&gt;//Diadem (tiara) of one of the founders of Hogwarts: Rowena Ravenclaw&lt;/span&gt;
&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;RavenclawDiadem&lt;/span&gt; &lt;span class="k"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;Horcrux&lt;/span&gt;

&lt;span class="c1"&gt;//The high school diary of Tom Riddle aka Lord Voldemort&lt;/span&gt;
&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;RiddleDiary&lt;/span&gt; &lt;span class="k"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;Horcrux&lt;/span&gt;

&lt;span class="c1"&gt;//The ring of Voldemort's maternal grandfather&lt;/span&gt;
&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;MarvoloGauntRing&lt;/span&gt; &lt;span class="k"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;Horcrux&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Let's create a list of magical objects that have been owned by the famous Harry Potter. Below is some scala code that does just that:&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;magicalObjectsOwnedByHarry&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;MagicalObject&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="n"&gt;elderWand&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;maraudersMap&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;riddleDiary&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;invisibilityCloak&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Let's zoom in on the type of the variable &lt;code&gt;magicalObjectsOwnedByHarry&lt;/code&gt;. It is a List that accepts elements of the type MagicalObject. Scala implements the List type as a Generic class, which allows us to do this. &lt;/p&gt;

&lt;p&gt;Let's confirm that the scala compiler is happy with our code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;scala&amp;gt; val magicalObjectsOwnedByHarry: List[MagicalObject] &lt;span class="o"&gt;=&lt;/span&gt; List&lt;span class="o"&gt;(&lt;/span&gt;elderWand, maraudersMap, riddleDiary, invisibilityCloak&lt;span class="o"&gt;)&lt;/span&gt;

magicalObjectsOwnedByHarry: List[MagicalObject] &lt;span class="o"&gt;=&lt;/span&gt; List&lt;span class="o"&gt;(&lt;/span&gt;ElderWand@66032b8d, MaraudersMap@1fb66050, RiddleDiary@28068327, InvisibilityCloak@65c2610f&lt;span class="o"&gt;)&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Generics is sometimes known as Parametric Polymorphism. It helps us write code that is more expressive and promotes code reusability without compromising type safety.&lt;br&gt;
When we use Generics, the scala compiler will be able to detect any unexpected type errors at compile time. &lt;/p&gt;

&lt;p&gt;In our example, if we try to insert a nonmagical item, the scala compiler will immediately alert us that there is an error with our code.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;scala&amp;gt; val magicalObjectsOwnedByHarry: List[MagicalObject] &lt;span class="o"&gt;=&lt;/span&gt; List&lt;span class="o"&gt;(&lt;/span&gt;elderWand, maraudersMap, riddleDiary, invisibilityCloak, &lt;span class="s2"&gt;"Non-magical kettle"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
                                                                                                                           ^
       error: &lt;span class="nb"&gt;type &lt;/span&gt;mismatch&lt;span class="p"&gt;;&lt;/span&gt;
        found   : String&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Non-magical kettle"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
        required: MagicalObject
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;You might be tempted to solve for the compile error by removing the type altogether. While this might make our code compile, it might fail later anyway with an error that is harder to debug. &lt;/p&gt;

&lt;p&gt;Consider the following snippet where we try and find all the dangerous objects owned by Harry.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;scala&amp;gt; scala&amp;gt; val magicalObjectsOwnedByHarryWithoutGenericType &lt;span class="o"&gt;=&lt;/span&gt; List&lt;span class="o"&gt;(&lt;/span&gt;elderWand, maraudersMap, riddleDiary, invisibilityCloak, &lt;span class="s2"&gt;"Non-magical kettle"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
magicalObjectsOwnedByHarryWithoutGenericType: List[Object] &lt;span class="o"&gt;=&lt;/span&gt; List&lt;span class="o"&gt;(&lt;/span&gt;ElderWand@772357e9, MaraudersMap@22ccd80f, RiddleDiary@45b7b254, InvisibilityCloak@3b47178c, Non-magical kettle&lt;span class="o"&gt;)&lt;/span&gt;

scala&amp;gt; val dangerousObjects &lt;span class="o"&gt;=&lt;/span&gt; magicalObjectsOwnedByHarryWithoutStrongTypes.filter&lt;span class="o"&gt;(&lt;/span&gt;magicObject &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; magicObject.isDangerous&lt;span class="o"&gt;)&lt;/span&gt;
                                                                                                             ^
       error: value isDangerous is not a member of Object

scala
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Our code fails with an error that is a lot harder to understand, as opposed to the neat type error we saw earlier. &lt;/p&gt;




&lt;ul&gt;
&lt;li&gt;You can check the full code sample &lt;a href="https://github.com/kamyashethia/ScalaForThePotterhead/blob/Generics/GenericsExample.scala"&gt;here&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;You can read more about generic classes in the official &lt;a href="https://docs.scala-lang.org/tour/generic-classes.html"&gt;scaladoc&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://en.wikipedia.org/wiki/Magical_objects_in_Harry_Potter"&gt;This&lt;/a&gt; fantastic wikipedia entry is a fun read about magical objects &lt;/li&gt;
&lt;/ul&gt;

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