Dust009 Dust009 - 9 days ago 5
Scala Question

Scala: Generate tuple of Ints

I want to generate a Vector of tuple of two Ints. For now, I do as follows:

(0 until 100).map(x => (x+1 until 100).map(y => (x,y))).flatten.filter { ... }


I wondered if there were any more efficient way to do this. I have the feeling that "flatten" slows down the code. Do I have to use "flatten" or can I use something else ?

PS1: If I don't use "flatten", I have: Vector(Vector(a,b),Vector(c,d),...) and not Vector((a,b),(c,d),...).

PS2: I use (x+1 until 100) in the second generator as I'm not interested in having tuples (a,b) and (b,a).

Answer
for {
  i <- 0 until 100
  j <- i+1 until 100
} yield (i,j)