AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |
Back to Blog
Flatmap scala12/27/2023 ![]() ![]() 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. However, with right-biased, Either can be very handy in creating a sequence of operation.įor instance, take an example of validating an object.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. You have to use “right projectable” to make it right-biased and flatmappable.Įither being un-biased is suitable for validation functionality. Up until Scala 2.12, Either was unbiased, which means the map and flatMap function don’t know which value to apply. Right biased means that functions such as map and flatMap only apply to the “right side” or the “happy scenario”, leaving the other side untouched. This is where the right bias came into the picture. These Monads hold two values, usually a happy and sad scenario. Another similar Monad type to Either includes Future (Success, Failure), Option (Some, None). That means it has two possible values, a right, and a left. However, Either is categorized as the Sum Type in ADT. To be a Monad type, you need to be able to apply a map or flatMap to the value. It transformed Either from unbiased type to a right-biased. Val either1 : Either = Right ( 1 ) val either2 : Either = Right ( 2 ) for yield one + two This makes it very hard to compose sequence using for comprehension. In Scala 2.1.1 and earlier, many people didn’t consider Either a Monad because it didn’t have a map and flatMap methods. In this article, I want to share some useful Either characteristic that I think is beneficial to know about Functional programming. Therefore, the function is predictable to the caller, letting the caller knows what potential result may happen. For instance, instead of blows up the exception unexpectedly, Either can return a Left (failure case) or Right (success case). The flatMap function is applicable to both Scalas Mutable and Immutable collection data structures. You may encounter some unexpected side effects when an exception is thrown. ![]() Without Either, your function is not predictable. One of the reasons to use Either type constructor is not to create any surprising output when running the program. As I learn more in-depth and more profound about functional programming, Either is not just a container that stores two values of left and right, but it is a Monad. 3 Useful Things About Either That You Want To KnowĮither is a new type of class that I get to learn when I was learning Scala.
0 Comments
Read More
Leave a Reply. |