| Thread overview | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
August 07, 2003 postinc | ||||
|---|---|---|---|---|
| ||||
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
| ||||
August 08, 2003 Re: postinc | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Carlos Santander B. | > 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 ---------------------------------------------------------------------------- --- | |||
August 08, 2003 Re: postinc | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Matthew Wilson | "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 | |||
August 08, 2003 Re: postinc | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Carlos Santander B. | "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. | |||
August 10, 2003 Re: postinc - 1 attachment | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Matthew Wilson Attachments: | "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. | |||
August 11, 2003 Re: postinc - 1 attachment | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Farmer | 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. > > > > > > | |||
August 13, 2003 Re: postinc | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Matthew Wilson | "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? | |||
August 13, 2003 Re: postinc | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Walter | 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!!!!
| |||
Copyright © 1999-2021 by the D Language Foundation
Permalink
Reply