Created
January 21, 2016 08:08
-
-
Save hossshy/273c6d70a1976caf6e35 to your computer and use it in GitHub Desktop.
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") | |
case 1 => println("One") | |
case _ => println("more than one") | |
} | |
} | |
def fibonacci(in: Int): Int = in match { | |
case n if n <= 0 => 0 // guard | |
case 1 => 1 | |
case n => fibonacci(n - 1) + fibonacci(n - 2) | |
} | |
// Pattern Matching and Lists | |
def sumOdd(in: List[Int]): Int = in match { | |
case Nil => 0 | |
case x :: rest if x % 2 == 1 => x + sumOdd(rest) | |
case _ :: rest => sumOdd(rest) | |
} | |
def noPairs[T](in: List[T]): List[T] = in match { | |
case Nil => Nil | |
case a :: b :: rest if a == b => noPairs(a :: rest) | |
case a :: rest => a :: noPairs(rest) | |
} | |
def ignore(in: List[String]): List[String] = in match { | |
case Nil => Nil | |
case _ :: "ignore" :: rest => ignore(rest) | |
case x :: rest => x :: ignore (rest) | |
} | |
def getStrings(in: List[Any]): List[String] = in match { | |
case Nil => Nil | |
case (s: String) :: rest => s :: getStrings(rest) | |
case _ :: rest => getStrings(rest) | |
} | |
def main(args: Array[String]) { | |
printNum(0) | |
printNum(1) | |
printNum(2) | |
println(fibonacci(8)) | |
// List of Any Types | |
val anyList = List(1, "A", 2, 2.5, 'b', null) | |
for (m <- anyList) { | |
m match { | |
case i: Int => println("Integer: " + i) | |
case s: String => println("String: " + s) | |
case f: Double => println("Double: " + f) | |
case other: AnyRef => println("Other: " + other.getClass.getName) | |
case _ => println("Null") | |
} | |
} | |
// List (cons cell) | |
val x = 1 | |
val rest = List(2,3,4) | |
println(x :: rest) | |
println(sumOdd(x :: rest)) | |
println(noPairs(List(1,2,3,3,3,3,4,1,1))) | |
println(ignore(List("a", "b", "ignore", "c", "d", "ignore", "e"))) | |
println(getStrings(anyList)) | |
} | |
} | |
---- | |
package chapter5 | |
/** | |
* Created by hoshi on 1/21/16. | |
*/ | |
case class Person(name: String, age: Int, valid: Boolean) | |
// It caues error: case-to-case inheritance is prohibited. To overcome this limitation, use extractors to pattern match on non-leaf nodes | |
//case class MarriedPerson(override val name: String, override val age: Int, override val valid: Boolean, spouse: Person) extends Person(name, age, valid) | |
case class MarriedPerson(name: String, age: Int, valid: Boolean, spouse: Person) | |
object CaseClassesMain { | |
// Mattern matching and Case Classes | |
def older(p: Person): Option[String] = p match { | |
case Person(name, age, true) if age > 35 => Some(name) | |
case _ => None | |
} | |
// Nested | |
def mOlder(p: MarriedPerson): Option[String] = p match { | |
case MarriedPerson(name, _, _, Person(_, age, true)) | |
if age > 35 => Some(name) | |
case _ => None | |
} | |
def main(args: Array[String]) { | |
val p = Person("David", 45, true) | |
println(p.name) | |
// p.name = "hoge" name of Person is immutable | |
println("A " + older(p)) | |
println("B " + older(Person("Fred", 73, false))) | |
println("C " + older(Person("Hoge", 24, true))) | |
val sally = MarriedPerson("Sally", 24, true, p) | |
println("M " + mOlder(sally)) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment