# HG changeset patch # User Nobuyasu Oshiro # Date 1357630906 -32400 # Node ID b316eec6fa7a94e4346c7af642bfe322d2a6c425 add sample files diff -r 000000000000 -r b316eec6fa7a chapter10/Element.scala --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/chapter10/Element.scala Tue Jan 08 16:41:46 2013 +0900 @@ -0,0 +1,61 @@ +object Element { + private class ArrayElement( + val contents: Array[String] + ) extends Element + private class LineElement(s: String) extends Element { + val contents = Array(s) + override def width = s.length + override def height = 1 + } + private class UniformElement( + ch: Char, + override val width: Int, + override val height: Int + ) extends Element { + private val line = ch.toString * width + def contents = Array.make(height, line) + } + def elem(contents: Array[String]): Element = + new ArrayElement(contents) + def elem(chr: Char, width: Int, height: Int): Element = + new UniformElement(chr, width, height) + def elem(line: String): Element = + new LineElement(line) +} + +import Element.elem +abstract class Element { + def contents: Array[String] + def width: Int = + if (height == 0) 0 else contents(0).length + def height: Int = contents.length + def above(that: Element): Element = { + val this1 = this widen that.width + val that1 = that widen this.width + elem(this1.contents ++ that1.contents) + } + def beside(that: Element): Element = { + val this1 = this heighten that.height + val that1 = that heighten this.height + elem ( + for ((line1, line2) <- this1.contents zip that1.contents) + yield line1 + line2) + } + def widen(w: Int): Element = + if (w <= width) this + else { + val left = elem(' ', (w - width) / 2, height) + val right = elem(' ', w- width - left.width, height) + left beside this beside right + } + def heighten(h: Int): Element = + if (h <= height) this + else { + val top = elem(' ', width, (h - height) / 2) + var bot = elem(' ', width, h - height - top.height) + top above this above bot + } + override def toString = contents mkString "\n" +} + + diff -r 000000000000 -r b316eec6fa7a chapter10/Element_main.scala --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/chapter10/Element_main.scala Tue Jan 08 16:41:46 2013 +0900 @@ -0,0 +1,90 @@ +import Element.elem + +abstract class Element { + def contents: Array[String] + val height: Int = contents.length + val width: Int = contents.length + if (height == 0) 0 else contents(0).length + def demo() { + println("Element's implementation invoked") + } + def above(that: Element): Element = + elem(this.contents ++ that.contents) + def beside(that: Element): Element = { + elem ( + for ( + (line1, line2) <- this.contents zip that.contents + ) yield line1 + line2 + ) + } + override def toString = contents mkString "\n" +} + + + +class ArrayElement(conts: Array[String]) extends Element { + def contents: Array[String] = conts + final override def demo() { + println("ArrayElement's implementation invoked") + } +} + +class LineElement(s: String) extends ArrayElement(Array(s)) { + override def width = s.length + override def height = 1 + override def main() { + println("LineElement's implementation invoked") + } +} + +/* + class LineElement(s: String) extends Element { + val contents = Array(s) + override def width = s.length + override def height = 1 + } + */ + +class Cat { + val dengerous = false +} + +class Tiger { + override val dangerous: Boolean, + private var age: Int +} extends Cat + +class Tiger(param1: Boolean, param2: Int) extends Cat { + override val dengerous = param1 + private var age = param2 +} + +class UniformElement { + ch: Char, + override val width: Int, + override val height +} extends Element { + private val line = ch.toString * width + def contents = Array.make(height, line) +} + +def invokeDemo(e: Element) { + e.demo() +} + + +def main() { + val ae = new ArrayElement(Array("hello", "world")) + println(ae.width) + val e: Element = new ArrayElement(Array("hello")) + println(e.width) + + val e1: Element = new ArrayElement(Array("hello", "world")) + val ae: ArrayElement = new LienElement("hello") + val e2: Element = ae + val e3: Element= new UniformElement(Element('x', 2, 3)) +} + +main() + + diff -r 000000000000 -r b316eec6fa7a chapter10/Spiral.scala --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/chapter10/Spiral.scala Tue Jan 08 16:41:46 2013 +0900 @@ -0,0 +1,27 @@ +import Element.elem + +object Spiral { + val space = elem(" ") + val corner = elem("+") + def spiral(nEdges: Int, direction: Int): Element = { + if (nEdges == 1) + elem("+") + else { + val sp = spiral(nEdges - 1, (direction + 3) % 4) + def verticalBar = elem('|', 1, sp.height) + def horizontalBar = elem('-', sp.width, 1) + if (direction == 0) + (corner beside horizontalBar) above (sp beside space) + else if (direction == 1) + (sp above space) beside (corner above verticalBar) + else if (direction == 2) + (space beside sp) above (horizontalBar beside corner) + else + (verticalBar above corner) beside (space above sp) + } + } + def main(args: Array[String]) { + val nSides = args(0).toInt + println(spiral(nSides, 0)) + } +} diff -r 000000000000 -r b316eec6fa7a chapter12/Doubling.scala --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/chapter12/Doubling.scala Tue Jan 08 16:41:46 2013 +0900 @@ -0,0 +1,15 @@ +abstract class IntQueue { + def get(): Int + def put(x: Int) +} + +trait Doubling extends IntQueue { + abstract override def put(x: Int) { super.put(2 * x) } +} + +class Animal +trait Furry extends Animal +trait HasLegs extends Animal +trait FourLegged extends HasLegs +class Cat extends Animal with Furry with FourLegged + diff -r 000000000000 -r b316eec6fa7a chapter12/PhilosophicalTrait.scala --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/chapter12/PhilosophicalTrait.scala Tue Jan 08 16:41:46 2013 +0900 @@ -0,0 +1,19 @@ +trait Philosophical { + def philosphize() { + println("I consume memory, therefore I am!") + } +} + +class Frog extends Philosphical { + override def toString = "green" +} + +class Animal +class Frog extends Animal with Philosophical { + override def toString = "green" +} + +class Rational(n: Int, d: Int) extends Ordered[Rational] { + def compare(that: Rational) = + (this.number * that.demon) - (that.number * this.denom) +} diff -r 000000000000 -r b316eec6fa7a chapter3/fileRead.scala --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/chapter3/fileRead.scala Tue Jan 08 16:41:46 2013 +0900 @@ -0,0 +1,17 @@ +import scala.io.Source + +if (args.length > 0) { + + for (line <- Source.fromFile(args(0)).getLines) + println(line.length + " "+ line) +} else + Console.err.println("Please enter filename") + + + + + + + + + diff -r 000000000000 -r b316eec6fa7a chapter3/fileRead2.scala --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/chapter3/fileRead2.scala Tue Jan 08 16:41:46 2013 +0900 @@ -0,0 +1,17 @@ +import scala.io.Source +def widthOfLength(s: String) = s.length.toString.length +if (args.length > 0) { + val lines = Source.fromFile(args(0)).getLines.toList + val longestLine = lines.reduceLeft( + (a, b) => if (a.length > b.length) a else b + ) + val maxWidth = widthOfLength(longestLine) + for (line <- lines) { + val numSpaces = maxWidth - widthOfLength(line) + val padding = " " * numSpaces + println(padding + line.length +" | " + line) + } +} +else + Console.err.println("Please enter filename") + diff -r 000000000000 -r b316eec6fa7a chapter3/hashSetImmutable.scala --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/chapter3/hashSetImmutable.scala Tue Jan 08 16:41:46 2013 +0900 @@ -0,0 +1,3 @@ +import scala.collection.immutable.HashSet +val hashSet = HashSet("Tomatoes", "Chiles") +println(hashSet + "Coriander") diff -r 000000000000 -r b316eec6fa7a chapter3/mapMutable.scala --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/chapter3/mapMutable.scala Tue Jan 08 16:41:46 2013 +0900 @@ -0,0 +1,12 @@ +import scala.collection.mutable.Map +val treasureMap = Map[Int, String]() +treasureMap += (1 -> "Go to island.") +treasureMap += (2 -> "Find big X on ground.") +treasureMap += (3 -> "Dig.") +println(treasureMap(2)) + +val romanNumeral = Map( + 1 -> "I", 2 -> "II", 3 -> "III", 4 -> "IV", 5 -> "V" +) +println(romanNumeral(4)) + diff -r 000000000000 -r b316eec6fa7a chapter3/setExample.scala --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/chapter3/setExample.scala Tue Jan 08 16:41:46 2013 +0900 @@ -0,0 +1,4 @@ + +var jetSet = Set("Boeing", "Airbus") +jetSet += "Lear" +println(jetSet.contains("Cessna")) diff -r 000000000000 -r b316eec6fa7a chapter3/setMutable.scala --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/chapter3/setMutable.scala Tue Jan 08 16:41:46 2013 +0900 @@ -0,0 +1,4 @@ +import scala.collection.mutable.Set +val movieSet = Set("Hitch", "Poltergeist") +movieSet += "Sharek" +println(movieSet) diff -r 000000000000 -r b316eec6fa7a chapter3/while.scala --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/chapter3/while.scala Tue Jan 08 16:41:46 2013 +0900 @@ -0,0 +1,17 @@ +def printArgs(args: Array[String]): Unit = { + var i = 0 + while (i < args.length) { + println(args(i)) + i += 1 + } +} + + +def printArgsFunctional(args: Array[String]): Unit = { + for (arg <- args) + println(arg) +} + +def printArgsFunctional2(args: Array[String]): Unit = { + args.foreach(println) +} diff -r 000000000000 -r b316eec6fa7a chapter7/do_while.scala --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/chapter7/do_while.scala Tue Jan 08 16:41:46 2013 +0900 @@ -0,0 +1,5 @@ +var line = "" +do { + line = readLine() + println("Read: "+ line) +} while (line != "") diff -r 000000000000 -r b316eec6fa7a chapter7/filter.scala --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/chapter7/filter.scala Tue Jan 08 16:41:46 2013 +0900 @@ -0,0 +1,13 @@ +val filesHere = (new java.io.File(".")).listFiles +for (file <- filesHere if file.getName.endsWith(".scala")) + println(file) + +for (file <- filesHere) + if (file.getName.endsWith(".scala")) + println(file) + +for ( + file <- filesHere + if file.isFile; + if file.getName.endsWith(".scala") +) println(file) diff -r 000000000000 -r b316eec6fa7a chapter7/finally.scala --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/chapter7/finally.scala Tue Jan 08 16:41:46 2013 +0900 @@ -0,0 +1,7 @@ +import java.io.FileReader +val file = new FileReader("input.txt") +try { + // +} finally { + file.close() +} diff -r 000000000000 -r b316eec6fa7a chapter7/for.scala --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/chapter7/for.scala Tue Jan 08 16:41:46 2013 +0900 @@ -0,0 +1,3 @@ +val fileHere = (new java.io.File(".")).listFiles +for (file <- fileHere) + println(file) diff -r 000000000000 -r b316eec6fa7a chapter7/input.txt --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/chapter7/input.txt Tue Jan 08 16:41:46 2013 +0900 @@ -0,0 +1,1 @@ +aaa diff -r 000000000000 -r b316eec6fa7a chapter7/match.scala --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/chapter7/match.scala Tue Jan 08 16:41:46 2013 +0900 @@ -0,0 +1,18 @@ +val firstArg = if (args.length > 0) args(0) else "" +firstArg match { + case "salt" => println("pepper") + case "chips" => println("salsa") + case "eggs" => println("bacon") + case _ => println("huh?") +} + + +val firstArg2 = if (!args.isEmpty) args(0) else "" +val friend = +firstArg2 match { + case "salt" => "pepper" + case "chips" => "salsa" + case "eggs" => "bacon" + case _ => "huh?" +} +println(friend) diff -r 000000000000 -r b316eec6fa7a chapter7/multiTable.scala --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/chapter7/multiTable.scala Tue Jan 08 16:41:46 2013 +0900 @@ -0,0 +1,17 @@ +def makeRowSeq(row: Int) = + for (col <- 1 to 10) yield { + val prod = (row * col).toString + val padding = " " * (4 - prod.length) + padding + prod + } + +def makeRow(row: Int) = makeRowSeq(row).mkString + +def multiTable() = { + val tableSeq = + for ( row <- 1 to 10) + yield makeRow(row) + tableSeq.mkString("\n") +} + +println(multiTable()) diff -r 000000000000 -r b316eec6fa7a chapter7/recursive.scala --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/chapter7/recursive.scala Tue Jan 08 16:41:46 2013 +0900 @@ -0,0 +1,7 @@ +def searchFrom(i: Int): Int = + if (i >= args.length) -1 + else if (args(i).startsWith("-")) searchFrom(i+1) + else if (args(i).endsWith(".scala")) i + else searchFrom(i + 1) +val i = searchFrom(0) + diff -r 000000000000 -r b316eec6fa7a chapter7/someGenerator.scala --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/chapter7/someGenerator.scala Tue Jan 08 16:41:46 2013 +0900 @@ -0,0 +1,26 @@ +val filesHere = (new java.io.File(".")).listFiles +def fileLines(file: java.io.File) = + scala.io.Source.fromFile(file).getLines.toList +def grep(pattern: String) = + for ( + file <- filesHere + if file.getName.endsWith(".scala"); + line <- fileLines(file) + if line.trim.matches(pattern) + ) println(file +": "+ line.trim) +grep(".*gcd.*") + +def grep2(pattern: String) = + for { + file <- filesHere + if file.getName.endsWith(".scala") + line <- fileLines(file) + trimmed = line.trim + if trimmed.matches(pattern) + } println(file +": "+ trimmed) +grep2(".*gcd.*") + + + + + diff -r 000000000000 -r b316eec6fa7a chapter7/try.scala --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/chapter7/try.scala Tue Jan 08 16:41:46 2013 +0900 @@ -0,0 +1,10 @@ +import java.io.FileReader +import java.io.FileNotFoundException +import java.io.IOException + +try { + val f = new FileReader("input.txt") +} catch { + case ex: FileNotFoundException => + case ex: IOException => +} diff -r 000000000000 -r b316eec6fa7a chapter7/trycatchfinally.scala --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/chapter7/trycatchfinally.scala Tue Jan 08 16:41:46 2013 +0900 @@ -0,0 +1,10 @@ +import java.net.URL +import java.net.MalformedURLException + +def urlFor(path: String) = + try { + new URL(path) + } catch { + case e: MalformedURLException = > + new URL("http://www.scala-lang.org") + } diff -r 000000000000 -r b316eec6fa7a chapter7/while.scala --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/chapter7/while.scala Tue Jan 08 16:41:46 2013 +0900 @@ -0,0 +1,11 @@ +def gcdLoop(x: Long, y: Long): Long = { + var a = x + var b = y + while (a != 0) { + val temp = a + a = b % a + b = temp + } + b +} + diff -r 000000000000 -r b316eec6fa7a chapter7/yield.scala --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/chapter7/yield.scala Tue Jan 08 16:41:46 2013 +0900 @@ -0,0 +1,21 @@ +val filesHere = (new java.io.File(".")).listFiles +def fileLines(file: java.io.File) = + scala.io.Source.fromFile(file).getLines.toList +def scalaFiles = + for { + file <- filesHere + if file.getName.endsWith(".scala") + } yield file + +for (f <- scalaFiles) println(f.getName) + +val forLineLengths = + for { + file <- filesHere + if file.getName.endsWith(".scala") + line <- fileLines(file) + trimmed = line.trim + if trimmed.matches(".*for.*") + } yield trimmed.length + +for (i <- forLineLengths) println(i) diff -r 000000000000 -r b316eec6fa7a chapter8/Approximate.scala --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/chapter8/Approximate.scala Tue Jan 08 16:41:46 2013 +0900 @@ -0,0 +1,11 @@ +def approximate(guess: Double): Double = + if (isGoodEnough(guess)) guess + else approximate(improve(guess)) + +def approximateLoop(initialGuess: Double) Double = { + var guess = initialGuess + while (!isGoodEnough(guess)) + guess = improve(guess) + guess +} + diff -r 000000000000 -r b316eec6fa7a chapter8/LongLines.scala --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/chapter8/LongLines.scala Tue Jan 08 16:41:46 2013 +0900 @@ -0,0 +1,21 @@ +import scala.io.Source +object LongLines { + def processFile(filename: String, width: Int ) { + val source = Source.fromFile(filename) + for (line <- source.getLines) + processLine(filename, width, line) + } + private def processLine(filename: String, width: Int, line: String) { + if (line.length > width) + println(filename + ": "+ line.trim) + } +} + +object FindLongLines { + def main(args: Array[String]) { + val width = args(0).toInt + for (arg <- args.drop(1)) + LongLines.processFile(arg, width) + } +} + diff -r 000000000000 -r b316eec6fa7a chapter8/LongLines2.scala --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/chapter8/LongLines2.scala Tue Jan 08 16:41:46 2013 +0900 @@ -0,0 +1,15 @@ +import scala.io.Source +object LongLines { + def processFile(filename: String, width: Int) { + def processLine(line: String) { + if (line.length > width) + print(filename +": "+ line) + } + val source = Source.fromFile(filename) + for (line <- source.getLines) + printLine(line) + } +} + + + diff -r 000000000000 -r b316eec6fa7a chapter9/ContainsNeg.scala --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/chapter9/ContainsNeg.scala Tue Jan 08 16:41:46 2013 +0900 @@ -0,0 +1,8 @@ +def containsNeg(nums: List[Int]): Boolean = { + var exists = false + for (num <- nums) + if (num < 0) + exists = true + exists +} + diff -r 000000000000 -r b316eec6fa7a chapter9/FileMatcher.scala --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/chapter9/FileMatcher.scala Tue Jan 08 16:41:46 2013 +0900 @@ -0,0 +1,31 @@ +object FileMatcher { + private def filesHere = (new java.io.File(".")).listFiles + /* + def filesEnding(query: String) = + for (file <- filesHere; if file.getName.endsWith(query)) + yield file + def filesContaining(query: String) = + for (file <- filesHere; if file.getName.contains(query)) + yield file + def filesRexex(query: String) = + for (file <- filesHere; if file.getName.matches(query)) + yield file + */ + def filesMatching(query: String, + matcher: (String, String) => Boolean) = { + for (file <- filesHere; if matcher(file.getName, query)) + yield file + } + def filesEnding(query: String) = + filesMatching(query, _.endsWith(_)) + def filesContaining(query: String) = + filesMatchin(query, _.contains(_)) + def filesRegex(query: String) = + filesMatching(query, _.matches(_)) +} + + + + + + diff -r 000000000000 -r b316eec6fa7a chapter9/MyAssertion.scala --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/chapter9/MyAssertion.scala Tue Jan 08 16:41:46 2013 +0900 @@ -0,0 +1,9 @@ +var assertionsEnabled = true +def myAssert(predicate: () => Boolean) = + if (assertionsEnabled && !predicate()) + throw new AssertionError + +def byNameAssert(predicate: => Boolean) = + if (assertionsEnabled && !predicate) + throw new AssertionError + diff -r 000000000000 -r b316eec6fa7a chapter9/WithPrintWriter.scala --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/chapter9/WithPrintWriter.scala Tue Jan 08 16:41:46 2013 +0900 @@ -0,0 +1,21 @@ +import java.io.File +import java.io.PrintWriter + +object WithPrintWriter { + def withPrintWriter(file: File, op: PrintWriter => Unit) { + val writer = new PrintWriter(file) + try { + op(writer) + } finally { + writer.close() + } + } + def main(args: Array[String]) { + withPrintWriter( + new File("date.txt"), + writer => writer.println(new java.util.Date) + ) + } +} + + diff -r 000000000000 -r b316eec6fa7a chapter9/WithPrintWriter2.scala --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/chapter9/WithPrintWriter2.scala Tue Jan 08 16:41:46 2013 +0900 @@ -0,0 +1,19 @@ +object WithPrintWriter2 { + + def main(args: Array[String]) { + val fiel =new File("data.txt") + withPrintWriter(file) { + writer => writer.println(new java.util.Data) + } + } + + def withPrintWriter(file: File)(op: PrintWriter => Unit) { + val writer = new PrintWriter(file) + try { + op(writer) + } finally { + writer.close() + } + } +} + diff -r 000000000000 -r b316eec6fa7a chapter9/date.txt --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/chapter9/date.txt Tue Jan 08 16:41:46 2013 +0900 @@ -0,0 +1,1 @@ +Sun Dec 09 00:34:23 JST 2012 diff -r 000000000000 -r b316eec6fa7a printargs.scala --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/printargs.scala Tue Jan 08 16:41:46 2013 +0900 @@ -0,0 +1,16 @@ +var i = 0 +println("imperative style") +while (i < args.length) { + println(args(i)) + i += 1 +} +println("") +println("functional style") +args.foreach(arg => println(arg)) +args.foreach((arg: String) => println(arg)) +args.foreach(println) + +println("") +println("for statement") +for (arg <- args) + println(arg) diff -r 000000000000 -r b316eec6fa7a test/JavaTest.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test/JavaTest.java Tue Jan 08 16:41:46 2013 +0900 @@ -0,0 +1,9 @@ +class JavaTest { + public static void main(String[] args) { + int i = 0; + while ( i < args.length ) { + System.out.println(args[i]); + i++; + } + } +} diff -r 000000000000 -r b316eec6fa7a test/test.scala --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test/test.scala Tue Jan 08 16:41:46 2013 +0900 @@ -0,0 +1,12 @@ +object test { + def main(args: Array[String]) { + args.foreach(arg => println(arg)) +/* + var i = 0 + while (i < args.length) { + println(args(i)) + i += 1 + } +*/ + } +}