Given a list of functions, where Context is some custom type:
List<Function<Context, CompletableFuture<Context>>> preprocessors;
I need to get a CompletableFuture<Context> having executed every single of those function in order feeding the async result of every iteration into the next one. So I need a general solution (for a variable list size) for the following statement with 3 elements in the list:
Context context;
CompletableFuture<Context> promise = preprocessors.get(0).apply(context)
.thenCompose((c) -> preprocessors.get(1).apply(c))
.thenCompose((c) -> preprocessors.get(2).apply(c));
Any ideas?
To get rid of indices, the above could also look like this:
Iterator<Function<Context, CompletableFuture<Context>>> it = preprecessors.iterator();
Context context;
CompletableFuture<Context> promise = it.next().apply(context)
.thenCompose((c) -> it.next().apply(c))
.thenCompose((c) -> it.next().apply(c));
Still, how do I generalise this further into a variable element count?