Re: Finding position of a value in an array

2021-02-06 Thread mw via Digitalmars-d-learn

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.




Re: Finding position of a value in an array

2021-02-06 Thread Ali Çehreli via Digitalmars-d-learn

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



Re: Finding position of a value in an array

2021-02-06 Thread mw via Digitalmars-d-learn
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.



Re: Finding position of a value in an array

2021-02-06 Thread Imperatorn via Digitalmars-d-learn

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


Re: Finding position of a value in an array

2021-02-06 Thread Rumbu via Digitalmars-d-learn
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



Re: Finding position of a value in an array

2019-12-31 Thread Daren Scot Wilson via Digitalmars-d-learn

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.



Re: Finding position of a value in an array

2019-12-31 Thread Steven Schveighoffer via Digitalmars-d-learn

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


Re: Finding position of a value in an array

2019-12-31 Thread H. S. Teoh via Digitalmars-d-learn
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


Re: Finding position of a value in an array

2019-12-31 Thread Steven Schveighoffer via Digitalmars-d-learn

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


Re: Finding position of a value in an array

2019-12-31 Thread Steven Schveighoffer via Digitalmars-d-learn

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


Re: Finding position of a value in an array

2019-12-31 Thread Steven Schveighoffer via Digitalmars-d-learn

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


Re: Finding position of a value in an array

2019-12-31 Thread bachmeier via Digitalmars-d-learn
On Tuesday, 31 December 2019 at 04:38:53 UTC, Daren Scot Wilson 
wrote:

On Monday, 30 December 2019 at 23:15:48 UTC, 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.



I had first tried myarray.index(myvalue) because I coulda sworn 
I wrote exactly that syntax a only a few days ago.  But I've 
been hopping between languages, some D, some Crystal, some C++, 
some Javascript, and with only two cerebral hemispheres, maybe 
I got confused. So, D doesn't have index()?  Is it called 
find()?  Something else?  It was hard to find the right stuff 
in the documentation.


You may have used indexOf, which works with strings. Why not 
anything else? No idea, as it really should work with other 
arguments.


Re: Finding position of a value in an array

2019-12-31 Thread MoonlightSentinel via Digitalmars-d-learn

On Monday, 30 December 2019 at 23:15:48 UTC, JN wrote:
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.


The main difference to other languages is that countUntil works 
an arbitrary ForwardRanges which might not offer random access, 
hence index would be misleading for them.


But improvements to the documentation could probably help to 
alleviate this confusion.


Re: Finding position of a value in an array

2019-12-30 Thread Paul Backus via Digitalmars-d-learn
On Tuesday, 31 December 2019 at 04:38:53 UTC, Daren Scot Wilson 
wrote:
 I'm needing to see more examples.  It might have something to 
do with the array I'm working with being inside a foreach loop. 
My code looks like this, with the problematic line duplicated 
to show some of the variations I tried:


import std.algorithm;

alias doo = ubyte;
struct Info
{
int x;
doo[NDOOS] doos;   // NDOOS = 10
}

immutable int INFO = 10;
Info[NINFOS]  infos;// global array, used heavily.

float foo_function(doo important_d){
  ...
  foreach (info; infos){

int i = info.doos.index(important_d);  
// #1
int i = info.doos.countUntil(d => d == important_d);   
// #2
int i = info.doos.countUntil!(d => d == important_d);  
// #3
int i = countUntil(d => d == important_d, info.doos);  
// #4
int i = countUntil!(d => d == important_d)(info.doos); 
// #5


if (i>=0)  {  // assuming -1 represents value not found
 ... do stuff with i ...
}
  }
  ...
}

All the lines shown give me

"template ... cannot deduce function from argument types..."

but one time I got, but cannot reproduce now, the error
"Error: template instance countUntil!((d) => d == 
important_d, doos) has no value"


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.)


Re: Finding position of a value in an array

2019-12-30 Thread Daren Scot Wilson via Digitalmars-d-learn

On Monday, 30 December 2019 at 23:15:48 UTC, 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.



I had first tried myarray.index(myvalue) because I coulda sworn I 
wrote exactly that syntax a only a few days ago.  But I've been 
hopping between languages, some D, some Crystal, some C++, some 
Javascript, and with only two cerebral hemispheres, maybe I got 
confused. So, D doesn't have index()?  Is it called find()?  
Something else?  It was hard to find the right stuff in the 
documentation.


