www.digitalmars.com         C & C++   DMDScript  

D - postinc

reply "Carlos Santander B." <carlos8294 msn.com> writes:
I kept in my head what Matthew said about implementing a for_each. I thought
this couldn't be so hard, so I checked how it works in STL (since I don't
know STL), and I wrote this:

template ForEach(InputIter) {
    alias void delegate (InputIter) Function;
    Function foreach(InputIter first,InputIter last,Function f) {
        for ( ; first!=last ; first++)
            f(first);
        return f;
    }
}

and if I try it with:

void f(int x) {
    printf("%d\n",x);
}
instance ForEach(int) Ints;
Ints.foreach(4,10,f);

It works just fine. Now the problem.

I decided I should test it also with something else, something like a node
in a linked list. For a very simple node, I implemented the postinc()
operator, but it just doesn't seem to work.

class Node {
    Node next;
    float x;
    this(float a) { x=a;}
    Node postinc() {
        // what goes here??
    }
}

I tried all the following:
1) return next;
2) this=next; return this;
3) this=next; return next;
4) Node tmp=next; this=next; return tmp;
5) return new Node(next.x);

But nothing seems to work. Most likely I have the wrong idea of what I have
to do, but I believe you guys understand what I want to accomplish.

-------------------------
Carlos Santander


---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.507 / Virus Database: 304 - Release Date: 2003-08-04
Aug 07 2003
parent reply "Matthew Wilson" <matthew stlsoft.org> writes:
 I kept in my head what Matthew said
The first step on the road to ruin ...
 about implementing a for_each. I thought
 this couldn't be so hard, so I checked how it works in STL (since I don't
 know STL), and I wrote this:

 template ForEach(InputIter) {
     alias void delegate (InputIter) Function;
     Function foreach(InputIter first,InputIter last,Function f) {
         for ( ; first!=last ; first++)
             f(first);
         return f;
     }
 }

 and if I try it with:

 void f(int x) {
     printf("%d\n",x);
 }
 instance ForEach(int) Ints;
 Ints.foreach(4,10,f);

 It works just fine. Now the problem.

 I decided I should test it also with something else, something like a node
 in a linked list. For a very simple node, I implemented the postinc()
 operator, but it just doesn't seem to work.

 class Node {
     Node next;
     float x;
     this(float a) { x=a;}
     Node postinc() {
         // what goes here??
     }
 }

 I tried all the following:
 1) return next;
 2) this=next; return this;
 3) this=next; return next;
 4) Node tmp=next; this=next; return tmp;
 5) return new Node(next.x);

 But nothing seems to work. Most likely I have the wrong idea of what I
have
 to do, but I believe you guys understand what I want to accomplish.
I know what you want to do, but I have no idea. I want to see an implementation of for_each() that works for two sequence types - say a built in array of double and a template-list of Thing objects (Thing is any UDT) - and with two completely different functors - say a stateful one that sums the .size of each element and a stateless one that dumps them to stdout. No offence to anyone on the NG - and *please* correct me if I'm wrong - but I expect Walter's the only one who currently could do this if it's possible, which I strongly suspect it is not. On both issues I'm dying to be wrong - partly because I simply want to learn how this is done - but until I am we can't say we're even in a position to ask the right questions about a DTL, never mind answer them Yours seeking enlightenment -- Matthew Wilson STLSoft moderator and C++ monomaniac mailto:matthew stlsoft.org http://www.stlsoft.org news://news.digitalmars.com/c++.stlsoft "I can't sleep nights till I found out who hurled what ball through what apparatus" -- Dr Niles Crane ---------------------------------------------------------------------------- ---
Aug 07 2003
next sibling parent reply "Carlos Santander B." <carlos8294 msn.com> writes:
"Matthew Wilson" <matthew stlsoft.org> wrote in message
news:bgvh60$205c$1 digitaldaemon.com...
| > I kept in my head what Matthew said
|
| The first step on the road to ruin ...
|

I certainly don't think so...

