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.EvenChunks/evenChunks - multiple declarations

Function evenChunks

This range splits a source range into chunkCount chunks of approximately equal length. Source must be a forward range with known length.

EvenChunks!Source evenChunks(Source) (
  Source source,
  size_t chunkCount
)
if (isForwardRange!Source && hasLength!Source);

Unlike chunks, evenChunks takes a chunk count (not size). The returned range will contain zero or more source.length / chunkCount + 1 elements followed by source.length / chunkCount elements. If source.length < chunkCount, some chunks will be empty.

chunkCount must not be zero, unless source is also empty.

Example

import std.algorithm.comparison : equal;
auto source = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
auto chunks = evenChunks(source, 3);
writeln(chunks[0]); // [1, 2, 3, 4]
writeln(chunks[1]); // [5, 6, 7]
writeln(chunks[2]); // [8, 9, 10]

Struct EvenChunks

This range splits a source range into chunkCount chunks of approximately equal length. Source must be a forward range with known length.

struct EvenChunks(Source)
  
if (isForwardRange!Source && hasLength!Source);

Unlike chunks, evenChunks takes a chunk count (not size). The returned range will contain zero or more source.length / chunkCount + 1 elements followed by source.length / chunkCount elements. If source.length < chunkCount, some chunks will be empty.

chunkCount must not be zero, unless source is also empty.

Constructors

NameDescription
this Standard constructor

Properties

NameTypeDescription
back[get] autoIndexing, slicing and bidirectional operations and range primitives. Provided only if hasSlicing!Source is true.
empty[get] boolForward range primitives. Always present.
front[get] autoForward range primitives. Always present.
length[get] size_tLength
save[get] typeof(this)Forward range primitives. Always present.

Methods

NameDescription
opIndex Indexing, slicing and bidirectional operations and range primitives. Provided only if hasSlicing!Source is true.
opSlice Indexing, slicing and bidirectional operations and range primitives. Provided only if hasSlicing!Source is true.
popBack Indexing, slicing and bidirectional operations and range primitives. Provided only if hasSlicing!Source is true.
popFront Forward range primitives. Always present.

Example

import std.algorithm.comparison : equal;
auto source = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
auto chunks = evenChunks(source, 3);
writeln(chunks[0]); // [1, 2, 3, 4]
writeln(chunks[1]); // [5, 6, 7]
writeln(chunks[2]); // [8, 9, 10]

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.