Created
March 28, 2016 04:22
-
-
Save shehaaz/a3aebf854f008a2591df to your computer and use it in GitHub Desktop.
Scala Options Explained
This file contains 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
/** | |
* Option Collections | |
* | |
* Some developers see Option as a safe replacement for null values, | |
* notifying users that the value may be missing and | |
* reducing the likelihood that its use will trigger a Null PointerException. | |
* Others see it as a safer way to build chains of operations, | |
* ensuring that only valid values will persist for the duration of the chain. | |
* | |
* The Option type is itself unimplemented but relies on two subtypes for the implementation: | |
* Some, a type-parameterized collection of one element; and | |
* None, an empty collection. The None type has no type parameters because it never contains contents. | |
*/ | |
var indeed: String = "Indeed" | |
var optionIndeed = Option(indeed) | |
indeed = null | |
optionIndeed = Option(indeed) | |
def divide(amt: Double, divisor: Double) : Option[Double] = | |
{ | |
if(divisor == 0) None | |
else Option(amt/divisor) | |
} | |
//divide: divide[](val amt: Double,val divisor: Double) => Option[Double] | |
val legit = divide(5,2) | |
//legit: Option[Double] = Some(2.5) | |
val illegal = divide(3,0) | |
//illegal: Option[Double] = None | |
val value_legit:Any = legit.getOrElse("You Divided by Zero Yo"); | |
//value_legit: Any = 2.5 | |
val value_illegal:Any = illegal.getOrElse("You Divided by Zero Yo"); | |
//value_illegal: Any = You Divided by Zero Yo | |
val odds = List(1, 3, 5) | |
//odds: List[Int] = List(1, 3, 5) | |
/** | |
* headOption, which returns the head element wrapped in an Option, | |
* ensuring that it will work even on empty lists. | |
*/ | |
val firstOdd = odds.headOption | |
//firstOdd: Option[Int] = Some(1) | |
val first:Any = firstOdd.getOrElse("BAD") | |
val evens = odds filter (_ % 2 == 0) | |
//evens: List[Int] = List() | |
val firstEven = evens.headOption | |
//firstEven: Option[Int] = None | |
val multipleOfFive = odds filter (_ % 5 == 0) | |
//multipleOfFive: List[Int] = List(5) | |
val words = List("risible", "scavenger", "GIST", "passenger", "ranger", "ranker") | |
//Predicate function: Functions that return a boolean. | |
val uppercase:Option[String] = words find (word => (word == word.toUpperCase)) | |
//uppercase: Option[String] = Some(GIST) | |
val o:Object = uppercase.getOrElse(new Animal()) | |
//o: Object = GIST | |
val a:Any = uppercase.getOrElse(5) | |
//a: Any = GIST | |
val s:String = uppercase.getOrElse("EMPTY STRING") | |
//s: String = GIST | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment