Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scala non-consecutive sub-array

I'm new to Scala. I see that there's a slice method for Arrays that can return a consecutive slice, like so:

scala> "zero|one|two|three|four|five".split("\\|").slice(2,5)
res3: Array[String] = Array(two, three, four)

Is there syntactic sugar somewhere for taking an arbitrary, non-consecutive, non-ascending sub-array? Something like:

scala> "zero|one|two|three|four|five".split("\\|").fictionalMethod(4,1,5)
res3: Array[String] = Array(four, one, five)
like image 880
Ken Williams Avatar asked Mar 20 '23 14:03

Ken Williams


1 Answers

The shortest line using only standard library functions I can think of would be

Array(4, 1, 5) map "zero|one|two|three|four|five".split("\\|")
like image 199
Owen Avatar answered Mar 27 '23 11:03

Owen