This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package chapter5 | |
/** | |
* Created by hoshi on 1/22/16. | |
*/ | |
object MainObj2 { | |
def main(args: Array[String]) { | |
// a pattern match down to a PartialFunction[A,B] | |
val list = List("A", 2, 3, "D") | |
var tmp = list.filter(a => a match { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package chapter5 | |
/** | |
* Created by hoshi on 1/20/16. | |
*/ | |
object MainObj { | |
// Basic Pattern mach | |
def printNum(int:Int): Unit = { | |
int match { | |
case 0 => println("Zero") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package chapter4 | |
import java.io.{FileReader, BufferedReader} | |
sealed trait Expr // sealed means only the class in this source file can extend it | |
case class Add(left:Expr, right:Expr) extends Expr | |
case class Mul(left:Expr, right:Expr) extends Expr | |
case class Val(value:Int) extends Expr | |
case class Var(name:String) extends Expr |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package chapter4 | |
/** | |
* Created by hoshi on 1/15/16. | |
*/ | |
object MainObj { | |
val areaOfRect: (Int, Int) => Int = (width: Int, height: Int) => { | |
width * height | |
} | |
// Same |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package chapter3.inheritance | |
/** | |
* Created by hoshi on 1/14/16. | |
*/ | |
class Vehicle(speed: Int) { | |
val mph: Int = speed | |
def race() = println("Racing MPH:" + speed) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package chapter3 | |
import java.util.Date | |
/** | |
* Created by hoshi on 1/13/16. | |
*/ | |
object CallByName { | |
def nano() = { | |
println("Getting nano") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package chapter3 | |
/** | |
* Created by hoshi on 1/12/16. | |
*/ | |
class Shape { | |
def area:Double = 0.0 | |
} | |
class Rectangle(val width:Double, val height:Double) extends Shape { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// For | |
for (book <- booklist) { | |
println(book) | |
} | |
// Using Filter | |
for (book <- booklist if book.contains("Scala")) { | |
println(s"Filter $book") | |
} | |
// Variable binding in for |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
object Chapter2 { | |
def main(args: Array[String]) { | |
// declare functions | |
def hello() = { "Hello World!" } | |
println(hello) | |
def hello2():String = { "Hello World2!" } | |
println(hello2) | |
def hello3 = "Hello World3!" | |
println(hello3) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
object Print1 { | |
def main(args: Array[String]) { | |
for {i <- 1 to 10 | |
j <- 1 to 10} | |
println(i * j) | |
} | |
} |