popFrontExactly

Eagerly advances r itself (not a copy) exactly n times (by calling r.popFront). popFrontExactly takes r by ref, so it mutates the original range. Completes in O(1) steps for ranges that support slicing, and have either length or are infinite. Completes in O(n) time for all other ranges.

Note: Unlike popFrontN, popFrontExactly will assume that the range holds at least n elements. This makes popFrontExactly faster than popFrontN, but it also means that if range does not contain at least n elements, it will attempt to call popFront on an empty range, which is undefined behavior. So, only use popFrontExactly when it is guaranteed that range holds at least n elements.

popBackExactly will behave the same but instead removes elements from the back of the (bidirectional) range instead of the front.

  1. void popFrontExactly(Range r, size_t n)
    void
    popFrontExactly
    (
    Range
    )
    (
    ref Range r
    ,
    size_t n
    )
    if ()
  2. void popBackExactly(Range r, size_t n)

Examples

1 import std.algorithm.comparison : equal;
2 import std.algorithm.iteration : filterBidirectional;
3 
4 auto a = [1, 2, 3];
5 a.popFrontExactly(1);
6 assert(a == [2, 3]);
7 a.popBackExactly(1);
8 assert(a == [2]);
9 
10 string s = "日本語";
11 s.popFrontExactly(1);
12 assert(s == "本語");
13 s.popBackExactly(1);
14 assert(s == "本");
15 
16 auto bd = filterBidirectional!"true"([1, 2, 3]);
17 bd.popFrontExactly(1);
18 assert(bd.equal([2, 3]));
19 bd.popBackExactly(1);
20 assert(bd.equal([2]));

See Also

Meta

Suggestion Box / Bug Report