Suppose I've got a function op: (Int, Int) => Future[Int]
and need to write a new function foo
:
def foo(xs: Seq[Int],
zero: Int,
op: (Int, Int) => Future[Int]): Future[Int] = ???
foo
should work as foldLeft
and apply op
sequentially to all elements in xs
, e.g.:
val op: (Int, Int) => Future[Int] = (x, y) => Future(x + y)
val xs = (1 to 10)
val fut = foo(xs, 0, op) // should return Future of 55
fut.value // Some(Success(55))
How would you implement foo
?
cats
are useful but I would prefer a plain Scala solution for now. – Disapprobation