Skip to content

Instantly share code, notes, and snippets.

View hossshy's full-sized avatar

Hossshy hossshy

View GitHub Profile
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 {
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")
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
package chapter4
/**
* Created by hoshi on 1/15/16.
*/
object MainObj {
val areaOfRect: (Int, Int) => Int = (width: Int, height: Int) => {
width * height
}
// Same
package chapter3.inheritance
/**
* Created by hoshi on 1/14/16.
*/
class Vehicle(speed: Int) {
val mph: Int = speed
def race() = println("Racing MPH:" + speed)
}
package chapter3
import java.util.Date
/**
* Created by hoshi on 1/13/16.
*/
object CallByName {
def nano() = {
println("Getting nano")
@hossshy
hossshy / Shape.scala
Created January 12, 2016 08:47
Chapter3 ごちゃまぜ。
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 {
// For
for (book <- booklist) {
println(book)
}
// Using Filter
for (book <- booklist if book.contains("Scala")) {
println(s"Filter $book")
}
// Variable binding in for
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)
object Print1 {
def main(args: Array[String]) {
for {i <- 1 to 10
j <- 1 to 10}
println(i * j)
}
}