0

I have a sequence of operations, and the results are packed in a tuple to be given as a parameter to a method.

  val bs = byteArrayToBitSet(ba)
  val d = bs.filter(_ < dBits)
  val c = ...
  val b = ...
  val a = ...
  val r = (f(a), f(b), f(c), f(d)) // <--
  ((fn _).tupled)(r)

I'd like to modified the tuple generation code something like this:

  val r = List(a,b,c,d).map(f(_)) // returns List not tuple

I need to change the List into tuple in order to use ((fn _).tupled)(r). How can I do that?

I may come up with a new method that gets List as an input if there is no way to convert list into tuple, but I'd like to have the tuple solution if possible.

prosseek
  • 182,215
  • 215
  • 566
  • 871

1 Answers1

0

A tuple's arity is known at compile time. A list's arity is only known at runtime. You will almost certainly not gain any profit from converting a list at runtime to a tuple.

In your example, fn supposedly has a fixed number of arguments, four. Using a list of size four doesn't help you here. You might look for a map method on tuples, but it doesn't exist:

(1, 2, 3, 4).map(_ + 1)   // method not found

See this question and answer for an approach that uses the Shapeless library to provide such functionality.

0__
  • 66,707
  • 21
  • 171
  • 266