Thread overview
Suggestion: needleless findSplit* methods
Dec 12, 2017
Piotr Mitana
Dec 12, 2017
bauss
Dec 12, 2017
bauss
Dec 12, 2017
Seb
Dec 12, 2017
Jonathan M Davis
December 12, 2017
Hello,

I'd like to make a little suggestion for phobos: enrich findSplit* method family with the "needleless" predicate option.

Motivation:

Let's have a range of string tokens that - after cutting of the initial brace - looks like this:

    auto range = ["23", "42", "14.3", "-323", "}"]

Now I want to map all the numbers to the array:

    auto array = range.findSplitBefore(x => !x.isNumeric).map(to!double).array;

As you can see, I do not need the second parameter of prediate, as I am not comparing the subsequent haystack elements with any needle - I check the condition on the haystack elements alone instead. Unfortunately, It's not that simple, because I have only this prototype:

     auto findSplitBefore(alias pred, R1, R2)(R1 haystack, R2 needle) //[...]

That's why I need to add a dummy parameter to achieve what I want to:

    auto array = range.findSplitBefore(x => !x.isNumeric)([""]).map(to!double).array;

So I suggest adding this prototype as well for convenience:

     auto findSplitBefore(alias pred, R)(R haystack) //[...]
December 12, 2017
On Tuesday, 12 December 2017 at 08:19:39 UTC, Piotr Mitana wrote:
> Hello,
>
> I'd like to make a little suggestion for phobos: enrich findSplit* method family with the "needleless" predicate option.
>
> [...]

Either do it yourself and create a pull-request or go here instead of posting in the forums:
https://dlang.org/bugstats.php
December 12, 2017
On Tuesday, 12 December 2017 at 09:17:21 UTC, bauss wrote:
> On Tuesday, 12 December 2017 at 08:19:39 UTC, Piotr Mitana wrote:
>> Hello,
>>
>> I'd like to make a little suggestion for phobos: enrich findSplit* method family with the "needleless" predicate option.
>>
>> [...]
>
> Either do it yourself and create a pull-request or go here instead of posting in the forums:
> https://dlang.org/bugstats.php

Oops, the link was meant to be: https://issues.dlang.org
December 12, 2017
On Tuesday, 12 December 2017 at 08:19:39 UTC, Piotr Mitana wrote:
> Hello,
>
> I'd like to make a little suggestion for phobos: enrich findSplit* method family with the "needleless" predicate option.
>
> [...]


... or you simply use `until`:


auto r = ["23", "42", "14.3", "-323", "}"];
r.until!(x => !x.isNumeric).map!(to!double).writeln;


Runnable example: https://run.dlang.io/is/zNRzpI
December 12, 2017
On Tuesday, December 12, 2017 09:25:57 Seb via Digitalmars-d wrote:
> On Tuesday, 12 December 2017 at 08:19:39 UTC, Piotr Mitana wrote:
> > Hello,
> >
> > I'd like to make a little suggestion for phobos: enrich findSplit* method family with the "needleless" predicate option.
> >
> > [...]
>
> ... or you simply use `until`:
>
>
> auto r = ["23", "42", "14.3", "-323", "}"];
> r.until!(x => !x.isNumeric).map!(to!double).writeln;
>
>
> Runnable example: https://run.dlang.io/is/zNRzpI

You can, but you end up with a different range type that way, whereas with findSplit*, you can retain the original range type with at least some ranges. So, improving findSplit* is a perfectly valid enhancement request. But for some use cases, until will certainly do the job.

- Jonathan M Davis