-1

I am studying Scala language and I do not understand this piece of code:

for {
  i <- Set(2,3)
  j <- 1 to i
  k <- i to 2 by -1
} yield (j,k)

What does it mean that j is a Range from 1 to i when i is a Set?

Gaël J
  • 11,274
  • 4
  • 17
  • 32
Giuseppe Boezio
  • 101
  • 1
  • 11

1 Answers1

2

I recommend that you read the Scala documentation about for-comprehension and/or this answer on SO: Confused with the for-comprehension to flatMap/Map transformation

The code you provided is strictly equivalent to the following one:

Set(2,3)
  .flatMap { i => 
    (1 to i).flatMap { j =>
      (i to 2 by -1).map { k =>
        (j,k)
      }
    }
  }

This way, you should see better that i is an "item" from the Set.

Gaël J
  • 11,274
  • 4
  • 17
  • 32