digitalmars.D.learn - Fetching an element using find
- Steven Schveighoffer (12/12) Jan 13 2020 I have a range (array), I want to find an element and get it from that
- H. S. Teoh (13/29) Jan 13 2020 [...]
- Steven Schveighoffer (4/15) Jan 13 2020 I certainly can (and did). I was wondering if there was something in
- H. S. Teoh (6/21) Jan 13 2020 [...]
- lithium iodate (15/30) Jan 13 2020 `adjoin` can be used to run an inline lambda:
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
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
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
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:[...] 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?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.
Jan 13 2020
On Monday, 13 January 2020 at 17:58:57 UTC, Steven Schveighoffer wrote:On 1/13/20 12:47 PM, H. S. Teoh wrote:`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;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