| >
| > But nothing seems to work. Most likely I have the wrong idea of what I
| have
| > to do, but I believe you guys understand what I want to accomplish.
|
| I know what you want to do, but I have no idea.
|
| I want to see an implementation of for_each() that works for two sequence
| types - say a built in array of double and a template-list of Thing
objects
| (Thing is any UDT) - and with two completely different functors - say a
| stateful one that sums the .size of each element and a stateless one that
| dumps them to stdout.
|

I'm not so sure if you understand. What I wanted was that by applying
postinc() to  a Node, it could move to the next one.

覧覧覧覧覧覧覧覧覧覧覧覧
Carlos Santander



---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.507 / Virus Database: 304 - Release Date: 2003-08-04
Aug 08 2003
parent reply "Matthew Wilson" <matthew stlsoft.org> writes:
"Carlos Santander B." <carlos8294 msn.com> wrote in message
news:bh0vuj$bnh$1 digitaldaemon.com...
 "Matthew Wilson" <matthew stlsoft.org> wrote in message
 news:bgvh60$205c$1 digitaldaemon.com...
 | > I kept in my head what Matthew said
 |
 | The first step on the road to ruin ...
 |

 I certainly don't think so...
Who is the bigger fool, the fool himself or the fool who follows him.
 | >
 | > But nothing seems to work. Most likely I have the wrong idea of what I
 | have
 | > to do, but I believe you guys understand what I want to accomplish.
 |
 | I know what you want to do, but I have no idea.
 |
 | I want to see an implementation of for_each() that works for two
sequence
 | types - say a built in array of double and a template-list of Thing
 objects
 | (Thing is any UDT) - and with two completely different functors - say a
 | stateful one that sums the .size of each element and a stateless one
that
 | dumps them to stdout.
 |

 I'm not so sure if you understand. What I wanted was that by applying
 postinc() to  a Node, it could move to the next one.
I did understand what you were saying. I was playing "development manager" by countering your need with a completely different and much broader requirement. ;) I want Walter to do this, I think, as it will save all us poor D-lovers-but-somewhat-lacking-in-groking-the-templates many hours of brain ache. Walter. Walter!! WALTER. _please_ help us, grovel groval :) Dr Proctor and the early morning gremlins.
Aug 08 2003
parent reply "Walter" <walter digitalmars.com> writes:
"Matthew Wilson" <matthew stlsoft.org> wrote in message
news:bh14tp$g9j$1 digitaldaemon.com...
 Walter. Walter!! WALTER. _please_ help us, grovel groval
Would a nice foreach that worked on arrays as easilly as collection classes do the thing?
Aug 12 2003
parent Terry Bayne <tbayne hiwaay.net> writes:
On Tue, 12 Aug 2003 20:18:00 -0700, Walter wrote:

 
 "Matthew Wilson" <matthew stlsoft.org> wrote in message
 news:bh14tp$g9j$1 digitaldaemon.com...
 Walter. Walter!! WALTER. _please_ help us, grovel groval
Would a nice foreach that worked on arrays as easilly as collection classes do the thing?
Yes!!!!
Aug 13 2003
prev sibling parent reply Farmer <itsFarmer. freenet.de> writes:
"Matthew Wilson" <matthew stlsoft.org> wrote in
news:bgvh60$205c$1 digitaldaemon.com: 

 I want to see an implementation of for_each() that works for two
 sequence types - say a built in array of double and a template-list of
 Thing objects (Thing is any UDT) - and with two completely different
 functors - say a stateful one that sums the .size of each element and a
 stateless one that dumps them to stdout.
 
 No offence to anyone on the NG - and *please* correct me if I'm wrong -
 but I expect Walter's the only one who currently could do this if it's
 possible, which I strongly suspect it is not.
I guess you just want to torture Walter by letting him write templated stuff. Well, I tought what's worse than writting templated stuff? Having to read templated stuff, written by someone else, of course. <grin> See the attachment for one solution of the given task. Note: In the example I used the template-list in combination with long[] build-in arrays, instead of plain build-in arrays of double.
 
 On both issues I'm dying to be wrong - partly because I simply want to
 learn how this is done - but until I am we can't say we're even in a
 position to ask the right questions about a DTL, never mind answer them
