Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6952921
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T14:24:42+00:00 2026-05-27T14:24:42+00:00

How can I create an array of functions in Scala so I can loop

  • 0

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)
   }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T14:24:42+00:00Added an answer on May 27, 2026 at 2:24 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to create an array of functions so I can call one
Can c++ create array of pointers to different classes dynamically loaded from dll? ps.without
I can create an array of buttons in Windows Form but how can i
How can I create an array of email addresses contained within a block of
How can I create an array of namespaces? And because it seems like a
I can create a sparse php array (or map) using the command: $myarray =
How can I create an empty one-dimensional string array?
I'm trying to create an array of strings that can be randomized and limited
I want to create an array which can be extended at any time by
I noticed that in my javascript, if I create an empty array, loop through

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.