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- 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*("` ("` M87, 5"!)=&5M5'EP93L-"B` ("` (`T*("` ("` 3&ES=$YO9&4 8W5R<F5N M=#L- M("` ("` (')E='5R;B!R<SL-"B` ("` ('T-"B` ('T-"B` (`T*("` 8VQA 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"B` ('L-"B` ("` ('5I;G0 :61X.PT*("` ("` 5%M=(&%R<F%Y.PT*("` 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 M92!!;&=O<FET:&TH271E<F%T;W(L($ET96U4>7!E*0T*>PT*("` 86QI87, M("` ("!W:&EL92`H:70N:&%S3F5X=" I*0T*("` ("` ("` 9BAI="YN97AT M<B!I="P 1G5N8W1I;VX 9BD-"B` ('L-"B` ("` ('=H:6QE("AI="YH87-. M;VED('!R:6YT*$ET96T =BD-"B` ('L-"B` ("` ('!R:6YT9B B)2XJ<UQN M>F4H271E;2!V*0T*("` ("` >PT*("` ("` ("` <VEZ92L]=BYS:7IE.PT* M<W, 4W1A=&5F=6P-"B` ('L-"B` ("` ('5I;G0 <VEZ93L-"B` ("` ('9O M"GL-"B` ('L-"B` ("` ("\O(&-R96%T92!,:7-T(&]F($]B:F5C=',-"B` 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("` ("` +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;#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.R!I*RLI(`T*("` ("` ("` 87)R87E;:5T]:3L-"B` ("` (`T*("` ("` 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;F=T:#TS.R M;W( *'5I;G0 :3TP.R!I(#P 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+DQI<W1)="P 0V]L;"Y4>7!E,2D-"B` ("` ("` ("YF;W)%86-H*&QI<W1/ M871E9G5L/6YE=R!I;G-T86YC92!->49U;F-S*&QO;F<I+E-T871E9G5L*"D[ 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("` ("!P<FEN=&8H(FYO9&4 <VEZ93H )61<;B(L('-T871E9G5L+G-I>F4I M.PT*("` ("` ("` <VEZ92L]<W1A=&5F=6PN<VEZ93L-"B` ("` ('T-" T* 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