www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Fetching an element using find

reply Steven Schveighoffer <schveiguy gmail.com> writes:
I have a range (array), I want to find an element and get it from that 
array.

The code I'm writing looks like this:

auto answer = arr.find!((item,x) => item.id == x)(id);
enforce(!answer.empty, "id not in the range");
auto realAnswer = answer.front;

I can't do find(id).front, because that relies on asserts, which throw 
errors and kill the program, and are not always compiled in. I need an 
exception thrown so I can recover and report the error to the user.

But I hate to write 3 lines of code to do this every time.

I'm not seeing an easy solution in Phobos, am I missing it?

-Steve
Jan 13 2020
parent reply "H. S. Teoh" <hsteoh quickfur.ath.cx> writes:
On Mon, Jan 13, 2020 at 12:39:30PM -0500, Steven Schveighoffer via
Digitalmars-d-learn wrote:
 I have a range (array), I want to find an element and get it from that
 array.
 
 The code I'm writing looks like this:
 
 auto answer = arr.find!((item,x) => item.id == x)(id);
 enforce(!answer.empty, "id not in the range");
 auto realAnswer = answer.front;
 
 I can't do find(id).front, because that relies on asserts, which throw
 errors and kill the program, and are not always compiled in. I need an
 exception thrown so I can recover and report the error to the user.
 
 But I hate to write 3 lines of code to do this every time.
 
 I'm not seeing an easy solution in Phobos, am I missing it?
[...] Why not write your own convenience wrapper? auto firstElement(R)(R r) if (isInputRange!R) { if (r.empty) throw new Exception(...); return r.front; } auto e = myData.find!(e => blah(e)).firstElement; T -- I think Debian's doing something wrong, `apt-get install pesticide', doesn't seem to remove the bugs on my system! -- Mike Dresser
Jan 13 2020
parent reply Steven Schveighoffer <schveiguy gmail.com> writes:
On 1/13/20 12:47 PM, H. S. Teoh wrote:
 
 Why not write your own convenience wrapper?
 
 	auto firstElement(R)(R r)
 		if (isInputRange!R)
 	{
 		if (r.empty) throw new Exception(...);
 		return r.front;
 	}
 
 	auto e = myData.find!(e => blah(e)).firstElement;
I certainly can (and did). I was wondering if there was something in Phobos to do it. -Steve
Jan 13 2020
next sibling parent "H. S. Teoh" <hsteoh quickfur.ath.cx> writes:
On Mon, Jan 13, 2020 at 12:58:57PM -0500, Steven Schveighoffer via
Digitalmars-d-learn wrote:
 On 1/13/20 12:47 PM, H. S. Teoh wrote:
 
 Why not write your own convenience wrapper?
 
 	auto firstElement(R)(R r)
 		if (isInputRange!R)
 	{
 		if (r.empty) throw new Exception(...);
 		return r.front;
 	}
 
 	auto e = myData.find!(e => blah(e)).firstElement;
I certainly can (and did). I was wondering if there was something in Phobos to do it.
[...] Maybe add a new function to Phobos? :-D T -- It always amuses me that Windows has a Safe Mode during bootup. Does that mean that Windows is normally unsafe?
Jan 13 2020
prev sibling parent lithium iodate <whatdoiknow doesntexist.net> writes:
On Monday, 13 January 2020 at 17:58:57 UTC, Steven Schveighoffer 
wrote:
 On 1/13/20 12:47 PM, H. S. Teoh wrote:
 
 Why not write your own convenience wrapper?
 
 	auto firstElement(R)(R r)
 		if (isInputRange!R)
 	{
 		if (r.empty) throw new Exception(...);
 		return r.front;
 	}
 
 	auto e = myData.find!(e => blah(e)).firstElement;
I certainly can (and did). I was wondering if there was something in Phobos to do it. -Steve
`adjoin` can be used to run an inline lambda: auto answer = arr.find!((item,x) => item.id == x)(id).adjoin!((n){enforce(!n.empty); return n.front;}); Using a simple alias you can have a flexible and nice to read solution: alias ensure(alias pred) = (n, const(char)[] msg = "`ensure` failed"){enforce(pred(n), msg); return n;}; // module scope for UFCS! auto answer = arr.find!((item,x) => item.id == x)(id).ensure!(n => !n.empty).front; or with custom message: auto answer = arr.find!((item,x) => item.id == x)(id).ensure!(n => !n.empty)("element with id not found").front;
Jan 13 2020