How to select inconsistent subset elements from an array using Scala and Spark?

In Python, I will do so.

>>> x
array([10,  9,  8,  7,  6,  5,  4,  3,  2])
>>> x[np.array([3, 3, 1, 8])]
array([7, 7, 9, 2])

This does not work in the Scala Spark shell:

scala> val indices = Array(3,2,0)
indices: Array[Int] = Array(3, 2, 0)

scala> val A = Array(10,11,12,13,14,15)
A: Array[Int] = Array(10, 11, 12, 13, 14, 15)

scala> A(indices)
<console>:28: error: type mismatch;
 found   : Array[Int]
 required: Int
              A(indices)

The foreach method also does not work:

scala> indices.foreach(println(_))
3
2
0

scala> indices.foreach(A(_))
<no output>

I want to get result B:

scala> val B = Array(A(3),A(2),A(0))
B: Array[Int] = Array(13, 12, 10)

However, I do not want to hard code it, because I do not know how many indexes there are or what will be in it.

+4
source share
2 answers

The shortest way that I can imagine is to flip your mental model and put indexes first:

indices map A

And I would suggest using liftfor returnOption

indices map A.lift
+7
source

map indices, lambda. , Array apply:

indices.map(index => A.apply(index))

apply:

indices.map(index => A(index))

:

indices.map(A(_))

, :

indices.map(A)

:

indices map A

foreach, Unit . :

indices.foreach(index => println(A(index)))
indices.map(A).foreach(println)
indices map A foreach println
+5

All Articles