![]() ![]() Imagine we have a nested data model and want to call some method on a property nested deeply inside an object graph. We will see how it can help us understand monads in Scala! Null-conditional operator Returns the option's value if it is nonempty, or null if it is empty.Today I will talk about an awesome feature of C# 6.0. Returns this Option if it is nonempty, otherwise return the result of evaluating alternative. Otherwise return None.ĭef orElse(alternative: => Option): Option Returns a Some containing the result of applying f to this Option's value if this Option is nonempty. Returns a singleton iterator returning the Option's value if it is nonempty, or an empty iterator if the option is empty. Returns true if the option is an instance of Some, false otherwise. Returns the option's value if the option is nonempty, otherwise return the result of evaluating default. Returns None if this Option is empty.Īpply the given procedure f to the option's value, if it is nonempty. Returns the result of applying f to this Option's value if this Option is nonempty. Otherwise, return None.ĭef flatMap(f: (A) => Option): Option Returns this Option if it is nonempty and applying the predicate p to this Option's value returns false. Otherwise, return None.ĭef filterNot(p: (A) => Boolean): Option ![]() Returns this Option if it is nonempty and applying the predicate p to this Option's value returns true. Returns true if this option is nonempty and the predicate p returns true when applied to this Option's value. , x_k), returns x_(n 1) where 0 Boolean): Boolean The nth element of this product, 0-based. Returns true if the option is None, false otherwise. For a complete list of methods available, please check official documentation of Scala. Exampleįollowing are the important methods which you can use while playing with Options. ![]() Println("b.getOrElse(10): " b.getOrElse(10) )įollowing is the example program to show how to use isEmpty() method to check if the option is None or not. Println("a.getOrElse(0): " a.getOrElse(0) ) Println("show(capitals.get( \"India\")) : " show(capitals.get( "India")) )įollowing is the example program to show how to use getOrElse() method to access a value or a default when no value is present. Println("show(capitals.get( \"Japan\")) : " show(capitals.get( "Japan")) ) The most common way to take optional values apart is through a pattern match. The following commands are used to compile and execute this program. Println("capitals.get( \"India\" ) : " capitals.get( "India" )) Println("capitals.get( \"France\" ) : " capitals.get( "France" )) Val capitals = Map("France" -> "Paris", "Japan" -> "Tokyo") The method will return Some if the record is found but None if the record is not found. Let's say we have a method that retrieves a record from the database based on a primary key. For example, the get method of returns either a value stored in the HashMap, or null if no value was found. Option type is used frequently in Scala programs and you can compare this with the null value available in Java which indicate no value. For instance, the get method of Scala's Map produces Some(value) if a value corresponding to a given key has been found, or None if the given key is not defined in the Map. ![]() An Option can be either Some or None object, which represents a missing value. Scala Option is a container for zero or one element of a given type. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |