Skip to content

Instantly share code, notes, and snippets.

@fedesilva
Last active December 29, 2015 15:39
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fedesilva/7691881 to your computer and use it in GitHub Desktop.
Save fedesilva/7691881 to your computer and use it in GitHub Desktop.
Options can and should be treated as any other collection. http://www.scala-lang.org/api/current/index.html#scala.Option
val o1 = Some("string")
//o1: Some[String] = Some(string)
val o2 = Some("")
//o2: Some[String] = Some()
val o3: Option[String] = None
//o3: Option[String] = None
o3.filter (_ != "").fold( "Bad Bad Bad")( x => s"Good, we got $x" )
//res8: String = Bad Bad Bad
o2.filter (_ != "").fold( "Bad Bad Bad")( x => s"Good, we got $x" )
//res9: String = Bad Bad Bad
o1.filter (_ != "").fold( "Bad Bad Bad")( x => s"Good, we got $x" )
//res10: String = Good, we got string
val mm = collection.mutable.Map[String,String]();
//mm: scala.collection.mutable.Map[String,String] = Map()
mm ++ o1.filter(_ != "").map( v => "key" -> v )
//res13: scala.collection.mutable.Map[String,String] = Map(key -> string)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment