List<CompletableFuture> futures = requestList.map(Client::makeRemoteCall).collect(toList);
List<Response> responses = futures.map(CompletableFuture::get).collect(toList);
Basically, we need to start all of the futures, then wait for all the futures. Eliminating the first call to `.collect` would force these remote calls to happen one at a time.While that Java code is neat, you don't need to get anywhere near concurrency to exhibit problems with side effects. Here's a program with two iterator chains and two calls to collect:
let foo = [1, 2, 3];
let bar: Vec<_> = foo.iter()
.map(|x| {
print!("{} ", x);
x + 10
})
.collect();
let qux: Vec<_> = bar.iter()
.map(|x| {
print!("{} ", x);
x * 10
})
.collect();
This program prints "1 2 3 11 12 13 ".Here's that same program, but with the first collect removed and the iterator chains collapsed into one:
let foo = [1, 2, 3];
let bar: Vec<_> = foo.iter()
.map(|x| {
print!("{} ", x);
x + 10
})
.map(|x| {
print!("{} ", x);
x * 10
})
.collect();
This program prints "1 11 2 12 3 13 ".When you have two iterator chains with two calls to collect, everything in the first iterator runs to completion, then the second iterator chain runs. When you collapse those two chains into one, you change the order in which things happen.
It's still true that a smarter type system would be able to track side effects, but like all effects systems, these things are viral, and it's not immediately clear whether the annotation burden makes up for itself in optimization potential.
Actually that's part of why ApplicativeDo was created: https://research.fb.com/publications/desugaring-haskells-do-... It's pretty cool.
A way to mark functions as pure for this purpose would be great! Especially if it's not as fraught as const in C++.
It was also a very, very long time ago, and so today's Rust might be different enough that those reasons don't apply any more.
Haskell can do cool optimizations that make it feel like magic sometimes.
Meanwhile mapping in Rust generally takes an iterator and produces another iterator that will apply the given closure to the current element as it's yielded. So the naive codegen for iter().map().map().map().collect() is exactly what list fusion is trying to produce -- no temporary lists.
TL;DR: making "map" having the monadic `T[U] -> T[V]` signature is really expensive. ¯\_(ツ)_/¯
Ah, yeah I had a vague memory that that was the case, which is why I asked.. I seem to recall it being removed, but I only follow Rust news, don't use it, so I wasn't sure of the reasons or implications.
You just demonstrated what I mean when I comment that const is fraught.
Purity is a different concept.
Purity more easily maps more closely to an abstract concept and is easier to for people reason about.
<Disclaimer: I don't implement compilers so, i can be totally wrong>
The real problem is that you're shoving the variable into a Vec<>, which means that you are doing heap allocation. This means that optimizing it out requires matching the heap allocation to the free, noting that the side effects of these two function calls are only about allocation. There's also issues with respect to inlining, dead-code elimination, and then doing data dependence analysis to prove that the two loops can be fused.