Thread overview | ||||||||
---|---|---|---|---|---|---|---|---|
|
March 01, 2012 Using lazily ? | ||||
---|---|---|---|---|
| ||||
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 ? |
March 01, 2012 Re: Using lazily ? | ||||
---|---|---|---|---|
| ||||
Posted in reply to bioinfornatics | 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; } } |
March 01, 2012 Re: Using lazily ? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Timon Gehr | Le jeudi 01 mars 2012 à 23:10 +0100, Timon Gehr a écrit :
> 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;
> }
> }
awesome :)
can use hasMember instead allMembers ?
|
March 01, 2012 Re: Using lazily ? | ||||
---|---|---|---|---|
| ||||
Posted in reply to bioinfornatics | On 03/01/2012 02:25 PM, bioinfornatics wrote: > Le jeudi 01 mars 2012 à 23:10 +0100, Timon Gehr a écrit : >> 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; >> } >> } > > awesome :) > > can use hasMember instead allMembers ? 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. Ali |
March 01, 2012 Re: Using lazily ? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ali Çehreli | 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
|
March 02, 2012 Re: Using lazily ? | ||||
---|---|---|---|---|
| ||||
Posted in reply to bearophile | On 03/02/2012 12:12 AM, bearophile wrote:
> 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
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;
}());
|
Copyright © 1999-2021 by the D Language Foundation