Scala 为什么可以在for内完成作业?

Scala 为什么可以在for内完成作业?,scala,Scala,我不理解以下代码片段: def stream[F[_]: ConcurrentEffect](implicit T: Timer[F], C: ContextShift[F]): Stream[F, Nothing] = { for { client <- BlazeClientBuilder[F](global).stream helloWorldAlg = HelloWorld.impl[F] jokeAlg = Jokes.impl[F

我不理解以下代码片段:

  def stream[F[_]: ConcurrentEffect](implicit T: Timer[F], C: ContextShift[F]): Stream[F, Nothing] = {
    for {
      client <- BlazeClientBuilder[F](global).stream
      helloWorldAlg = HelloWorld.impl[F]
      jokeAlg = Jokes.impl[F](client)

      // Combine Service Routes into an HttpApp.
      // Can also be done via a Router if you
      // want to extract a segments not checked
      // in the underlying routes.
      httpApp = (
        UsersvcRoutes.helloWorldRoutes[F](helloWorldAlg) <+>
          UsersvcRoutes.jokeRoutes[F](jokeAlg)
        ).orNotFound

      // With Middlewares in place
      finalHttpApp = Logger.httpApp(true, true)(httpApp)

      exitCode <- BlazeServerBuilder[F](global)
        .bindHttp(8080, "0.0.0.0")
        .withHttpApp(finalHttpApp)
        .serve
    } yield exitCode
  }.drain

也许最好用许多例子来解释

for { 
  x <- List(1, 2, 3)
  y = x * 2 
} yield y
同时

for {
  x <- List(1, 2, 3)
  y = x * 2
  z = x * 3
} yield y
同时

for {
  x <- List(1, 2, 3)
  y = x * 2
  i <- List(4, 5, 6)
  z = x * 3
} yield y

我们看到了一个模式,为什么它不可能呢?它只是被翻译成一张地图。您不需要所有这些来测试,您可以为{x做一个简单的测试,我对Scala没有信心,也不知道我可以用那种方式编写;
for {
  x <- List(1, 2, 3)
  y = x * 2
  z = x * 3
} yield y
List(1, 2, 3)
  .map { x =>
    val y = x * 2
    val z = x * 3
    (x, y, z)       // note the Tuple3
  }
  .map { case (x, y, z) => y }
for {
  x <- List(1, 2, 3)
  y = x * 2
  i <- List(4, 5, 6)
  z = x * 3
} yield y
List(1, 2, 3)
  .map { x =>
    val y = x * 2
    (x, y)         // note the Tuple2
  }
  .flatMap { case (x, y) =>
    List(4, 5, 6)
      .map { i =>
        val z = x * 3
        (i, z)     // note the Tuple2
      }
      .map { case (i, z) => y }
  }