So now I know about countUntil, and I'm glad the question has 
gotten some traction for others to make progress with their code. 
 I'm needing to see more examples.  It might have something to do 
with the array I'm working with being inside a foreach loop. My 
code looks like this, with the problematic line duplicated to 
show some of the variations I tried:


import std.algorithm;

alias doo = ubyte;
struct Info
{
int x;
doo[NDOOS] doos;   // NDOOS = 10
}

immutable int INFO = 10;
Info[NINFOS]  infos;// global array, used heavily.

float foo_function(doo important_d){
  ...
  foreach (info; infos){

int i = info.doos.index(important_d);  // 
#1
int i = info.doos.countUntil(d => d == important_d);   // 
#2
int i = info.doos.countUntil!(d => d == important_d);  // 
#3
int i = countUntil(d => d == important_d, info.doos);  // 
#4
int i = countUntil!(d => d == important_d)(info.doos); // 
#5


if (i>=0)  {  // assuming -1 represents value not found
 ... do stuff with i ...
}
  }
  ...
}

All the lines shown give me

"template ... cannot deduce function from argument types..."

but one time I got, but cannot reproduce now, the error
"Error: template instance countUntil!((d) => d == 
important_d, doos) has no value"




Re: Finding position of a value in an array

2019-12-30 Thread JN via Digitalmars-d-learn

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.




Re: Finding position of a value in an array

2019-12-30 Thread mipri via Digitalmars-d-learn

On Monday, 30 December 2019 at 19:46:50 UTC, Ron Tarrant wrote:

Thanks, mipri. Got it sorted. Here's a working proof...

```
import std.stdio;
import std.algorithm;
import std.conv;

void main(string[] args)
{
MyObject[] objectArray;
MyObject newObject;
MyObject findPointer;
long index;

int lastObjectID = 7;
int foundObjectIndex;

for(int i; i < 12; i++)
{
lastObjectID++;
newObject = new MyObject(lastObjectID);
objectArray ~= newObject;

if(i is 5)
{
findPointer = newObject;
}
}

for(int i; i < objectArray.length; i++)
{
		writeln("object: ", cast(MyObject*)objectArray[i], ", ID: ", 
objectArray[i].objectID);

}

index = objectArray.countUntil(findPointer);
	writeln("findPointer: ", findPointer, ", at address: ", 
cast(MyObject*)findPointer, " is a MyObject pointer in the 
objectArray with an index of ", index, ", address: ", 
cast(MyObject*)objectArray[index], ", ID: ", 
objectArray[index].objectID);


} // main()


class MyObject
{
int objectID;

this(int ordinal)
{
objectID = ordinal;

} // this()

} // class MyObject

```


Compare:

  import std.stdio;
  import std.algorithm;
  import std.conv;

  void main(string[] args)
  {
MyObject[] objectArray;
MyObject newObject;
MyObject findPointer;
long index;

int foundObjectIndex;

for(int i; i < 12; i++)
{
newObject = new MyObject();
objectArray ~= newObject;

if(i is 5)
{
findPointer = newObject;
}
}

for(int i; i < objectArray.length; i++)
{
writeln("object: ", cast(MyObject*)objectArray[i]);
}

index = objectArray.countUntil(findPointer);
  	writeln("findPointer: ", findPointer, ", at address: ", 
cast(MyObject*)findPointer, " is a MyObject pointer in the 
objectArray with an index of ", index, ", address: ", 
cast(MyObject*)objectArray[index]);


  } // main()


  class MyObject {}

With output:

  object: 7F2DC37C3000
  object: 7F2DC37C3020
  object: 7F2DC37C3030
  object: 7F2DC37C3040
  object: 7F2DC37C3050
  object: 7F2DC37C3060
  object: 7F2DC37C3070
  object: 7F2DC37C3080
  object: 7F2DC37C3090
  object: 7F2DC37C30A0
  object: 7F2DC37C30B0
  object: 7F2DC37C30C0
  findPointer: x297.MyObject, at address: 7F2DC37C3060 is a 
MyObject pointer in the objectArray with an index of 5, address: 
7F2DC37C3060




Re: Finding position of a value in an array

2019-12-30 Thread Ron Tarrant via Digitalmars-d-learn

On Monday, 30 December 2019 at 19:39:04 UTC, mipri wrote:

