我有一个网络请求的未来结果,我需要计算它的大小。如果当前响应有项目,我需要再次请求获取下一组,等等。如果当前响应为空,我就完成了。
我现在拥有的代码:
def list(prefix: String, lastItem: Option[String] = None, last: Seq[BucketItem] = Nil): Future[Iterable[BucketItem]] = {
Logger.debug(s"list :: prefix=$prefix, lastItem=$lastItem, lastItems=${last.size}")
for {
current <- s3.get(name, None, Some(prefix), delimiter, lastItem, None) map listResponse // type is Seq[BucketItem]
next <- list(prefix, Some(current.last.name), last ++ current) if !(current.isEmpty)
} yield last ++ current
}
这似乎工作正常,直到 current 没有更多元素,并且我得到 NoSuchElementException 试图获取 current.last.name
我理解如果 !(current.isEmpty) 扩展到过滤器的条件,所以这不是我真正想要的。我想要的是:
sequentially:
eval current
if current has more items, recursively call list to get the next set of items
yield the concatenated sequence of the right type (all the previous entries plus the last0
我在这里使用 for 理解来轻松处理收集期货(至少这是我过去的做法)。有什么指导/要读的东西吗?我对 scala 还很陌生,所以请温柔一点。