View source code
Display the source code in std/range.d from which this page was generated on
github.
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 using
local clone.
Page wiki
View or edit the community-maintained wiki page associated with this page.
Function std.range.chooseAmong
Choose one of multiple ranges at runtime.
The ranges may be different, but they must have compatible element types. The
result is a range that offers the weakest capabilities of all Ranges
.
Prototype
auto chooseAmong(Ranges...)( size_t index, Ranges rs ) if (Ranges.length > 2 && is(typeof(choose(true, rs[0], rs[1]))) && is(typeof(chooseAmong(0, rs[1..__dollar]))));
Parameters
Name | Description |
---|---|
index | which range to choose , must be less than the number of ranges |
rs | two or more ranges |
Returns
The indexed
range. If rs
consists of only
one range, the return type is an
alias of that range's type.
Example
import std.algorithm : equal; int[] arr1 = [ 1, 2, 3, 4 ]; int[] arr2 = [ 5, 6 ]; int[] arr3 = [ 7 ]; { auto s = chooseAmong(0, arr1, arr2, arr3); auto t = s.save; assert(s.length == 4); assert(s[2] == 3); s.popFront(); assert(equal(t, [1, 2, 3, 4][])); } { auto s = chooseAmong(1, arr1, arr2, arr3); assert(s.length == 2); s.front = 8; assert(equal(s, [8, 6][])); } { auto s = chooseAmong(1, arr1, arr2, arr3); assert(s.length == 2); s[1] = 9; assert(equal(s, [8, 9][])); } { auto s = chooseAmong(1, arr2, arr1, arr3)[1..3]; assert(s.length == 2); assert(equal(s, [2, 3][])); } { auto s = chooseAmong(0, arr1, arr2, arr3); assert(s.length == 4); assert(s.back == 4); s.popBack(); s.back = 5; assert(equal(s, [1, 2, 5][])); s.back = 3; assert(equal(s, [1, 2, 3][])); } { uint[] foo = [1,2,3,4,5]; uint[] bar = [6,7,8,9,10]; auto c = chooseAmong(1,foo, bar); assert(c[3] == 9); c[3] = 42; assert(c[3] == 42); assert(c.moveFront() == 6); assert(c.moveBack() == 10); assert(c.moveAt(4) == 10); } { import std.range : cycle; auto s = chooseAmong(1, cycle(arr2), cycle(arr3)); assert(isInfinite!(typeof(s))); assert(!s.empty); assert(s[100] == 7); }
Authors
Andrei Alexandrescu, David Simcha, and Jonathan M Davis. Credit for some of the ideas in building this module goes to Leonardo Maffi.