You don't use the variable next
so there is no way you can get the result of s3.get
calls after the first one. Also, your if !(current.isEmpty)
will invoke withFilter
on a Future
value, which could produce a failed Future
, which is not what you want here.
Here is a simpler solution:
def list(prefix: String, lastItem: Option[String] = None, accum: Seq[BucketItem] = Vector()): Future[Iterable[BucketItem]] = {
s3.get(name, None, Some(prefix), delimiter, lastItem, None) flatMap { current =>
if (current.isEmpty)
Future.successful(accum)
else
list(prefix, bucketItems.last.name, accum ++ current)
}
}
A for-comprehension can only represent a map
nested inside of flatMap
calls (or foreach
calls inside of each other) with interspersed calls to withFilter
. All we need here is a single call to flatMap
, so there is no use for for
.
I have also replaced List
with Vector
, it performs better when appending elements to the end.