![]() See how efficiently flatMap has done it for us, we can also do the same thing using method flatten, similarly we can also use flatMap operation wherever we flatten nested Options wrappers to flatten the structure as per our needs. So if we look at the signatures of map() and flatMap() methods in class Option they look like this: Now let’ s first look the above operations help us dealing with Option, So let’s get started with moving toward Options. In my previous blog, map and flatMap in Scala for collections we had a quite basic but yet effective understanding how map() and flatMap() works with collections in scala, keeping in contrast with my last blog, one may find it pretty weird like the methods that works on collections can also work on Futures and Options too, but we will look how these methods put us at ease while handling Futures and Options. Not to worry, we will see examples of functions which return Option in upcoming tutorials.In this blog, we would be looking at how map() and flatMap() operations work with Option and Future of scala, literally speaking both Futures and Options are very effective features of scala, A Future lets us have a value from some task on a differnt thread and Option provides us a hand from null of java as using null in scala is seen a very bad approach in functional programming.By being explicit that such a function will return an Option, any consumer of the function can avoid NullPointerException by using getOfElse() or Pattern Matching.What have we learned? It's all about intent!.An Option can be thought of as collections of zero or one elements. As a consumer of that function call, you would not know that the function could potentially return null. An Option is a data structure that contains either a single value, or no value at all.But, let's take a pause and assume that you were calling some function to populate your variable glazedDonutName.Sure, you can argue that we have not achieved much!. #Scala option how to#Step 3: How to use Pattern Matching with Option Println("\nStep 3: How to use Pattern Matching with Option")Ĭase Some(name) => println(s"Received donut name = $name")Ĭase None => println(s"No donut name was found!") You've already seen from Step 2 that you can use the getOrElse() function to get a default value in case your variable had no data.īut you can also use Pattern Matching as shown below: #Scala option free#And you are absolutely right! But Pattern Matching is your friend :) let's look at Step 3 below.ģ. How to use Pattern Matching with Optionįeel free to review the two previous tutorials Pattern Matching and Tuples.At this point, you may be asking yourself that this exception is similar to your NullPointerException.If you were to call the get() function as shown in Step 1, you will get the exception : None.get.We've used the handy getOrElse() function from the Option to supply a default value.Step 2: How to use Option and None - a basic example You should see the following output when you run your Scala application in IntelliJ: Val glazedDonutTaste: Option = Some("Very Tasty") Println("Step 1: How to use Option and Some - a basic example") You can then assign its value using the Some class and pass in your donut taste String of Very Tasty. But instead of its type being just a String, you will declare it as an Option of type String using the syntax Option. Let's declare a simple String called glazedDonutTaste. 1. How to use Option and Some - a basic example ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |