View source code
Display the source code in std/range/package.d from which thispage was generated on github.
Report a bug
If you spot a problem with this page, click here to create aGitHub issue.
Improve this page
Quickly fork, edit online, and submit a pull request for this page.Requires a signed-in GitHub account. This works well for small changes.If you'd like to make larger changes you may want to consider usinglocal clone.
Function std.range.chooseAmong
Choose one of multiple ranges at runtime.
auto chooseAmong(Ranges...)(
size_t index,
scope return Ranges rs
)
if (Ranges .length >= 2 && allSatisfy!(isInputRange, staticMap!(Unqual, Ranges)) && !is(CommonType!(staticMap!(ElementType, Ranges)) == void));
The ranges may have different capabilities, but they must have compatible element types. The
result is a range that offers the weakest capabilities of all Ranges.
Parameters
| Name | Description |
|---|---|
| index | which range to choose, must be less than the number of ranges |
| rs | two or more ranges |
Returns
A range type dependent on Ranges.
Example
auto test()
{
import std .algorithm .comparison : equal;
scope arr1 = [1, 2, 3, 4];
scope arr2 = [5, 6];
scope arr3 = [7];
{
auto s = chooseAmong(0, arr1, arr2, arr3);
auto t = s .save;
writeln(s .length); // 4
writeln(s[2]); // 3
s .popFront();
assert(equal(t, only(1, 2, 3, 4)));
}
// result elements can be modified
{
auto s = chooseAmong(1, arr1, arr2, arr3);
writeln(s .length); // 2
s .front = 8;
assert(equal(s, only(8, 6)));
s[1] = 9;
assert(equal(s, only(8, 9)));
// original range was mutated
assert(equal(s, arr2));
}
return 0;
}
// works at runtime
auto a = test();
// and at compile time
static b = test();
Example
Result has minimum capabilities of all given ranges
auto test()
{
import std .algorithm .comparison : equal;
scope arr1 = [1, 2, 3, 4];
scope arr2 = [8, 9];
scope arr3 = [10];
// slicing
{
auto s = chooseAmong(1, arr2, arr1, arr3)[1 .. 3];
writeln(s .length); // 2
assert(equal(s, only(2, 3)));
}
// bidirectional
{
auto s = chooseAmong(0, arr1, arr2, arr3);
writeln(s .length); // 4
writeln(s .back); // 4
s .popBack();
s .back = 5;
assert(equal(s, only(1, 2, 5)));
s .back = 3;
assert(equal(s, only(1, 2, 3)));
}
// range primitives
{
uint[5] foo = [1, 2, 3, 4, 5];
uint[5] bar = [6, 7, 8, 9, 10];
auto c = chooseAmong(1, foo[], bar[]);
writeln(c[3]); // 9
c[3] = 42;
writeln(c[3]); // 42
writeln(c .moveFront()); // 6
writeln(c .moveBack()); // 10
writeln(c .moveAt(4)); // 10
}
// composability
{
import std .range : cycle;
auto s = chooseAmong(0, cycle(arr2), cycle(arr3));
assert(isInfinite!(typeof(s)));
assert(!s .empty);
writeln(s[100]); // 8
writeln(s[101]); // 9
assert(s[0 .. 3] .equal(only(8, 9, 8)));
}
return 0;
}
// works at runtime
auto a = test();
// and at compile time
static b = test();
Authors
Andrei Alexandrescu, David Simcha, Jonathan M Davis, and Jack Stouffer. Credit for some of the ideas in building this module goes to Leonardo Maffi.
License
Copyright © 1999-2026 by the D Language Foundation | Page generated by ddox.