/* * 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> case class Person(name: String, isMale: Boolean, children: Person*) defined class Person scala> val lara = Person("Lara", false) val bob = Person("Bob", true) val julie = Person("Julie", false, lara, bob) val persons = List(lara, bob, julie) lara: Person = Person(Lara,false,WrappedArray()) bob: Person = Person(Bob,true,WrappedArray()) julie: Person = Person(Julie,false,WrappedArray(Person(Lara,false,WrappedArray()), Person(Bob,true,WrappedArray()))) persons: List[Person] = List(Person(Lara,false,WrappedArray()), Person(Bob,true,WrappedArray()), Person(Julie,false,WrappedArray(Person(Lara,false,WrappedArray()), Person(Bob,true,WrappedArray())))) scala> persons filter (p => !p.isMale) flatMap (p => (p.children map (c => (p.name, c.name)))) res0: List[(String, String)] = List((Julie,Lara), (Julie,Bob)) scala> persons withFilter (p => !p.isMale) flatMap (p => (p.children map (c => (p.name, c.name)))) res1: List[(String, String)] = List((Julie,Lara), (Julie,Bob)) scala> for (p <- persons; if !p.isMale; c <- p.children) yield (p.name, c.name) res2: List[(String, String)] = List((Julie,Lara), (Julie,Bob)) scala> for (x <- List(1, 2); y <- List("one", "two")) yield (x, y) res3: List[(Int, java.lang.String)] = List((1,one), (1,two), (2,one), (2,two)) scala> case class Book(title: String, authors: String*) defined class Book scala> val books: List[Book] = List( Book( "Structure and Interpretation of Computer Programs", "Abelson, Harold", "Sussman, Gerald J." ), Book( "Principles of Compiler Design", "Aho, Alfred", "Ullman, Jeffrey" ), Book( "Programming in Modula-2", "Wirth, Niklaus" ), Book( "Elements of ML Programming", "Ullman, Jeffrey" ), Book( "The Java Language Specification", "Gosling, James", "Joy, Bill", "Steele, Guy", "Bracha, Gilad" ) ) books: List[Book] = List(Book(Structure and Interpretation of Computer Programs,WrappedArray(Abelson, Harold, Sussman, Gerald J.)), Book(Principles of Compiler Design,WrappedArray(Aho, Alfred, Ullman, Jeffrey)), Book(Programming in Modula-2,WrappedArray(Wirth, Niklaus)), Book(Elements of ML Programming,WrappedArray(Ullman, Jeffrey)), Book(The Java Language Specification,WrappedArray(Gosling, James, Joy, Bill, Steele, Guy, Bracha, Gilad))) scala> for (b <- books; a <- b.authors if a startsWith "Gosling") yield b.title res4: List[String] = List(The Java Language Specification) scala> for (b <- books if (b.title indexOf "Program") >= 0) yield b.title res5: List[String] = List(Structure and Interpretation of Computer Programs, Programming in Modula-2, Elements of ML Programming) scala> for (b1 <- books; b2 <- books if b1 != b2; a1 <- b1.authors; a2 <- b2.authors if a1 == a2) yield a1 res6: List[String] = List(Ullman, Jeffrey, Ullman, Jeffrey) scala> def removeDuplicates[A](xs: List[A]): List[A] = { if (xs.isEmpty) xs else xs.head :: removeDuplicates( xs.tail filter (x => x != xs.head) ) } removeDuplicates: [A](xs: List[A])List[A] scala> removeDuplicates(res6) res7: List[String] = List(Ullman, Jeffrey)