View source code
Display the source code in std/range/package.d from which this page was generated on github.
Report a bug
If you spot a problem with this page, click here to create a Bugzilla 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 using local clone.

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.

Take!R take(R) (
  R input,
  size_t n
)
if (isInputRange!(Unqual!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.

Parameters

NameDescription
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

NameTypeDescription
source CRC.RUser accessible in read and write

Properties

NameTypeDescription
back[get, set] autoRange primitives
empty[get] boolRange primitives
front[get, set] autoRange primitives
length[get] size_tRange primitives
maxLength[get] size_tAccess to maximal length of the range.
save[get] TakeRange primitives

Methods

NameDescription
moveAt (index) Range primitives
moveBack () Range primitives
moveFront () Range primitives
opIndex (index) Range primitives
opIndexAssign (v, index) Range primitives
opSlice (i, j) Range primitives
popBack () Range primitives
popFront () Range primitives

Aliases

NameDescription
opDollar Range primitives

Alias Take

Parameters

NameDescription
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.

License

Boost License 1.0.