std.algorithm.iteration.splitter - multiple declarations
- Function splitter
- Function splitter
- Function splitter
- Function splitter
Function splitter
Lazily splits a range using an element as a separator. This can be used with any narrow string type or sliceable range type, but is most popular with string types.
Two adjacent separators are considered to surround an empty element in
the split range. Use on the result to compress
empty elements.
filter!(a => !a.empty)
If the empty range is given, the result is a range with one empty element. If a range with one separator is given, the result is a range with two empty elements.
If splitting a string on whitespace and token compression is desired,
consider using without specifying a separator (see fourth overload
below).
splitter
Prototype
auto splitter(alias pred, Range, Separator)( Range r, Separator s ) if (is(typeof(binaryFun!pred(r.front, s)) : bool) && (hasSlicing!Range && hasLength!Range || isNarrowString!Range));
Parameters
| Name | Description |
|---|---|
| pred | The predicate for comparing each element with the separator,
defaulting to "a == b". |
| r | The input range to be
split. Must support slicing and .length. |
| s | The element to be treated as the separator between range segments to be split. |
Constraints
The predicate pred needs to accept an element of and the
separator r.
s
Returns
An input range of the subranges of elements between separators. If
is a forward range or bidirectional range, the returned range will be
likewise.
r
See Also
std.regex.splitter for a version that splits using a regular expression defined separator.
Example
import std.algorithm.comparison : equal; assert(equal(splitter("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, 0), w)); a = [ 0 ]; assert(equal(splitter(a, 0), [ (int[]).init, (int[]).init ])); a = [ 0, 1 ]; assert(equal(splitter(a, 0), [ [], [1] ])); w = [ [0], [1], [2] ]; assert(equal(splitter!"a.front == b"(w, 1), [ [[0]], [[2]] ]));
Function splitter
Similar to the previous overload of , except this one uses another
range as a separator. This can be used with any narrow string type or sliceable
range type, but is most popular with string types.
splitter
Two adjacent separators are considered to surround an empty element in
the split range. Use on the result to compress
empty elements.
filter!(a => !a.empty)
Prototype
auto splitter(alias pred, Range, Separator)( Range r, Separator s ) if (is(typeof(binaryFun!pred(r.front, s.front)) : bool) && (hasSlicing!Range || isNarrowString!Range) && isForwardRange!Separator && (hasLength!Separator || isNarrowString!Separator));
Parameters
| Name | Description |
|---|---|
| pred | The predicate for comparing each element with the separator,
defaulting to "a == b". |
| r | The input range to be split. |
| s | The forward range to
be treated as the separator between segments of to be split. |
Constraints
The predicate pred needs to accept an element of and an
element of r.
s
Returns
An input range of the subranges of elements between separators. If
is a forward range or bidirectional range, the returned range will be
likewise.
r
See Also
std.regex.splitter for a version that splits using a regular expression defined separator.
Example
import std.algorithm.comparison : equal; assert(equal(splitter("hello world", " "), [ "hello", "world" ])); int[] a = [ 1, 2, 0, 0, 3, 0, 4, 5, 0 ]; int[][] w = [ [1, 2], [3, 0, 4, 5, 0] ]; assert(equal(splitter(a, [0, 0]), w)); a = [ 0, 0 ]; assert(equal(splitter(a, [0, 0]), [ (int[]).init, (int[]).init ])); a = [ 0, 0, 1 ]; assert(equal(splitter(a, [0, 0]), [ [], [1] ]));
Function splitter
Similar to the previous overload of , except this one does not use a separator.
Instead, the predicate is an unary function on the splitterinput range's element type.
Two adjacent separators are considered to surround an empty element in
the split range. Use on the result to compress
empty elements.
filter!(a => !a.empty)
Prototype
auto splitter(alias isTerminator, Range)( Range input ) if (isForwardRange!Range && is(typeof(unaryFun!isTerminator(input.front))));
Parameters
| Name | Description |
|---|---|
| isTerminator | The predicate for deciding where to split the range. |
| input | The input range to
be split. |
Constraints
The predicate isTerminator needs to accept an element of .
input
Returns
An input range of the subranges of elements between separators. If
is a forward range or bidirectional range, the returned range will be
likewise.
input
See Also
std.regex.splitter for a version that splits using a regular expression defined separator.
Example
import std.algorithm.comparison : equal; assert(equal(splitter!"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 == 0"(a), w)); a = [ 0 ]; assert(equal(splitter!"a == 0"(a), [ (int[]).init, (int[]).init ])); a = [ 0, 1 ]; assert(equal(splitter!"a == 0"(a), [ [], [1] ])); w = [ [0], [1], [2] ]; assert(equal(splitter!"a.front == 1"(w), [ [[0]], [[2]] ]));
Function splitter
Lazily splits the string into words, using whitespace as the delimiter.
s
This function is string specific and, contrary to
, runs of whitespace will be merged together
(no empty tokens will be produced).
splitter!(std.uni.isWhite)
Prototype
auto splitter(C)( C[] s ) if (isSomeChar!C);
Parameters
| Name | Description |
|---|---|
| s | The string to be split. |
Returns
An input range of slices of the original string split by whitespace.
Example
import std.algorithm.comparison : equal; auto a = " a bcd ef gh "; assert(equal(splitter(a), ["a", "bcd", "ef", "gh"][]));