Finding position of a value in an array

Steven Schveighoffer schveiguy at gmail.com
Tue Dec 31 14:41:49 UTC 2019


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.

I have also created a bufref library [1] which is intended to help with 
using phobos algorithms and not losing the positional information.

import std.stdio;
import std.algorithm;
import bufref;

void main()
{
     string x = "hello, world!";
     writeln(x.bwin.find('o').bufRef.pos);
}

-Steve

[1] http://code.dlang.org/packages/bufref


More information about the Digitalmars-d-learn mailing list