/* * 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> List(1, 2, 3) map (_ + 1) res0: List[Int] = List(2, 3, 4) scala> Set(1, 2, 3) map (_ * 2) res1: scala.collection.immutable.Set[Int] = Set(2, 4, 6) scala> val xs = List(1, 2, 3, 4, 5) xs: List[Int] = List(1, 2, 3, 4, 5) scala> val git = xs grouped 3 git: Iterator[List[Int]] = non-empty iterator scala> git.next() res2: List[Int] = List(1, 2, 3) scala> git.next() res3: List[Int] = List(4, 5) scala> val sit = xs sliding 3 sit: Iterator[List[Int]] = non-empty iterator scala> sit.next() res4: List[Int] = List(1, 2, 3) scala> sit.next() res5: List[Int] = List(2, 3, 4) scala> sit.next() res6: List[Int] = List(3, 4, 5) scala> val fruit = Set("apple", "orange", "peach", "banana") fruit: scala.collection.immutable.Set[java.lang.String] = Set(apple, orange, peach, banana) scala> fruit("peach") res7: Boolean = true scala> fruit("potato") res8: Boolean = false scala> var s = Set(1, 2, 3) s: scala.collection.immutable.Set[Int] = Set(1, 2, 3) scala> s += 4; s -= 2 scala> s res9: scala.collection.immutable.Set[Int] = Set(1, 3, 4) scala> val s = collection.mutable.Set(1, 2, 3) s: scala.collection.mutable.Set[Int] = Set(1, 2, 3) scala> s += 4 res10: s.type = Set(1, 4, 2, 3) scala> s -= 2 res11: s.type = Set(1, 4, 3) scala> val myOrdering = Ordering.fromLessThan[String](_ > _) myOrdering: scala.math.Ordering[String] = scala.math.Ordering$$anon$4@49f2afad scala> import scala.collection.immutable.TreeSet import scala.collection.immutable.TreeSet scala> TreeSet.empty(myOrdering) res12: scala.collection.immutable.TreeSet[String] = TreeSet() scala> val set = TreeSet.empty[String] set: scala.collection.immutable.TreeSet[String] = TreeSet() scala> val numbers = set + ("one", "two", "three", "four") numbers: scala.collection.immutable.TreeSet[String] = TreeSet(four, one, three, two) scala> numbers range ("one", "two") res13: scala.collection.immutable.TreeSet[String] = TreeSet(one, three) scala> numbers from "three" res14: scala.collection.immutable.TreeSet[String] = TreeSet(three, two) scala> def f(x: String) = { println("taking my time."); Thread.sleep(100) x.reverse } f: (x: String)String scala> val cache = collection.mutable.Map[String, String]() cache: scala.collection.mutable.Map[String,String] = Map() scala> def cachedF(s: String) = cache.getOrElseUpdate(s, f(s)) cachedF: (s: String)String scala> cachedF("abc") taking my time. res15: String = cba scala> cachedF("abc") res16: String = cba scala> import scala.collection.mutable.{Map, SynchronizedMap, HashMap} object MapMaker { def makeMap: Map[String, String] = { new HashMap[String, String] with SynchronizedMap[String, String] { override def default(key: String) = "Why do you want to know?" } } } import scala.collection.mutable.{Map, SynchronizedMap, HashMap} defined module MapMaker scala> val capital = MapMaker.makeMap capital: scala.collection.mutable.Map[String,String] = Map() scala> capital ++= List("US" -> "Washington", "France" -> "Paris", "Japan" -> "Tokyo") res17: capital.type = Map((France,Paris), (US,Washington), (Japan,Tokyo)) scala> capital("Japan") res18: String = Tokyo scala> capital("New Zealand") res19: String = Why do you want to know? scala> capital += ("New Zealand" -> "Wellington") res20: capital.type = Map((New Zealand,Wellington), (France,Paris), (US,Washington), (Japan,Tokyo)) scala> capital("New Zealand") res21: String = Wellington scala> val str = 1 #:: 2 #:: 3 #:: Stream.empty str: scala.collection.immutable.Stream[Int] = Stream(1, ?) scala> def fibFrom(a: Int, b: Int): Stream[Int] = a #:: fibFrom(b, a + b) fibFrom: (a: Int,b: Int)Stream[Int] scala> val fibs = fibFrom(1, 1).take(7) fibs: scala.collection.immutable.Stream[Int] = Stream(1, ?) scala> fibs.toList res22: List[Int] = List(1, 1, 2, 3, 5, 8, 13) scala> val vec = scala.collection.immutable.Vector.empty vec: scala.collection.immutable.Vector[Nothing] = Vector() scala> val vec2 = vec :+ 1 :+ 2 vec2: scala.collection.immutable.Vector[Int] = Vector(1, 2) scala> val vec3 = 100 +: vec2 vec3: scala.collection.immutable.Vector[Int] = Vector(100, 1, 2) scala> vec3(0) res23: Int = 100 scala> val vec = Vector(1, 2, 3) vec: scala.collection.immutable.Vector[Int] = Vector(1, 2, 3) scala> vec updated (2, 4) res24: scala.collection.immutable.Vector[Int] = Vector(1, 2, 4) scala> vec res25: scala.collection.immutable.Vector[Int] = Vector(1, 2, 3) scala> collection.immutable.IndexedSeq(1, 2, 3) res26: scala.collection.immutable.IndexedSeq[Int] = Vector(1, 2, 3) scala> val stack = scala.collection.immutable.Stack.empty stack: scala.collection.immutable.Stack[Nothing] = Stack() scala> val hasOne = stack.push(1) hasOne: scala.collection.immutable.Stack[Int] = Stack(1) scala> stack res27: scala.collection.immutable.Stack[Nothing] = Stack() scala> hasOne.top res28: Int = 1 scala> hasOne.pop res29: scala.collection.immutable.Stack[Int] = Stack() scala> val empty = scala.collection.immutable.Queue[Int]() empty: scala.collection.immutable.Queue[Int] = Queue() scala> val has1 = empty.enqueue(1) has1: scala.collection.immutable.Queue[Int] = Queue(1) scala> val has123 = has1.enqueue(List(2, 3)) has123: scala.collection.immutable.Queue[Int] = Queue(1, 2, 3) scala> val (element, has23) = has123.dequeue element: Int = 1 has23: scala.collection.immutable.Queue[Int] = Queue(2, 3) scala> 1 to 3 res30: scala.collection.immutable.Range.Inclusive with scala.collection.immutable.Range.ByOne = Range(1, 2, 3) scala> 5 to 14 by 3 res31: scala.collection.immutable.Range = Range(5, 8, 11, 14) scala> 1 until 3 res32: scala.collection.immutable.Range with scala.collection.immutable.Range.ByOne = Range(1, 2) scala> val set = collection.immutable.TreeSet.empty[Int] set: scala.collection.immutable.TreeSet[Int] = TreeSet() scala> set + 1 + 3 + 3 res33: scala.collection.immutable.TreeSet[Int] = TreeSet(1, 3) scala> val bits = scala.collection.immutable.BitSet.empty bits: scala.collection.immutable.BitSet = BitSet() scala> val moreBits = bits + 3 + 4 + 4 moreBits: scala.collection.immutable.BitSet = BitSet(3, 4) scala> moreBits(3) res34: Boolean = true scala> moreBits(0) res35: Boolean = false scala> val map = collection.immutable.ListMap( scala> map(2) :8: error: not found: value map map(2) ^ scala> val buf = collection.mutable.ArrayBuffer.empty[Int] buf: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer() scala> buf += 1 res37: buf.type = ArrayBuffer(1) scala> buf += 10 res38: buf.type = ArrayBuffer(1, 10) scala> buf.toArray res39: Array[Int] = Array(1, 10) scala> val buf = collection.mutable.ListBuffer.empty[Int] buf: scala.collection.mutable.ListBuffer[Int] = ListBuffer() scala> buf += 1 res40: buf.type = ListBuffer(1) scala> buf += 10 res41: buf.type = ListBuffer(1, 10) scala> buf.toList res42: List[Int] = List(1, 10) scala> val buf = new StringBuilder buf: StringBuilder = StringBuilder() scala> buf += 'a' res43: buf.type = StringBuilder(a) scala> buf ++= "bcdef" res44: buf.type = StringBuilder(a, b, c, d, e, f) scala> buf.toString res45: java.lang.String = abcdef scala> val queue = new scala.collection.mutable.Queue[String] queue: scala.collection.mutable.Queue[String] = Queue() scala> queue += "a" res46: queue.type = Queue(a) scala> queue ++= List("b", "c") res47: queue.type = Queue(a, b, c) scala> queue res48: scala.collection.mutable.Queue[String] = Queue(a, b, c) scala> queue.dequeue res49: String = a scala> queue res50: scala.collection.mutable.Queue[String] = Queue(b, c) scala> val stack = new scala.collection.mutable.Stack[Int] stack: scala.collection.mutable.Stack[Int] = Stack() scala> stack.push(1) res51: stack.type = Stack(1) scala> stack res52: scala.collection.mutable.Stack[Int] = Stack(1) scala> stack.push(2) res53: stack.type = Stack(2, 1) scala> stack res54: scala.collection.mutable.Stack[Int] = Stack(2, 1) scala> stack.top res55: Int = 2 scala> stack res56: scala.collection.mutable.Stack[Int] = Stack(2, 1) scala> stack.pop res57: Int = 2 scala> stack res58: scala.collection.mutable.Stack[Int] = Stack(1) scala> val map = collection.mutable.HashMap.empty[Int,String] map: scala.collection.mutable.HashMap[Int,String] = Map() scala> map += (1 -> "make a web site") res59: map.type = Map((1,make a web site)) scala> map += (3 -> "profit!") res60: map.type = Map((1,make a web site), (3,profit!)) scala> map(1) res61: String = make a web site scala> map contains 2 res62: Boolean = false scala> val bits = scala.collection.mutable.BitSet.empty bits: scala.collection.mutable.BitSet = BitSet() scala> bits += 1 res63: bits.type = BitSet(1) scala> bits += 3 res64: bits.type = BitSet(1, 3) scala> bits res65: scala.collection.mutable.BitSet = BitSet(1, 3)