THIS USER NEEDS HELP THIS USER NEEDS HELP - 1 month ago 10
Scala Question

`to` function for a number in Scala?

What does

to
function do in:

rdd.flatMap(x => x.to(3))


?

rdd
is composed of
{1, 2, 3, 3}
and the above function returns
{1, 2, 3, 2, 3, 3, 3}

I have been googling "scala number to function" and its variants, but can't seem to find what it does.

p2. p2.
Answer

To function

To create a Range in Scala, use the predefined methods to and by.

Example:

1 to 3 will give Range(1, 2, 3)

What does to function do in RDD

a) Looking into map function:

sc.range(1L, 6L).map(x => x to 3).collect.foreach(println)

This prints

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

b) Looking into flatMap function:

sc.range(1L, 6L).flatMap(x => x to 3).collect.foreach(println)

This prints

1
2
3
2
3
3

It combines mapping and flattening. flatMap takes a function that works on the nested lists and then concatenates the results back together.

An important thing to understand about flatMap is that anything that looks like an empty array will disappear. so NumericRange() doesn't appears in flatMap result .

Comments