{"id":1121,"date":"2018-01-07T20:46:54","date_gmt":"2018-01-07T20:46:54","guid":{"rendered":"http:\/\/blog.cloudxlab.com\/?p=1121"},"modified":"2018-01-08T07:39:40","modified_gmt":"2018-01-08T07:39:40","slug":"scala-tutorial-part-2","status":"publish","type":"post","link":"https:\/\/cloudxlab.com\/blog\/scala-tutorial-part-2\/","title":{"rendered":"A Simple Tutorial on Scala \u2013 Part \u2013 2"},"content":{"rendered":"<p>Welcome back to the Scala tutorial.<\/p>\n<p>This post is the continuation of <a href=\"https:\/\/cloudxlab.com\/blog\/scala-tutorial-part-1\/\">A Simple Tutorial on Scala &#8211; Part &#8211; 1<\/a><\/p>\n<p>In the Part-1 we learned the following topics on Scala<\/p>\n<ul class=\"ili-indent\">\n<li>Scala Features<\/li>\n<li>Variables and Methods<\/li>\n<li>Condition and Loops<\/li>\n<li>Variables and Type Inference<\/li>\n<li>Classes and Objects<\/li>\n<\/ul>\n<p>Keeping up the same pace, we will learn the following topics in the 2nd part of the Scala series.<\/p>\n<ul class=\"ili-indent\">\n<li>Functions Representation<\/li>\n<li>Collections<\/li>\n<li>Sequence and Sets<\/li>\n<li>Tuples and Maps<\/li>\n<li>Higher Order Functions<\/li>\n<li>Build Tool &#8211; SBT<\/li>\n<\/ul>\n<h2>Functions Representation<\/h2>\n<p><span style=\"font-weight: 400;\">We have already discussed functions. We can write a function in different styles in Scala. The first style is the usual way of defining a function.<\/span><\/p>\n<pre class=\"lang:scala decode:true\">scala&gt; def add(x : int, y : int) : Int = {\r\n         return x + y\r\n       }<\/pre>\n<p><span style=\"font-weight: 400;\">Please note that the return type is specified as Int.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">In the second style, please note that the return type is omitted, also there is no &#8220;return&#8221; keyword. The Scala compiler will infer the return type of the function in this case.<\/span><\/p>\n<pre class=\"lang:scala decode:true \">scala&gt; def add(x : int, y : int) = { \/\/return type is inferred\r\n         x + y \/\/\"return\" keyword is optional\r\n       }<\/pre>\n<p><span style=\"font-weight: 400;\">If the function body has just one statement, then the curly braces are optional. In the third style, please note that there are no curly braces.<\/span><\/p>\n<pre class=\"lang:scala decode:true\">def add(x : Int, y : Int) = x + y<\/pre>\n<p><!--more--><\/p>\n<h2>Collections Overview<\/h2>\n<p><span style=\"font-weight: 400;\">Scala collections provide different ways to store data.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">The Scala collections hierarchy represents a structure of collections that can contain data in different ways.<\/span><\/p>\n<p><img class=\"aligncenter size-full wp-image-1122\" src=\"http:\/\/blog.cloudxlab.com\/wp-content\/uploads\/2018\/01\/collections.png\" alt=\"\" width=\"613\" height=\"346\" srcset=\"https:\/\/cloudxlab.com\/blog\/wp-content\/uploads\/2018\/01\/collections.png 613w, https:\/\/cloudxlab.com\/blog\/wp-content\/uploads\/2018\/01\/collections-300x169.png 300w\" sizes=\"(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 984px) 61vw, (max-width: 1362px) 45vw, 600px\" \/><\/p>\n<p><span style=\"font-weight: 400;\">At the top is the &#8220;traversable&#8221;, something that can be traversed. Next in the hierarchy is the iterable. It contains data that can be iterated or looped over. Beyond iterable, we have a Sequence, a set, and a map. The sequence consists of linear Sequence and indexed Sequence.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">In Linear Sequence, we iterate over one by one to find the elements that we want. A linked list is an example of linear Sequence. An indexed Sequence gives the ability to directly access the value inside of a sequence.<\/span><\/p>\n<h2>Collections &#8211; Sequence and Sets<\/h2>\n<p><span style=\"font-weight: 400;\">A sequence is an ordered collection of data. Elements in the sequence may or may not be indexed. Examples of sequence are array, list, and vector<\/span><\/p>\n<h3>Sequence &#8211; Array<\/h3>\n<p>Below are the properties of array<\/p>\n<ul class=\"ili-indent\">\n<li><span style=\"font-weight: 400;\">An array contains elements of the same type. <\/span><\/li>\n<li><span style=\"font-weight: 400;\">Arrays are fixed in size and contain an ordered sequence of data. <\/span><\/li>\n<li><span style=\"font-weight: 400;\">Array values are contiguous in memory, which means that the values are stored in consecutive memory addresses. <\/span><\/li>\n<li><span style=\"font-weight: 400;\">Array elements are indexed by position.<\/span><\/li>\n<\/ul>\n<pre class=\"lang:scala decode:true\">scala&gt; var languages = Array(\"Ruby\", \"SQL\", \"Python\")<\/pre>\n<p><span style=\"font-weight: 400;\">In the above code, variable language is an array which contains 3 elements of string type &#8220;Ruby&#8221;, &#8220;SQL&#8221; and &#8220;Python&#8221;. <\/span><\/p>\n<p><span style=\"font-weight: 400;\">We can access the array elements by their indexes. <\/span><\/p>\n<pre class=\"lang:scala decode:true \">scala&gt; var languages = Array(\"Ruby\", \"SQL\", \"Python\")\r\n\r\nScala&gt; languages(0)\r\nres0: String = Ruby<\/pre>\n<p><span style=\"font-weight: 400;\">Please note that arrays have a zero-based index. Which means to access the first element, languages(0) must be used. <\/span><\/p>\n<p><span style=\"font-weight: 400;\">Also, arrays in Scala are mutable. We can change the values at specific indexes.\u00a0<\/span><\/p>\n<p><span style=\"font-weight: 400;\">To iterate over array elements we can use the &#8216;for&#8217; loop. Let&#8217;s iterate over languages array. <\/span><\/p>\n<pre class=\"lang:scala decode:true\">scala&gt; for(x &lt;- languages) {\r\n       println(x);\r\n       }\r\nRuby\r\nC++\r\nPython<\/pre>\n<p><span style=\"font-weight: 400;\">As you can see, each element of the languages array gets printed on the screen. The left arrow operator is called generator. We&#8217;re iterating over the languages array one by one, assigning the element&#8217;s value to x and printing x.<\/span><\/p>\n<h3><img class=\"aligncenter size-full wp-image-1109\" src=\"http:\/\/blog.cloudxlab.com\/wp-content\/uploads\/2018\/01\/Practice-lab-1.png\" alt=\"Practice scala on cloudxlab\" width=\"810\" height=\"450\" srcset=\"https:\/\/cloudxlab.com\/blog\/wp-content\/uploads\/2018\/01\/Practice-lab-1.png 810w, https:\/\/cloudxlab.com\/blog\/wp-content\/uploads\/2018\/01\/Practice-lab-1-300x167.png 300w, https:\/\/cloudxlab.com\/blog\/wp-content\/uploads\/2018\/01\/Practice-lab-1-768x427.png 768w\" sizes=\"(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 984px) 61vw, (max-width: 1362px) 45vw, 600px\" \/><\/h3>\n<h3>Sequence &#8211; List<\/h3>\n<p><span style=\"font-weight: 400;\">List in Scala represents a linked list having elements such that each element has a value and pointer to the next element. These lists have poor performance as data could be located anywhere in the memory. <\/span><\/p>\n<p><span style=\"font-weight: 400;\">Compared to an array, a list is very flexible, as you do not have to worry about exceeding the size of the list. <\/span><\/p>\n<p><span style=\"font-weight: 400;\">Theoretically, lists are unbounded in size, but practically their size is limited by the amount of memory allocated to the JVM.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Let us do a hands-on and create a list of integers 1, 2 and 3. <\/span><\/p>\n<pre class=\"lang:scala decode:true\">scala&gt; var number_list = List(1, 2, 3)<\/pre>\n<p><span style=\"font-weight: 400;\">To add a new element to the list, type number_list :+ 4<\/span><\/p>\n<pre class=\"lang:scala decode:true \">scala&gt; var number_list = List(1, 2, 3)\r\n\r\nscala&gt; number_list :+ 4\r\nres0: List[Int] = List(1,2,3,4)<\/pre>\n<p><span style=\"font-weight: 400;\">As you can see, element 4 is added to the list. <\/span><\/p>\n<p><span style=\"font-weight: 400;\">Let us try to change the value at index 1.<\/span><\/p>\n<pre class=\"lang:default decode:true \">scala&gt; number_list(1) = 7\r\n&lt;console&gt;:13: error: value update is not a member of List[Int]\r\n         number_list(1) = 7\r\n         ^<\/pre>\n<p>We&#8217;ve got an error. We can not change the value since lists are immutable.<\/p>\n<h3>Sets<\/h3>\n<p><span style=\"font-weight: 400;\">A set in Scala is a bag of data with no duplicates. Also, the ordering is not guaranteed in sets.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Let us create a set with integers 76, 5, 9, 1 and 2. <\/span><\/p>\n<pre class=\"lang:scala decode:true\">scala&gt; var set = Set(76, 5, 9, 1, 2)\r\nset: scala.collection.immutable.Set[Int] = Set(5, 1, 9, 2, 76)<\/pre>\n<p><span style=\"font-weight: 400;\">Let us add 9 to it. <\/span><\/p>\n<pre class=\"lang:scala decode:true\">scala&gt; var set = Set(76, 5, 9, 1, 2)\r\nset: scala.collection.immutable.Set[Int] = Set(5, 1, 9, 2, 76)\r\n\r\nscala&gt; set = set + 9\r\nset: scala.collection.immutable.Set[Int] = Set(5, 1, 9, 2, 76)<\/pre>\n<p><span style=\"font-weight: 400;\">Since duplicates are not allowed in sets, 9 is not added to the set. <\/span><\/p>\n<p><span style=\"font-weight: 400;\">Let&#8217;s add 20. <\/span><\/p>\n<pre class=\"lang:scala decode:true \">scala&gt; var set = Set(76, 5, 9, 1, 2)\r\nset: scala.collection.immutable.Set[Int] = Set(5, 1, 9, 2, 76)\r\n\r\nscala&gt; set = set + 20\r\nset: scala.collection.immutable.Set[Int] = Set(5, 20, 1, 9, 2, 76)<\/pre>\n<p><span style=\"font-weight: 400;\">We can see that 20 is added to the set. Note the order of 20. As discussed, set does not guarantee the\u00a0ordering. set(5) will result in boolean true as 5 is present in the set. set(14) will result in boolean false as 14 is not present in the set.<\/span><\/p>\n<h2>Collections &#8211; Tuples and Maps<\/h2>\n<h3>Tuples<\/h3>\n<p><span style=\"font-weight: 400;\">Unlike an array and list, tuples can hold elements of different data types. Let&#8217;s create a tuple with elements 14, 45.69 and &#8220;Australia&#8221;). We can create it either with<\/span><\/p>\n<pre class=\"lang:scala decode:true\">scala&gt; var t = (14, 45.69, \"Australia\")\r\nt: (Int, Double, String) = (14, 45, 69, Australia)<\/pre>\n<p><span style=\"font-weight: 400;\">or with <\/span><\/p>\n<pre class=\"lang:scala decode:true \">scala&gt; var t = Tuple3(14, 45.69, \"Australia\")\r\nt: (Int, Double, String) = (14, 45, 69, Australia)<\/pre>\n<p><span style=\"font-weight: 400;\">In the second syntax, we are explicitly specifying that tuple will contain 3 elements.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Tuples can be accessed using a 1-based accessor for each value. To access the first element, type t._1. <\/span><\/p>\n<pre class=\"lang:scala decode:true \">scala&gt; t._1\r\nres0: Int = 14<\/pre>\n<p><span style=\"font-weight: 400;\">Likewise, you can access the third element by t._3<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Please note that Tuples are immutable. <\/span><\/p>\n<h3>Maps<\/h3>\n<p><span style=\"font-weight: 400;\">Scala maps are a collection of key\/value pairs. Maps allow indexing values by a specific key for fast access. You can think of a Scala map as a Java Hashmap and Python dictionary.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Let&#8217;s do a hands-on on Scala maps. We will start by creating a Colors map that contains key\/value pair of two colors and their hex code. <\/span><\/p>\n<pre class=\"lang:scala decode:true\">scala&gt; var colors = Map(\"red\" -&gt; \"#FF0000\", \"yellow\" -&gt; \"#FFFF00\")\r\n<\/pre>\n<p><span style=\"font-weight: 400;\">To see the hex code of color yellow, type colors(&#8220;yellow&#8221;). <\/span><\/p>\n<pre class=\"lang:default decode:true\">scala&gt; var colors = Map(\"red\" -&gt; \"#FF0000\", \"yellow\" -&gt; \"#FFFF00\")\r\n\r\nscala&gt; colors(\"yellow\")\r\nres0: String = #FFFF00<\/pre>\n<p><span style=\"font-weight: 400;\">As you can see, the hex code of yellow is #FFFF00. <\/span><\/p>\n<p><span style=\"font-weight: 400;\">We can add new key\/value pairs using += operator. Let&#8217;s add color green and its hex value. <\/span><\/p>\n<pre class=\"lang:scala decode:true\">scala&gt; colors += \"green\" -&gt; \"#000000\"<\/pre>\n<p><span style=\"font-weight: 400;\">Type colors to see the list of updated key\/value pairs. <\/span><\/p>\n<pre class=\"lang:scala decode:true\">scala&gt; colors\r\nres0: scala.collection.immutable.Map(String.String) = Map(red -&gt; #FF0000, yellow -&gt; #FFFF00, green -&gt;\r\n#008000)<\/pre>\n<p><span style=\"font-weight: 400;\">We can also remove key\/value pair using -= operator. Let&#8217;s remove key &#8220;red&#8221;. <\/span><\/p>\n<pre class=\"lang:default decode:true\">scala&gt; colors -= \"red\"\r\n\r\nscala&gt; colors\r\nres0: scala.collection.immutable.Map(String.String) = Map(yellow -&gt; #FFFF00, green -&gt; #008000)<\/pre>\n<p><span style=\"font-weight: 400;\">You can see that key &#8220;red&#8221; is no more in the map.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Scala provides two types of maps &#8211; immutable and mutable. Please note by default, Scala uses the immutable Maps. It means that you can not change the value of a key. If you want to use the mutable maps, import scala.collection.mutable.Map class explicitly<\/span><\/p>\n<h2><b>Higher Order Functions<\/b><\/h2>\n<p><span style=\"font-weight: 400;\">In Scala, a higher-order function is a function which takes another function as an argument. A higher-order function describes &#8220;how&#8221; the work is to be done in a collection.<\/span><\/p>\n<h3>Higher Order Function &#8211; Map<\/h3>\n<p><span style=\"font-weight: 400;\">Let&#8217;s learn the higher order function map. <\/span><\/p>\n<p><img class=\"aligncenter size-large wp-image-1125\" src=\"http:\/\/blog.cloudxlab.com\/wp-content\/uploads\/2018\/01\/Scala-Introduction-1-1024x767.png\" alt=\"\" width=\"840\" height=\"629\" srcset=\"https:\/\/cloudxlab.com\/blog\/wp-content\/uploads\/2018\/01\/Scala-Introduction-1-1024x767.png 1024w, https:\/\/cloudxlab.com\/blog\/wp-content\/uploads\/2018\/01\/Scala-Introduction-1-300x225.png 300w, https:\/\/cloudxlab.com\/blog\/wp-content\/uploads\/2018\/01\/Scala-Introduction-1-768x576.png 768w, https:\/\/cloudxlab.com\/blog\/wp-content\/uploads\/2018\/01\/Scala-Introduction-1-1200x899.png 1200w, https:\/\/cloudxlab.com\/blog\/wp-content\/uploads\/2018\/01\/Scala-Introduction-1.png 1365w\" sizes=\"(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 1362px) 62vw, 840px\" \/><\/p>\n<p><span style=\"font-weight: 400;\">The map applies the function to each value in the collection and returns a new collection. Say we have a list of integers 3, 7, 13 and 16 and we want to add one to each value in the list. Using higher order map function, we can map over the list and add one to each value. As displayed on the image above, we have a new list with values 4, 8, 14 and 17.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Let&#8217;s do a hands-on. Define a list of integers 1, 2 and 3.\u00a0<\/span><\/p>\n<pre class=\"lang:scala decode:true\">scala&gt; var list = List(1,2,3)<\/pre>\n<p><span style=\"font-weight: 400;\">Let&#8217;s add 1 to each element using the map function.\u00a0 <\/span><\/p>\n<pre class=\"lang:scala decode:true \">scala&gt; list.map(x =&gt; x + 1)\r\nres0: List[Int] = List(2, 3, 4)<\/pre>\n<p><span style=\"font-weight: 400;\">As you can see, we have a new list with integers 2, 3 and 4<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Here map function adds one to every value in the list. Each value in the list has the name &#8220;x&#8221;. There is another syntax to define the map, where instead of giving a name to every value being used in the function, we use a placeholder underscore to represent the value.<\/span><\/p>\n<pre class=\"lang:default decode:true\">scala&gt; list.map(_+ 1)<\/pre>\n<h3>Higher Order Function &#8211; flatMap<\/h3>\n<p><span style=\"font-weight: 400;\">flatMap takes a function as an argument and this function must return a collection. The collection could be empty.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">The flatMap is called on a collection. The flatMap applies the function passed to it as an argument on each value in the collection just like map.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">The returned collections from each call of function are then merged together to form a new collection.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Let&#8217;s understand it with an example<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Let&#8217;s define a list of languages. <\/span><\/p>\n<pre class=\"lang:scala decode:true \">var list = List(\"Python\", \"Go\")<\/pre>\n<p><span style=\"font-weight: 400;\">Here, we have a list of strings. A string is nothing but a sequence of characters. <\/span><\/p>\n<p><span style=\"font-weight: 400;\">Now, we define a flatMap.<\/span><\/p>\n<pre class=\"lang:scala decode:true \">scala&gt; list.flatMap(lang =&gt; lang + \"#\")\r\nres0: List(Char) = List(P, y, t, h, o, n, #, G, o, #)<\/pre>\n<p><span style=\"font-weight: 400;\">flatMap appends &#8220;#&#8221; to every string in the list and then flattens down the string to characters. As you can see, # is appended to python and go and then &#8220;python#&#8221; and &#8220;go#&#8221; flatten down into the sequence of characters. <\/span><\/p>\n<p><span style=\"font-weight: 400;\">We can also define the same function using flatMap(_+ &#8220;#&#8221;) where the underscore is a placeholder for each value in the list.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">So, the size of output collection could be larger than the size of input collection.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">While in the case of map the input and output collection were of the same size.<\/span><\/p>\n<h3>Higher Order Function &#8211; Filter<\/h3>\n<p><span style=\"font-weight: 400;\">Let&#8217;s learn the higher order function Filter. The filter applies a function to each value in the collection and returns a new collection with values that satisfy a condition specified in the function. Let&#8217;s understand it with an example.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">We have a list of languages Scala, R, Python, Go and SQL. To filter out languages which contain capital S, we use the following command<\/span><\/p>\n<pre class=\"lang:scala decode:true\">scala&gt; var list = List(\"Scala\", \"R\", \"Python\", \"Go\", \"SQL\")\r\n\r\nscala&gt; list.filter(lang =&gt; lang.contains(\"S\"))\r\nres0: List(String) = List(Scala, SQL)<\/pre>\n<p><span style=\"font-weight: 400;\">As you can see we have a new list now with only Scala and SQL as values. Each value in the list has name &#8220;lang&#8221;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Each of the previous higher order functions returned a new collection after applying the transformation. But at times we do not want the functions to return a new collection. It is a waste of memory resources on JVM if we do not want a return value. Higher order function &#8216;foreach&#8217; allows us to apply a function to each value of collection without returning a new collection. <\/span><\/p>\n<p><span style=\"font-weight: 400;\">Let&#8217;s say we have a list of values 1 and 2 and we just want to print each value in the list. We can use foreach function for this as we are not interested in the return value. To use foreach higher order function, type<\/span><\/p>\n<pre class=\"lang:scala decode:true \">scala&gt; var list = List(1,2)\r\nlist: List[Int] = List(1,2)\r\n\r\nscala&gt; var list.foreach(println)\r\n1\r\n2<\/pre>\n<p><span style=\"font-weight: 400;\">As you can see value 1 and 2 are printed on the screen.<\/span><\/p>\n<h3>Higher Order Function &#8211; Reduce<\/h3>\n<p><span style=\"font-weight: 400;\">Reduce is a very important concept in the MapReduce world. Let&#8217;s say we have a list and we want to reduce it to add values together. Let&#8217;s understand it with an example. <\/span><\/p>\n<p><span style=\"font-weight: 400;\">In our example, reduce adds the first two elements 3 and 7 which results in 10. Then it adds 10 to the next element which is 13 resulting in 23. Then it adds 23 to the next element in the list which is 16. <\/span><\/p>\n<pre class=\"lang:scala decode:true \">scala&gt; var list = List(3, 7, 13, 16)\r\n\r\nscala&gt; list.reduce((x, y) =&gt; x + y)\r\nres0: Int = 39\r\n<\/pre>\n<p><span style=\"font-weight: 400;\">As you can see, that final result of the reduce function is 39<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Here the reduce function takes two values named x and y and adds them.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">We can also define the reduce function using list.reduce(<\/span><i><span style=\"font-weight: 400;\"> + <\/span><\/i><span style=\"font-weight: 400;\">). Here the two underscores are placeholders for the two values.<\/span><\/p>\n<h2>Build Tool &#8211; SBT<\/h2>\n<p><span style=\"font-weight: 400;\">So far, we have just compiled and run one .scala file. If we are working on a big project containing hundreds of source files, it becomes really tedious to compile these files manually. We&#8217;ll then need a build tool to manage the compilation of all these files. SBT is a build tool for Scala and Java projects, similar to Java&#8217;s Maven or ant. <\/span><\/p>\n<p><span style=\"font-weight: 400;\">SBT is already installed on <a href=\"https:\/\/cloudxlab.com\/lab\/\">CloudxLab<\/a> so you can compile your Scala project directly on it.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Let&#8217;s understand how to use SBT to build a Scala project. We&#8217;ve provided a sample code on the <a href=\"https:\/\/github.com\/cloudxlab\/bigdata\">CloudxLab GitHub repository<\/a>. Clone the CloudxLab GitHub repository in your home folder in CloudxLab.<\/span><\/p>\n<p>Follow the instructions given in the video below.<\/p>\n<p>&nbsp;<\/p>\n<div style=\"max-width: 1778px;\">\n<div style=\"left: 0; width: 100%; height: 0; position: relative; padding-bottom: 56.25%;\"><iframe title=\"Scala - Build Tool - SBT\" src=\"https:\/\/www.youtube.com\/embed\/-I2xbN1egsM?rel=0\" style=\"border: 0; top: 0; left: 0; width: 100%; height: 100%; position: absolute;\" allowfullscreen scrolling=\"no\" allow=\"encrypted-media; accelerometer; clipboard-write; gyroscope; picture-in-picture\"><\/iframe><\/div>\n<\/div>\n<p><script type=\"text\/javascript\">window.addEventListener(\"message\",function(e){\n                window.parent.postMessage(e.data,\"*\");\n            },false);<\/script><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Welcome back to the Scala tutorial. This post is the continuation of A Simple Tutorial on Scala &#8211; Part &#8211; 1 In the Part-1 we learned the following topics on Scala Scala Features Variables and Methods Condition and Loops Variables and Type Inference Classes and Objects Keeping up the same pace, we will learn the &hellip; <a href=\"https:\/\/cloudxlab.com\/blog\/scala-tutorial-part-2\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;A Simple Tutorial on Scala \u2013 Part \u2013 2&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":1127,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[14],"tags":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v16.2 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>A Simple Tutorial on Scala \u2013 Part \u2013 2 | CloudxLab Blog<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/cloudxlab.com\/blog\/scala-tutorial-part-2\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"A Simple Tutorial on Scala \u2013 Part \u2013 2 | CloudxLab Blog\" \/>\n<meta property=\"og:description\" content=\"Welcome back to the Scala tutorial. This post is the continuation of A Simple Tutorial on Scala &#8211; Part &#8211; 1 In the Part-1 we learned the following topics on Scala Scala Features Variables and Methods Condition and Loops Variables and Type Inference Classes and Objects Keeping up the same pace, we will learn the &hellip; Continue reading &quot;A Simple Tutorial on Scala \u2013 Part \u2013 2&quot;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/cloudxlab.com\/blog\/scala-tutorial-part-2\/\" \/>\n<meta property=\"og:site_name\" content=\"CloudxLab Blog\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/cloudxlab\" \/>\n<meta property=\"article:published_time\" content=\"2018-01-07T20:46:54+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2018-01-08T07:39:40+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/cloudxlab.com\/blog\/wp-content\/uploads\/2018\/01\/learn-scala-4.png\" \/>\n\t<meta property=\"og:image:width\" content=\"1024\" \/>\n\t<meta property=\"og:image:height\" content=\"512\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@CloudxLab\" \/>\n<meta name=\"twitter:site\" content=\"@CloudxLab\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\">\n\t<meta name=\"twitter:data1\" content=\"11 minutes\">\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebSite\",\"@id\":\"https:\/\/cloudxlab.com\/blog\/#website\",\"url\":\"https:\/\/cloudxlab.com\/blog\/\",\"name\":\"CloudxLab Blog\",\"description\":\"Learn AI, Machine Learning, Deep Learning, Devops &amp; Big Data\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":\"https:\/\/cloudxlab.com\/blog\/?s={search_term_string}\",\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"ImageObject\",\"@id\":\"https:\/\/cloudxlab.com\/blog\/scala-tutorial-part-2\/#primaryimage\",\"inLanguage\":\"en-US\",\"url\":\"https:\/\/cloudxlab.com\/blog\/wp-content\/uploads\/2018\/01\/learn-scala-4.png\",\"contentUrl\":\"https:\/\/cloudxlab.com\/blog\/wp-content\/uploads\/2018\/01\/learn-scala-4.png\",\"width\":1024,\"height\":512},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/cloudxlab.com\/blog\/scala-tutorial-part-2\/#webpage\",\"url\":\"https:\/\/cloudxlab.com\/blog\/scala-tutorial-part-2\/\",\"name\":\"A Simple Tutorial on Scala \\u2013 Part \\u2013 2 | CloudxLab Blog\",\"isPartOf\":{\"@id\":\"https:\/\/cloudxlab.com\/blog\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/cloudxlab.com\/blog\/scala-tutorial-part-2\/#primaryimage\"},\"datePublished\":\"2018-01-07T20:46:54+00:00\",\"dateModified\":\"2018-01-08T07:39:40+00:00\",\"author\":{\"@id\":\"https:\/\/cloudxlab.com\/blog\/#\/schema\/person\/0efa3c54df68406de820ea466f002d3c\"},\"breadcrumb\":{\"@id\":\"https:\/\/cloudxlab.com\/blog\/scala-tutorial-part-2\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/cloudxlab.com\/blog\/scala-tutorial-part-2\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/cloudxlab.com\/blog\/scala-tutorial-part-2\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"item\":{\"@type\":\"WebPage\",\"@id\":\"https:\/\/cloudxlab.com\/blog\/\",\"url\":\"https:\/\/cloudxlab.com\/blog\/\",\"name\":\"Home\"}},{\"@type\":\"ListItem\",\"position\":2,\"item\":{\"@id\":\"https:\/\/cloudxlab.com\/blog\/scala-tutorial-part-2\/#webpage\"}}]},{\"@type\":\"Person\",\"@id\":\"https:\/\/cloudxlab.com\/blog\/#\/schema\/person\/0efa3c54df68406de820ea466f002d3c\",\"name\":\"Abhinav Singh\",\"image\":{\"@type\":\"ImageObject\",\"@id\":\"https:\/\/cloudxlab.com\/blog\/#personlogo\",\"inLanguage\":\"en-US\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/fc74fe31169bf872f6ab11bbab621d53?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/fc74fe31169bf872f6ab11bbab621d53?s=96&d=mm&r=g\",\"caption\":\"Abhinav Singh\"},\"sameAs\":[\"https:\/\/cloudxlab.com\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","_links":{"self":[{"href":"https:\/\/cloudxlab.com\/blog\/wp-json\/wp\/v2\/posts\/1121"}],"collection":[{"href":"https:\/\/cloudxlab.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cloudxlab.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cloudxlab.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/cloudxlab.com\/blog\/wp-json\/wp\/v2\/comments?post=1121"}],"version-history":[{"count":8,"href":"https:\/\/cloudxlab.com\/blog\/wp-json\/wp\/v2\/posts\/1121\/revisions"}],"predecessor-version":[{"id":1132,"href":"https:\/\/cloudxlab.com\/blog\/wp-json\/wp\/v2\/posts\/1121\/revisions\/1132"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/cloudxlab.com\/blog\/wp-json\/wp\/v2\/media\/1127"}],"wp:attachment":[{"href":"https:\/\/cloudxlab.com\/blog\/wp-json\/wp\/v2\/media?parent=1121"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cloudxlab.com\/blog\/wp-json\/wp\/v2\/categories?post=1121"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cloudxlab.com\/blog\/wp-json\/wp\/v2\/tags?post=1121"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}