parallelMap

Processes a range of items in worker tasks and returns them as an ordered range.

The items of the returned stream are in the same order as input. Note that this may require dynamic buffering of results, so it is recommended to use unordered mapping if possible.

  1. auto parallelMap(R items, shared(TaskPool) task_pool, ChannelConfig channel_config)
  2. auto parallelMap(R items, ChannelConfig channel_config)
    parallelMap
    (
    alias fun
    R
    )
    if (
    isInputRange!R &&
    isWeaklyIsolated!(ElementType!R)
    &&
    isWeaklyIsolated!(typeof(fun(ElementType!R.init)))
    )

Examples

import std.algorithm : map;
import std.array : array;
import std.range : iota;

auto res = iota(100)
	.parallelMap!(i => 2 * i)
	.array;
assert(res == iota(100).map!(i => 2 * i).array);
import std.algorithm : isPermutation, map;
import std.array : array;
import std.random : uniform;
import std.range : iota;
import core.time : msecs;
import vibe.core.core : sleep;

// forcing a random computation result order still results in the same
// output order
auto res = iota(100)
	.parallelMap!((i) {
		sleep(uniform(0, 100).msecs);
		return 2 * i;
	})
	.array;
assert(res == iota(100).map!(i => 2 * i).array);

See Also

parallelUnorderedMap

Meta