Skip to content

Instantly share code, notes, and snippets.

@hossshy
Created January 12, 2016 03:19
Show Gist options
  • Save hossshy/576f5da1a373e410e3f6 to your computer and use it in GitHub Desktop.
Save hossshy/576f5da1a373e410e3f6 to your computer and use it in GitHub Desktop.
// For
for (book <- booklist) {
println(book)
}
// Using Filter
for (book <- booklist if book.contains("Scala")) {
println(s"Filter $book")
}
// Variable binding in for
for {book <- booklist
bookVal = book.toUpperCase}
println(s"$bookVal : $book")
// Yield
var scalabooks = for {
book <- booklist
if book.contains("a")
} yield book
for (sb <- scalabooks)
println("Yield aBook: " + sb)
// Try/finally
try {
throw new Exception("something")
} catch {
case e: java.io.IOException => println("IOE")
case n: NullPointerException => println("nullp")
case t: Exception => println("exp")
}finally {
println("This will always be printed.")
}
// try parseInt
println(
try {
Integer.parseInt("dog")
} catch { case _ : Throwable => 0 }
)
println(
try {
Integer.parseInt("44")
} catch { case _ : Throwable => 0 }
)
// Match
println(
44 match {
case 44 => true
case _ => false
}
)
println(
"David" match {
case "Elwood" => 77
case "David" => 45
case _ => 0
}
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment