December 31, 2019
On 12/31/19 9:41 AM, Steven Schveighoffer wrote:

> import std.stdio;
> import std.algorithm;
> import bufref;
> 
> void main()
> {
>      string x = "hello, world!";
>      writeln(x.bwin.find('o').bufRef.pos);
> }

for the original example:

    int[] a = [77,66,55,44];
    int i = a.bwin.find(55).bufRef.pos;
    assert(i == 2);

-Steve
December 31, 2019
On 12/31/19 9:47 AM, Steven Schveighoffer wrote:

> for the original example:
> 
>      int[] a = [77,66,55,44];
>      int i = a.bwin.find(55).bufRef.pos;

sorry, should be size_t i.

-Steve
December 31, 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).


T

-- 
Indifference will certainly be the downfall of mankind, but who cares? -- Miquel van Smoorenburg
December 31, 2019
On 12/31/19 2:52 PM, H. S. Teoh wrote:
> 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.

So I looked way way back in history. This predates our usage of github PRs, so all I have is commit messages for a lot of these. I was wrong on the reasoning for it being deprecated, see below.

1. phobos std.string module contained a "find" function, which looked for a dchar in a string. Earlier (probably in Phobos 1, which I never used) I think it was just for char, but was switched to dhcar in 2007 (https://github.com/dlang/phobos/commit/224c570c1b191f4d3701160265961149e174cc71)

2. In https://github.com/dlang/phobos/commit/a4c244f2a8037fb35b3d8c758aa3c60edfc45fbd, it was changed to "indexOf", improving on the API.

3. In https://github.com/dlang/phobos/commit/d90a1a94e0cbb8fbe1e96299c254dd8ec24c9a83 it was switched to be generic based on the character type.

4. In https://github.com/dlang/phobos/commit/3ea2debb8c4a6a707447c684e94f651924efaa96 a range version is added to std.algorithm, which uses only range functions, but has a special version for narrow strings which does what std.string.indexOf does.

5. In https://github.com/dlang/phobos/commit/c2f018066ab649bd7fd3cd6a07aa151a5cea9549 indexOf is renamed to countUntil to disambiguate the two functions (with almost identical results, but different implementations). Note that at this time, countUntil on a string did NOT use autodecoding (it had a special case to make it return the same thing as indexOf).

At some point since then, countUntil was altered to remove the special case for narrow strings, and returned what one might expect.

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

Well, I must agree with your conclusion, even if the evidence doesn't corroborate your story ;)

-Steve
December 31, 2019
On Tuesday, 31 December 2019 at 06:01:36 UTC, Paul Backus wrote:

> countUntil operates on ranges, and static arrays aren't ranges. To get a range from a static array, you have to slice it with the `[]` operator:
>
>     int i = info.doos[].countUntil(important_d);
>
> (Why can't static arrays be ranges? Because ranges can shrink, via popFront, but a static array can never change its length.)

Aha, adding [] made the compiler happy.

February 06, 2021
On Sunday, 29 December 2019 at 08:26:58 UTC, Daren Scot Wilson wrote:
> Reading documentation... Array, Algorithms, ... maybe I've been up too late... how does one obtain the index of, say, 55 in an array like this
>
>     int[] a = [77,66,55,44];
>
> I want to do something like:
>
>     int i = a.find_value_returning_its_index(55);
>     assert(i==2)
>
> I'm sure it's obvious but I'm not seeing it right now.

Just reactivating this post to tell you that I lost 15 minutes of my life searching for a basic way to obtain the position of an element in an array; Out of frustration, I ended to write my own indexOf function. Any library on Earth has such function and it's not named countUntil, nor giveMeTheDamnPositionOfXInThatArray.

It seems that D is specialised in finding needles in haystacks, not finding elements in arrays.

https://issues.dlang.org/show_bug.cgi?id=21615

February 06, 2021
On Saturday, 6 February 2021 at 15:47:05 UTC, Rumbu wrote:
> On Sunday, 29 December 2019 at 08:26:58 UTC, Daren Scot Wilson wrote:
>> [...]
>
> Just reactivating this post to tell you that I lost 15 minutes of my life searching for a basic way to obtain the position of an element in an array; Out of frustration, I ended to write my own indexOf function. Any library on Earth has such function and it's not named countUntil, nor giveMeTheDamnPositionOfXInThatArray.
>
> It seems that D is specialised in finding needles in haystacks, not finding elements in arrays.
>
> https://issues.dlang.org/show_bug.cgi?id=21615

+1
February 06, 2021
On Tuesday, 31 December 2019 at 14:52:55 UTC, Steven Schveighoffer wrote:
> On 12/31/19 9:47 AM, Steven Schveighoffer wrote:
>
>> for the original example:
>> 
>>      int[] a = [77,66,55,44];
>>      int i = a.bwin.find(55).bufRef.pos;
>
> sorry, should be size_t i.
>


Unsigned?

Then how the function signal no such element found?

The convention of indexOf is to use -1.

February 06, 2021
On 2/6/21 2:26 PM, mw wrote:
> On Tuesday, 31 December 2019 at 14:52:55 UTC, Steven Schveighoffer wrote:
>> On 12/31/19 9:47 AM, Steven Schveighoffer wrote:
>>
>>> for the original example:
>>>
>>>      int[] a = [77,66,55,44];
>>>      int i = a.bwin.find(55).bufRef.pos;
>>
>> sorry, should be size_t i.
>>
> 
> 
> Unsigned?
> 
> Then how the function signal no such element found?
> 
> The convention of indexOf is to use -1.
> 

I bet it returns size_t.max in that case.

Ali

February 07, 2021
On Saturday, 6 February 2021 at 22:32:53 UTC, Ali Çehreli wrote:
> On 2/6/21 2:26 PM, mw wrote:
>> On Tuesday, 31 December 2019 at 14:52:55 UTC, Steven Schveighoffer wrote:
>>> On 12/31/19 9:47 AM, Steven Schveighoffer wrote:
>>>
>>>> for the original example:
>>>>
>>>>      int[] a = [77,66,55,44];
>>>>      int i = a.bwin.find(55).bufRef.pos;
>>>
>>> sorry, should be size_t i.
>>>
>> 
>> 
>> Unsigned?
>> 
>> Then how the function signal no such element found?
>> 
>> The convention of indexOf is to use -1.
>> 
>
> I bet it returns size_t.max in that case.


Make it harder to port code from other languages.


1 2 3
Next ›   Last »