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

 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

 | 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

 | 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.
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