www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Using lazily ?

reply bioinfornatics <bioinfornatics fedoraproject.org> writes:
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
next sibling parent reply Timon Gehr <timon.gehr gmx.ch> writes:
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
parent reply =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
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
Mar 01 2012
parent reply bearophile <bearophileHUGS lycos.com> writes:
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
parent Timon Gehr <timon.gehr gmx.ch> writes:
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; }());
Mar 02 2012
prev sibling parent bioinfornatics <bioinfornatics fedoraproject.org> writes:
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;
      }
 }=20

awesome :) can use hasMember instead allMembers ?
Mar 01 2012