我正在研究scalaz的析取类型,我注意到了方法ap
/** Apply a function in the environment of the right of this disjunction. */
def ap[AA >: A, C](f: => AA \/ (B => C)): (AA \/ C) =
f flatMap (ff => map(ff(_)))
我想我明白它的作用.现在我想知道何时以及为什么要实际使用它?有没有使用此ap功能的例子?
免责声明:下面的代码片段与正在进行的Coursera课程之一相关.我们认为它只是出于学习目的而发布,不应该用于提交作为家庭作业的解决方案.
正如下面的评论所述,我们需要将Futures列表转换为列表的单个Future.更重要的是,如果至少有一个输入期货失败,那么最终的未来将会失败.
我遇到了以下实现,我完全不明白.
/** Given a list of futures `fs`, returns the future holding the list of values of all the futures from `fs`.
* The returned future is completed only once all of the futures in `fs` have been completed.
* The values in the list are in the same order as corresponding futures `fs`.
* If any of the futures `fs` fails, the resulting future also fails.
*/
def all[T](fs: List[Future[T]]): Future[List[T]] =
fs.foldRight(Future(Nil:List[T]))((f, fs2) =>
for { …Run Code Online (Sandbox Code Playgroud)