Name a phobos function!
Dukc
ajieskola at gmail.com
Mon Mar 15 13:35:24 UTC 2021
I am currently authoring a pull request to Phobos, here:
https://github.com/dlang/phobos/pull/7794. I'm trying to get a
new function added. Currently it's named `splitBy`, and it's
behaviour is probably best described by documentation:
-------------------------
Splits a forward range into subranges in places determined by a
binary predicate. 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. Otherwise, they are
part of the same subrange. If the elements are compared with an
inequality (!=) operator, consider $(LREF chunkBy) instead, as
it's likely faster to execute.
Params:
pred = Predicate for determining where to split. The earlier
element in the source range is always given as the first argument.
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, 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.
See_also:
$(LREF splitter), which uses elements as splitters instead of
element-to-element
relations.
```
auto splitBy(alias pred, Range)(Range r) if
(isForwardRange!Range);
nothrow pure @system unittest
{
import std.algorithm.comparison : equal;
import std.range : dropExactly;
auto source = [4, 3, 2, 11, 0, -3, -3, 5, 3, 0];
auto result1 = source.splitBy!((a,b) => a <= b);
assert(result1.save.equal!equal([
[4, 3, 2],
[11, 0, -3],
[-3],
[5, 3, 0]
]));
//splitBy, like chunkBy, is currently a reference range (this
may change
//in future). Remember to call `save` when appropriate.
auto result2 = result1.dropExactly(2);
assert(result1.save.equal!equal([
[-3],
[5, 3, 0]
]));
}
```
-------------------------
The pull request is currently going well, tests pass and one
approval. Atila seems to be generally receptive, but is "slightly
unsure of the naming". I thought, what a better place to name a
function than the D forum?
The function has already changed name once. Originally it was
named `chunkByAny`, with the predicate being inverted. Credit for
the current name goes to Paul Backus. But can somebody do even
better?
More information about the Digitalmars-d
mailing list