Finding position of a value in an array

H. S. Teoh hsteoh at
Tue Dec 31 19:52:43 UTC 2019

On Tue, Dec 31, 2019 at 09:41:49AM -0500, Steven Schveighoffer via Digitalmars-d-learn wrote:
> On 12/30/19 6:15 PM, JN wrote:
> > On Sunday, 29 December 2019 at 08:31:13 UTC, mipri wrote:
> > > 
> > > int i = a.countUntil!(v => v == 55);
> > > assert(i == 2);
> > 
> > I also had to ask because I couldn't find it. In other languages
> > it's named "index()", "indexOf()" or "find()". D is the only
> > language I know which uses the "countUntil" scheme. And even so it's
> > not obvious from the name if it's the index of the element or number
> > of preceding elements.
> > 
> indexOf used to be in std.algorithm I believe. It was nixed for having
> too simple an implementation I believe.

No, it still exists as std.string.indexOf.

IIRC, the story goes like this: countUntil was the original way of doing
this, but, no thanks to autodecoding, it returns the wrong value for
strings.  So indexOf was introduced to fix the problem for strings, but
since it was string-specific it was added to std.string instead.

Either that, or indexOf already existed in std.string, but no thanks to
autodecoding we couldn't generalize it to ranges without breaking code,
so the range version was named countUntil instead.

Either way, it's yet another reason to kill autodecoding with fire (and
extreme prejudice).


Indifference will certainly be the downfall of mankind, but who cares? -- Miquel van Smoorenburg

More information about the Digitalmars-d-learn mailing list