Skip to content

Instantly share code, notes, and snippets.

@joecwu
Last active August 29, 2015 14:25
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 joecwu/79c93ae3768bac2116c0 to your computer and use it in GitHub Desktop.
Save joecwu/79c93ae3768bac2116c0 to your computer and use it in GitHub Desktop.
val a : Option[String] = Some("abc")
if(a.isEmpty)
println("do something if a has value "+a.get)
else
println("do something if a is empty")
val a : Option[String] = Some("abc")
a.map(v=>println("do someting if a has value "+v)).getOrElse("do something if a is empty")
val b = Option[Int] = a.map{ v =>
if(v=="abc") 123 else 456
}
val c = Option[Int] = a.flatMap{ v =>
if(v=="abc") Some(123) else None
}
def getFutureString() : Future[String] = Future{ Thread.sleep(3000); "abc" }
val str = getFutureString()
// str: scala.concurrent.Future[String]
str.map{ s=> println("I got "+s) }
// res: scala.concurrent.Future[Unit]
// after 3 seconds...
// I got abc
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment