Reference

std/parallel

One thing going faster: Parallel, a pipeline whose stages run on the workers of the pool, the parallel() that starts one from anything that can be iterated, and Cut, what a collection implements to be cut into chunks without being copied first.

const squares = (0..1000).parallel().map({ _ * _ }).toList().await()
print squares.length()

The words are the ones of the sequential pipeline - map, filter, filterMap, collect, toList, count, sum, minBy, maxBy, find, forEach - and a terminal answers a Task, because the work runs while the caller's worker is free to do something else. Results arrive in input order, always: the input is cut into chunks whose borders depend on its length alone (at most 64 of them), every chunk runs as a task an idle worker may take, and the chunks are read back in order. So the answer never depends on the number of workers, the load of the machine or which chunk finished first (docs/design/CONCURRENCY.md section 4).

Where the chunks come from. A Cut cuts itself: a List and an Array into lists of their items, a Range of integers arithmetically into smaller ranges - (0..1000000).parallel() never builds a list of a million numbers. Everything else that can be iterated - a Set, a Map, the characters of a String, a pipeline, a type of the program's own - is read once into a list, which is then cut the same way. Either way the borders are the same function of the length, so the answer is too.

One pass per chunk. The stages of a pipeline are fused: map followed by filter runs every chunk once, item by item through both closures, and builds no list between them. What a chunk hands back is what the terminal keeps of it - its items for toList, a number for count and sum, at most one item for find, minBy and maxBy.

Modules

Everything