ruby blocks (Re: foreach, an analogy)
    Bill Baxter 
    dnewsgroup at billbaxter.com
       
    Wed Oct 18 22:14:06 PDT 2006
    
    
  
Jarrett Billingsley wrote:
> "Bill Baxter" <dnewsgroup at billbaxter.com> wrote in message 
> news:eh6rva$1anj$2 at digitaldaemon.com...
> 
>> I think it may be Ruby blocks.
> 
> They're just anonymous functions which happen to come after the function 
> call's closing paren.. I wouldn't really say that they're incredibly 
> earth-shattering or the answer to everything.  
We don't need the answer to everything, just the answer to how to 
generalize and make foreach more consistent.
 > And D can almost do them
> already.  Instead of:
> 
> something.each do |item|
>     puts item
> end
> 
> You can have:
> 
> something.each((int item) {
>     writefln(item);
> });
> 
> In fact the "allowing a trailing function literal" has been proposed (by 
> myself included), which would allow:
> 
> something.each()(int item)
> {
>     writefln(item);
> }
> // maybe there'd need to be a semicolon here?
> 
> Which is damn close if you ask me. 
That does look pretty good.
How do you write an inorder traverser?  With Ruby:
def traverse
   left.traverse {|node| yield node }
   yield self
   right.traverse {|node| yield node }
end
Then you do:
atree.traverse()(Node node)
{
     writefln(item);
}
--bb
    
    
More information about the Digitalmars-d-announce
mailing list