Skip to content

An overload of splitter which takes a predicate and no separator but allows you to keep the separator #10761

@jmdavis

Description

@jmdavis

This code works

void main()
{
    import std.algorithm.comparison : equal;
    import std.algorithm.iteration : splitter;
    import std.algorithm.searching : canFind;

    auto range = "16x16+0-2".splitter!(a => "x+-".canFind(a))();
    assert(equal(range, ["16", "16", "0", "2"]));
}

However, there it strips out the separators, so in the case that someone actually wants to keep them, doesn't work. The overloads of splitter which take the separator as an argument give the ability to keep the separator. So, a solution can be hacked together with something like

void main()
{
    import std.algorithm.comparison : equal;
    import std.algorithm.iteration : splitter;
    import std.algorithm.searching : canFind;
    import std.typecons : Yes;

    auto range = "16x16+0-2".splitter!((a, b) => "x+-".canFind(a), Yes.keepSeparators)('x');
    assert(equal(range, ["16", "x", "16", "+", "0", "-", "2"]));
}

but it does involve passing in a separator that's ignored, which isn't ideal.

I don't know how often it would actually be useful to pass both a predicate and keep the separator, but it did come up in discord when someone wanted to do it, and since the other overload lets you keep the separator, arguably the one that doesn't take an explicit separator should as well.

Either way, it's here as a possible enhancement.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions