Mercurial > hg > Members > nobuyasu > scala
changeset 0:b316eec6fa7a draft default tip
add sample files
line wrap: on
line diff
--- /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" +} + +
--- /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() + +
--- /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)) + } +}
--- /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 +
--- /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) +}
--- /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") + + + + + + + + +
--- /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") +
--- /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")
--- /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)) +
--- /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"))
--- /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)
--- /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) +}
--- /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 != "")
--- /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)
--- /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() +}
--- /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)
--- /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
--- /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)
--- /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())
--- /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) +
--- /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.*") + + + + +
--- /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 => +}
--- /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") + }
--- /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 +} +
--- /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)
--- /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 +} +
--- /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) + } +} +
--- /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) + } +} + + +
--- /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 +} +
--- /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(_)) +} + + + + + +
--- /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 +
--- /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) + ) + } +} + +
--- /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() + } + } +} +
--- /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
--- /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)