You can definitely do it:

  $ rdmd --eval 'int a, b, c; [, , 
].countUntil().writeln'

  2

But you need to have an array of pointers.


Thanks, mipri. Got it sorted. Here's a working proof...

```
import std.stdio;
import std.algorithm;
import std.conv;

void main(string[] args)
{
MyObject[] objectArray;
MyObject newObject;
MyObject findPointer;
long index;

int lastObjectID = 7;
int foundObjectIndex;

for(int i; i < 12; i++)
{
lastObjectID++;
newObject = new MyObject(lastObjectID);
objectArray ~= newObject;

if(i is 5)
{
findPointer = newObject;
}
}

for(int i; i < objectArray.length; i++)
{
		writeln("object: ", cast(MyObject*)objectArray[i], ", ID: ", 
objectArray[i].objectID);

}

index = objectArray.countUntil(findPointer);
	writeln("findPointer: ", findPointer, ", at address: ", 
cast(MyObject*)findPointer, " is a MyObject pointer in the 
objectArray with an index of ", index, ", address: ", 
cast(MyObject*)objectArray[index], ", ID: ", 
objectArray[index].objectID);


} // main()


class MyObject
{
int objectID;

this(int ordinal)
{
objectID = ordinal;

} // this()

} // class MyObject

```



Re: Finding position of a value in an array

2019-12-30 Thread Ron Tarrant via Digitalmars-d-learn

On Monday, 30 December 2019 at 19:46:50 UTC, Ron Tarrant wrote:


Thanks, mipri. Got it sorted. Here's a working proof...


Forgot to show the output:

object: 17B0A831000, ID: 8
object: 17B0A831020, ID: 9
object: 17B0A831060, ID: 10
object: 17B0A831080, ID: 11
object: 17B0A8310A0, ID: 12
object: 17B0A8310C0, ID: 13
object: 17B0A8310E0, ID: 14
object: 17B0A831100, ID: 15
object: 17B0A831120, ID: 16
object: 17B0A831140, ID: 17
object: 17B0A831160, ID: 18
object: 17B0A831180, ID: 19
findPointer: find_in_array_object.MyObject, at address: 
17B0A8310C0 is a MyObject pointer in the objectArray with an 
index of 5, address: 17B0A8310C0, ID: 13


Re: Finding position of a value in an array

2019-12-30 Thread mipri via Digitalmars-d-learn

On Monday, 30 December 2019 at 19:08:27 UTC, Ron Tarrant wrote:
On Monday, 30 December 2019 at 17:12:26 UTC, MoonlightSentinel 
wrote:


D disallows implicit conversion from integers to pointers and 
hence they cannot be compared. You would need to explicitly 
cast your ulong to an appropriate pointer type


I'm not trying to convert, just wade through an array of 
pointers to find a specific pointer using searchUntil().


I mean, it's not a big deal if I can't do it.


You can definitely do it:

  $ rdmd --eval 'int a, b, c; [, , ].countUntil().writeln'
  2

But you need to have an array of pointers.


Re: Finding position of a value in an array

2019-12-30 Thread Ron Tarrant via Digitalmars-d-learn

On Monday, 30 December 2019 at 19:08:27 UTC, Ron Tarrant wrote:

I'm not trying to convert, just wade through an array of 
pointers to find a specific pointer using searchUntil().


** that should read: countUntil(), not searchUntil() *

Turns out I was getting too complicated. countUntil() works on 
object references as well, even if printing object references to 
a shell doesn't seem to differentiate between them.


Re: Finding position of a value in an array

2019-12-30 Thread Ron Tarrant via Digitalmars-d-learn
On Monday, 30 December 2019 at 17:12:26 UTC, MoonlightSentinel 
wrote:


D disallows implicit conversion from integers to pointers and 
hence they cannot be compared. You would need to explicitly 
cast your ulong to an appropriate pointer type


I'm not trying to convert, just wade through an array of pointers 
to find a specific pointer using searchUntil().


I mean, it's not a big deal if I can't do it. Adding an ID 
property and searching on that is just as effective and likely 
just as efficient.


Re: Finding position of a value in an array

2019-12-30 Thread Ron Tarrant via Digitalmars-d-learn

On Monday, 30 December 2019 at 14:41:55 UTC, mipri wrote:


What's your code? 'find_in_array_object.MyObject' doesn't look
like a pointer.


