digitalmars.D - Proposal: delegates as aggregates in foreach statements
- Chris Nicholson-Sauls (41/41) May 05 2006 An idea occurred to me last night, which I'm sure must have come up befo...
- Bruno Medeiros (5/53) May 07 2006 Hum, seems like a sound proposal. I think it could be good.
- Chris Nicholson-Sauls (3/59) May 14 2006 Well at least you thought so. Doesn't look like it caught anyone else's...
- James Dunne (17/81) May 14 2006 Just because nobody replies doesn't mean nobody reads. There's a lot of...
- Chris Nicholson-Sauls (11/96) May 15 2006 True enough. I didn't really mean anything scathing by it, anyhow. Hon...
- Oskar Linde (9/18) May 15 2006 You get it almost as sweet and simple by using a a wrapper struct. See
- James Dunne (11/111) May 15 2006 And how!
An idea occurred to me last night, which I'm sure must have come up before. If it hasn't, I'm shocked, but I'm bringing it up (again?) anyway. Why not allow a delegate (or even function pointer?) to be used as the "aggregate" parameter to a foreach statement, requiring that it expose the same signature as a valid opApply method? For example: One could even get real cute and use anonymous delegates: This, I think, would stand in the place of many uses of iterator objects and mutators. -- Chris Nicholson-Sauls
May 05 2006
Chris Nicholson-Sauls wrote:An idea occurred to me last night, which I'm sure must have come up before. If it hasn't, I'm shocked, but I'm bringing it up (again?) anyway. Why not allow a delegate (or even function pointer?) to be used as the "aggregate" parameter to a foreach statement, requiring that it expose the same signature as a valid opApply method? For example: One could even get real cute and use anonymous delegates: This, I think, would stand in the place of many uses of iterator objects and mutators. -- Chris Nicholson-SaulsHum, seems like a sound proposal. I think it could be good. -- Bruno Medeiros - CS/E student http://www.prowiki.org/wiki4d/wiki.cgi?BrunoMedeiros#D
May 07 2006
Bruno Medeiros wrote:Chris Nicholson-Sauls wrote:Well at least you thought so. Doesn't look like it caught anyone else's eye. -- Chris Nicholson-SaulsAn idea occurred to me last night, which I'm sure must have come up before. If it hasn't, I'm shocked, but I'm bringing it up (again?) anyway. Why not allow a delegate (or even function pointer?) to be used as the "aggregate" parameter to a foreach statement, requiring that it expose the same signature as a valid opApply method? For example: One could even get real cute and use anonymous delegates: This, I think, would stand in the place of many uses of iterator objects and mutators. -- Chris Nicholson-SaulsHum, seems like a sound proposal. I think it could be good.
May 14 2006
Chris Nicholson-Sauls wrote:Bruno Medeiros wrote:Just because nobody replies doesn't mean nobody reads. There's a lot of content to plow through during the weekdays on this newsgroup. I find myself guilty of "Mark Thread As Read" (Thunderbird) a lot recently for the threads I'm not interested in... Anyway, about your proposal... I don't much see much utility in *anonymous* delegates for iteration (lots of code duplication for commonly-iterated objects; and what's the point of including complex iterator code next to the body of the iteration itself?). But, being able to select an iterator method for a class without breaking that method into its own iterator object does have its uses. For instance, reverse iteration and tree-walkers (pre-order, in-order, post-order, etc.) look to be good candidates here. -- Regards, James DunneChris Nicholson-Sauls wrote:Well at least you thought so. Doesn't look like it caught anyone else's eye. -- Chris Nicholson-SaulsAn idea occurred to me last night, which I'm sure must have come up before. If it hasn't, I'm shocked, but I'm bringing it up (again?) anyway. Why not allow a delegate (or even function pointer?) to be used as the "aggregate" parameter to a foreach statement, requiring that it expose the same signature as a valid opApply method? For example: One could even get real cute and use anonymous delegates: This, I think, would stand in the place of many uses of iterator objects and mutators. -- Chris Nicholson-SaulsHum, seems like a sound proposal. I think it could be good.
May 14 2006
James Dunne wrote:Chris Nicholson-Sauls wrote:True enough. I didn't really mean anything scathing by it, anyhow. Honestly, it was a bump message. :)Bruno Medeiros wrote:Just because nobody replies doesn't mean nobody reads. There's a lot of content to plow through during the weekdays on this newsgroup. I find myself guilty of "Mark Thread As Read" (Thunderbird) a lot recently for the threads I'm not interested in...Chris Nicholson-Sauls wrote:Well at least you thought so. Doesn't look like it caught anyone else's eye. -- Chris Nicholson-SaulsAn idea occurred to me last night, which I'm sure must have come up before. If it hasn't, I'm shocked, but I'm bringing it up (again?) anyway. Why not allow a delegate (or even function pointer?) to be used as the "aggregate" parameter to a foreach statement, requiring that it expose the same signature as a valid opApply method? For example: One could even get real cute and use anonymous delegates: This, I think, would stand in the place of many uses of iterator objects and mutators. -- Chris Nicholson-SaulsHum, seems like a sound proposal. I think it could be good.Anyway, about your proposal... I don't much see much utility in *anonymous* delegates for iteration (lots of code duplication for commonly-iterated objects; and what's the point of including complex iterator code next to the body of the iteration itself?). But, being able to select an iterator method for a class without breaking that method into its own iterator object does have its uses. For instance, reverse iteration and tree-walkers (pre-order, in-order, post-order, etc.) look to be good candidates here.I think what I had in mind (and just didn't "vocalize") in using anonymous delegates, was to use them like wrappers around a list of other delegate calls or the like (multi-phase looping, perhaps). I do see your point that in most cases if you were going to do that, you may as well just write the logic into the iteration block itself. The tree-walkers and suchlike that you mention, though -- that's exactly the sort of thing I'm after. Being able to just do (foreach (i, x; &tree.walkInOrder) {...}) with .walkInOrder() being a simple method would be, pardon the colloquialism, wicked sweet. -- Chris Nicholson-Sauls
May 15 2006
Chris Nicholson-Sauls skrev:I think what I had in mind (and just didn't "vocalize") in using anonymous delegates, was to use them like wrappers around a list of other delegate calls or the like (multi-phase looping, perhaps). I do see your point that in most cases if you were going to do that, you may as well just write the logic into the iteration block itself. The tree-walkers and suchlike that you mention, though -- that's exactly the sort of thing I'm after. Being able to just do (foreach (i, x; &tree.walkInOrder) {...}) with .walkInOrder() being a simple method would be, pardon the colloquialism, wicked sweet.You get it almost as sweet and simple by using a a wrapper struct. See for instance Andrew Fedoniouk's tree implementation: http://www.digitalmars.com/d/archives/digitalmars/D/dtl/378.html That allows: foreach(Node n; parent.forward) // all children from first to last foreach(Node n; parent.backward) // all children from last to first foreach(Node n; parent.deep) // all descendants - children and their /Oskar
May 15 2006
Chris Nicholson-Sauls wrote:James Dunne wrote:Seems to have worked then...Chris Nicholson-Sauls wrote:True enough. I didn't really mean anything scathing by it, anyhow. Honestly, it was a bump message. :)Bruno Medeiros wrote:Just because nobody replies doesn't mean nobody reads. There's a lot of content to plow through during the weekdays on this newsgroup. I find myself guilty of "Mark Thread As Read" (Thunderbird) a lot recently for the threads I'm not interested in...Chris Nicholson-Sauls wrote:Well at least you thought so. Doesn't look like it caught anyone else's eye. -- Chris Nicholson-SaulsAn idea occurred to me last night, which I'm sure must have come up before. If it hasn't, I'm shocked, but I'm bringing it up (again?) anyway. Why not allow a delegate (or even function pointer?) to be used as the "aggregate" parameter to a foreach statement, requiring that it expose the same signature as a valid opApply method? For example: One could even get real cute and use anonymous delegates: This, I think, would stand in the place of many uses of iterator objects and mutators. -- Chris Nicholson-SaulsHum, seems like a sound proposal. I think it could be good.And how! -- -----BEGIN GEEK CODE BLOCK----- Version: 3.1 GCS/MU/S d-pu s:+ a-->? C++++$ UL+++ P--- L+++ !E W-- N++ o? K? w--- O M-- V? PS PE Y+ PGP- t+ 5 X+ !R tv-->!tv b- DI++(+) D++ G e++>e h>--->++ r+++ y+++ ------END GEEK CODE BLOCK------ James DunneAnyway, about your proposal... I don't much see much utility in *anonymous* delegates for iteration (lots of code duplication for commonly-iterated objects; and what's the point of including complex iterator code next to the body of the iteration itself?). But, being able to select an iterator method for a class without breaking that method into its own iterator object does have its uses. For instance, reverse iteration and tree-walkers (pre-order, in-order, post-order, etc.) look to be good candidates here.[snip] Being able to just do (foreach (i, x; &tree.walkInOrder) {...}) with .walkInOrder() being a simple method would be, pardon the colloquialism, wicked sweet. -- Chris Nicholson-Sauls
May 15 2006