digitalmars.D.learn - Using lazily ?
- bioinfornatics (18/18) Mar 01 2012 dear,
- Timon Gehr (14/32) Mar 01 2012 struct S{
- bioinfornatics (3/13) Mar 01 2012 awesome :)
- =?UTF-8?B?QWxpIMOHZWhyZWxp?= (15/28) Mar 01 2012 No, because both of those are compile-time features. Even if you used
- bearophile (7/9) Mar 01 2012 I'd like it to be written:
- Timon Gehr (8/17) Mar 02 2012 static foreach should also be available for declarations. I have a lot
dear, Noob question for know if D provide a shorter way i explain we have a struct S: struct S{ string member1; string member2; string member3; } we parse a file: File f =3D File("a path", "r"); S s; sise_t tokenLength =3D "member1".length; foreach( char[] line; f.byLine() ) mixin("s." ~ line[0 .. tokenLength] ~ " =3D " ~ line[tokenLength .. $]" ); // do not work because lien is not kno at compile time I know this do not works i.e comment but it will save some line by checking if is member1 2 or 3 They are a shorter way to do this use lazy ?
Mar 01 2012
On 03/01/2012 10:50 PM, bioinfornatics wrote:dear, Noob question for know if D provide a shorter way i explain we have a struct S: struct S{ string member1; string member2; string member3; } we parse a file: File f = File("a path", "r"); S s; sise_t tokenLength = "member1".length; foreach( char[] line; f.byLine() ) mixin("s." ~ line[0 .. tokenLength] ~ " = " ~ line[tokenLength .. $]" ); // do not work because lien is not kno at compile time I know this do not works i.e comment but it will save some line by checking if is member1 2 or 3 They are a shorter way to do this use lazy ?struct S{ string member1; string member2; string member3; } S s; size_t tokenLength = "member1".length; void main(){ foreach(char[] line; stdin.byLine()) foreach(m;__traits(allMembers,S)){ if(line[0..tokenLength] == m) mixin("s."~m) = line[tokenLength .. $].idup; } }
Mar 01 2012
Le jeudi 01 mars 2012 =C3=A0 23:10 +0100, Timon Gehr a =C3=A9crit :S s; size_t tokenLength =3D "member1".length; void main(){ foreach(char[] line; stdin.byLine()) foreach(m;__traits(allMembers,S)){ if(line[0..tokenLength] =3D=3D m) mixin("s."~m) =3D line[tokenLength=20 .. $].idup; } }=20awesome :) can use hasMember instead allMembers ?
Mar 01 2012
On 03/01/2012 02:25 PM, bioinfornatics wrote:Le jeudi 01 mars 2012 à 23:10 +0100, Timon Gehr a écrit :No, because both of those are compile-time features. Even if you used hasMember, the answer will always be true: if (__traits(hasMember, S, "member1")) Yes, S has member1. Note that Timon's inner foreach is a compile-time foreach, which is the equivalent of the following three lines: if(line[0..tokenLength] == "member1") s.member1 = line[tokenLength .. $].idup; if(line[0..tokenLength] == "member2") s.member2 = line[tokenLength .. $].idup; if(line[0..tokenLength] == "member3") s.member3 = line[tokenLength .. $].idup; There is no inner foreach looop that is executed at runtime. AliS s; size_t tokenLength = "member1".length; void main(){ foreach(char[] line; stdin.byLine()) foreach(m;__traits(allMembers,S)){ if(line[0..tokenLength] == m) mixin("s."~m) = line[tokenLength .. $].idup; } }awesome :) can use hasMember instead allMembers ?
Mar 01 2012
Ali:Note that Timon's inner foreach is a compile-time foreach, which is the equivalent of the following three lines:I'd like it to be written: static foreach (...) {... In the meantime an annotation helps clarify the code for the person that will read the code: /*static*/ foreach (...) {... Bye, bearophile
Mar 01 2012
On 03/02/2012 12:12 AM, bearophile wrote:Ali:static foreach should also be available for declarations. I have a lot of code of the following form: mixin({ string r; foreach(x; [".","..","..."]) r~=X!q{...}; return r; }());Note that Timon's inner foreach is a compile-time foreach, which is the equivalent of the following three lines:I'd like it to be written: static foreach (...) {... In the meantime an annotation helps clarify the code for the person that will read the code: /*static*/ foreach (...) {... Bye, bearophile
Mar 02 2012