std.range.Take/take - multiple declarations
Function take
Lazily takes only up to n elements of a range. This is
particularly useful when using with infinite ranges.
Unlike takeExactly, take does not require that there
are n or more elements in input. As a consequence, length
information is not applied to the result unless input also has
length information.
Parameters
| Name | Description |
|---|---|
| input | an input range
to iterate over up to n times |
| n | the number of elements to take |
Returns
At minimum, an input range. If the range offers random access
and length, take offers them as well.
Example
import std .algorithm .comparison : equal;
int[] arr1 = [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ];
auto s = take(arr1, 5);
writeln(s .length); // 5
writeln(s[4]); // 5
assert(equal(s, [ 1, 2, 3, 4, 5 ][]));
Example
If the range runs out before n elements, take simply returns the entire
range (unlike takeExactly, which will cause an assertion failure if
the range ends prematurely):
import std .algorithm .comparison : equal;
int[] arr2 = [ 1, 2, 3 ];
auto t = take(arr2, 5);
writeln(t .length); // 3
assert(equal(t, [ 1, 2, 3 ]));
Alias/Struct Take
Lazily takes only up to n elements of a range. This is
particularly useful when using with infinite ranges.
struct Take(Range)
if (isInputRange!(Unqual!Range) && !(!isInfinite!(Unqual!Range) && hasSlicing!(Unqual!Range) || is(Range T == Take!T)));
alias Take(R)
= R;
Unlike takeExactly, take does not require that there
are n or more elements in input. As a consequence, length
information is not applied to the result unless input also has
length information.
Struct Take
Fields
| Name | Type | Description |
|---|---|---|
source
|
CRC | User accessible in read and write |
Properties
| Name | Type | Description |
|---|---|---|
back[get, set]
|
auto | Range primitives |
empty[get]
|
bool | Range primitives |
front[get, set]
|
auto | Range primitives |
length[get]
|
size_t | Range primitives |
maxLength[get]
|
size_t | Access to maximal length of the range. |
save[get]
|
Take | Range primitives |
Methods
| Name | Description |
|---|---|
moveAt
|
Range primitives |
moveBack
|
Range primitives |
moveFront
|
Range primitives |
opIndex
|
Range primitives |
opIndexAssign
|
Range primitives |
opSlice
|
Range primitives |
popBack
|
Range primitives |
popFront
|
Range primitives |
Aliases
| Name | Description |
|---|---|
opDollar
|
Range primitives |
Alias Take
Parameters
| Name | Description |
|---|---|
| input | an input range
to iterate over up to n times |
| n | the number of elements to take |
Returns
At minimum, an input range. If the range offers random access
and length, take offers them as well.
Example
import std .algorithm .comparison : equal;
int[] arr1 = [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ];
auto s = take(arr1, 5);
writeln(s .length); // 5
writeln(s[4]); // 5
assert(equal(s, [ 1, 2, 3, 4, 5 ][]));
Example
If the range runs out before n elements, take simply returns the entire
range (unlike takeExactly, which will cause an assertion failure if
the range ends prematurely):
import std .algorithm .comparison : equal;
int[] arr2 = [ 1, 2, 3 ];
auto t = take(arr2, 5);
writeln(t .length); // 3
assert(equal(t, [ 1, 2, 3 ]));
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.