Last active
August 29, 2015 14:25
-
-
Save joecwu/79c93ae3768bac2116c0 to your computer and use it in GitHub Desktop.
Blog Scala Monad http://blog.joecwu.com/2015/07/scava-scala-monad.html
This file contains hidden or 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
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") |
This file contains hidden or 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
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 | |
} |
This file contains hidden or 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 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