Re: Equivalent of FirstOrDefault with ranges

2016-09-02 Thread Nicholas Wilson via Digitalmars-d-learn
On Friday, 2 September 2016 at 06:56:07 UTC, Lutger wrote: I was looking for something like FirstOrDefault* from .NET in phobos. For example, I have this piece of code: string findBobOrReturnNull(string[] names) { auto r = names.find("bob"); if(r.empty) return null; return r.front;

Re: Equivalent of FirstOrDefault with ranges

2016-09-02 Thread Cauterite via Digitalmars-d-learn
On Friday, 2 September 2016 at 06:56:07 UTC, Lutger wrote: You could do: names.find("bob").chain(only(``)).front; It's not very expressive though.

Equivalent of FirstOrDefault with ranges

2016-09-02 Thread Lutger via Digitalmars-d-learn
I was looking for something like FirstOrDefault* from .NET in phobos. For example, I have this piece of code: string findBobOrReturnNull(string[] names) { auto r = names.find("bob"); if(r.empty) return null; return r.front; } assert(findBobOrReturnNull(["alice", "bob"]) == "bob"); a