Agree. begin 644 Iterator.d M:6UP;W)T('-T<FEN9SL-" T*=&5M<&QA=&4 0V]L;&5C=&EO;BA4*0T*>PT* M("` 86QI87, 5"!4>7!E,3L-"B` (`T*("` 8VQA<W, 3&ES=`T*("` >PT* M("` ("` 3&ES=$YO9&4 :&5A9#L-"B` ("` ($QI<W1.;V1E('1A:6P[#0H M("` ("`-"B` ("` ('9O:60 87!P96YD*%0 =BD-"B` ("` ('L-"B` ("` M("` (&YE=R!,:7-T3F]D92AV+"!T86EL*3L-"B` ("` ("` (&EF("AH96%D M(#T]/2!N=6QL*2`-"B` ("` ("` ("` (&AE860]=&%I;#L-"B` ("` ('T- M"B` ("` (`T*("` ("` 3&ES=$ET(&=E=$5N=6UE<F%T;W(H*0T*("` ("` M>PT*("` ("` ("` <F5T=7)N(&YE=R!,:7-T270H:&5A9"D[#0H ("` ("!] M#0H ("!]#0H ("`-"B` (&-L87-S($QI<W1)=`T*("` >PT*("` ("` 86QI M87, 5"!)=&5M5'EP93L-"B` ("` (`T*("` ("` 3&ES=$YO9&4 8W5R<F5N M=#L-"B` ("` ('1H:7,H3&ES=$YO9&4 <W1A<G0I#0H ("` ("![#0H ("` M("` ("!C=7)R96YT/7-T87)T.PT*("` ("` ?0T*("` ("` #0H ("` ("!B M:70 :&%S3F5X=" I#0H ("` ("![#0H ("` ("` ("!R971U<FX *&-U<G)E M;G0 (3T](&YU;&PI(#\ =')U92`Z(&9A;'-E.PT*("` ("` ?0T*("` ("` M#0H ("` ("!4(&YE>'0H*0T*("` ("` >PT*("` ("` ("` 5"!R<SUC=7)R M96YT+F1A=&$[#0H ("` ("` ("!C=7)R96YT/6-U<G)E;G0N;F5X=#L-"B` M("` ("` (')E='5R;B!R<SL-"B` ("` ('T-"B` ('T-"B` (`T*("` 8VQA M<W, 3&ES=$YO9&4-"B` ('L-"B` ("` ($QI<W1.;V1E(&YE>'0[#0H ("` M("!4(&1A=&$[#0H ("` ("`-"B` ("` ('1H:7,H5"!D871A+"!I;F]U="!, M:7-T3F]D92!P<F5V3F]D92D-"B` ("` ('L-"B` ("` ("` ('1H:7,N9&%T M83UD871A.PT*("` ("` ("` :68 *'!R979.;V1E("$]/2!N=6QL*2`-"B` M("` ("` ("` ('!R979.;V1E+FYE>'0]=&AI<SL-"B` ("` ("` ('!R979. M;V1E/71H:7,[#0H ("` ("!]#0H ("!]#0H-"B` (&-L87-S($%R<F%Y270- M"B` ('L-"B` ("` ('5I;G0 :61X.PT*("` ("` 5%M=(&%R<F%Y.PT*("` M("` #0H ("` ("!T:&ES*%1;72!A<G)A>2D-"B` ("` ('L-"B` ("` ("` M('1H:7,N87)R87D]87)R87D[#0H ("` ("!]#0H ("` ("`-"B` ("` (&)I M="!H87-.97AT*"D-"B` ("` ('L-"B` ("` ("` (')E='5R;B`H:61X(#P M87)R87DN;&5N9W1H*2`_('1R=64 .B!F86QS93L-"B` ("` ('T-"B` ("` M(`T*("` ("` 5"!N97AT*"D-"B` ("` ('L-"B` ("` ("` (')E='5R;B!A M<G)A>5MI9' K*UT[#0H ("` ("!]#0H ("!]#0H-"GT-" T*#0IT96UP;&%T M92!!;&=O<FET:&TH271E<F%T;W(L($ET96U4>7!E*0T*>PT*("` 86QI87, M=F]I9"!D96QE9V%T92`H271E;51Y<&4I($1E;&5G871E.PT*("` #0H ("!V M;VED(&9O<D5A8V H271E<F%T;W( :70L($1E;&5G871E(&8I#0H ("![#0H M("` ("!W:&EL92`H:70N:&%S3F5X=" I*0T*("` ("` ("` 9BAI="YN97AT M*"DI.PT*("` ?0T*("` #0H ("!A;&EA<R!V;VED(&9U;F-T:6]N("A)=&5M M5'EP92D 1G5N8W1I;VX[#0H ("`-"B` ('9O:60 9F]R16%C:"A)=&5R871O M<B!I="P 1G5N8W1I;VX 9BD-"B` ('L-"B` ("` ('=H:6QE("AI="YH87-. M97AT*"DI#0H ("` ("` ("!F*&ET+FYE>'0H*2D[#0H ("!]#0I]#0H-" T* M#0H-"G1E;7!L871E($UY1G5N8W,H271E;2`Z($]B:F5C="D #0I[#0H ("!V M;VED('!R:6YT*$ET96T =BD-"B` ('L-"B` ("` ('!R:6YT9B B)2XJ<UQN M(BQV+G1O4W1R:6YG*"DI.PT*("` ?2` #0H ("`-"B` (&-L87-S(%-T871E M9G5L#0H ("![#0H ("` ("!U:6YT('-I>F4[#0H ("` ("!V;VED('-U;5-I M>F4H271E;2!V*0T*("` ("` >PT*("` ("` ("` <VEZ92L]=BYS:7IE.PT* M("` ("` ?2` #0H ("!]#0I]#0H-"G1E;7!L871E($UY1G5N8W,H271E;2D M#0I[#0H ("!V;VED('!R:6YT*$ET96T =BD-"B` ('L-"B` ("` ('!R:6YT M9B B)2XJ<UQN(BQT;U-T<FEN9RAV*2D[#0H ("!]("`-"B` (`T*("` 8VQA M<W, 4W1A=&5F=6P-"B` ('L-"B` ("` ('5I;G0 <VEZ93L-"B` ("` ('9O M:60 <W5M4VEZ92A)=&5M('8I#0H ("` ("![#0H ("` ("` ("!S:7IE*SUV M+G-I>F4[#0H ("` ("!]("`-"B` ('T-"GT-" T*#0IV;VED(%]M86EN*"D- M"GL-"B` ('L-"B` ("` ("\O(&-R96%T92!,:7-T(&]F($]B:F5C=',-"B` M("` (&EN<W1A;F-E($-O;&QE8W1I;VXH3V)J96-T*2!#;VQL7T]B:F5C=#L- M"B` ("` ($-O;&Q?3V)J96-T+DQI<W0 ;&ES=#UN97< 0V]L;%]/8FIE8W0N M3&ES=" I.PT*("` ("` ;&ES="YA<'!E;F0H;F5W($]B:F5C=" I*3L-"B` M("` (&QI<W0N87!P96YD*&YE=R!/8FIE8W0H*2D[#0H ("` ("!L:7-T+F%P M<&5N9"AN97< 3V)J96-T*"DI.PT*("` ("` #0H ("` ("`O+R!P<FEN="!, M:7-T(&ET96US#0H ("` ("!I;G-T86YC92!->49U;F-S*$]B:F5C="D 37E& M=6YC<U]/8FIE8W0[#0H ("` ("`-"B` ("` (&EN<W1A;F-E($%L9V]R:71H M;2A#;VQL7T]B:F5C="Y,:7-T270L($-O;&Q?3V)J96-T+E1Y<&4Q*0T*("` M("` ("` +F9O<D5A8V H;&ES="YG971%;G5M97)A=&]R*"DL($UY1G5N8W-? M3V)J96-T+G!R:6YT*3L-"B` ("` (`T*("` ("` +R\ 9V5T($QI<W0 :71E M;2!S:7IE#0H ("` ("!->49U;F-S7T]B:F5C="Y3=&%T969U;"!S=&%T969U M;#UN97< 37E&=6YC<U]/8FIE8W0N4W1A=&5F=6PH*3L-"B` ("` (`T*("` M("` :6YS=&%N8V4 06QG;W)I=&AM*$-O;&Q?3V)J96-T+DQI<W1)="P 0V]L M;%]/8FIE8W0N5'EP93$I#0H ("` ("` ("`N9F]R16%C:"AL:7-T+F=E=$5N M=6UE<F%T;W(H*2P <W1A=&5F=6PN<W5M4VEZ92D[#0H ("` ("!P<FEN=&8H M(E-I>F4 :7, )75<;B(L('-T871E9G5L+G-I>F4I.PT*("` ?0T*("` #0H M("![#0H ("` ("`O+R!C<F5A=&4 3&ES="!O9B!L;VYG(&%R<F%Y<PT*#0H M("` ("`O+R!I;FET(&%R<F%Y#0H ("` ("!L;VYG6UT 87)R87D];F5W(&QO M;F=;-5T[#0H ("` ("!F;W( *'5I;G0 :3TP.R!I(#P 87)R87DN;&5N9W1H M.R!I*RLI(`T*("` ("` ("` 87)R87E;:5T]:3L-"B` ("` (`T*("` ("` M:6YS=&%N8V4 0V]L;&5C=&EO;BAL;VYG6UTI($-O;&P[#0H ("` ("!#;VQL M+DQI<W0 ;&ES=$]F07)R87ES/6YE=R!#;VQL+DQI<W0H*3L-"B` ("` (&QI M<W1/9D%R<F%Y<RYA<'!E;F0H87)R87DI.PT*#0H ("` ("`O+R!A9&0 ;6]R M92!A<G)A>7,-"B` ("` (&QO;F=;72!C;W!Y/6%R<F%Y+F1U<#L-"B` ("` M(&9O<B`H=6EN="!I/3`[(&D /"!C;W!Y+FQE;F=T:#L :2LK*2`-"B` ("` M("` (&-O<'E;:5TK/3$P.PT*("` ("` ;&ES=$]F07)R87ES+F%P<&5N9"AC M;W!Y*3L-" T*("` ("` 8V]P>3UA<G)A>2YD=7`[#0H ("` ("!C;W!Y+FQE M;F=T:#TS.R`O+R!S:')I;FL ;&%S="!A<G)A>2!F;W( 9G5N#0H ("` ("!F M;W( *'5I;G0 :3TP.R!I(#P 8V]P>2YL96YG=& [(&DK*RD #0H ("` ("` M("!C;W!Y6VE=*STQ,#`[#0H ("` ("!L:7-T3V9!<G)A>7,N87!P96YD*&-O M<'DI.PT*("` ("` #0H ("` ("`O+R!P<FEN="!A;&P :71E;7, ;V8 =&AE M(&%R<F%Y<R!T:&%T(&%R92!C;VYT86EN960 :6X =&AE($QI<W0 #0H ("` M("!V;VED(&QI<W1!<G)A>2AL;VYG6UT 87)R87DI#0H ("` ("![#0H ("` M("` ("!I;G-T86YC92!#;VQL96-T:6]N*&QO;F<I($-O;&Q?;&]N9SL-"B` M("` ("` ('!R:6YT9B B;F]D92!C;VYT96YT.EQN(BD[#0H-"B` ("` ("` M(&EN<W1A;F-E($%L9V]R:71H;2A#;VQL7VQO;F<N07)R87E)="P 0V]L;%]L M;VYG+E1Y<&4Q*0T*("` ("` ("` ("` +F9O<D5A8V H;F5W($-O;&Q?;&]N M9RY!<G)A>4ET*&%R<F%Y*2P :6YS=&%N8V4 37E&=6YC<RAL;VYG*2YP<FEN M="D[#0H ("` ("!]#0H-"B` ("` (&EN<W1A;F-E($%L9V]R:71H;2A#;VQL M+DQI<W1)="P 0V]L;"Y4>7!E,2D-"B` ("` ("` ("YF;W)%86-H*&QI<W1/ M9D%R<F%Y<RYG971%;G5M97)A=&]R*"DL(&QI<W1!<G)A>2D[#0H-" T*("` M("` +R\ 9V5T($QI<W0 :71E;2!S:7IE#0H ("` ("!U:6YT('-I>F4[#0H M("` ("!V;VED(&=E=$%R<F%Y4VEZ92AL;VYG6UT 87)R87DI#0H ("` ("![ M#0H ("` ("` ("!I;G-T86YC92!->49U;F-S*&QO;F<I+E-T871E9G5L('-T M871E9G5L/6YE=R!I;G-T86YC92!->49U;F-S*&QO;F<I+E-T871E9G5L*"D[ M#0H ("` ("` ("!I;G-T86YC92!#;VQL96-T:6]N*&QO;F<I($-O;&Q?;&]N M9SL-" T*("` ("` ("` :6YS=&%N8V4 06QG;W)I=&AM*$-O;&Q?;&]N9RY! M<G)A>4ET+"!L;VYG*0T*("` ("` ("` ("` +F9O<D5A8V H;F5W($-O;&Q? M;&]N9RY!<G)A>4ET*&%R<F%Y*2P <W1A=&5F=6PN<W5M4VEZ92D[#0H ("` M("` ("!P<FEN=&8H(FYO9&4 <VEZ93H )61<;B(L('-T871E9G5L+G-I>F4I M.PT*("` ("` ("` <VEZ92L]<W1A=&5F=6PN<VEZ93L-"B` ("` ('T-" T* M("` ("` :6YS=&%N8V4 06QG;W)I=&AM*$-O;&PN3&ES=$ET+"!#;VQL+E1Y M<&4Q*0T*("` ("` ("` +F9O<D5A8V H;&ES=$]F07)R87ES+F=E=$5N=6UE M<F%T;W(H*2P 9V5T07)R87E3:7IE*3L-" T*("` ("` <')I;G1F*")3:7IE A(&ES("5U7&XB+"!S:7IE*3L-"B` ('T-"B` (`T*?0T* ` end
Aug 10 2003
parent "Matthew Wilson" <matthew stlsoft.org> writes:
Just want to acknowledge that I've got this. I've just got a bit "under"
with workload, and cannot look at the template stuff now, but I promise to
do so in a few weeks.

Hopefully all you good people will have it sorted by then. ;)

"Farmer" <itsFarmer. freenet.de> wrote in message
news:Xns93D3A64045070itsFarmer 63.105.9.61...
 "Matthew Wilson" <matthew stlsoft.org> wrote in
 news:bgvh60$205c$1 digitaldaemon.com:

 I want to see an implementation of for_each() that works for two
 sequence types - say a built in array of double and a template-list of
 Thing objects (Thing is any UDT) - and with two completely different
 functors - say a stateful one that sums the .size of each element and a
 stateless one that dumps them to stdout.

 No offence to anyone on the NG - and *please* correct me if I'm wrong -
 but I expect Walter's the only one who currently could do this if it's
 possible, which I strongly suspect it is not.
I guess you just want to torture Walter by letting him write templated stuff. Well, I tought what's worse than writting templated stuff? Having to read templated stuff, written by someone else, of course. <grin> See the attachment for one solution of the given task. Note: In the example I used the template-list in combination with long[] build-in arrays, instead of plain build-in arrays of double.
 On both issues I'm dying to be wrong - partly because I simply want to
 learn how this is done - but until I am we can't say we're even in a
 position to ask the right questions about a DTL, never mind answer them
Agree.
Aug 10 2003