Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scala Array Slicing with Tuple

I try to slice an 1D Array[Double] using the slice method. I've written a method which returns the start and end index as a tuple (Int,Int).

  def getSliceRange(): (Int,Int) = {
    val start =   ...
    val end =  ...
    return (start,end)
  }

How can I use the return value of getSliceRange directly?

I tried:

myArray.slice.tupled(getSliceRange())

But this gives my a compile-Error:

Error:(162, 13) missing arguments for method slice in trait IndexedSeqOptimized;
follow this method with `_' if you want to treat it as a partially applied function
  myArray.slice.tupled(getSliceRange())
like image 597
Raphael Roth Avatar asked Jun 29 '16 11:06

Raphael Roth


1 Answers

I think the problem is the implicit conversion from Array to ArrayOps (which gets slice from GenTraversableLike).

val doubleArray = Array(1d, 2, 3, 4)

(doubleArray.slice(_, _)).tupled

Function.tupled[Int, Int, Array[Double]](doubleArray.slice)

(doubleArray.slice: (Int, Int) => Array[Double]).tupled
like image 191
Peter Neyens Avatar answered Oct 29 '22 00:10

Peter Neyens