Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

scala : it is impossible to put a tuple as a function's argument

Tags:

tuples

scala

I can't pass a tuple as a method parameter:

scala> val c:Stream[(Int,Int,Int)]= Stream.iterate((1, 0, 1))((a:Int,b:Int,c:Int) => (b,c,a+b))
<console>:11: error: type mismatch;
 found   : (Int, Int, Int) => (Int, Int, Int)
 required: ((Int, Int, Int)) => (Int, Int, Int)

thanks.

like image 700
lolveley Avatar asked Jul 03 '15 12:07

lolveley


People also ask

Can a function take a tuple as an argument?

A tuple can also be passed as a single argument to the function.

How many arguments can a tuple take?

A tuple can be an argument, but only one - it's just a variable of type tuple . In short, functions are built in such a way that they take an arbitrary number of arguments.

Does Scala have tuples?

In Scala, a tuple is a value that contains a fixed number of elements, each with its own type. Tuples are immutable. Tuples are especially handy for returning multiple values from a method.

Can we have variables of different types inside a tuple Scala?

Thankfully, Scala already has a built-in tuple type, which is an immutable data structure that we can use for holding up to 22 elements with different types.


2 Answers

Just as the function literal:

(x:Int) => x + 1

is a function of one argument, the following

(x:Int, y: Int, z: Int) => x + y + z

is a function of three arguments, not one argument of a 3tuple

You can make this work neatly using a case statement:

scala> val c: Stream[(Int,Int,Int)] = 
       Stream.iterate((1, 0, 1)){ case (a, b, c) => (b, c, a+b) }

c: Stream[(Int, Int, Int)] = Stream((1,0,1), ?)

An alternative is to pass the tuple, but that's really ugly due to all the _1 accessors:

scala> val c:Stream[(Int,Int,Int)] = 
         Stream.iterate((1, 0, 1))( t => (t._2, t._3, t._1 + t._2) )

c: Stream[(Int, Int, Int)] = Stream((1,0,1), ?)
like image 93
DNA Avatar answered Oct 16 '22 05:10

DNA


The lambda (a:Int,b:Int,c:Int) => (b,c,a+b) is a function taking three arguments. You want it to take one tuple, so you can write ((a:Int,b:Int,c:Int)) => (b,c,a+b). But this gives an error!

error: not a legal formal parameter.
Note: Tuples cannot be directly destructured in method or function parameters.
      Either create a single parameter accepting the Tuple3,
      or consider a pattern matching anonymous function: `{ case (param1, ..., param3) => ... }
       (((a:Int,b:Int,c:Int)) => (b,c,a+b))
         ^

Luckily, the error suggests a solution: { case (a, b, c) => (b, c, a+b) }

like image 34
ReyCharles Avatar answered Oct 16 '22 05:10

ReyCharles