Scala Slicing an Array Using a Tuple

I am trying to cut a 1D Array[Double] using the slice method. I wrote a method that 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 getSliceRange return value directly?

I tried:

 myArray.slice.tupled(getSliceRange()) 

But this gives me compilation-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()) 
+5
source share
2 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 
+5
source

Two options are here, the first is to call your function twice:

 myArray.slice(getSliceRange()._1, getSliceRange()._2) 

or save Tuple in advance:

 val myTuple: (Int, Int) = getSliceRange() myArray.slice(myTuple._1, myTuple._2) 

Edit: I leave this here just in case, but Peter Naines posted the expected answer.

+1
source

All Articles