如何在 Scala 中用 Map 替换 Yield?

发布于 2024-12-26 03:56:46 字数 171 浏览 1 评论 0原文

我怎样才能删除这个产量?我想使用地图而不是:

val cols = for(x <- 0 to 6) yield for(y <- 0 to 5) yield apply(x, y)

这可能吗?

谢谢!

此致, 约翰

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我是有多爱你 2025-01-02 03:56:46

这将非常简单:

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

UPD

地图可能会有变化。如果您想要没有嵌套结构的平面对象,最好使用 flatMap 而不是仅仅 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)] = ...

或者只是在最后展平结果:

scala> val cols = (0 to 6).map(x => (0 to 5).map( y => apply(x,y))).flatten
//cols: scala.collection.immutable.IndexedSeq[(Int, Int)] = ...

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)] = ...
囍孤女 2025-01-02 03:56:46

当您有多个生成器时,最里面的生成器将转换为 map,其余的将转换为 flatMap。 (在您的情况下,这些不是嵌套生成器。)

以下示例可能会有所帮助:

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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文