Scala flatMap的变体
今天看到了flatMap的变体flatMapM
flatMapM{
_.map(getUserWithKey).getOrElse(Future.failed(UnavailableUserKey))……
点进去后看发现:
/**
*LikeflatMapbutallowstheflatMapfunctiontoexecuteasynchronously.
*
*@paramftheasyncfunctiontoproduceanewbodyparserfromtheresultofthisbodyparser
*@paramecThecontexttoexecutethesuppliedfunctionwith.
* Thecontextispreparedonthecallingthread.
*@returnthetransformedbodyparser
*@see[[flatMap]]
*@see[[play.api.libs.iteratee.Iteratee#flatMapM]]
*/
defflatMapM[B](f:A=>Future[BodyParser[B]])(implicitec:ExecutionContext):BodyParser[B]={
//prepareexecutioncontextasbodyparserobjectmaycrossthreadboundary
implicitvalpec=ec.prepare()
newBodyParser[B]{
defapply(request:RequestHeader)=self(request).flatMapM{
caseRight(a)=>
f(a).map{_.apply(request)}(pec)
caseleft=>
Future.successful{
Done[Array[Byte],Either[Result,B]](left.asInstanceOf[Either[Result,B]])
}
}(pec)
overridedeftoString=self.toString
}
}
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。