vibe.d beta banner
get vibe.d
0.10.1

Asynchronous I/O that doesn’t get in your way, written in D

Function parallelMap

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

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

auto parallelMap(alias fun, R) (
  R items,
  ChannelConfig channel_config = ChannelConfig.init
)
if (isInputRange!R && isWeaklyIsolated!(ElementType!R) && isWeaklyIsolated!(typeof(fun(ElementType!R.init))));

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.

See also

parallelUnorderedMap

Example

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);

Example

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);
Authors

Sönke Ludwig

Copyright

© 2021 Sönke Ludwig

License

Subject to the terms of the MIT license, as written in the included LICENSE.txt file.