Re: Best way to check for an element in an array?

2014-04-23 Thread FrankLike via Digitalmars-d-learn
Hi,everyone, It works maybe the best: import std.stdio; template isin(T){ bool isin(T[] Array,T Element){ foreach(T ArrayElement; Array){ if(Element==ArrayElement){ return true;} } return false; } } void main(string[] args) { int[] stuff=[0,1,2,3,4,5

Re: Best way to check for an element in an array?

2014-04-23 Thread FrankLike via Digitalmars-d-learn
Hi,everyone, This code must add the 'break', import std.stdio; int x=0; template isin(T){ bool isin(T[] Array,T Element){ bool rtn=false; foreach(T ArrayElement; Array){ if(Element==ArrayElement){ rtn=true; break; ← //here add break

Re: Best way to check for an element in an array?

2014-04-22 Thread Steven Schveighoffer via Digitalmars-d-learn
On Mon, 21 Apr 2014 23:25:39 -0400, Taylor Hillegeist wrote: So I find myself Doing this kind of thing very frequently. I have a Array of Somethings and i want to see if "something specific" is inside the array. I wrote a template for it. but is this the best way to do this kind of thing

Re: Best way to check for an element in an array?

2014-04-21 Thread Ali Çehreli via Digitalmars-d-learn
On 04/21/2014 09:22 PM, Taylor Hillegeist wrote: > Question though? why doesn't canFind() work on statically > allocated arrays? That's an issue all of face from time to time. (Happened to me again last week. :) ) The reason is, algorithms like canFind work with input ranges and fixed-length

Re: Best way to check for an element in an array?

2014-04-21 Thread Taylor Hillegeist via Digitalmars-d-learn
On Tuesday, 22 April 2014 at 03:57:33 UTC, Timothee Cour via Digitalmars-d-learn wrote: you can use stuff.canFind(2) but sometimes it'd be more convenient to have the other way around (UFCS chains etc); how about: bool isIn(T,T2...)(T needle, T2 haystack) if(__traits(compiles,T.init==T2[0].i

Re: Best way to check for an element in an array?

2014-04-21 Thread Timothee Cour via Digitalmars-d-learn
you can use stuff.canFind(2) but sometimes it'd be more convenient to have the other way around (UFCS chains etc); how about: bool isIn(T,T2...)(T needle, T2 haystack) if(__traits(compiles,T.init==T2[0].init)){ foreach(e;haystack){ if(needle==e) return true; } return false; } unittest{

Best way to check for an element in an array?

2014-04-21 Thread Taylor Hillegeist via Digitalmars-d-learn
So I find myself Doing this kind of thing very frequently. I have a Array of Somethings and i want to see if "something specific" is inside the array. I wrote a template for it. but is this the best way to do this kind of thing. I feel like it doesn't help with readability. Is there a better wa