{"id":532,"date":"2011-09-27T08:24:00","date_gmt":"2011-09-27T08:24:00","guid":{"rendered":"http:\/\/www.javacodegeeks.com\/2012\/10\/scala-tutorial-tuples-lists-methods-on-lists-and-strings.html"},"modified":"2012-10-21T20:11:08","modified_gmt":"2012-10-21T20:11:08","slug":"scala-tutorial-tuples-lists-methods-on","status":"publish","type":"post","link":"https:\/\/www.javacodegeeks.com\/2011\/09\/scala-tutorial-tuples-lists-methods-on.html","title":{"rendered":"Scala Tutorial &#8211; Tuples, Lists, methods on Lists and Strings"},"content":{"rendered":"<div dir=\"ltr\" style=\"text-align: left\">\n<h2>             Preface<\/h2>\n<p>This is the second in a planned series of tutorials on programming in Scala for first-time programmers, with specific reference to my Fall 2011 course<a href=\"http:\/\/icl-f11.utcompling.com\/\"> Introduction to Computational Linguistics. <\/a>You can see the other tutorials here on this blog; they are also listed on the course\u2019s <a href=\"http:\/\/icl-f11.utcompling.com\/links\">links page.<\/a><\/p>\n<p>This tutorial focuses on Tuples and Lists, which are two constructs for working with groups of elements. You won\u2019t get much done without the latter, and the former are so incredibly useful you probably find yourself using them a lot.<\/p>\n<h2>             Tuples<\/h2>\n<p>We saw in the previous tutorial how a single value can be assigned to a variable and then used in various contexts. A <strong>Tuple<\/strong> is a generalization of that: a collection of two, three, four, and more values. Each value can have its own type.<\/p>\n<pre class=\"brush: scala;\">scala&gt; val twoInts = (3,9)\r\ntwoInts: (Int, Int) = (3,9)\r\n \r\nscala&gt; val twoStrings = (\"hello\", \"world\")\r\ntwoStrings: (java.lang.String, java.lang.String) = (hello,world)\r\n \r\nscala&gt; val threeDoubles = (3.14, 11.29, 1.5)\r\nthreeDoubles: (Double, Double, Double) = (3.14,11.29,1.5)\r\n \r\nscala&gt; val intAndString = (7, \"lucky number\")\r\nintAndString: (Int, java.lang.String) = (7,lucky number)\r\n \r\nscala&gt; val mixedUp = (1, \"hello\", 1.16)\r\nmixedUp: (Int, java.lang.String, Double) = (1,hello,1.16)\r\n<\/pre>\n<p>The elements of a Tuple can be recovered in a few different ways. One way is to use a Tuple when initializing some variables, each of which takes on the value of the corresponding position in the Tuple on the right side of the equal sign.<\/p>\n<pre class=\"brush: scala;\">scala&gt; val (first, second) = twoInts\r\nfirst: Int = 3\r\nsecond: Int = 9\r\n \r\nscala&gt; val (numTimes, thingToSay, price) = mixedUp\r\nnumTimes: Int = 1\r\nthingToSay: java.lang.String = hello\r\nprice: Double = 1.16\r\n<\/pre>\n<p>Scala peels off the values and assigns them to each of the single variables. This becomes very useful in the context of functions that return Tuples. For example, consider a function that provides the left and right edges of a range when you give it the midpoint of the range and the size of the interval on each side of the midpoint.<\/p>\n<pre class=\"brush: scala;\">scala&gt; def rangeAround(midpoint: Int, size: Int) = (midpoint - size, midpoint + size)\r\nrangeAround: (midpoint: Int, size: Int)(Int, Int)\r\n<\/pre>\n<p>Since <strong>rangeAround<\/strong> returns a Tuple (specifically, a Pair), we can call it and set variables for the left and right directly from the function call.<\/p>\n<pre class=\"brush: scala;\">scala&gt; val (left, right) = rangeAround(21, 3)\r\nleft: Int = 18\r\nright: Int = 24\r\n<\/pre>\n<p>Another way to access the values in a Tuple is via indexation, using \u201c<strong>_n<\/strong>\u201d where <em>n<\/em> is the index of the item you want.<\/p>\n<pre class=\"brush: scala;\">scala&gt; print(mixedUp._1)\r\n1\r\nscala&gt; print(mixedUp._2)\r\nhello\r\nscala&gt; print(mixedUp._3)\r\n1.16\r\n<\/pre>\n<p>The syntax on this is a bit odd, but you\u2019ll get used to it.<\/p>\n<p>Tuples are an amazingly useful feature in a programming language. You\u2019ll see some examples of their utility as we progress.<\/p>\n<h2>             Lists<\/h2>\n<p>Lists are collections of ordered items that will be familiar to anyone who has done any shopping. Tuples are obviously related to lists, but they are less versatile in that they must be created in a single statement, they have a bounded length (about 20 or so), and they don\u2019t support operations that perform computations on all of their elements.<\/p>\n<p>In Scala, we can create lists of Strings, Ints, and Doubles (and more).<\/p>\n<pre class=\"brush: scala;\">scala&gt; val groceryList = List(\"apples\", \"milk\", \"butter\")\r\ngroceryList: List[java.lang.String] = List(apples, milk, butter)\r\n \r\nscala&gt; val odds = List(1,3,5,7,9)\r\nodds: List[Int] = List(1, 3, 5, 7, 9)\r\n \r\nscala&gt; val multinomial = List(.2, .4, .15, .25)\r\nmultinomial: List[Double] = List(0.2, 0.4, 0.15, 0.25)\r\n<\/pre>\n<p>We see that Scala responds that a List has been created, along with brackets around the type of the elements it contains. So, <strong>List[Int]<\/strong> is read as \u201ca List of Ints\u201d and so on. This is to say that List is a parameterized data structure: it is a container that holds elements of specific types. We\u2019ll see how knowing this allows us to do different things with Lists parameterized by different types.<\/p>\n<p>We can also create Lists with mixtures of types.<\/p>\n<pre class=\"brush: scala;\">scala&gt; val intsAndDoubles = List(1, 1.5, 2, 2.5)\r\nintsAndDoubles: List[Double] = List(1.0, 1.5, 2.0, 2.5)\r\n \r\nscala&gt; val today = List(\"August\", 23, 2011)\r\ntoday: List[Any] = List(August, 23, 2011)\r\n<\/pre>\n<p>Types are sometimes autoconverted, such as converting Ints to Doubles for <strong>intsAndDoubles<\/strong>, but often there is no obvious generalizable type. For example, <strong>today<\/strong> is a <strong>List[Any]<\/strong>, which means it is a List of Anys \u2014 and Any is the most general type in Scala, the supertype of all types. It\u2019s sort of like saying \u201cYeah, I have a list of\u2026 well, you know\u2026 stuff.\u201d<\/p>\n<p>Lists can also contain Lists (and Lists of Lists, and Lists of Lists of Lists\u2026).<\/p>\n<pre class=\"brush: scala;\">scala&gt; val embedded = List(List(1,2,3), List(10,30,50), List(200,400), List(1000))\r\nembedded: List[List[Int]] = List(List(1, 2, 3), List(10, 30, 50), List(200, 400), List(1000))\r\n<\/pre>\n<p>The type of <strong>embedded<\/strong> is <strong>List[List[Int]]<\/strong>, which you can read as \u201ca List of Lists of Ints.\u201d<\/p>\n<h2>             List methods<\/h2>\n<p>Okay, so now that we have some lists, what can we do with them? A lot, actually. One of the most basic properties of a list is its length, which you can get by using \u201c<strong>.length<\/strong>\u201d after the variable that refers to the list.<\/p>\n<pre class=\"brush: scala;\">scala&gt; groceryList.length\r\nres19: Int = 3\r\n \r\nscala&gt; odds.length\r\nres20: Int = 5\r\n \r\nscala&gt; embedded.length\r\nres21: Int = 4\r\n<\/pre>\n<p>Notice that the length of <strong>embedded<\/strong> is 4, which is the number of Lists it contains (not the number of elements in those lists).<div style=\"display:inline-block; margin: 15px 0;\"> <div id=\"adngin-JavaCodeGeeks_incontent_video-0\" style=\"display:inline-block;\"><\/div> <\/div><\/p>\n<p>The notation <strong><em>variable.method<\/em><\/strong> indicates that you are invoking a function that is specific to the type of that variable on the value in that variable. Okay, that was a mouthful. Scala is an object-oriented language, which means that every value has a set of actions that comes with it. Which actions are available depends on its type. So, above, we called the <strong>length<\/strong> method that is available to Lists on each of the list values given above. You didn\u2019t realize it in the previous tutorial, but you were using methods when you added Ints or concatenated Strings \u2014 it\u2019s just that Scala allows us to go without \u201c.\u201d and paretheses in certain cases. If we don\u2019t drop them, here\u2019s what it looks like.<\/p>\n<pre class=\"brush: scala;\">scala&gt; 2.+(3)\r\nres25: Double = 5.0\r\n \r\nscala&gt; \"Portis\".+(\"head\")\r\nres26: java.lang.String = Portishead\r\n<\/pre>\n<p>What is going on is that Ints have a method called \u201c<strong>+<\/strong>\u201d and Strings have a <em>different<\/em> method called \u201c<strong>+<\/strong>\u201c. They could have been called \u201cbill\u201d and \u201cbob\u201d, but that would be harder to remember, among other things. Ints have other methods, such as \u201c<strong>&#8211;<\/strong>\u201c, \u201c<strong>*<\/strong>\u201c, and \u201c<strong>\/<\/strong>\u201c, that Strings don\u2019t have. (Note: I\u2019m now returning to omitting the \u201c.\u201d and paretheses.)<\/p>\n<pre class=\"brush: scala;\">scala&gt; 5-3\r\nres27: Int = 2\r\n \r\nscala&gt; \"walked\" - \"ed\"\r\n&lt;console&gt;:8: error: value - is not a member of java.lang.String\r\n\"walked\" - \"ed\"\r\n<\/pre>\n<p>Scala complains that we tried to use the \u201c<strong>&#8211;<\/strong>\u201d method on a String, since Strings don\u2019t have such a method. On the other hand, Ints don\u2019t have a method called <strong>length<\/strong>, while Strings do.<\/p>\n<pre class=\"brush: scala;\">scala&gt; 5.length\r\n&lt;console&gt;:8: error: value length is not a member of Int\r\n5.length\r\n^\r\n \r\nscala&gt; \"walked\".length\r\nres31: Int = 6\r\n<\/pre>\n<p>With Strings, <strong>length<\/strong> returns the number of characters, whereas with Lists, it is the number of elements. The String <strong>length<\/strong> method could have been called \u201cnumberOfCharacters\u201d, but \u201clength\u201d is easier to remember and it allows us to treat Strings like other sequences and think of them similarly.<\/p>\n<p>Lets return to Lists and what we can do with them. \u201cAddition\u201d of two lists is their concatenation and is indicated with \u201c<strong>++<\/strong>\u201c.<\/p>\n<pre class=\"brush: scala;\">scala&gt; val evens = List(2,4,6,8)\r\nevens: List[Int] = List(2, 4, 6, 8)\r\n \r\nscala&gt; val nums = odds ++ evens\r\nnums: List[Int] = List(1, 3, 5, 7, 9, 2, 4, 6, 8)\r\n<\/pre>\n<p>We can append a single item to the front of a List with \u201c<strong>::<\/strong>\u201c.<\/p>\n<pre class=\"brush: scala;\">scala&gt; val zeroToNine = 0 :: nums\r\nzeroToNine: List[Int] = List(0, 1, 3, 5, 7, 9, 2, 4, 6, 8)\r\n<\/pre>\n<p>And sort a list with <strong>sorted<\/strong>, and reverse it with <strong>reverse<\/strong>, and do both in sequence.<\/p>\n<pre class=\"brush: scala;\">scala&gt; zeroToNine.sorted\r\nres42: List[Int] = List(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)\r\n \r\nscala&gt; zeroToNine.reverse\r\nres43: List[Int] = List(8, 6, 4, 2, 9, 7, 5, 3, 1, 0)\r\n \r\nscala&gt; zeroToNine.sorted.reverse\r\nres44: List[Int] = List(9, 8, 7, 6, 5, 4, 3, 2, 1, 0)\r\n<\/pre>\n<p>What the last line says is \u201ctake <strong>zeroToNine<\/strong>, get a new sorted list from it, and then reverse that list.\u201d Notice that calling these functions never changes <strong>zeroToNine<\/strong> itself! That is because List is <em>immutable<\/em>: you cannot change it, so all of these operations return new Lists. This property of Lists brings with it many benefits that we\u2019ll return to later.<\/p>\n<p>Note: immutability is different from the <strong>val\/var<\/strong> distinction. It is common to think that a <strong>val<\/strong> variable is immutable, but it is not \u2014 it is fixed and cannot be reassigned. The following examples all involve immutable Lists, but the fixed variable is a <strong>val<\/strong> while the reassignable variable is a <strong>var<\/strong>.<\/p>\n<pre class=\"brush: scala;\">scala&gt; val fixed = List(1,2)\r\nfixed: List[Int] = List(1, 2)\r\n \r\nscala&gt; fixed = List(3,4)\r\n&lt;console&gt;:8: error: reassignment to val\r\nfixed = List(3,4)\r\n^\r\n \r\nscala&gt; var reassignable = List(5,6)\r\nreassignable: List[Int] = List(5, 6)\r\n \r\nscala&gt; reassignable = List(7,8)\r\nreassignable: List[Int] = List(7, 8)\r\n<\/pre>\n<p>One of the things one frequently wants to do with a list is access its elements directly. This is done via indexation into the list, starting with 0 for the first element, 1 for the second element, and so on.<\/p>\n<pre class=\"brush: scala;\">scala&gt; odds\r\nres48: List[Int] = List(1, 3, 5, 7, 9)\r\n \r\nscala&gt; odds(0)\r\nres49: Int = 1\r\n \r\nscala&gt; odds(1)\r\nres50: Int = 3\r\n<\/pre>\n<p>Starting with 0 for the index of the first element is standard practice in computer science. It might seem strange at first, but you\u2019ll get used to it fairly quickly.<\/p>\n<p>We can of course use any Int expression to access an item in a list.<\/p>\n<pre class=\"brush: scala;\">scala&gt; zeroToNine(3)\r\nres63: Int = 5\r\n \r\nscala&gt; zeroToNine(5-2)\r\nres64: Int = 5\r\n \r\nscala&gt; val index = 3\r\nindex: Int = 3\r\n \r\nscala&gt; zeroToNine(index)\r\nres65: Int = 5\r\n<\/pre>\n<p>If we ask for an index that is equal to or greater than the number of elements in the list, we get an error.<\/p>\n<pre class=\"brush: scala;\">scala&gt; odds(10)\r\njava.lang.IndexOutOfBoundsException: 10\r\nat scala.collection.LinearSeqOptimized$class.apply(LinearSeqOptimized.scala:51)\r\nat scala.collection.immutable.List.apply(List.scala:45)\r\nat .&lt;init&gt;(&lt;console&gt;:9)\r\nat .&lt;clinit&gt;(&lt;console&gt;)\r\nat .&lt;init&gt;(&lt;console&gt;:11)\r\nat .&lt;clinit&gt;(&lt;console&gt;)\r\nat $export(&lt;console&gt;)\r\nat sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)\r\nat sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)\r\nat sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)\r\nat java.lang.reflect.Method.invoke(Method.java:597)\r\nat scala.tools.nsc.interpreter.IMain$ReadEvalPrint.call(IMain.scala:592)\r\nat scala.tools.nsc.interpreter.IMain$Request$$anonfun$10.apply(IMain.scala:828)\r\nat scala.tools.nsc.interpreter.Line$$anonfun$1.apply$mcV$sp(Line.scala:43)\r\nat scala.tools.nsc.io.package$$anon$2.run(package.scala:31)\r\nat java.lang.Thread.run(Thread.java:680)\r\n<\/pre>\n<p>Looking at all that, you might be thinking \u201cWTF?\u201d It\u2019s called the stack trace, and it gives you a detailed breakdown of where problems happened in a bit of code. For beginning programmers, this is likely to look overwhelming and intimidating \u2014 you can safely glaze over it for now, but before long, it will be necessary to be able to use the stack trace to identify problems in your code and address them.<\/p>\n<p>Another useful method is <strong>slice<\/strong>, which gives you a sublist from one index up to, but not including, another.<\/p>\n<pre class=\"brush: scala;\">scala&gt; zeroToNine\r\nres55: List[Int] = List(0, 1, 3, 5, 7, 9, 2, 4, 6, 8)\r\n \r\nscala&gt; zeroToNine.slice(2,6)\r\nres56: List[Int] = List(3, 5, 7, 9)\r\n<\/pre>\n<p>So, the slice gave us a list with the elements from index 2 (the third element) up to index 5 (the sixth element).<\/p>\n<p>Returning briefly to Strings \u2014 other List methods than <strong>length<\/strong> work with them too.<\/p>\n<pre class=\"brush: scala;\">scala&gt; val artist = \"DJ Shadow\"\r\nartist: java.lang.String = DJ Shadow\r\n \r\nscala&gt; artist(3)\r\nres0: Char = S\r\n \r\nscala&gt; artist.slice(3,6)\r\nres1: String = Sha\r\n \r\nscala&gt; artist.reverse\r\nres2: String = wodahS JD\r\n \r\nscala&gt; artist.sorted\r\nres3: String = \" DJSadhow\"\r\n<\/pre>\n<p>On lists that contain numbers, we can use the <strong>sum<\/strong> method.<\/p>\n<pre class=\"brush: scala;\">scala&gt; odds.sum\r\nres59: Int = 25\r\n \r\nscala&gt; multinomial.sum\r\nres60: Double = 1.0\r\n<\/pre>\n<p>However, if the list contains non-numeric values, <strong>sum<\/strong> isn\u2019t valid.<\/p>\n<pre class=\"brush: scala;\">scala&gt; groceryList.sum\r\n&lt;console&gt;:9: error: could not find implicit value for parameter num: Numeric[java.lang.String]\r\ngroceryList.sum\r\n^\r\n<\/pre>\n<p>What is going on is some very cool and useful automagical behavior by Scala involving implicits. We\u2019ll come back to that later, but for now you can happily use <strong>sum<\/strong> on Lists of Ints and Doubles.<\/p>\n<p>One thing we often want to do with lists is obtain a String representation of their contents in some visually useful way. For example, we might want a grocery list to be a String with one item per line, or a list of Ints to have a comma between each element. The <strong>mkString<\/strong> method does just what we need.<\/p>\n<pre class=\"brush: scala;\">scala&gt; groceryList.mkString(\"\\n\")\r\nres22: String =\r\napples\r\nmilk\r\nbutter\r\n \r\nscala&gt; odds.mkString(\",\")\r\nres23: String = 1,3,5,7,9\r\n<\/pre>\n<p>Want to know if a list contains a particular element? Use <strong>contains<\/strong> on the list.<\/p>\n<pre class=\"brush: scala;\">scala&gt; groceryList.contains(\"milk\")\r\nres4: Boolean = true\r\n \r\nscala&gt; groceryList.contains(\"coffee\")\r\nres5: Boolean = false\r\n<\/pre>\n<p>And now we arrive at <strong>Booleans<\/strong>, another of the most important basic types. They play a major role in conditional execution, which we\u2019ll cover in the next tutorial.<\/p>\n<p>There are actually many more methods available for lists, which you can see by going to <a href=\"http:\/\/www.scala-lang.org\/api\/current\/scala\/collection\/immutable\/List.html\">the entry for List<\/a> in t<a href=\"http:\/\/www.scala-lang.org\/api\/current\/index.html#package\">he Scala API<\/a>. API stands for Application Programming Interface \u2014 in other words a collection of specifications for what you can do with various components of the Scala programming language. I\u2019m going to do my best to give you the methods you need for now, but eventually you will need to be able to look at the API entries for Scala types to see what methods are available, what they do and how to use them.<\/p>\n<p>Some of the most important methods on Lists we haven\u2019t covered are <strong>map<\/strong>, <strong>filter<\/strong>, <strong>foldLeft<\/strong>, and <strong>reduce<\/strong>. We\u2019ll come back to them in detail later, but for now here is a teaser that should give you an intuitive sense of what they do.<\/p>\n<pre class=\"brush: scala;\">scala&gt; val odds = List(1,3,5,7,9)\r\nodds: List[Int] = List(1, 3, 5, 7, 9)\r\n \r\nscala&gt; odds.map(1+)\r\nres6: List[Int] = List(2, 4, 6, 8, 10)\r\n \r\nscala&gt; odds.filter(4&lt;)\r\nres7: List[Int] = List(5, 7, 9)\r\n \r\nscala&gt; odds.foldLeft(10)(_ + _)\r\nres8: Int = 35\r\n \r\nscala&gt; odds.filter(6&gt;).map(_.toString).reduce(_ + \",\" + _)\r\nres9: java.lang.String = 1,3,5\r\n<\/pre>\n<p>Now we\u2019re getting functional. :)<\/p>\n<p><strong><i>Reference: <\/i><\/strong><a href=\"http:\/\/bcomposes.wordpress.com\/2011\/08\/24\/first-steps-in-scala-for-beginning-programmers-part-2\/\">First steps in Scala for beginning programmers, Part 2<\/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-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\/11\/scala-tutorial-code-blocks-coding-style.html\">Scala Tutorial &#8211; code blocks, coding style, closures, scala documentation project<\/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\/08\/what-features-of-java-have-been-dropped.html\">What features of Java have been dropped in Scala?<\/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 the second in a planned series of tutorials on programming in Scala for first-time programmers, with specific reference to my Fall 2011 course Introduction to Computational Linguistics. You can see the other tutorials here on this blog; they are also listed on the course\u2019s links page. This tutorial focuses on Tuples and &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-532","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 - Tuples, Lists, methods on Lists and Strings - Java Code Geeks<\/title>\n<meta name=\"description\" content=\"PrefaceThis is the second in a planned series of tutorials on programming in Scala for first-time programmers, with specific reference to my Fall 2011\" \/>\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\/09\/scala-tutorial-tuples-lists-methods-on.html\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Scala Tutorial - Tuples, Lists, methods on Lists and Strings - Java Code Geeks\" \/>\n<meta property=\"og:description\" content=\"PrefaceThis is the second in a planned series of tutorials on programming in Scala for first-time programmers, with specific reference to my Fall 2011\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.javacodegeeks.com\/2011\/09\/scala-tutorial-tuples-lists-methods-on.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-09-27T08:24:00+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2012-10-21T20:11:08+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\\\/09\\\/scala-tutorial-tuples-lists-methods-on.html#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2011\\\/09\\\/scala-tutorial-tuples-lists-methods-on.html\"},\"author\":{\"name\":\"Jason Baldridge\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/person\\\/95ef2670a4040b0f7101c48dba2795c0\"},\"headline\":\"Scala Tutorial &#8211; Tuples, Lists, methods on Lists and Strings\",\"datePublished\":\"2011-09-27T08:24:00+00:00\",\"dateModified\":\"2012-10-21T20:11:08+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2011\\\/09\\\/scala-tutorial-tuples-lists-methods-on.html\"},\"wordCount\":1810,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2011\\\/09\\\/scala-tutorial-tuples-lists-methods-on.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\\\/09\\\/scala-tutorial-tuples-lists-methods-on.html#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2011\\\/09\\\/scala-tutorial-tuples-lists-methods-on.html\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2011\\\/09\\\/scala-tutorial-tuples-lists-methods-on.html\",\"name\":\"Scala Tutorial - Tuples, Lists, methods on Lists and Strings - Java Code Geeks\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2011\\\/09\\\/scala-tutorial-tuples-lists-methods-on.html#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2011\\\/09\\\/scala-tutorial-tuples-lists-methods-on.html#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2012\\\/10\\\/scala-logo.jpg\",\"datePublished\":\"2011-09-27T08:24:00+00:00\",\"dateModified\":\"2012-10-21T20:11:08+00:00\",\"description\":\"PrefaceThis is the second in a planned series of tutorials on programming in Scala for first-time programmers, with specific reference to my Fall 2011\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2011\\\/09\\\/scala-tutorial-tuples-lists-methods-on.html#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.javacodegeeks.com\\\/2011\\\/09\\\/scala-tutorial-tuples-lists-methods-on.html\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2011\\\/09\\\/scala-tutorial-tuples-lists-methods-on.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\\\/09\\\/scala-tutorial-tuples-lists-methods-on.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; Tuples, Lists, methods on Lists and Strings\"}]},{\"@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 - Tuples, Lists, methods on Lists and Strings - Java Code Geeks","description":"PrefaceThis is the second in a planned series of tutorials on programming in Scala for first-time programmers, with specific reference to my Fall 2011","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\/09\/scala-tutorial-tuples-lists-methods-on.html","og_locale":"en_US","og_type":"article","og_title":"Scala Tutorial - Tuples, Lists, methods on Lists and Strings - Java Code Geeks","og_description":"PrefaceThis is the second in a planned series of tutorials on programming in Scala for first-time programmers, with specific reference to my Fall 2011","og_url":"https:\/\/www.javacodegeeks.com\/2011\/09\/scala-tutorial-tuples-lists-methods-on.html","og_site_name":"Java Code Geeks","article_publisher":"https:\/\/www.facebook.com\/javacodegeeks","article_published_time":"2011-09-27T08:24:00+00:00","article_modified_time":"2012-10-21T20:11:08+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\/09\/scala-tutorial-tuples-lists-methods-on.html#article","isPartOf":{"@id":"https:\/\/www.javacodegeeks.com\/2011\/09\/scala-tutorial-tuples-lists-methods-on.html"},"author":{"name":"Jason Baldridge","@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/person\/95ef2670a4040b0f7101c48dba2795c0"},"headline":"Scala Tutorial &#8211; Tuples, Lists, methods on Lists and Strings","datePublished":"2011-09-27T08:24:00+00:00","dateModified":"2012-10-21T20:11:08+00:00","mainEntityOfPage":{"@id":"https:\/\/www.javacodegeeks.com\/2011\/09\/scala-tutorial-tuples-lists-methods-on.html"},"wordCount":1810,"commentCount":0,"publisher":{"@id":"https:\/\/www.javacodegeeks.com\/#organization"},"image":{"@id":"https:\/\/www.javacodegeeks.com\/2011\/09\/scala-tutorial-tuples-lists-methods-on.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\/09\/scala-tutorial-tuples-lists-methods-on.html#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.javacodegeeks.com\/2011\/09\/scala-tutorial-tuples-lists-methods-on.html","url":"https:\/\/www.javacodegeeks.com\/2011\/09\/scala-tutorial-tuples-lists-methods-on.html","name":"Scala Tutorial - Tuples, Lists, methods on Lists and Strings - Java Code Geeks","isPartOf":{"@id":"https:\/\/www.javacodegeeks.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.javacodegeeks.com\/2011\/09\/scala-tutorial-tuples-lists-methods-on.html#primaryimage"},"image":{"@id":"https:\/\/www.javacodegeeks.com\/2011\/09\/scala-tutorial-tuples-lists-methods-on.html#primaryimage"},"thumbnailUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/scala-logo.jpg","datePublished":"2011-09-27T08:24:00+00:00","dateModified":"2012-10-21T20:11:08+00:00","description":"PrefaceThis is the second in a planned series of tutorials on programming in Scala for first-time programmers, with specific reference to my Fall 2011","breadcrumb":{"@id":"https:\/\/www.javacodegeeks.com\/2011\/09\/scala-tutorial-tuples-lists-methods-on.html#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.javacodegeeks.com\/2011\/09\/scala-tutorial-tuples-lists-methods-on.html"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.javacodegeeks.com\/2011\/09\/scala-tutorial-tuples-lists-methods-on.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\/09\/scala-tutorial-tuples-lists-methods-on.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; Tuples, Lists, methods on Lists and Strings"}]},{"@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\/532","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=532"}],"version-history":[{"count":0,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/posts\/532\/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=532"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/categories?post=532"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/tags?post=532"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}