It's an array of objects... or, what it's trying to be, an array 
of object pointers.


Re: Finding position of a value in an array

2019-12-30 Thread MoonlightSentinel via Digitalmars-d-learn

On Monday, 30 December 2019 at 14:30:12 UTC, Ron Tarrant wrote:
I was trying to do this with an array of pointers, but I get an 
error (which suggests to me that I don't know what data type a 
pointer is):


It's not a ulong? Have I forgotten so much?


D disallows implicit conversion from integers to pointers and 
hence they cannot be compared. You would need to explicitly cast 
your ulong to an appropriate pointer type


Re: Finding position of a value in an array

2019-12-30 Thread mipri via Digitalmars-d-learn

On Monday, 30 December 2019 at 14:30:12 UTC, Ron Tarrant wrote:
On Sunday, 29 December 2019 at 09:44:18 UTC, MoonlightSentinel 
wrote:



int i = a.countUntil(55);



I was trying to do this with an array of pointers, but I get an 
error (which suggests to me that I don't know what data type a 
pointer is):


find_in_array_object.d(25): Error: cannot cast expression 
newObject of type find_in_array_object.MyObject to ulong


What's your code? 'find_in_array_object.MyObject' doesn't look
like a pointer.


Re: Finding position of a value in an array

2019-12-30 Thread Ron Tarrant via Digitalmars-d-learn
On Sunday, 29 December 2019 at 09:44:18 UTC, MoonlightSentinel 
wrote:



int i = a.countUntil(55);



I was trying to do this with an array of pointers, but I get an 
error (which suggests to me that I don't know what data type a 
pointer is):


find_in_array_object.d(25): Error: cannot cast expression 
newObject of type find_in_array_object.MyObject to ulong
find_in_array_object.d(34): Error: template 
std.algorithm.searching.countUntil cannot deduce function from 
argument types !()(MyObject[], ulong), candidates are:

C:\D\dmd2\windows\bin\..\..\src\phobos\std\algorithm\searching.d(768):std.algorithm.searching.countUntil(alias pred 
= "a == b", R, Rs...)(R haystack, Rs needles) if (isForwardRange!R && (Rs.length > 0) && 
(isForwardRange!(Rs[0]) == isInputRange!(Rs[0])) && is(typeof(startsWith!pred(haystack, needles[0]))) && 
(Rs.length == 1 || is(typeof(countUntil!pred(haystack, needles[1..__dollar])
C:\D\dmd2\windows\bin\..\..\src\phobos\std\algorithm\searching.d(856):
std.algorithm.searching.countUntil(alias pred = "a == b", R, N)(R haystack, N needle) 
if (isInputRange!R && is(typeof(binaryFun!pred(haystack.front, needle)) : bool))
C:\D\dmd2\windows\bin\..\..\src\phobos\std\algorithm\searching.d(915):
std.algorithm.searching.countUntil(alias pred, R)(R haystack) if (isInputRange!R 
&& is(typeof(unaryFun!pred(haystack.front)) : bool))

It's not a ulong? Have I forgotten so much?


Re: Finding position of a value in an array

2019-12-29 Thread Ron Tarrant via Digitalmars-d-learn
On Sunday, 29 December 2019 at 09:44:18 UTC, MoonlightSentinel 
wrote:

On Sunday, 29 December 2019 at 08:31:13 UTC, mipri wrote:
On Sunday, 29 December 2019 at 08:26:58 UTC, Daren Scot Wilson 
wrote:



int i = a.countUntil!(v => v == 55);
assert(i == 2);


A predicate isn’t required, countUntil accepts single elements:

int i = a.countUntil(55);


I was just about to go looking for something like this. Thanks, 
guys.


Re: Finding position of a value in an array

2019-12-29 Thread MoonlightSentinel via Digitalmars-d-learn

On Sunday, 29 December 2019 at 08:31:13 UTC, mipri wrote:
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.


int i = a.countUntil!(v => v == 55);
assert(i == 2);


A predicate isn’t required, countUntil accepts single elements:

int i = a.countUntil(55);


Re: Finding position of a value in an array

2019-12-29 Thread mipri via Digitalmars-d-learn
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.


int i = a.countUntil!(v => v == 55);
assert(i == 2);

It might not be an obvious application of countUntil, but it's
listed at https://dlang.org/phobos/std_algorithm_searching.html