Map, Map and flatMap in Scala
来源:互联网 发布:js设置innerhtml 编辑:程序博客网 时间:2024/05/21 09:59
One of the things I like about Scala is it’s collections framework. As a non CS graduate I only very lightly covered functional programming at university and I’d never come across it until Scala. One the benefits of Scala is that the functional programming concepts can be introduced slowly to the programmer. One of the first places you’ll start to use functional constructs is with the collections framework.
Chances are your first collection will be a list of items and we might want to apply a function to each item in the list in some way.
Map works by applying a function to each element in the list.
So there are some occasions where you want to return a sequence or list from the function, for example an Option
flatMap works applying a function that returns a sequence for each element in the list, and flattening the results into the original list. This is easier to show than to explain:
This comes in really useful with the built in Option class because an option can be considered a sequence that is either empty or has 1 item.
So with that all covered, lets look at how you can apply those concepts to a Map. Now a map can be implemented a number of different ways, but regardless of how it is implemented it can be thought of as a sequence of Tuples, where a tuple is a pair of items, the key and the value.
We can access a tuple by accessing the inner variables _1 and _2
So we want to think about using map and flatMap on our Map, but because of the way a map works it often doesn’t make quite the same sense, we probably don’t want to apply a function to the tuple, but to the value side of the tuple, leaving the key as is, so for example we might want to double all the values. Map provides us with a function to do exactly that.
But in my case I wanted to do something more like flat map in this case, I want a map to come out that misses out the key 1 because it’s value is None. flatMap doesn’t work on maps like mapValues, it get’s passed the tuple and if it returns a List single items you’ll get a list back, but if you return a tuple you’ll get a Map back.
Ok so we are pretty close to using options with flatMap, we need to filter out our None’s, we can do returning a list with just e => f(e._2) and we’ll get the list of values without the None’s, but that isn’t really what I want. What I need to do is return an Option containing a tuple. So here’s our updated function:
and here’s how we might call it:
but this is pretty ugly, all those _1 and _2’s make me sad. If only there was a nice way of unapplying the tuple into variables. Given that this works in python and in a number of places in scala I thought this code should work:
I spent way too long today looking at this (in 5 minute chunks broken by meetings to be fair), before I gave in and asked a coworker what the hell I was missing. The answer is seems is that an unapply is normally only executed in a PartialFunction, which in scala is most easily defined as a case statement. So this is the code that works as expected:
Note that we switch to using curly braces, indicating a function block rather than parameters, and the function is a case statement. This means that the function block we pass to flatMap is a partialFunction that is only invoked for items that match the case statement, and in the case statement the unapply method on tuple is called to extract the contents of the tuple into the variables. This form of variable extraction is very common, and you’ll see it used a lot.
There is of course another way of writing that code that doesn’t use flatMap. Since what we are doing is removing all members of the map that don’t match a predicate, this is a use for the filter method:
- Map, Map and flatMap in Scala
- scala中map与flatMap浅析
- flatMap Map
- Scala Learning(2): map, flatMap, filter与For表达式
- scala必知必会之列表的map,flatMap,zip和reduce
- scala基础36-List的map/flatmap/filter等操作
- Scala基础应用(7)- map/flatMap和filter
- map与flatmap区别
- Spark map flatMap
- Swift FlatMap or Map
- swift - map() 与flatMap()
- spark:map mapPartitions flatmap
- map和flatmap
- spark:map mapPartitions flatmap
- <spark>flatmap 和 map
- map和flatMap
- map与flatmap区别
- map,reduce,filter,flatmap
- php xdebug工具的使用语法
- noip1999 Cantor表 (找规律)
- 单例模式常用方法
- 网站内部优化的六个基本要点
- 导入excel数据,导出数据到excel
- Map, Map and flatMap in Scala
- 冒泡排序算法
- 拓展欧几里得学习小记
- nc后台两张单同名字字段数据交换
- [POJ2104] K-th Number/[POJ2761] Feed the dogs
- iOS 以byte数组作为密钥的3des加密
- hdu1757
- HDU 2255 奔小康赚大钱 POJ 2195 Going Home 最大权完美匹配 KM算法
- 在Eclipse中安装Maven插件以及创建Maven项目