Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Invoke a method using a tuple as the parameter list [duplicate]

I wonder what is the best way to do this.

val foo = Some("a")
val bar = Some(2)

def baz(a: String, b: Int) = if((b % 2) == 0) Some(a+","+b) else None

(x zip y) flatMap baz //does not compile of course
(x zip y) flatMap { x => baz(x._1, x._2) } //ugly

I would presume that Odersky et al. have another trick up theirs sleeve to reduce the noise in this example.

So the question is how to fight the clutter here assuming you are not allowed to change the implementation of baz (e.g. def baz(a: (String Int))).

like image 935
Joa Ebert Avatar asked Jun 12 '11 14:06

Joa Ebert


1 Answers

This question has already been answered here: scala tuple unpacking

First, make foo to a function by partial application, then call tupled with your parameter list:

(foo _).tupled(myTuple)
like image 178
notan3xit Avatar answered Oct 17 '22 07:10

notan3xit