【发布时间】:2014-01-24 22:06:47
【问题描述】:
如果我理解正确的话
object Application extends Controller {
def page1 = Action { Ok("Hello") }
}
可以写成
object Application extends Controller {
val f : Result = { Ok("Hello") }
def page1 = Action( f )
// i.e. Action.apply( f )
}
documentation 表示动作本质上是一个 (Request[A] => Result) 函数,它处理请求并生成要发送给客户端的结果。
由于 Action 本质上是一个函数,我们可以将函数 f 应用于 Action,即 Action.apply(f)。希望到目前为止我是正确的。
下面这段代码,
def index = Action { implicit request =>
Async {
val cursor = collection.find(
BSONDocument(), BSONDocument()).cursor[Patient]
val futureList = cursor.toList
futureList.map { patients => Ok(Json.toJson(patients)) }
}
}
如果我要写作
def index = Action(f)
我希望能够编写一个函数 f。我的伪代码是
val f: Result =
//a function that takes Request[A] and returns Result
{
(request :Request[A]) =>
Async {
val cursor = collection.find(
BSONDocument(), BSONDocument()).cursor[Patient]
val futureList = cursor.toList
futureList.map { patients => Ok(Json.toJson(patients)) }
}
}
而且我仍在努力使这项工作正常进行。编写函数的任何帮助都会有所帮助。
【问题讨论】:
标签: scala playframework functional-programming