www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Suggestion: needleless findSplit* methods

reply Piotr Mitana <the.mail.of.mi2 gmail.com> writes:
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) //[...]
Dec 12 2017
next sibling parent reply bauss <jj_1337 live.dk> writes:
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
Dec 12 2017
parent bauss <jj_1337 live.dk> writes:
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
Dec 12 2017
prev sibling parent reply Seb <seb wilzba.ch> writes:
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
Dec 12 2017
parent Jonathan M Davis <newsgroup.d jmdavisprog.com> writes:
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
Dec 12 2017