user568866
user568866

Reputation:

Array of functions?

How can I create an array of functions in Scala so I can loop over them and call them to perform calculations?

def f1(x: Int, y: Int): Int = x + y

def f2(x: Int, y: Int): Int = x - y

def f3(x: Int, y: Int): Int = x * y

def test() {
   val someValues1 = List(1, 2)
   val someValues2 = List(3, 4)
   val fnList = Array(f1,f2,f3)

   for (f <- fnList; a <- someValues1; b <- someValues2) {
     val result = f(a, b)
     println("Result=" + result)
   }
}

Upvotes: 11

Views: 5585

Answers (4)

Rex Kerr
Rex Kerr

Reputation: 167921

It's not clear what you're asking, but you might also want to know that

val fnList = Array[(Int,Int) => Int](_ + _, _ - _, _ * _)

works.

Upvotes: 4

J&#246;rg W Mittag
J&#246;rg W Mittag

Reputation: 369624

In your question, you say that you want an array of functions. But there isn't a single function in your entire code snippet!

I'm guessing by the names f1, f2 and f3 that you thought those were functions. But they aren't: the def keyword is used to declare methods, not functions. Those two are fundamentally different!

So, if you want to use an array of functions, just make f1, f2 and f3 actually be functions instead of methods:

val f1: (Int, Int) => Int = _ + _
val f2: (Int, Int) => Int = _ - _
val f3: (Int, Int) => Int = _ * _

An alternative to that would be to actually read the error message, which tells you exactly what the problem is and how to fix it:

error: missing arguments for method f1 in object $iw;
follow this method with `_' if you want to treat it as a partially applied function
          val fnList = Array(f1,f2,f3)
                             ^

So, if you simply do what the error message tells you to do, namely convert the methods to functions using the _ operator, everything will work fine:

val fnList = Array(f1 _, f2 _, f3 _)

The reason why you are getting the error message is simply because Scala allows you to leave off the parentheses in a method call. So, f1 just means "call f1 without an argument list", which is an error, because f1 takes two arguments.

In some cases, when it is really painfully blatantly obvious that the argument cannot possibly be anything but a function, Scala will perform the conversion from method to function for you automatically. So, a third option would be to make it painfully blatantly obvious to Scala that you do have an array of functions, by, you know, declaring it as an array of functions:

val fnList: Array[(Int, Int) => Int] = Array(f1, f2, f3) // or
val fnList = Array[(Int, Int) => Int](f1, f2, f3)

Now that Scala knows that fnList is an array of functions, it no longer tries to call f1, instead it converts it to a function.

Upvotes: 27

Eastsun
Eastsun

Reputation: 18869

Or write as:

scala> val fnList = Array(f1 _,f2 _,f3 _)
fnList: Array[(Int, Int) => Int] = Array(<function2>, <function2>, <function2>)

Upvotes: 7

Shimi Bandiel
Shimi Bandiel

Reputation: 5747

Just provide the Type parameter for your Array:

val fnList = Array[(Int,Int)=>Int](f1, f2, f3)

Upvotes: 4

Related Questions