Skip to content

Instantly share code, notes, and snippets.

@sam
Created March 20, 2014 21:53
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 sam/9674725 to your computer and use it in GitHub Desktop.
Save sam/9674725 to your computer and use it in GitHub Desktop.

Scalaisms

def common[T<:Equals](a: T, b: T): Option[T] = {
	if(a == b) Some(a) else None
}

def common[T<:AnyVal](a: T, b: T): Option[T] = {
	if(a == b) Some(a) else None
}
scala.collection.breakOut
Seq(Some("a"), Some("b"), None) flatMap identity[Option[String]]
val maybeName = Some("bob")

if(maybeName.isDefined) maybeName.get else "N/A"

// vs:
maybeName match {
	case Some(name) => name
	case None => "N/A"
}

// vs:
maybeName getOrElse "N/A"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment