Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to replace a yield with a map in Scala?

Tags:

yield

scala

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

like image 473
user1137701 Avatar asked Jan 08 '12 23:01

user1137701


2 Answers

That would be pretty simple:

val cols = (0 to 6).map(x => (0 to 5).map( y => apply(x,y)))

UPD

There may be variations in maps. 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)] = ...
like image 164
om-nom-nom Avatar answered Oct 17 '22 05:10

om-nom-nom


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
like image 37
missingfaktor Avatar answered Oct 17 '22 05:10

missingfaktor