diff --git a/std/range/package.d b/std/range/package.d index cf0362ae2ab..5bd5b010064 100644 --- a/std/range/package.d +++ b/std/range/package.d @@ -1574,18 +1574,14 @@ Returns: alias of that range's type. */ 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 .. $])))) +if (Ranges.length >= 2 + && allSatisfy!(isInputRange, staticMap!(Unqual, Ranges)) + && !is(CommonType!(staticMap!(ElementType, Ranges)) == void)) { - return choose(index == 0, rs[0], chooseAmong(index - 1, rs[1 .. $])); -} - -/// ditto -auto chooseAmong(Ranges...)(size_t index, Ranges rs) -if (Ranges.length == 2 && is(typeof(choose(true, rs[0], rs[1])))) -{ - return choose(index == 0, rs[0], rs[1]); + static if (Ranges.length == 2) + return choose(index == 0, rs[0], rs[1]); + else + return choose(index == 0, rs[0], chooseAmong(index - 1, rs[1 .. $])); } /// @@ -1923,11 +1919,23 @@ Returns: and `length`, `take` offers them as well. */ Take!R take(R)(R input, size_t n) -if (isInputRange!(Unqual!R) && !isInfinite!(Unqual!R) && hasSlicing!(Unqual!R) && - !is(R T == Take!T)) +if (isInputRange!(Unqual!R)) { - import std.algorithm.comparison : min; - return input[0 .. min(n, input.length)]; + alias U = Unqual!R; + static if (is(R T == Take!T)) + { + import std.algorithm.comparison : min; + return R(input.source, min(n, input._maxAvailable)); + } + else static if (!isInfinite!U && hasSlicing!U) + { + import std.algorithm.comparison : min; + return input[0 .. min(n, input.length)]; + } + else + { + return Take!R(input, n); + } } /// ditto @@ -2158,21 +2166,6 @@ pure @safe nothrow unittest assert(equal(t, [ 1, 2, 3 ])); } -/// ditto -Take!R take(R)(R input, size_t n) -if (is(R T == Take!T)) -{ - import std.algorithm.comparison : min; - return R(input.source, min(n, input._maxAvailable)); -} - -/// ditto -Take!(R) take(R)(R input, size_t n) -if (isInputRange!(Unqual!R) && (isInfinite!(Unqual!R) || !hasSlicing!(Unqual!R) && !is(R T == Take!T))) -{ - return Take!R(input, n); -} - pure @safe nothrow unittest { import std.algorithm.comparison : equal; @@ -10714,16 +10707,12 @@ private: /// ditto auto refRange(R)(R* range) -if (isInputRange!R && !is(R == class)) -{ - return RefRange!R(range); -} - -/// ditto -auto refRange(R)(R* range) -if (isInputRange!R && is(R == class)) +if (isInputRange!R) { - return *range; + static if (!is(R == class)) + return RefRange!R(range); + else + return *range; } /*****************************************************************************/