我有以下我不明白的代码段:
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
中进行分配,例如:
helloWorldAlg = HelloWorld.impl[F]
也许最好用很多例子来解释
for {
x <- List(1, 2, 3)
y = x * 2
} yield y
对]减半>
List(1, 2, 3) .map { x => val y = x * 2 (x, y) // note the Tuple2 } .map { case (x, y) => y }
whilst
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 }
whilst
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 } }
[生成器
p <- e
后跟值定义p' = e'
,然后沿链传递tuple(p, p')
时,我们看到一个模式。