2

As I know, the following expression

     for{i <- 0 to 10
         j <- 0 to 10} {...}

equals to

  for(i <- 0 to 10) {
     for(j <- 0 to 10) {
        .....
     }
  }

but, how to transform the following expression into the first case?

  for(i <- 0 to 10) {
     **execute()**
     for(j <- 0 to 10) {
        .....
     }
  }

1 Answer 1

5

You can simply add in a line than runs the execution and stores the result in a variable. Eg.:

for{i <- 0 to 10;
    k = doSomethingWith(i);
    j <- 0 to 10} {... do something with any or all of i,j,k ...}

If the execution is side-effecting only (ie. return type is Unit), or you don't care about the return value, just assign to underscore. Eg:

for{i <- 0 to 10;
    _ = println(s"i = $i");
    j <- 0 to 10} {... do something with i,j ...}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.