splitter

Lazily splits a range r whenever a predicate isTerminator returns true for an element.

As above, two adjacent separators are considered to surround an empty element in the split range.

  1. auto splitter(Range r, Separator s)
  2. auto splitter(Range r, Separator s)
  3. auto splitter(Range r)
    splitter
    (
    alias isTerminator
    Range
    )
    (
    Range r
    )
    if (
    isForwardRange!Range &&
    is(typeof(unaryFun!isTerminator(r.front)))
    )
  4. auto splitter(Range s)

Parameters

r Range

The forward range to be split.

isTerminator

A unary predicate for deciding where to split the range.

Return Value

Type: auto

A forward range of slices of the original range split by whitespace.

Examples

import std.ascii : isWhite;
import std.algorithm.comparison : equal;
import std.algorithm.iteration : splitter;

string str = "Hello World\t!";
assert(str.splitter!(isWhite).equal(["Hello", "World", "!"]));
import std.algorithm.comparison : equal;
import std.range.primitives : front;

assert(equal(splitter!(a => a == '|')("a|bc|def"), [ "a", "bc", "def" ]));
assert(equal(splitter!(a => a == ' ')("hello  world"), [ "hello", "", "world" ]));

int[] a = [ 1, 2, 0, 0, 3, 0, 4, 5, 0 ];
int[][] w = [ [1, 2], [], [3], [4, 5], [] ];
assert(equal(splitter!(a => a == 0)(a), w));

a = [ 0 ];
assert(equal(splitter!(a => a == 0)(a), [ (int[]).init, (int[]).init ]));

a = [ 0, 1 ];
assert(equal(splitter!(a => a == 0)(a), [ [], [1] ]));

w = [ [0], [1], [2] ];
assert(equal(splitter!(a => a.front == 1)(w), [ [[0]], [[2]] ]));

Meta