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.

Members

Aliases

opDollar
alias opDollar = length
moveAt
auto moveAt(size_t index)
moveBack
auto moveBack()
moveFront
auto moveFront()
opIndex
auto ref opIndex(size_t index)
opIndexAssign
void opIndexAssign(ElementType!R v, size_t index)
opSlice
auto opSlice(size_t i, size_t j)
popBack
void popBack()
popFront
void popFront()
back
auto ref back [@property getter]
ElementType!R back [@property setter]

Range primitives

Functions

Properties

empty
bool empty [@property getter]
front
auto ref front [@property getter]
ElementType!R front [@property setter]
length
size_t length [@property getter]

Range primitives

maxLength
size_t maxLength [@property getter]

Access to maximal length of the range. Note: the actual length of the range depends on the underlying range. If it has fewer elements, it will stop before maxLength is reached.

save
Take save [@property getter]

Range primitives

Variables

source
R source;

User accessible in read and write

Return Value

At minimum, an input range. If the range offers random access and length, take offers them as well.

Examples

import std.algorithm.comparison : equal;

int[] arr1 = [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ];
auto s = take(arr1, 5);
assert(s.length == 5);
assert(s[4] == 5);
assert(equal(s, [ 1, 2, 3, 4, 5 ][]));

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);
assert(t.length == 3);
assert(equal(t, [ 1, 2, 3 ]));

Meta