digitalmars.D.learn - Test for element in array
- Paolo Invernizzi (11/11) Jul 13 2006 Hi all,
- BCS (13/29) Jul 13 2006 import std.stdio;
- Dave (4/38) Jul 13 2006 That's a very elegant solution and it should work, but according to my
- BCS (9/53) Jul 13 2006 Bizarre
- Kirk McDonald (6/71) Jul 13 2006 Hmmm. "Lambda properties." Fun.
- Andrei Khropov (3/4) Jul 13 2006 Yeah, this new delegate syntax is cool !
- Don Clugston (4/9) Jul 14 2006 Wow! I didn't know you could have a naked {} like that as a void
- BCS (3/17) Jul 14 2006 Not quite naked, without the ending () the type is "bool delegate(void)"...
- Lars Ivar Igesund (8/26) Jul 14 2006 The () is for calling it, {} is fine if you want pass (what I would call
- Dave (2/63) Jul 13 2006 Ah... Thanks.
- Andrei Khropov (4/20) Jul 13 2006 Despite BCS proposed a cool solution I think 'in' should be built-in for...
- BCS (8/34) Jul 13 2006 the semantics are backwards, not a killer but its a little inconsistent.
- Regan Heath (19/50) Jul 13 2006 True.. (wild idea alert) unless we make it valid to index an array with ...
- Paolo Invernizzi (6/15) Jul 14 2006 I would be satisfied also if it just returns a boolean value, like a
- Chris Nicholson-Sauls (23/44) Jul 14 2006 That's most likely the best solution for the moment. Although, you coul...
- Ivan Senji (9/42) Jul 14 2006 Yeah, some think it is some think it isn't, I think it isn't but it is
- Andrei Khropov (6/16) Jul 14 2006 AA is also often called dictionary or map (in STL for example).
- BCS (12/32) Jul 14 2006 How about allow void[TYPE]? No values, only keys.
- Carlos Santander (8/53) Jul 14 2006 It was possible some time ago, but it got forbidden. What I've been doin...
- Derek Parnell (9/14) Jul 14 2006 =
- Kirk McDonald (12/32) Jul 14 2006 You can also have a sort of poor-man's multiset with:
- Paolo Invernizzi (9/23) Jul 15 2006 That's what I liked from python... with dicts (aka AA) and lists (aka
Hi all, Someone can suggest me the best way for testing the presence of an element in an array? Coming from python, something like... if( x in array_of_x && y in array_of_y ){ ... } The workaround, apart from using a simple 'for' loop, is to use an associative array, and test for key... --- Paolo
Jul 13 2006
Paolo Invernizzi wrote:Hi all, Someone can suggest me the best way for testing the presence of an element in an array? Coming from python, something like... if( x in array_of_x && y in array_of_y ){ ... } The workaround, apart from using a simple 'for' loop, is to use an associative array, and test for key... --- Paoloimport std.stdio; void main() { int[] a = new int[10]; int j=1; // populate a foreach(int i, inout int k; a) k=i; if({foreach(i;a) if(i==j) return true; return false;}) writef("true\n"); else writef("false\n"); }
Jul 13 2006
BCS wrote:Paolo Invernizzi wrote:That's a very elegant solution and it should work, but according to my tests it doesn't (darn). It never returns false. Set j = 100 once and give it a try.Hi all, Someone can suggest me the best way for testing the presence of an element in an array? Coming from python, something like... if( x in array_of_x && y in array_of_y ){ ... } The workaround, apart from using a simple 'for' loop, is to use an associative array, and test for key... --- Paoloimport std.stdio; void main() { int[] a = new int[10]; int j=1; // populate a foreach(int i, inout int k; a) k=i; if({foreach(i;a) if(i==j) return true; return false;}) writef("true\n"); else writef("false\n"); }
Jul 13 2006
Dave wrote:BCS wrote:Bizarre -- if({foreach(i;a) if(i==j) return true; return false;}) ++ if({foreach(i;a) if(i==j) return true; return false;}()) this fixes it. WT... Oh. {return false;}.typeof == bool delegate() {return false;}().typeof == boolPaolo Invernizzi wrote:That's a very elegant solution and it should work, but according to my tests it doesn't (darn). It never returns false. Set j = 100 once and give it a try.Hi all, Someone can suggest me the best way for testing the presence of an element in an array? Coming from python, something like... if( x in array_of_x && y in array_of_y ){ ... } The workaround, apart from using a simple 'for' loop, is to use an associative array, and test for key... --- Paoloimport std.stdio; void main() { int[] a = new int[10]; int j=1; // populate a foreach(int i, inout int k; a) k=i; if({foreach(i;a) if(i==j) return true; return false;}) writef("true\n"); else writef("false\n"); }
Jul 13 2006
BCS wrote:Dave wrote:Hmmm. "Lambda properties." Fun. -- Kirk McDonald Pyd: Wrapping Python with D http://dsource.org/projects/pyd/wikiBCS wrote:Bizarre -- if({foreach(i;a) if(i==j) return true; return false;}) ++ if({foreach(i;a) if(i==j) return true; return false;}()) this fixes it. WT... Oh. {return false;}.typeof == bool delegate() {return false;}().typeof == boolPaolo Invernizzi wrote:That's a very elegant solution and it should work, but according to my tests it doesn't (darn). It never returns false. Set j = 100 once and give it a try.Hi all, Someone can suggest me the best way for testing the presence of an element in an array? Coming from python, something like... if( x in array_of_x && y in array_of_y ){ ... } The workaround, apart from using a simple 'for' loop, is to use an associative array, and test for key... --- Paoloimport std.stdio; void main() { int[] a = new int[10]; int j=1; // populate a foreach(int i, inout int k; a) k=i; if({foreach(i;a) if(i==j) return true; return false;}) writef("true\n"); else writef("false\n"); }
Jul 13 2006
BCS wrote:if({foreach(i;a) if(i==j) return true; return false;}())Yeah, this new delegate syntax is cool ! --
Jul 13 2006
Andrei Khropov wrote:BCS wrote:Wow! I didn't know you could have a naked {} like that as a void delegate. That is cool. Looks as though D has sucked all the juice out of C++, and is moving onto the scripting languages...if({foreach(i;a) if(i==j) return true; return false;}())Yeah, this new delegate syntax is cool !
Jul 14 2006
Don Clugston wrote:Andrei Khropov wrote:Not quite naked, without the ending () the type is "bool delegate(void)" a.k.a. always true.BCS wrote:Wow! I didn't know you could have a naked {} like that as a void delegate. That is cool. Looks as though D has sucked all the juice out of C++, and is moving onto the scripting languages...if({foreach(i;a) if(i==j) return true; return false;}())Yeah, this new delegate syntax is cool !
Jul 14 2006
BCS wrote:Don Clugston wrote:The () is for calling it, {} is fine if you want pass (what I would call naked) a delegate to a function taking delegates as arguments. -- Lars Ivar Igesund blog at http://larsivi.net DSource & #D: larsiviAndrei Khropov wrote:Not quite naked, without the ending () the type is "bool delegate(void)" a.k.a. always true.BCS wrote:Wow! I didn't know you could have a naked {} like that as a void delegate. That is cool. Looks as though D has sucked all the juice out of C++, and is moving onto the scripting languages...if({foreach(i;a) if(i==j) return true; return false;}())Yeah, this new delegate syntax is cool !
Jul 14 2006
BCS wrote:Dave wrote:Ah... Thanks.BCS wrote:Bizarre -- if({foreach(i;a) if(i==j) return true; return false;}) ++ if({foreach(i;a) if(i==j) return true; return false;}()) this fixes it. WT... Oh. {return false;}.typeof == bool delegate() {return false;}().typeof == boolPaolo Invernizzi wrote:That's a very elegant solution and it should work, but according to my tests it doesn't (darn). It never returns false. Set j = 100 once and give it a try.Hi all, Someone can suggest me the best way for testing the presence of an element in an array? Coming from python, something like... if( x in array_of_x && y in array_of_y ){ ... } The workaround, apart from using a simple 'for' loop, is to use an associative array, and test for key... --- Paoloimport std.stdio; void main() { int[] a = new int[10]; int j=1; // populate a foreach(int i, inout int k; a) k=i; if({foreach(i;a) if(i==j) return true; return false;}) writef("true\n"); else writef("false\n"); }
Jul 13 2006
Paolo Invernizzi wrote:Hi all, Someone can suggest me the best way for testing the presence of an element in an array? Coming from python, something like... if( x in array_of_x && y in array_of_y ){ ... } The workaround, apart from using a simple 'for' loop, is to use an associative array, and test for key... --- PaoloDespite BCS proposed a cool solution I think 'in' should be built-in for usual (non-associative) arrays too. why not? --
Jul 13 2006
Andrei Khropov wrote:Paolo Invernizzi wrote:thanksHi all, Someone can suggest me the best way for testing the presence of an element in an array? Coming from python, something like... if( x in array_of_x && y in array_of_y ){ ... } The workaround, apart from using a simple 'for' loop, is to use an associative array, and test for key... --- PaoloDespite BCS proposed a cool solutionI think 'in' should be built-in for usual (non-associative) arrays too. why not?the semantics are backwards, not a killer but its a little inconsistent. With aa, the type of the LHS is the type of the index if(a in b) b[a]; // valid with non-aa, the type of the LHS would be the type of the stored value. The above wont always work for a non-aa. Also which a in b is found? The first? An array of all? The last?
Jul 13 2006
On Thu, 13 Jul 2006 16:46:46 -0700, BCS <BCS pathlink.com> wrote:Andrei Khropov wrote:True.. (wild idea alert) unless we make it valid to index an array with a value, which in turn returns the index of that value. It would perform a linear search from start to end (or match) therefore returning the 'first' match.Paolo Invernizzi wrote:thanksHi all, Someone can suggest me the best way for testing the presence of an element in an array? Coming from python, something like... if( x in array_of_x && y in array_of_y ){ ... } The workaround, apart from using a simple 'for' loop, is to use an associative array, and test for key... --- PaoloDespite BCS proposed a cool solutionI think 'in' should be built-in for usual (non-associative) arrays too. why not?the semantics are backwards, not a killer but its a little inconsistent. With aa, the type of the LHS is the type of the index if(a in b) b[a]; // valid with non-aa, the type of the LHS would be the type of the stored value.The above wont always work for a non-aa. Also which a in b is found? The first? An array of all? The last?In the case of 'in' aren't you just asking "is it in there", in which case it could return the first, or last, doesn't really matter. However, 'in' does return a pointer to the value .. I think returning the first is the least surprising, most useful thing to do. It would also be consistent with the index by value idea above. So, if you want the last match you say: if (a in b.reverse) and if you want the 2nd, 3rd, 4th etc p = (a in b); //gets first p = (a in b[b[*p]..$]); //2nd p = (a in b[b[*p]..$]); //3rd p = (a in b[b[*p]..$]); //4th funky! ;) Regan
Jul 13 2006
BCS wrote:the semantics are backwards, not a killer but its a little inconsistent. With aa, the type of the LHS is the type of the index if(a in b) b[a]; // valid with non-aa, the type of the LHS would be the type of the stored value. The above wont always work for a non-aa. Also which a in b is found? The first? An array of all? The last?I would be satisfied also if it just returns a boolean value, like a simple inclusion test... But for now, I will use the BCS solutions! Thanks! --- Paolo
Jul 14 2006
Paolo Invernizzi wrote:BCS wrote:That's most likely the best solution for the moment. Although, you could generalize it by taking advantage of IFTI and defining this somewhere: Then your test would just be: I think it reads pretty well, even if it isn't quite as "clean" as (bar in foo) in comparison. Which, incidentally, also does read well. -- Chris Nicholson-Saulsthe semantics are backwards, not a killer but its a little inconsistent. With aa, the type of the LHS is the type of the index if(a in b) b[a]; // valid with non-aa, the type of the LHS would be the type of the stored value. The above wont always work for a non-aa. Also which a in b is found? The first? An array of all? The last?I would be satisfied also if it just returns a boolean value, like a simple inclusion test... But for now, I will use the BCS solutions! Thanks! --- Paolo
Jul 14 2006
BCS wrote:Andrei Khropov wrote:Yeah, some think it is some think it isn't, I think it isn't but it is an old discussion and probably one we will never agree on. IMO: normal arrays store *values* which are indexed by a key (index) associative arrays store *keys* and the value is just an additional information about the key. That is only my opinion as some people think that AA's also are about values.Paolo Invernizzi wrote:thanksHi all, Someone can suggest me the best way for testing the presence of an element in an array? Coming from python, something like... if( x in array_of_x && y in array_of_y ){ ... } The workaround, apart from using a simple 'for' loop, is to use an associative array, and test for key... --- PaoloDespite BCS proposed a cool solutionI think 'in' should be built-in for usual (non-associative) arrays too. why not?the semantics are backwards, not a killer but its a little inconsistent.
Jul 14 2006
Ivan Senji wrote:Yeah, some think it is some think it isn't, I think it isn't but it is an old discussion and probably one we will never agree on. IMO: normal arrays store values which are indexed by a key (index) associative arrays store keys and the value is just an additional information about the key. That is only my opinion as some people think that AA's also are about values.AA is also often called dictionary or map (in STL for example). If you speak about some kind of collection where only value (and its presense or absense in the collection) is important it's probably better to call it a set. --
Jul 14 2006
Andrei Khropov wrote:Ivan Senji wrote:How about allow void[TYPE]? No values, only keys. void[char[]] whos; ... // fill whos if("Bob" in whos) Hire("Bob"); if("Capone" in whos) whos.remove("Capone"); whos["Bubba"]; // add Bubba auto him = whos["Bubba"]; // can't do that with a void // ( or auto him = whos["Bubba"]; // == bool.true auto who = whos["Capone"]; // == bool.false // )Yeah, some think it is some think it isn't, I think it isn't but it is an old discussion and probably one we will never agree on. IMO: normal arrays store values which are indexed by a key (index) associative arrays store keys and the value is just an additional information about the key. That is only my opinion as some people think that AA's also are about values.AA is also often called dictionary or map (in STL for example). If you speak about some kind of collection where only value (and its presense or absense in the collection) is important it's probably better to call it a set.
Jul 14 2006
BCS escribió:Andrei Khropov wrote:It was possible some time ago, but it got forbidden. What I've been doing (and I think others too) is: char [] [char []] whos; And set the element to both the key and the value. However, I admit I liked void [T]. -- Carlos Santander BernalIvan Senji wrote:How about allow void[TYPE]? No values, only keys. void[char[]] whos; .... // fill whos if("Bob" in whos) Hire("Bob"); if("Capone" in whos) whos.remove("Capone"); whos["Bubba"]; // add Bubba auto him = whos["Bubba"]; // can't do that with a void // ( or auto him = whos["Bubba"]; // == bool.true auto who = whos["Capone"]; // == bool.false // )Yeah, some think it is some think it isn't, I think it isn't but it is an old discussion and probably one we will never agree on. IMO: normal arrays store values which are indexed by a key (index) associative arrays store keys and the value is just an additional information about the key. That is only my opinion as some people think that AA's also are about values.AA is also often called dictionary or map (in STL for example). If you speak about some kind of collection where only value (and its presense or absense in the collection) is important it's probably better to call it a set.
Jul 14 2006
On Sat, 15 Jul 2006 02:46:24 +1000, Carlos Santander = <csantander619 gmail.com> wrote:It was possible some time ago, but it got forbidden. What I've been =doing (and I think others too) is: char [] [char []] whos; And set the element to both the key and the value. However, I admit I ==liked void [T].Actually I use bool[ char[] ] whos; whos["Capone"] =3D true; -- = Derek Parnell Melbourne, Australia
Jul 14 2006
Derek Parnell wrote:On Sat, 15 Jul 2006 02:46:24 +1000, Carlos Santander <csantander619 gmail.com> wrote:You can also have a sort of poor-man's multiset with: int[ char[] ] ms; One could easily wrap this with a more multiset-ish interface, but to add an item you just say: ++ms["moo"]; And to remove: if (--ms["moo"] <= 0) ms.remove("moo"); -- Kirk McDonald Pyd: Wrapping Python with D http://dsource.org/projects/pyd/wikiIt was possible some time ago, but it got forbidden. What I've been doing (and I think others too) is: char [] [char []] whos; And set the element to both the key and the value. However, I admit I liked void [T].Actually I use bool[ char[] ] whos; whos["Capone"] = true;
Jul 14 2006
Kirk McDonald wrote:Derek Parnell wrote: You can also have a sort of poor-man's multiset with: int[ char[] ] ms; One could easily wrap this with a more multiset-ish interface, but to add an item you just say: ++ms["moo"]; And to remove: if (--ms["moo"] <= 0) ms.remove("moo");That's what I liked from python... with dicts (aka AA) and lists (aka arrays) you can have basically every container you want. And some newbie reading the code can grasp the meaning without loosing himself in a plethora of containers... Now python users are complaining against too much features added in the language... --- Paolo
Jul 15 2006