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 way? Maybe i missed something in the std library.
import std.stdio;
template FNDR(T){
bool isIn(T Element, T[] Array){
bool rtn=false;
foreach(T ArrayElement; Array){
if(Element==ArrayElement){
rtn=true;
}
}
return rtn;
}
}
void main(string[] args)
{
int[3] stuff=[0,1,2];
if (FNDR!int.isIn(2,stuff))
{
writeln("Hello World!");
}
}
Is there a way maybe to make it look like this?
import std.stdio;
template FNDR(T){
bool contains(T[] Array,T Element){
bool rtn=false;
foreach(T ArrayElement; Array){
if(Element==ArrayElement){
rtn=true;
}
}
return rtn;
}
}
void main(string[] args)
{
int[3] stuff=[0,1,2];
if (stuff.contains(2)) // Much clean! stuff.FNDR!int.contains(2) doesn't work
{
writeln("Hello World!");
}
}
I'm interested in what you guys think? what is the cleanest way to do this?