![]() In the following example, the third field may be null in the database, so it’s handled using Some and None, as shown:ĭB. For instance, they’re baked into the Play Framework’s Anorm database library, where you use Option/Some/None for database table fields that can be null. Once you begin working with third-party Scala libraries, you’ll see that Option is used to handle situations where a variable may not have a value. You can pass anonymous functions into the collection methods.Methods like flatten, flatMap, and others are built to work well with Option values.That example works because the collect method takes a partial function, and the anonymous function that’s passed in is only defined for Some values it ignores the None values. The collect method provides another way to achieve the same result: Because an Option is a collection of zero or one elements, you can convert this list of Int values by adding flatten to map:Īs shown in Recipe 10.16, “Combine map and flatten with flatMap”, this is the same as calling flatMap: By passing the toInt method into the map method, you can convert every element in the collection into a Some or None value: Imagine you want a list of all the integers that can be converted from that list of strings. For instance, starting with a list of strings like this: That example prints the value if toInt returns a Some, but bypasses the println statement if toInt returns a None.Īnother good way to access the toInt result is with a match expression:Ĭase None => println("That didn't work.")Īnother great feature of Option is that it plays well with Scala collections. To get the actual value if the method succeeds, or use a default value if the method fails, use getOrElse:īecause an Option is a collection with zero or one elements, the foreach method can be used in many situations: As a consumer of a method that returns an Option, there are several good ways to call it and access its result: The toInt example shows how to declare a method that returns an Option. X: Option = None Getting the value from an Option This is what it looks like when it fails and returns a None: This is what toInt looks like in the REPL when it succeeds and returns a Some: Here’s another way to write the same function:ĭef toInt(s: String): Option = allCatch.opt(s.toInt)Īlthough this is a simple function, it shows the common pattern, as well as the syntax.įor a more complicated example, see the readTextFile example in Recipe 20.5. It takes a String as input and returns a Some if the String is successfully converted to an Int, otherwise it returns a None: The toInt method used in this book shows how to return an Option from a method. Let us consider an example which calls lines.flatMap (a > a.split (‘ ‘)), is a flatMap which will create new files off RDD with records of 6 number as shown in the below picture as it splits the records into separate words with spaces in between them. Annotations SerialVersionUID () Source View. flatMap operation of transformation is done from one to many. Using Either/Left/Right when you need the error message (pre-Scala 2.10) A view that flatmaps elements of the underlying collection.Using Try/Success/Failure when you need the error message (Scala 2.10 and newer).This recipe adds these additional solutions: See that recipe for examples of how to use an Option in those situations. Converting null results from other code (such as Java code) into an Option.This method works on two things success and fail. ![]() What it basically do is it just evaluates the value of the variable and return us the alternative value if the value is empty. ![]() Also, this method exists for both some and none class in scala.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |