View mode: basic / threaded / horizontal-split · Log in · Help
April 27, 2012
generic indexOf() for arrays ?
I'd like to know if there's a generic function over arrays to 
find the index of a specific elemnt therein, something like, say:

int indexOf(S) (in S[] arr, S elem);

which works the same way the std.string.indexOf() function 
works... couldn't find anything in the std.array module for this 
scenario. Would be nice to have this functionality built-in 
somehow.

Or is there a completely different / better approach to this in D?
Thx, M.
April 27, 2012
Re: generic indexOf() for arrays ?
On Friday, 27 April 2012 at 19:49:33 UTC, M.Gore wrote:
> I'd like to know if there's a generic function over arrays to 
> find the index of a specific elemnt therein, something like, 
> say:
>
> int indexOf(S) (in S[] arr, S elem);
>
> which works the same way the std.string.indexOf() function 
> works... couldn't find anything in the std.array module for 
> this scenario. Would be nice to have this functionality 
> built-in somehow.
>
> Or is there a completely different / better approach to this in 
> D?
> Thx, M.

countUntil in std.algorithm should work fine.

http://dlang.org/phobos/std_algorithm.html#countUntil

Regards,
Brad Anderson
April 27, 2012
Re: generic indexOf() for arrays ?
> countUntil in std.algorithm should work fine.
>
> http://dlang.org/phobos/std_algorithm.html#countUntil

Yup, it does! Just didn't see it there in the algorithm 
collection.
Thanks a lot.
April 28, 2012
Re: generic indexOf() for arrays ?
On 4/28/12 3:51 AM, Brad Anderson wrote:
> On Friday, 27 April 2012 at 19:49:33 UTC, M.Gore wrote:
>> I'd like to know if there's a generic function over arrays to find the
>> index of a specific elemnt therein, something like, say:
>>
>> int indexOf(S) (in S[] arr, S elem);
>>
>> which works the same way the std.string.indexOf() function works...
>> couldn't find anything in the std.array module for this scenario.
>> Would be nice to have this functionality built-in somehow.
>>
>> Or is there a completely different / better approach to this in D?
>> Thx, M.
>
> countUntil in std.algorithm should work fine.
>
> http://dlang.org/phobos/std_algorithm.html#countUntil
>
> Regards,
> Brad Anderson

---
sizediff_t indexOf(alias pred = "a == b", R1, R2)(R1 haystack, R2 needle);

Scheduled for deprecation. Please use std.algorithm.countUntil instead.

Same as countUntil. This symbol has been scheduled for deprecation 
because it is easily confused with the homonym function in std.string.
---

But isn't it the same funcionality? Why use a different name for the 
same funcionality?
April 28, 2012
Re: generic indexOf() for arrays ?
On Saturday, April 28, 2012 12:18:57 Ary Manzana wrote:
> But isn't it the same funcionality? Why use a different name for the
> same funcionality?

It's _not_ the same functionality, even if it's similar.

std.string.indexOf varies drastically from std.algorithm.countUntil and in a 
way that easily leads to confusion if they're both named indexOf.

std.array.indexOf returns the index of the string that you passed in. That 
number doesn't say _anything_ about how many characters are before that sub-
string. If str.indexOf("hello") returns 6, that's index 6, but it could be the 
third character rather than the sixth, because strings are UTF-8 and therefore 
variably-lengthed encoded.

std.algorithm.countUntil returns the number of elements in the range up to the 
range passed in and does not necessarily have any relation with any index 
(particularly if the range isn't random access). In the case of 
str.countUntil("hello"), it will return one minus the number of the character 
that "hello" starts at. So, if it's the third character, then the result will 
be 2, whereas the result of std.string.indexOf could have been 6.

Also, the result of indexOf isn't necessarily an index (particularly in the 
case of strings - but also in the case where a range is not random access), so 
calling countUntil indexOf is arguably a poor choice.

- Jonathan M Davis
Top | Discussion index | About this forum | D home