splitter

Similar to the previous overload of splitter, except this one does not use a separator. Instead, the predicate is an unary function on the input range's element type. The isTerminator predicate is passed to std.functional.unaryFun and can either accept a string, or any callable that can be executed via pred(element, s).

Two adjacent separators are considered to surround an empty element in the split range. Use filter!(a => !a.empty) on the result to compress empty elements.

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

Parameters

isTerminator

The predicate for deciding where to split the range.

input
Type: Range

The input range to be split.

Constraints: The predicate isTerminator needs to accept an element of input.

Return Value

Type: auto

An input range of the subranges of elements between separators. If input is a forward range or bidirectional range, the returned range will be likewise.

Examples

1 import std.algorithm.comparison : equal;
2 import std.range.primitives : front;
3 
4 assert(equal(splitter!(a => a == ' ')("hello  world"), [ "hello", "", "world" ]));
5 int[] a = [ 1, 2, 0, 0, 3, 0, 4, 5, 0 ];
6 int[][] w = [ [1, 2], [], [3], [4, 5], [] ];
7 assert(equal(splitter!(a => a == 0)(a), w));
8 a = [ 0 ];
9 assert(equal(splitter!(a => a == 0)(a), [ (int[]).init, (int[]).init ]));
10 a = [ 0, 1 ];
11 assert(equal(splitter!(a => a == 0)(a), [ [], [1] ]));
12 w = [ [0], [1], [2] ];
13 assert(equal(splitter!(a => a.front == 1)(w), [ [[0]], [[2]] ]));

See Also

std.regex._splitter for a version that splits using a regular expression defined separator.

Meta