There is a nice syntactic sugar in Scala that allows us to combine map and flatMap functions in a more readable way, using for comprehensions. I will not get into Try and Futures for now, as they would deserve more than just a few setences each, but I urge you to read more about both them if you’re interested. Val result: List = list.flatMap(elem => isBigEnough(elem)) but we can call this function in flatMap to return a filtered list of integers it returns: List(None, None, Some(4), Some(8)) Val result: List] = list.map(elem => isBigEnough(elem)) such that for any value smaller than or equal 3, it returns Noneĭef isBigEnough(value: Int): Option = if (value > 3) Some(value) else None and let's introduce a function that returns Option On the other hand, the flatMap function can be even more useful with Options, as flattening a list of Option(value) returns a list of values, with only the values which exist, filtering out the None values. a doSomeWork function will only be executed if there is Some(Result) which can either have the value of Some(Result) or None let's re-introduce an optional result with the type of Option So we can use this to conditionally execute a function without handling the None case. Calling the map function on an Option (which can be either a Some(value) or None) will execute the function only if the value is an instance of Some. I’ve written about Scala Options before but I didn’t mention how useful map and flatMap functions are with the Option container. And we can implement them for our own classes as well. A good example are container classes like Option, Future and Try. Map and flatMap functions exist in other classes as well, not just collections. If interested, you can find more in this Stack Overflow discussion. One thing I’ve learned so far is that not every construct that has a flatMap function is a monad, and that Scala has more flexible monad-like types. I wouldn’t dive deeper into the theory as I have only started scratching the surface myself, but I was constantly coming across the importance of the flatMap function for defining monads in functional programming. list.map(subList => subList.map(increment)) in the list of lists, without flattening it if we were to call a map instead of flatMap we would instead increments the integer values Val result = list.flatMap(subList => subList.map(increment)) on the sublists List(1, 2) and List(4, 8) calling flatMap should result in the map functions executed let us use the previously defined increment function again if we have the same list of lists of integers Therefore, flatmap would execute the transformation function on each list element of the listOfLists and return a single flattened list with all the transformed elements. we can convert it into a list of integers which maintains all the elements of the sublists Val listOfLists: List] = List(List(1, 2), List(4, 8), List()) if we have a list of lists of integers named, creatively, listOfLists and return a new list with the incremented valuesįlatMap is similar to map, with the requirement that the elements of the collection are collections as well, and with an additional step of flattening the elements of a collection after executing the transformation function.įlatten is a standard operation of converting a lists of lists into one list with all the elements. map will call the increment function on each element of the list let's introduce a list of integersĭef increment(value: Int): Int = value + 1 So we can think of the function supplied to map as a transformation function, which we can use to transform a collection of N elements into a new collection of N transformed elements. Map is applying a function on each element of a collection similar to foreach, with the difference that map returns a new collection as a result. It is used to execute a function for each element of a collection. Let’s recall the foreach function mentioned in the previous post: list.foreach(print). They are quite significant functions in Scala, and functional programming in general, so they rightfully deserve their own post. Map and flatMap are implemented for all collection types in the Scala collection library. Now it’s time to introduce two great examples of higher order functions, map and flatMap. I wrote about higher order functions in my last post and I mentioned their importance in Scala, but I didn’t want to digress too much.
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |