Last active
May 17, 2018 17:13
-
-
Save xodhx4/159dbbe586274c925d310d52af086996 to your computer and use it in GitHub Desktop.
Map(dictionary) initialize and pattern matching example
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
// initialize scala mutable map | |
var book = collection.mutable.Map[String,String]() | |
book += ("Key" => "Value") | |
// initialize scala immutable map | |
val book2 = Map("Tap" -> "name") | |
def printtuple(str: String, bookmap: collection.mutable.Map[String,String]) { | |
// Get scala Map value from Key | |
val number = bookmap.get(str) | |
number match { | |
// If there are no value for key, then return None | |
case None => println("Not found") | |
// get() will return like Some("Value"), so pattern matching like this | |
case Some(n) => println(s"$str=$n") | |
} | |
} |
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
def pascalLine(origin: List[Int], next: List[Int]): List[Int] = { | |
origin match { | |
case x::xs::Nil => | |
val sum: Int = x + xs | |
1::sum::next | |
case x::xs::xss => | |
val sum: Int = x+xs | |
pascalLine(xs::xss, sum::next) | |
case _ => | |
List(1, 1) | |
} | |
} | |
/* Make Pascal's Triangle next line | |
val x = List(1, 2, 1) | |
val y = pascalLine(x, List(1)) | |
# List(1, 3, 3, 1) | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment