std.range.take
- multiple declarations
- Function take
- Struct Take
Function take
Lazily takes only
up to
elements of a range. This is
particularly useful when using with infinite ranges. If the range
offers random access and n
length
,
offers them as well.
Take
Prototype
Take!R take(R)( R input, size_t n ) if (isInputRange!(Unqual!R) && !isInfinite!(Unqual!R) && hasSlicing!(Unqual!R) && !is(R T == Take!T));
Example
import std.algorithm : 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 ][]));
Example
If the range runs out before
elements, n
simply returns the entire
range (unlike take
takeExactly
, which will cause an assertion failure if
the range ends prematurely):
import std.algorithm : equal; int[] arr2 = [ 1, 2, 3 ]; auto t = take(arr2, 5); assert(t.length == 3); assert(equal(t, [ 1, 2, 3 ]));
Struct Take
Lazily takes only
up to n
elements of a range. This is
particularly useful when using with infinite ranges. If the range
offers random access and length
,
offers them as well.
Take
Example
import std.algorithm : 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 ][]));
Example
If the range runs out before n
elements,
simply returns the entire
range (unlike take
takeExactly
, which will cause an assertion failure if
the range ends prematurely):
import std.algorithm : equal; int[] arr2 = [ 1, 2, 3 ]; auto t = take(arr2, 5); assert(t.length == 3); assert(equal(t, [ 1, 2, 3 ]));
Authors
Andrei Alexandrescu, David Simcha, and Jonathan M Davis. Credit for some of the ideas in building this module goes to Leonardo Maffi.