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 6672273
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T03:29:19+00:00 2026-05-26T03:29:19+00:00

I was wondering how you would write a method in Scala that takes a

  • 0

I was wondering how you would write a method in Scala that takes a function f and a list of arguments args where each arg is a range. Suppose I have three arguments (Range(0,2), Range(0,10), and Range(1, 5)). Then I want to iterate over f with all the possibilities of those three arguments.

var sum = 0.0
for (a <- arg(0)) {
  for (b <- arg(1)) {
    for (c <- arg(2)) {
      sum += f(a, b, c)
    }
  }
}

However, I want this method to work for functions with a variable number of arguments. Is this possible?

Edit: is there any way to do this when the function does not take a list, but rather takes a standard parameter list or is curried?

  • 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-26T03:29:20+00:00Added an answer on May 26, 2026 at 3:29 am

    That’s a really good question!

    You want to run flatMap in sequence over a list of elements of arbitrary size. When you don’t know how long your list is, you can process it with recursion, or equivalently, with a fold.

    scala> def sequence[A](lss: List[List[A]]) = lss.foldRight(List(List[A]())) {
         |    (m, n) => for (x <- m; xs <- n) yield x :: xs
         | }
    scala> sequence(List(List(1, 2), List(4, 5), List(7)))
    res2: List[List[Int]] = List(List(1, 4, 7), List(1, 5, 7), List(2, 4, 7), List(2
    , 5, 7))
    

    (If you can’t figure out the code, don’t worry, learn how to use Hoogle and steal it from Haskell)

    You can do this with Scalaz (in general it starts with a F[G[X]] and returns a G[F[X]], given that the type constructors G and F have the Traverse and Applicative capabilities respectively.

    scala> import scalaz._
    import scalaz._
    
    scala> import Scalaz._
    import Scalaz._
    
    scala> List(List(1, 2), List(4, 5), List(7)).sequence
    res3: List[List[Int]] = List(List(1, 4, 7), List(1, 5, 7), List(2, 4, 7), List(2
    , 5, 7))
    
    scala> Seq(some(1), some(2)).sequence
    res4: Option[Seq[Int]] = Some(List(1, 2))
    
    scala> Seq(some(1), none[Int]).sequence
    res5: Option[Seq[Int]] = None
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am wondering how you would write a simple java method finding the closest
I was wondering how I would write a method to count the number of
I wanted to write an extension-method that would work on dictionaries whose values were
I was wondering if someone would be able to help me write a CQL
I've been wondering about how hard it would be to write some Python code
I tried to write generic function that remove the duplicate elements from array. public
I'm wondering if it's possible to write a passthrough extension method for IQueryable which
I am wondering how you would approach this problem I have two Taxrates that
I'm trying to write code that will infer types from a parameter list and
I'm wondering if there is an event that can be handled or a method

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.