[std.algorithm.iteration] Tweak splitter, splitWhen docs (#10733)

Update splitter cheat sheet for non-separator overloads.
Add constraint for splitWhen's `pred`.
`splitter!isTerminator` always returns a forward range.
This commit is contained in:
Nick Treleaven 2025-04-02 13:37:50 +01:00 committed by GitHub
parent ff612b867c
commit d9fd09d73a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -52,7 +52,7 @@ $(T2 reduce,
$(T2 splitWhen, $(T2 splitWhen,
Lazily splits a range by comparing adjacent elements.) Lazily splits a range by comparing adjacent elements.)
$(T2 splitter, $(T2 splitter,
Lazily splits a range by a separator.) Lazily splits a range by a separator, element predicate or whitespace.)
$(T2 substitute, $(T2 substitute,
`[1, 2].substitute(1, 0.1)` returns `[0.1, 2]`.) `[1, 2].substitute(1, 0.1)` returns `[0.1, 2]`.)
$(T2 sum, $(T2 sum,
@ -3010,10 +3010,10 @@ if (isInputRange!Range)
/** /**
Splits a forward range into subranges in places determined by a binary Splits a forward range into subranges in places determined by a binary
predicate. predicate called with adjacent elements.
When iterating, one element of `r` is compared with `pred` to the next When iterating, one element of `r` is compared with `pred` to the next
element. If `pred` return true, a new subrange is started for the next element. element. If `pred` returns true, a new subrange is started for the next element.
Otherwise, they are part of the same subrange. Otherwise, they are part of the same subrange.
If the elements are compared with an inequality (!=) operator, consider If the elements are compared with an inequality (!=) operator, consider
@ -3023,7 +3023,8 @@ Params:
pred = Predicate for determining where to split. The earlier element in the pred = Predicate for determining where to split. The earlier element in the
source range is always given as the first argument. source range is always given as the first argument.
r = A $(REF_ALTTEXT forward range, isForwardRange, std,range,primitives) to be split. r = A $(REF_ALTTEXT forward range, isForwardRange, std,range,primitives) to be split.
Returns: a range of subranges of `r`, split such that within a given subrange,
Returns: A range of subranges of `r`, split such that within a given subrange,
calling `pred` with any pair of adjacent elements as arguments returns `false`. calling `pred` with any pair of adjacent elements as arguments returns `false`.
Copying the range currently has reference semantics, but this may change in the future. Copying the range currently has reference semantics, but this may change in the future.
@ -3033,7 +3034,7 @@ relations.
*/ */
auto splitWhen(alias pred, Range)(Range r) auto splitWhen(alias pred, Range)(Range r)
if (isForwardRange!Range) if (is(typeof(binaryFun!pred(r.front, r.front)) : bool) && isForwardRange!Range)
{ import std.functional : not; { import std.functional : not;
return ChunkByImpl!(not!pred, not!pred, GroupingOpType.binaryAny, Range)(r); return ChunkByImpl!(not!pred, not!pred, GroupingOpType.binaryAny, Range)(r);
} }
@ -5537,9 +5538,9 @@ Returns:
one separator is given, the result is a range with two empty elements. one separator is given, the result is a range with two empty elements.
See_Also: See_Also:
$(REF _splitter, std,regex) for a version that splits using a regular expression defined separator, - $(REF _splitter, std,regex) for a version that splits using a regular expression defined separator.
$(REF _split, std,array) for a version that splits eagerly and - $(REF _split, std,array) for a version that splits eagerly.
$(LREF splitWhen), which compares adjacent elements instead of element against separator. - $(LREF splitWhen), which compares adjacent elements instead of element against separator.
*/ */
auto splitter(alias pred = "a == b", auto splitter(alias pred = "a == b",
Flag!"keepSeparators" keepSeparators = No.keepSeparators, Flag!"keepSeparators" keepSeparators = No.keepSeparators,
@ -6318,8 +6319,7 @@ Params:
deciding where to split the range. deciding where to split the range.
Returns: Returns:
An $(REF_ALTTEXT input range, isInputRange, std,range,primitives) of slices of A forward range of slices of the original range split by whitespace.
the original range split by whitespace.
+/ +/
auto splitter(alias isTerminator, Range)(Range r) auto splitter(alias isTerminator, Range)(Range r)
if (isForwardRange!Range && is(typeof(unaryFun!isTerminator(r.front)))) if (isForwardRange!Range && is(typeof(unaryFun!isTerminator(r.front))))