Scala中数字的`to`函数?

THI*_*ELP 1 scala apache-spark

to函数有什么作用:

rdd.flatMap(x => x.to(3))
Run Code Online (Sandbox Code Playgroud)

?

rdd{1, 2, 3, 3}上面的函数组成并且返回{1, 2, 3, 2, 3, 3, 3} 我一直在谷歌搜索“函数的Scala数”及其变体,但似乎无法找到它的作用。

bob*_*bob 5

发挥作用

Range在 Scala 中创建,请使用预定义的方法toby

例子:

1 to 3 会给 Range(1, 2, 3)

函数在 RDD 中做什么

a)查看地图功能

sc.range(1L, 6L).map(x => x to 3).collect.foreach(println)
Run Code Online (Sandbox Code Playgroud)

这打印

NumericRange(1, 2, 3)
NumericRange(2, 3)
NumericRange(3)
NumericRange() // 4 to 3 returns an empty Range
NumericRange() // 5 to 3 returns an empty Range
Run Code Online (Sandbox Code Playgroud)

b)查看 flatMap 函数

sc.range(1L, 6L).flatMap(x => x to 3).collect.foreach(println)
Run Code Online (Sandbox Code Playgroud)

这打印

1
2
3
2
3
3
Run Code Online (Sandbox Code Playgroud)

它结合了映射和展。flatMap 接受一个处理嵌套列表的函数,然后将结果连接在一起。

了解 flatMap 的重要一点是,任何看起来像empty array will disappear. 所以NumericRange()不会出现在flatMapresult 中。