/* * Copyright (C) 2007-2008 Artima, Inc. All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * * Automatically generated Scala interpreter transcript from: * * Programming in Scala (First Edition, Version 6) * by Martin Odersky, Lex Spoon, Bill Venners * * http://booksites.artima.com/programming_in_scala */ scala> val fruit: List[String] = List("apples", "oranges", "pears") val nums: List[Int] = List(1, 2, 3, 4) val diag3: List[List[Int]] = List( List(1, 0, 0), List(0, 1, 0), List(0, 0, 1) ) val empty: List[Nothing] = List() fruit: List[String] = List(apples, oranges, pears) nums: List[Int] = List(1, 2, 3, 4) diag3: List[List[Int]] = List(List(1, 0, 0), List(0, 1, 0), List(0, 0, 1)) empty: List[Nothing] = List() scala> val List(a, b, c) = fruit a: String = apples b: String = oranges c: String = pears scala> val a :: b :: rest = fruit a: String = apples b: String = oranges rest: List[String] = List(pears) scala> List(1, 2) ::: List(3, 4, 5) res0: List[Int] = List(1, 2, 3, 4, 5) scala> List() ::: List(1, 2, 3) res1: List[Int] = List(1, 2, 3) scala> List(1, 2, 3) ::: List(4) res2: List[Int] = List(1, 2, 3, 4) scala> List(1, 2, 3).length res3: Int = 3 scala> val abcde = List('a', 'b', 'c', 'd', 'e') abcde: List[Char] = List(a, b, c, d, e) scala> abcde.last res4: Char = e scala> abcde.init res5: List[Char] = List(a, b, c, d) scala> abcde.reverse res6: List[Char] = List(e, d, c, b, a) scala> abcde res7: List[Char] = List(a, b, c, d, e) scala> abcde take 2 res8: List[Char] = List(a, b) scala> abcde drop 2 res9: List[Char] = List(c, d, e) scala> abcde splitAt 2 res10: (List[Char], List[Char]) = (List(a, b),List(c, d, e)) scala> abcde apply 2 // rare in Scala res11: Char = c scala> abcde(2) // rare in Scala res12: Char = c scala> abcde.indices res13: List[Int] = List(0, 1, 2, 3, 4) scala> abcde.indices zip abcde res14: List[(Int, Char)] = List((0,a), (1,b), (2,c), (3,d), (4,e)) scala> val zipped = abcde zip List(1, 2, 3) zipped: List[(Char, Int)] = List((a,1), (b,2), (c,3)) scala> abcde.zipWithIndex res15: List[(Char, Int)] = List((a,0), (b,1), (c,2), (d,3), (e,4)) scala> abcde.toString res16: String = List(a, b, c, d, e) scala> abcde mkString ("[", ",", "]") res17: String = [a,b,c,d,e] scala> abcde mkString "" res18: String = abcde scala> abcde.mkString res19: String = abcde scala> abcde mkString ("List(", ", ", ")") res20: String = List(a, b, c, d, e) scala> val buf = new StringBuilder buf: StringBuilder = scala> abcde addString (buf, "(", ";", ")") res21: StringBuilder = (a;b;c;d;e) scala> val arr = abcde.toArray arr: Array[Char] = Array(a, b, c, d, e) scala> arr.toString res22: String = Array(a, b, c, d, e) scala> arr.toList res23: List[Char] = List(a, b, c, d, e) scala> val arr2 = new Array[Int](10) arr2: Array[Int] = Array(0, 0, 0, 0, 0, 0, 0, 0, 0, 0) scala> List(1, 2, 3) copyToArray (arr2, 3) scala> arr2.toString res25: String = Array(0, 0, 0, 1, 2, 3, 0, 0, 0, 0) scala> val it = abcde.elements it: Iterator[Char] = non-empty iterator scala> it.next res26: Char = a scala> it.next res27: Char = b scala> def msort[T](less: (T, T) => Boolean) (xs: List[T]): List[T] = { def merge(xs: List[T], ys: List[T]): List[T] = (xs, ys) match { case (Nil, _) => ys case (_, Nil) => xs case (x :: xs1, y :: ys1) => if (less(x, y)) x :: merge(xs1, ys) else y :: merge(xs, ys1) } val n = xs.length / 2 if (n == 0) xs else { val (ys, zs) = xs splitAt n merge(msort(less)(ys), msort(less)(zs)) } } msort: [T]((T, T) => Boolean)(List[T])List[T] scala> msort((x: Int, y: Int) => x < y)(List(5, 7, 1, 3)) res28: List[Int] = List(1, 3, 5, 7) scala> val intSort = msort((x: Int, y: Int) => x < y) _ intSort: (List[Int]) => List[Int] = scala> List(1, 2, 3) map (_ + 1) res29: List[Int] = List(2, 3, 4) scala> val words = List("the", "quick", "brown", "fox") words: List[java.lang.String] = List(the, quick, brown, fox) scala> words map (_.length) res30: List[Int] = List(3, 5, 5, 3) scala> words map (_.toList.reverse.mkString) res31: List[String] = List(eht, kciuq, nworb, xof) scala> words map (_.toList) res32: List[List[Char]] = List(List(t, h, e), List(q, u, i, c, k), List(b, r, o, w, n), List(f, o, x)) scala> words flatMap (_.toList) res33: List[Char] = List(t, h, e, q, u, i, c, k, b, r, o, w, n, f, o, x) scala> List.range(1, 5) flatMap ( i => List.range(1, i) map (j => (i, j)) ) res34: List[(Int, Int)] = List((2,1), (3,1), (3,2), (4,1), (4,2), (4,3)) scala> var sum = 0 sum: Int = 0 scala> List(1, 2, 3, 4, 5) foreach (sum += _) scala> sum res36: Int = 15 scala> List(1, 2, 3, 4, 5) filter (_ % 2 == 0) res37: List[Int] = List(2, 4) scala> words filter (_.length == 3) res38: List[java.lang.String] = List(the, fox) scala> List(1, 2, 3, 4, 5) partition (_ % 2 == 0) res39: (List[Int], List[Int]) = (List(2, 4),List(1, 3, 5)) scala> List(1, 2, 3, 4, 5) find (_ % 2 == 0) res40: Option[Int] = Some(2) scala> List(1, 2, 3, 4, 5) find (_ <= 0) res41: Option[Int] = None scala> List(1, 2, 3, -4, 5) takeWhile (_ > 0) res42: List[Int] = List(1, 2, 3) scala> words dropWhile (_ startsWith "t") res43: List[java.lang.String] = List(quick, brown, fox) scala> List(1, 2, 3, -4, 5) span (_ > 0) res44: (List[Int], List[Int]) = (List(1, 2, 3),List(-4, 5)) scala> def hasZeroRow(m: List[List[Int]]) = m exists (row => row forall (_ == 0)) hasZeroRow: (List[List[Int]])Boolean scala> hasZeroRow(diag3) res45: Boolean = false scala> def sum(xs: List[Int]): Int = (0 /: xs) (_ + _) sum: (List[Int])Int scala> def product(xs: List[Int]): Int = (1 /: xs) (_ * _) product: (List[Int])Int scala> ("" /: words) (_ +" "+ _) res46: java.lang.String = the quick brown fox scala> (words.head /: words.tail) (_ +" "+ _) res47: java.lang.String = the quick brown fox scala> def flattenLeft[T](xss: List[List[T]]) = (List[T]() /: xss) (_ ::: _) def flattenRight[T](xss: List[List[T]]) = (xss :\ List[T]()) (_ ::: _) flattenLeft: [T](List[List[T]])List[T] flattenRight: [T](List[List[T]])List[T] scala> def flattenRight[T](xss: List[List[T]]) = (xss :\ List()) (_ ::: _) :15: error: type mismatch; found : List[T] required: List[Nothing] (xss :\ List()) (_ ::: _) ^ scala> List(1, -3, 4, 2, 6) sort (_ < _) res48: List[Int] = List(-3, 1, 2, 4, 6) scala> words sort (_.length > _.length) res49: List[java.lang.String] = List(quick, brown, fox, the) scala> List.apply(1, 2, 3) res50: List[Int] = List(1, 2, 3) scala> List.range(1, 5) res51: List[Int] = List(1, 2, 3, 4) scala> List.range(1, 9, 2) res52: List[Int] = List(1, 3, 5, 7) scala> List.range(9, 1, -3) res53: List[Int] = List(9, 6, 3) scala> List.make(5, 'a') res54: List[Char] = List(a, a, a, a, a) scala> List.make(3, "hello") res55: List[java.lang.String] = List(hello, hello, hello) scala> val zipped = "abcde".toList zip List(1, 2, 3) zipped: List[(Char, Int)] = List((a,1), (b,2), (c,3)) scala> List.unzip(zipped) res56: (List[Char], List[Int]) = (List(a, b, c),List(1, 2, 3)) scala> val xss = List(List('a', 'b'), List('c'), List('d', 'e')) xss: List[List[Char]] = List(List(a, b), List(c), List(d, e)) scala> List.flatten(xss) res57: List[Char] = List(a, b, c, d, e) scala> List.concat(List('a', 'b'), List('c')) res58: List[Char] = List(a, b, c) scala> List.concat(List(), List('b'), List('c')) res59: List[Char] = List(b, c) scala> List.concat() res60: List[Nothing] = List() scala> List.map2(List(10, 20), List(3, 4, 5)) (_ * _) res61: List[Int] = List(30, 80) scala> List.forall2(List("abc", "de"), List(3, 2)) (_.length == _) res62: Boolean = true scala> List.exists2(List("abc", "de"), List(3, 2)) (_.length != _) res63: Boolean = false scala> msort((x: Char, y: Char) => x > y)(abcde) res64: List[Char] = List(e, d, c, b, a) scala> abcde sort (_ > _) res65: List[Char] = List(e, d, c, b, a) scala> msort[Char](_ > _)(abcde) res66: List[Char] = List(e, d, c, b, a) scala> def msortSwapped[T](xs: List[T])(less: (T, T) => Boolean): List[T] = msort(less)(xs) msortSwapped: [T](List[T])((T, T) => Boolean)List[T] scala> msortSwapped(abcde)(_ > _) res67: List[Char] = List(e, d, c, b, a)