how can I remove this yield's? I wanna use a map instead of:
val cols = for(x <- 0 to 6) yield for(y <- 0 to 5) yield apply(x, y)
Is this possible?
Thanks!
Best regards, John
That would be pretty simple:
val cols = (0 to 6).map(x => (0 to 5).map( y => apply(x,y)))
There may be variations in map
s. If you want flat object without nested structure it is better to use flatMap
instead of just map
:
def apply(x: Int, y: Int) = (x,y)
scala> val cols = (0 to 6).map(x => (0 to 5).map( y => apply(x,y)))
//cols: scala.collection.immutable.IndexedSeq[scala.collection.immutable.IndexedSeq[(Int, Int)]] = ...
scala> val cols = (0 to 6).flatMap(x => (0 to 5).map( y => apply(x,y)))
//cols: scala.collection.immutable.IndexedSeq[(Int, Int)] = ...
or just flatten result at the end:
scala> val cols = (0 to 6).map(x => (0 to 5).map( y => apply(x,y))).flatten
//cols: scala.collection.immutable.IndexedSeq[(Int, Int)] = ...
When you have more than one generators, the innermost one translates to map
, and rest translate to flatMap
. (In your case, those aren't nested generators.)
The following examples may help:
scala> val xs, ys, zs = Vector(1, 4, 5)
xs: scala.collection.immutable.Vector[Int] = Vector(1, 4, 5)
ys: scala.collection.immutable.Vector[Int] = Vector(1, 4, 5)
zs: scala.collection.immutable.Vector[Int] = Vector(1, 4, 5)
scala> for {
| x <- xs
| y <- ys
| z <- zs
| } yield (x, y, z)
res0: scala.collection.immutable.Vector[(Int, Int, Int)] = Vector((1,1,1), (1,1,4), (1,1,5), (1,4,1), (1,4,4), (1,4,5), (1,5,1), (1,5,4), (1,5,5), (4,1,1), (4,1,4), (4,1,5), (4,4,1), (4,4,4), (4,4,5), (4,5,1), (4,5,4), (4,5,5), (5,1,1), (5,1,4), (5,1,5), (5,4,1), (5,4,4), (5,4,5), (5,5,1), (5,5,4), (5,5,5))
scala> xs flatMap { x =>
| ys flatMap { y =>
| zs map { z =>
| (x, y, z)
| }
| }
| }
res1: scala.collection.immutable.Vector[(Int, Int, Int)] = Vector((1,1,1), (1,1,4), (1,1,5), (1,4,1), (1,4,4), (1,4,5), (1,5,1), (1,5,4), (1,5,5), (4,1,1), (4,1,4), (4,1,5), (4,4,1), (4,4,4), (4,4,5), (4,5,1), (4,5,4), (4,5,5), (5,1,1), (5,1,4), (5,1,5), (5,4,1), (5,4,4), (5,4,5), (5,5,1), (5,5,4), (5,5,5))
scala> res0 == res1
res2: Boolean = true
scala> for {
| x <- xs
| y <- ys
| } yield for {
| z <- zs
| } yield (x, y, z)
res3: scala.collection.immutable.Vector[scala.collection.immutable.Vector[(Int, Int, Int)]] = Vector(Vector((1,1,1), (1,1,4), (1,1,5)), Vector((1,4,1), (1,4,4), (1,4,5)), Vector((1,5,1), (1,5,4), (1,5,5)), Vector((4,1,1), (4,1,4), (4,1,5)), Vector((4,4,1), (4,4,4), (4,4,5)), Vector((4,5,1), (4,5,4), (4,5,5)), Vector((5,1,1), (5,1,4), (5,1,5)), Vector((5,4,1), (5,4,4), (5,4,5)), Vector((5,5,1), (5,5,4), (5,5,5)))
scala> xs flatMap { x =>
| ys map { y =>
| zs map { z =>
| (x, y, z)
| }
| }
| }
res4: scala.collection.immutable.Vector[scala.collection.immutable.Vector[(Int, Int, Int)]] = Vector(Vector((1,1,1), (1,1,4), (1,1,5)), Vector((1,4,1), (1,4,4), (1,4,5)), Vector((1,5,1), (1,5,4), (1,5,5)), Vector((4,1,1), (4,1,4), (4,1,5)), Vector((4,4,1), (4,4,4), (4,4,5)), Vector((4,5,1), (4,5,4), (4,5,5)), Vector((5,1,1), (5,1,4), (5,1,5)), Vector((5,4,1), (5,4,4), (5,4,5)), Vector((5,5,1), (5,5,4), (5,5,5)))
scala> res3 == res4
res5: Boolean = true
scala> for {
| x <- xs
| } yield for {
| y <- ys
| z <- zs
| } yield (x, y, z)
res6: scala.collection.immutable.Vector[scala.collection.immutable.Vector[(Int, Int, Int)]] = Vector(Vector((1,1,1), (1,1,4), (1,1,5), (1,4,1), (1,4,4), (1,4,5), (1,5,1), (1,5,4), (1,5,5)), Vector((4,1,1), (4,1,4), (4,1,5), (4,4,1), (4,4,4), (4,4,5), (4,5,1), (4,5,4), (4,5,5)), Vector((5,1,1), (5,1,4), (5,1,5), (5,4,1), (5,4,4), (5,4,5), (5,5,1), (5,5,4), (5,5,5)))
scala> xs map { x =>
| ys flatMap { y =>
| zs map { z =>
| (x, y, z)
| }
| }
| }
res7: scala.collection.immutable.Vector[scala.collection.immutable.Vector[(Int, Int, Int)]] = Vector(Vector((1,1,1), (1,1,4), (1,1,5), (1,4,1), (1,4,4), (1,4,5), (1,5,1), (1,5,4), (1,5,5)), Vector((4,1,1), (4,1,4), (4,1,5), (4,4,1), (4,4,4), (4,4,5), (4,5,1), (4,5,4), (4,5,5)), Vector((5,1,1), (5,1,4), (5,1,5), (5,4,1), (5,4,4), (5,4,5), (5,5,1), (5,5,4), (5,5,5)))
scala> res6 == res7
res8: Boolean = true
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With