如何链接路由(使用`~`)是函数的结果?

时间:2017-07-20 08:31:30

标签: akka-http spray-routing

scala> import akka.http.scaladsl.server._; import Directives._
import akka.http.scaladsl.server._
import Directives._

假设我有两种类型的函数(例如Int)到Route

scala> lazy val r1: Int => Route = ???
r1: Int => akka.http.scaladsl.server.Route = <lazy>

scala> lazy val r2: Int => Route = ???
r2: Int => akka.http.scaladsl.server.Route = <lazy>

我可以这样写一条路线:

scala> lazy val composite: Route = path("foo"){ r1(1) } ~ path("bar"){ r2(1) }
composite: akka.http.scaladsl.server.Route = <lazy>

我想要做的是将函数组合与~路径链一起使用。也就是说,我喜欢这样才能工作:

scala> lazy val composite: Int => Route = path("foo")(r1) ~ path("bar")(r2)

除非它没有: - (

<console>:31: error: type mismatch;
 found   : Int => akka.http.scaladsl.server.Route
    (which expands to)  Int => (akka.http.scaladsl.server.RequestContext => scala.concurrent.Future[akka.http.scaladsl.server.RouteResult])
 required: akka.http.scaladsl.server.Route
    (which expands to)  akka.http.scaladsl.server.RequestContext => scala.concurrent.Future[akka.http.scaladsl.server.RouteResult]
       lazy val composite: Int => Route = path("foo")(r1) ~ path("bar")(r2)
                                                      ^

修改

我试图使用无点函数组合来做到这一点。正如下面的 Ramon 的答案所示,如果您愿意复制函数应用程序(但这是我想要避免的),这很容易做到。那就是:

lazy val composite: Int => Route 
  = i => path("foo")(r1(i)) ~ path("bar")(r2(i))

注意

使用 scalaz ,我可以这样做:

scala> import akka.http.scaladsl.server._; import Directives._; import scalaz.syntax.monad._; import scalaz.std.function._
import akka.http.scaladsl.server._
import Directives._
import scalaz.syntax.monad._
import scalaz.std.function._

scala> lazy val r1: Int => Route = ???
r1: Int => akka.http.scaladsl.server.Route = <lazy>

scala> lazy val r2: Int => Route = ???
r2: Int => akka.http.scaladsl.server.Route = <lazy>

scala> lazy val composite = for (x <- r1; y <- r2) yield path("foo")(x) ~ path("bar")(y)
composite: Int => akka.http.scaladsl.server.Route = <lazy>

这是如此之好,但ConjunctionMagnet.fromRouteGenerator中隐含的akka.http.scaladsl.server让我有理由认为它可能直接在akka-http中

1 个答案:

答案 0 :(得分:1)

与您提供的scalaz示例相同的是:

lazy val composite: Int => Route = 
  (i) => path("foo")(r1(i)) ~ path("bar")(r2(i))

同样,您可以匿名参数名称,但这会产生2个参数:

lazy val composite: (Int,Int) => Route = path("foo")(r1(_)) ~ path("bar")(r2(_))