{"id":689,"date":"2011-11-16T13:11:00","date_gmt":"2011-11-16T13:11:00","guid":{"rendered":"http:\/\/www.javacodegeeks.com\/2012\/10\/scala-tutorial-code-blocks-coding-style-closures-scala-documentation-project.html"},"modified":"2012-10-21T20:39:47","modified_gmt":"2012-10-21T20:39:47","slug":"scala-tutorial-code-blocks-coding-style","status":"publish","type":"post","link":"https:\/\/www.javacodegeeks.com\/2011\/11\/scala-tutorial-code-blocks-coding-style.html","title":{"rendered":"Scala Tutorial &#8211; code blocks, coding style, closures, scala documentation project"},"content":{"rendered":"<div dir=\"ltr\" style=\"text-align: left\">\n<h2>Preface<\/h2>\n<p>This is part 12 of tutorials for first-time programmers getting into Scala. Other posts are on this blog, and you can get links to those and other resources on <a href=\"http:\/\/icl-f11.utcompling.com\/links\">the links page of the Computational Linguistics course<\/a> I\u2019m creating these for.&nbsp;Additionally you can find this and other tutorial series on the JCG Java <a href=\"http:\/\/www.javacodegeeks.com\/p\/java-tutorials.html\">Tutorials page<\/a>.<\/p>\n<p>This post isn\u2019t so much a tutorial as a comment on coding style with a few pointers on how code blocks in Scala work. It was instigated by patterns I was noting in my students\u2019 code; namely, that they were packing <strong>everything<\/strong> into one-liners with map after map with map after map, etc. These  map-over-mapValues-over-map sequences of statements can be almost incomprensible, both for some other person reading the code, and even for the person writing the code. I do admit to a fair amount of guilt in using such sequences of operations in class lectures and even in some of these tutorials. It works well in the REPL and when you have lots of text to explain what is going on around the piece of code in question, but it seems to have given a bad model for writing actual code. Oops!<\/p>\n<p>So taking a step back, it is important to break operation sequences up a bit, but it isn\u2019t always obvious to beginners how one can do so. Also, some students indicated that they had gotten the impression that one should try to pack everything onto one line if possible, and that breaking things up was somehow less advanced or less Scala-like. This is hardly the case. In fact much to the contrary: it is crucial to use strategies that allow readers of your code to see the logic behind your statements. This isn\u2019t just for others \u2014 you are likely to be a reader of your own code, often months after you originally wrote it, and you want to be kind to your future self.<\/p>\n<h2>A simple example<\/h2>\n<p>I\u2019m giving an example here. of what you can do to give your code more breathing space. It\u2019s not a very meaningful example, but it serves the purpose without being very complex. We begin by creating a list of all the letters in the alphabet.<\/p>\n<pre class=\"brush: scala;\">scala&gt; val letters = \"abcdefghijklmnopqrstuvwxyz\".split(\"\").toList.tail\r\nletters: List[java.lang.String] = List(a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z)\r\n\r\n<\/pre>\n<p>Okay, now here\u2019s our (pointless) task: we want to create a map from every letter (from \u2018a\u2019 to \u2018x\u2019) to a list containing that letter and the two letters that follow it in reverse alphabetical order. (Did I mention this was a pointless task in and of itself?) Here\u2019s a one-liner that can do it.<\/p>\n<pre class=\"brush: scala;\">scala&gt; letters.zip((1 to 26).toList.sliding(3).toList).toMap.mapValues(_.map(x =&gt; letters(x-1)).sorted.reverse)\r\nres0: scala.collection.immutable.Map[java.lang.String,List[java.lang.String]] = Map(e -&gt; List(g, f, e), s -&gt; List(u, t, s), x -&gt; List(z, y, x), n -&gt; List(p, o, n), j -&gt; List(l, k, j), t -&gt; List(v, u, t), u -&gt; List(w, v, u), f -&gt; List(h, g, f), a -&gt; List(c, b, a), m -&gt; List(o, n, m), i -&gt; List(k, j, i), v -&gt; List(x, w, v), q -&gt; List(s, r, q), b -&gt; List(d, c, b), g -&gt; List(i, h, g), l -&gt; List(n, m, l), p -&gt; List(r, q, p), c -&gt; List(e, d, c), h -&gt; List(j, i, h), r -&gt; List(t, s, r), w -&gt; List(y, x, w), k -&gt; List(m, l, k), o -&gt; List(q, p, o), d -&gt; List(f, e, d))\r\n<\/pre>\n<p>That did it, but that one-liner isn\u2019t clear at all, so we should break things up a bit. Also, what is \u201c_\u201d and what is \u201cx\u201d? (By which I mean, what are they in terms of the <strong>logic<\/strong> of the program? We know they are ways of referring to the elements being mapped over, but they don\u2019t help the human reading the code understand what is going on.)<\/p>\n<p>Let\u2019s start by creating the sliding list of number ranges.<\/p>\n<pre class=\"brush: scala;\">scala&gt; val ranges = (1 to 26).toList.sliding(3).toList\r\nranges: List[List[Int]] = List(List(1, 2, 3), List(2, 3, 4), List(3, 4, 5), List(4, 5, 6), List(5, 6, 7), List(6, 7, 8), List(7, 8, 9), List(8, 9, 10), List(9, 10, 11), List(10, 11, 12), List(11, 12, 13), List(12, 13, 14), List(13, 14, 15), List(14, 15, 16), List(15, 16, 17), List(16, 17, 18), List(17, 18, 19), List(18, 19, 20), List(19, 20, 21), List(20, 21, 22), List(21, 22, 23), List(22, 23, 24), List(23, 24, 25), List(24, 25, 26))\r\n\r\n<\/pre>\n<p>It\u2019s quite clear what that is now. (The <strong>sliding<\/strong> function is a beautiful thing, especially for natural language processing problems.)<\/p>\n<p>Next, we zip the letters with the ranges and create a <strong>Map<\/strong> from the pairs using <strong>toMap<\/strong>. This produces a Map from letters to lists of three numbers. Note that the lengths of the two lists are different: <em>letters<\/em> has 26 elements and <em>ranges<\/em> has 24, which means that the last two elements of <em>letters<\/em> (\u2018y\u2019 and \u2018z\u2019) get dropped in the zipped list.<div style=\"display:inline-block; margin: 15px 0;\"> <div id=\"adngin-JavaCodeGeeks_incontent_video-0\" style=\"display:inline-block;\"><\/div> <\/div><\/p>\n<pre class=\"brush: scala;\">scala&gt; val letter2range = letters.zip(ranges).toMap\r\nletter2range: scala.collection.immutable.Map[java.lang.String,List[Int]] = Map(e -&gt; List(5, 6, 7), s -&gt; List(19, 20, 21), x -&gt; List(24, 25, 26), n -&gt; List(14, 15, 16), j -&gt; List(10, 11, 12), t -&gt; List(20, 21, 22), u -&gt; List(21, 22, 23), f -&gt; List(6, 7, 8), a -&gt; List(1, 2, 3), m -&gt; List(13, 14, 15), i -&gt; List(9, 10, 11), v -&gt; List(22, 23, 24), q -&gt; List(17, 18, 19), b -&gt; List(2, 3, 4), g -&gt; List(7, 8, 9), l -&gt; List(12, 13, 14), p -&gt; List(16, 17, 18), c -&gt; List(3, 4, 5), h -&gt; List(8, 9, 10), r -&gt; List(18, 19, 20), w -&gt; List(23, 24, 25), k -&gt; List(11, 12, 13), o -&gt; List(15, 16, 17), d -&gt; List(4, 5, 6))\r\n\r\n<\/pre>\n<p>Note that we could have broken this into two steps, first creating the zipped list and then calling <strong>toMap<\/strong> on it. However, it is perfectly clear what the intent is when one zips two lists (creating a list of pairs) and then uses <strong>toMap<\/strong> on it immediately, so this is certainly a case where it makes sense to put multiple operations on a single line.<\/p>\n<p>At this point we could of course process the <em>letter2range<\/em> <strong>Map<\/strong> using a one-liner.<\/p>\n<pre class=\"brush: scala;\">scala&gt; letter2range.mapValues(_.map(x =&gt; letters(x-1)).sorted.reverse)\r\nres1: scala.collection.immutable.Map[java.lang.String,List[java.lang.String]] = Map(e -&gt; List(g, f, e), s -&gt; List(u, t, s), x -&gt; List(z, y, x), n -&gt; List(p, o, n), j -&gt; List(l, k, j), t -&gt; List(v, u, t), u -&gt; List(w, v, u), f -&gt; List(h, g, f), a -&gt; List(c, b, a), m -&gt; List(o, n, m), i -&gt; List(k, j, i), v -&gt; List(x, w, v), q -&gt; List(s, r, q), b -&gt; List(d, c, b), g -&gt; List(i, h, g), l -&gt; List(n, m, l), p -&gt; List(r, q, p), c -&gt; List(e, d, c), h -&gt; List(j, i, h), r -&gt; List(t, s, r), w -&gt; List(y, x, w), k -&gt; List(m, l, k), o -&gt; List(q, p, o), d -&gt; List(f, e, d))\r\n\r\n<\/pre>\n<p>This is better than what we started with because we at least know what <em>letter2range<\/em> is, but it still isn\u2019t clear what is going on after that. To make this more comprehensible, we can break it up over multiple lines and give more descriptive names to the variables. The following produces the same result as above.<\/p>\n<pre class=\"brush: scala;\">letter2range.mapValues (\r\n  range =&gt; {\r\n    val alphavalues = range.map (number =&gt; letters(number-1))\r\n    alphavalues.sorted.reverse\r\n  }\r\n)\r\n\r\n<\/pre>\n<p>Notice that:<\/p>\n<ul>\n<li>I called it <em>range<\/em> rather than <em>_<\/em> which is a better indicator of what <strong>mapValues<\/strong> is working with.<\/li>\n<li>After the =&gt; I use an open left bracket {<\/li>\n<li>The next lines are a block of code that I can use like any block of code, which means I can create variables and break things down into smaller, more understandable steps. For example the line creating <em>alphavalues<\/em> makes it clear that we are taking a range and mapping it to the corresponding indices in the <em>letters<\/em> list (e.g., the range 2, 3, 4 becomes \u2018b\u2019,&#8217;c\u2019,&#8217;d\u2019). For such a list, we then sort and reverse it (okay, so it started out sorted, but you can imagine plenty of times you need to do such sorting).<\/li>\n<li>The last line of that block is what the result of the overall <strong>mapValue<\/strong> for that element (here, indicated by the variable <em>range<\/em>) is.<\/li>\n<\/ul>\n<p>Basically, we get a lot more breathing room, and this becomes even more essential as you dig deeper or do more complex operations during a map-within-a-map operation. Having said that, you should ask yourself whether you should just create and use a function that has a clear semantics and does the job for you. For example, here\u2019s an alternative to the above strategy that is perhaps clearer.<\/p>\n<pre class=\"brush: scala;\">def lookupSortAndReverse (range: List[Int], alpha: List[String]) =\r\n  range.map(number =&gt; alpha(number-1).sorted.reverse)\r\n\r\n<\/pre>\n<p>We\u2019ve defined a function that takes a range and a list of letters (called alpha in the function) and produces the sorted and reversed list of letters corresponding to the numbers in the range. In other words, it is what the anonymous function defined after <em>range<\/em> in the previous code block did. We can thus easily use it at the top-level <strong>mapValue<\/strong> operation with completely clear intent and comprehensibility.<\/p>\n<pre class=\"brush: scala;\">letter2range.mapValues(range =&gt; lookupSortAndReverse(range, letters))\r\n<\/pre>\n<p>Of course, you should especially consider creating such functions if you use the same operation in multiple places.<\/p>\n<h2>Closures<\/h2>\n<p>One further final note. Note that I passed the <em>letters<\/em> list into the <strong>lookupSortAndReverse<\/strong> function such that its value was bound to the function internal variable <em>alpha<\/em>. You may wonder whether I needed to include that, or whether it is possible to directly access the letters list in the function. In fact you can: <span style=\"text-decoration: underline\">provided that <em>letters<\/em> has already been defined<\/span>, we can do the following.<\/p>\n<pre class=\"brush: scala;\">def lookupSortAndReverseCapture (range: List[Int]) =\r\n  range.map(number =&gt; letters(number-1).sorted.reverse)\r\n\r\nletter2range.mapValues(range =&gt; lookupSortAndReverseCapture(range))\r\n<\/pre>\n<p>This is called a <strong>closure<\/strong>, meaning that the function has incorporated free variables (here, <em>letters<\/em>) that come from <em>outside<\/em> its own scope. I generally don\u2019t use this strategy with named functions like this, but there are many natural situations for using closures. In fact you do it all the time when you are creating anonymous functions as arguments to functions like <strong>map<\/strong> and <strong>mapValue<\/strong> and their cousins. As a reminder, here was the map-within-a-mapValue anonymous function we defined before.<\/p>\n<pre class=\"brush: scala;\">letter2range.mapValues (\r\n  range =&gt; {\r\n    val alphavalues = range.map (number =&gt; letters(number-1))\r\n    alphavalues.sorted.reverse\r\n  }\r\n)\r\n\r\n<\/pre>\n<p>The <em>letters<\/em> variable has been \u201cclosed over\u201d in the anonymous function<em> range =&gt; { \u2026 }<\/em>, which is not very different from what we did with the closure-style <strong>lookupSortAndReverse<\/strong> function.<\/p>\n<h2>All the code in one spot<\/h2>\n<p>Since there are some dependencies between the different steps in this tutorial that could get things mixed up, here\u2019s all the code in one spot such that you can run it easily.<\/p>\n<pre class=\"brush: scala;\">\/\/ Get a list of the letters\r\nval letters = \"abcdefghijklmnopqrstuvwxyz\".split(\"\").toList.tail\r\n\r\n\/\/ Now create a list that maps each letter to a list containing itself\r\n\/\/ and the two letters after it, in reverse alphabetical\r\n\/\/ order. (Bizarre, but hey, it's a simple example. BTW, we lose y and\r\n\/\/ z in the process.)\r\n\r\nletters.zip((1 to 26).toList.sliding(3).toList).toMap.mapValues(_.map(x =&gt; letters(x-1)).sorted.reverse)\r\n\r\n\/\/ Pretty unintelligible. Let's break things up a bit\r\n\r\nval ranges = (1 to 26).toList.sliding(3).toList\r\nval letter2range = letters.zip(ranges).toMap\r\nletter2range.mapValues(_.map(x =&gt; letters(x-1)).sorted.reverse)\r\n\r\n\/\/ Okay, that's better. But it is easier to interpret the latter if we break things up a bit\r\n\r\nletter2range.mapValues (\r\n  range =&gt; {\r\n    val alphavalues = range.map (number =&gt; letters(number-1))\r\n    alphavalues.sorted.reverse\r\n  }\r\n)\r\n\r\n\/\/ We can also do the one-liner coherently if we have a helper function.\r\n\r\ndef lookupSortAndReverse (range: List[Int], alpha: List[String]) =\r\n  range.map(number =&gt; alpha(number-1).sorted.reverse)\r\n\r\nletter2range.mapValues(range =&gt; lookupSortAndReverse(range, letters))\r\n\r\n\/\/ Note that we can \"capture\" the letters value, though this makes the\r\n\/\/ requires letters to be defined before lookupSortAndReverse in the\r\n\/\/ program.\r\n\r\ndef lookupSortAndReverseCapture (range: List[Int]) =\r\n  range.map(number =&gt; letters(number-1).sorted.reverse)\r\n\r\nletter2range.mapValues(range =&gt; lookupSortAndReverseCapture(range))\r\n\r\n<\/pre>\n<h2>Wrapup<\/h2>\n<p>Hopefully this will encourage you to use clearer coding style and demonstrates some aspects of code blocks that you may not have realized. However, this just scratches the surface of writing clearer code, and a lot of it will just come with time and practice and realizing how necessary it is when you look back at code you wrote months ago.<\/p>\n<p>Note that one easy thing you can do to create better code is to try to stick established coding conventions. For example, see t<a href=\"http:\/\/docs.scala-lang.org\/style\/\">he coding guidelines for Scala<\/a> on the <a href=\"http:\/\/docs.scala-lang.org\/\">Scala documentation project<\/a>. There is also a lot of other very useful stuff, including tutorials, and it is actively evolving and growing!<\/p>\n<p><strong><i>Reference: <\/i><\/strong><a href=\"http:\/\/bcomposes.wordpress.com\/2011\/11\/14\/first-steps-in-scala-for-beginning-programmers-part-12\/\">First steps in Scala for beginning programmers, Part 12<\/a> from our <a href=\"http:\/\/www.javacodegeeks.com\/p\/jcg.html\">JCG partner<\/a> Jason Baldridge at the <a href=\"http:\/\/bcomposes.wordpress.com\/\">Bcomposes<\/a> blog.<\/p>\n<p><strong><i>Related Articles :<\/i><\/strong><\/p>\n<ul>\n<li><a href=\"http:\/\/www.javacodegeeks.com\/2011\/09\/scala-tutorial-scala-repl-expressions.html\">Scala Tutorial &#8211; Scala REPL, expressions, variables, basic types, simple functions, saving and running programs, comments<\/a><\/li>\n<li><a href=\"http:\/\/www.javacodegeeks.com\/2011\/09\/scala-tutorial-tuples-lists-methods-on.html\">Scala Tutorial &#8211; Tuples, Lists, methods on Lists and Strings<\/a><\/li>\n<li><a href=\"http:\/\/www.javacodegeeks.com\/2011\/09\/scala-tutorial-conditional-execution.html\">Scala Tutorial &#8211; conditional execution with if-else blocks and matching<\/a><\/li>\n<li><a href=\"http:\/\/www.javacodegeeks.com\/2011\/10\/scala-tutorial-iteration-for.html\">Scala Tutorial &#8211; iteration, for expressions, yield, map, filter, count<\/a><\/li>\n<li><a href=\"http:\/\/www.javacodegeeks.com\/2011\/10\/scala-tutorial-regular-expressions.html\">Scala Tutorial &#8211; regular expressions, matching<\/a><\/li>\n<li><a href=\"http:\/\/www.javacodegeeks.com\/2011\/10\/scala-tutorial-regular-expressions_05.html\">Scala Tutorial &#8211; regular expressions, matching and substitutions with the scala.util.matching API<\/a><\/li>\n<li><a href=\"http:\/\/www.javacodegeeks.com\/2011\/10\/scala-tutorial-maps-sets-groupby.html\">Scala Tutorial &#8211; Maps, Sets, groupBy, Options, flatten, flatMap<\/a><\/li>\n<li><a href=\"http:\/\/www.javacodegeeks.com\/2011\/10\/scala-tutorial-scalaiosource-accessing.html\">Scala Tutorial &#8211; scala.io.Source, accessing files, flatMap, mutable Maps<\/a><\/li>\n<li><a href=\"http:\/\/www.javacodegeeks.com\/2011\/10\/scala-tutorial-objects-classes.html\">Scala Tutorial &#8211; objects, classes, inheritance, traits, Lists with multiple related types, apply<\/a><\/li>\n<li><a href=\"http:\/\/www.javacodegeeks.com\/2011\/10\/scala-tutorial-scripting-compiling-main.html\">Scala Tutorial &#8211; scripting, compiling, main methods, return values of functions<\/a><\/li>\n<li><a href=\"http:\/\/www.javacodegeeks.com\/2011\/11\/scala-tutorial-sbt-scalabha-packages.html\">Scala Tutorial &#8211; SBT, scalabha, packages, build systems<\/a><\/li>\n<li><a href=\"http:\/\/www.javacodegeeks.com\/2011\/09\/fun-with-function-composition-in-scala.html\">Fun with function composition in Scala<\/a><\/li>\n<li><a href=\"http:\/\/www.javacodegeeks.com\/2011\/08\/how-scala-changed-way-i-think-about-my.html\">How Scala changed the way I think about my Java Code<\/a><\/li>\n<li><a href=\"http:\/\/www.javacodegeeks.com\/2011\/09\/testing-with-scala.html\">Testing with Scala<\/a><\/li>\n<li><a href=\"http:\/\/www.javacodegeeks.com\/2010\/12\/things-every-programmer-should-know.html\">Things Every Programmer Should Know<\/a><\/li>\n<\/ul>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Preface This is part 12 of tutorials for first-time programmers getting into Scala. Other posts are on this blog, and you can get links to those and other resources on the links page of the Computational Linguistics course I\u2019m creating these for.&nbsp;Additionally you can find this and other tutorial series on the JCG Java Tutorials &hellip;<\/p>\n","protected":false},"author":67,"featured_media":227,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[20],"tags":[235],"class_list":["post-689","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scala","tag-scala-tutorial"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.5 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Scala Tutorial - code blocks, coding style, closures, scala documentation project - Java Code Geeks<\/title>\n<meta name=\"description\" content=\"PrefaceThis is part 12 of tutorials for first-time programmers getting into Scala. Other posts are on this blog, and you can get links to those and other\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.javacodegeeks.com\/2011\/11\/scala-tutorial-code-blocks-coding-style.html\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Scala Tutorial - code blocks, coding style, closures, scala documentation project - Java Code Geeks\" \/>\n<meta property=\"og:description\" content=\"PrefaceThis is part 12 of tutorials for first-time programmers getting into Scala. Other posts are on this blog, and you can get links to those and other\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.javacodegeeks.com\/2011\/11\/scala-tutorial-code-blocks-coding-style.html\" \/>\n<meta property=\"og:site_name\" content=\"Java Code Geeks\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/javacodegeeks\" \/>\n<meta property=\"article:published_time\" content=\"2011-11-16T13:11:00+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2012-10-21T20:39:47+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/scala-logo.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"150\" \/>\n\t<meta property=\"og:image:height\" content=\"150\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Jason Baldridge\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@http:\/\/twitter.com\/jasonbaldridge\" \/>\n<meta name=\"twitter:site\" content=\"@javacodegeeks\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Jason Baldridge\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"12 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2011\\\/11\\\/scala-tutorial-code-blocks-coding-style.html#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2011\\\/11\\\/scala-tutorial-code-blocks-coding-style.html\"},\"author\":{\"name\":\"Jason Baldridge\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/person\\\/95ef2670a4040b0f7101c48dba2795c0\"},\"headline\":\"Scala Tutorial &#8211; code blocks, coding style, closures, scala documentation project\",\"datePublished\":\"2011-11-16T13:11:00+00:00\",\"dateModified\":\"2012-10-21T20:39:47+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2011\\\/11\\\/scala-tutorial-code-blocks-coding-style.html\"},\"wordCount\":1536,\"commentCount\":2,\"publisher\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2011\\\/11\\\/scala-tutorial-code-blocks-coding-style.html#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2012\\\/10\\\/scala-logo.jpg\",\"keywords\":[\"Scala Tutorial\"],\"articleSection\":[\"Scala\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.javacodegeeks.com\\\/2011\\\/11\\\/scala-tutorial-code-blocks-coding-style.html#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2011\\\/11\\\/scala-tutorial-code-blocks-coding-style.html\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2011\\\/11\\\/scala-tutorial-code-blocks-coding-style.html\",\"name\":\"Scala Tutorial - code blocks, coding style, closures, scala documentation project - Java Code Geeks\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2011\\\/11\\\/scala-tutorial-code-blocks-coding-style.html#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2011\\\/11\\\/scala-tutorial-code-blocks-coding-style.html#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2012\\\/10\\\/scala-logo.jpg\",\"datePublished\":\"2011-11-16T13:11:00+00:00\",\"dateModified\":\"2012-10-21T20:39:47+00:00\",\"description\":\"PrefaceThis is part 12 of tutorials for first-time programmers getting into Scala. Other posts are on this blog, and you can get links to those and other\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2011\\\/11\\\/scala-tutorial-code-blocks-coding-style.html#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.javacodegeeks.com\\\/2011\\\/11\\\/scala-tutorial-code-blocks-coding-style.html\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2011\\\/11\\\/scala-tutorial-code-blocks-coding-style.html#primaryimage\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2012\\\/10\\\/scala-logo.jpg\",\"contentUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2012\\\/10\\\/scala-logo.jpg\",\"width\":150,\"height\":150},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2011\\\/11\\\/scala-tutorial-code-blocks-coding-style.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.javacodegeeks.com\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"JVM Languages\",\"item\":\"https:\\\/\\\/www.javacodegeeks.com\\\/category\\\/jvm-languages\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Scala\",\"item\":\"https:\\\/\\\/www.javacodegeeks.com\\\/category\\\/jvm-languages\\\/scala\"},{\"@type\":\"ListItem\",\"position\":4,\"name\":\"Scala Tutorial &#8211; code blocks, coding style, closures, scala documentation project\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#website\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/\",\"name\":\"Java Code Geeks\",\"description\":\"Java Developers Resource Center\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#organization\"},\"alternateName\":\"JCG\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.javacodegeeks.com\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#organization\",\"name\":\"Exelixis Media P.C.\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2022\\\/06\\\/exelixis-logo.png\",\"contentUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2022\\\/06\\\/exelixis-logo.png\",\"width\":864,\"height\":246,\"caption\":\"Exelixis Media P.C.\"},\"image\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/logo\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/www.facebook.com\\\/javacodegeeks\",\"https:\\\/\\\/x.com\\\/javacodegeeks\"]},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/person\\\/95ef2670a4040b0f7101c48dba2795c0\",\"name\":\"Jason Baldridge\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/b755d282f869512990e0ce9118c71ccd859fad42163f8e5d62d180ea42ea9720?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/b755d282f869512990e0ce9118c71ccd859fad42163f8e5d62d180ea42ea9720?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/b755d282f869512990e0ce9118c71ccd859fad42163f8e5d62d180ea42ea9720?s=96&d=mm&r=g\",\"caption\":\"Jason Baldridge\"},\"sameAs\":[\"http:\\\/\\\/bcomposes.wordpress.com\\\/\",\"http:\\\/\\\/www.linkedin.com\\\/pub\\\/jason-baldridge\\\/5\\\/629\\\/9b2\",\"https:\\\/\\\/x.com\\\/http:\\\/\\\/twitter.com\\\/jasonbaldridge\"],\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/author\\\/jason-baldridge\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Scala Tutorial - code blocks, coding style, closures, scala documentation project - Java Code Geeks","description":"PrefaceThis is part 12 of tutorials for first-time programmers getting into Scala. Other posts are on this blog, and you can get links to those and other","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.javacodegeeks.com\/2011\/11\/scala-tutorial-code-blocks-coding-style.html","og_locale":"en_US","og_type":"article","og_title":"Scala Tutorial - code blocks, coding style, closures, scala documentation project - Java Code Geeks","og_description":"PrefaceThis is part 12 of tutorials for first-time programmers getting into Scala. Other posts are on this blog, and you can get links to those and other","og_url":"https:\/\/www.javacodegeeks.com\/2011\/11\/scala-tutorial-code-blocks-coding-style.html","og_site_name":"Java Code Geeks","article_publisher":"https:\/\/www.facebook.com\/javacodegeeks","article_published_time":"2011-11-16T13:11:00+00:00","article_modified_time":"2012-10-21T20:39:47+00:00","og_image":[{"width":150,"height":150,"url":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/scala-logo.jpg","type":"image\/jpeg"}],"author":"Jason Baldridge","twitter_card":"summary_large_image","twitter_creator":"@http:\/\/twitter.com\/jasonbaldridge","twitter_site":"@javacodegeeks","twitter_misc":{"Written by":"Jason Baldridge","Est. reading time":"12 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.javacodegeeks.com\/2011\/11\/scala-tutorial-code-blocks-coding-style.html#article","isPartOf":{"@id":"https:\/\/www.javacodegeeks.com\/2011\/11\/scala-tutorial-code-blocks-coding-style.html"},"author":{"name":"Jason Baldridge","@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/person\/95ef2670a4040b0f7101c48dba2795c0"},"headline":"Scala Tutorial &#8211; code blocks, coding style, closures, scala documentation project","datePublished":"2011-11-16T13:11:00+00:00","dateModified":"2012-10-21T20:39:47+00:00","mainEntityOfPage":{"@id":"https:\/\/www.javacodegeeks.com\/2011\/11\/scala-tutorial-code-blocks-coding-style.html"},"wordCount":1536,"commentCount":2,"publisher":{"@id":"https:\/\/www.javacodegeeks.com\/#organization"},"image":{"@id":"https:\/\/www.javacodegeeks.com\/2011\/11\/scala-tutorial-code-blocks-coding-style.html#primaryimage"},"thumbnailUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/scala-logo.jpg","keywords":["Scala Tutorial"],"articleSection":["Scala"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.javacodegeeks.com\/2011\/11\/scala-tutorial-code-blocks-coding-style.html#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.javacodegeeks.com\/2011\/11\/scala-tutorial-code-blocks-coding-style.html","url":"https:\/\/www.javacodegeeks.com\/2011\/11\/scala-tutorial-code-blocks-coding-style.html","name":"Scala Tutorial - code blocks, coding style, closures, scala documentation project - Java Code Geeks","isPartOf":{"@id":"https:\/\/www.javacodegeeks.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.javacodegeeks.com\/2011\/11\/scala-tutorial-code-blocks-coding-style.html#primaryimage"},"image":{"@id":"https:\/\/www.javacodegeeks.com\/2011\/11\/scala-tutorial-code-blocks-coding-style.html#primaryimage"},"thumbnailUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/scala-logo.jpg","datePublished":"2011-11-16T13:11:00+00:00","dateModified":"2012-10-21T20:39:47+00:00","description":"PrefaceThis is part 12 of tutorials for first-time programmers getting into Scala. Other posts are on this blog, and you can get links to those and other","breadcrumb":{"@id":"https:\/\/www.javacodegeeks.com\/2011\/11\/scala-tutorial-code-blocks-coding-style.html#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.javacodegeeks.com\/2011\/11\/scala-tutorial-code-blocks-coding-style.html"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.javacodegeeks.com\/2011\/11\/scala-tutorial-code-blocks-coding-style.html#primaryimage","url":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/scala-logo.jpg","contentUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/scala-logo.jpg","width":150,"height":150},{"@type":"BreadcrumbList","@id":"https:\/\/www.javacodegeeks.com\/2011\/11\/scala-tutorial-code-blocks-coding-style.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.javacodegeeks.com\/"},{"@type":"ListItem","position":2,"name":"JVM Languages","item":"https:\/\/www.javacodegeeks.com\/category\/jvm-languages"},{"@type":"ListItem","position":3,"name":"Scala","item":"https:\/\/www.javacodegeeks.com\/category\/jvm-languages\/scala"},{"@type":"ListItem","position":4,"name":"Scala Tutorial &#8211; code blocks, coding style, closures, scala documentation project"}]},{"@type":"WebSite","@id":"https:\/\/www.javacodegeeks.com\/#website","url":"https:\/\/www.javacodegeeks.com\/","name":"Java Code Geeks","description":"Java Developers Resource Center","publisher":{"@id":"https:\/\/www.javacodegeeks.com\/#organization"},"alternateName":"JCG","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.javacodegeeks.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.javacodegeeks.com\/#organization","name":"Exelixis Media P.C.","url":"https:\/\/www.javacodegeeks.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/logo\/image\/","url":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","contentUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","width":864,"height":246,"caption":"Exelixis Media P.C."},"image":{"@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/javacodegeeks","https:\/\/x.com\/javacodegeeks"]},{"@type":"Person","@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/person\/95ef2670a4040b0f7101c48dba2795c0","name":"Jason Baldridge","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/b755d282f869512990e0ce9118c71ccd859fad42163f8e5d62d180ea42ea9720?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/b755d282f869512990e0ce9118c71ccd859fad42163f8e5d62d180ea42ea9720?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/b755d282f869512990e0ce9118c71ccd859fad42163f8e5d62d180ea42ea9720?s=96&d=mm&r=g","caption":"Jason Baldridge"},"sameAs":["http:\/\/bcomposes.wordpress.com\/","http:\/\/www.linkedin.com\/pub\/jason-baldridge\/5\/629\/9b2","https:\/\/x.com\/http:\/\/twitter.com\/jasonbaldridge"],"url":"https:\/\/www.javacodegeeks.com\/author\/jason-baldridge"}]}},"_links":{"self":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/posts\/689","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/users\/67"}],"replies":[{"embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/comments?post=689"}],"version-history":[{"count":0,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/posts\/689\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/media\/227"}],"wp:attachment":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/media?parent=689"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/categories?post=689"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/tags?post=689"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}