Skip to content

Instantly share code, notes, and snippets.

@xodhx4
Last active May 17, 2018 17:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xodhx4/159dbbe586274c925d310d52af086996 to your computer and use it in GitHub Desktop.
Save xodhx4/159dbbe586274c925d310d52af086996 to your computer and use it in GitHub Desktop.
Map(dictionary) initialize and pattern matching example
// 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")
}
}
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