Florian Frank <flori / nixe.ping.de> wrote:
> Robert Klemme wrote:
>
>> AFAIK it's implemented in module Enumerable but apart from that
>> you're right. Even if it was implemented in Ruby it would be
>> implemented in terms of #each and thus iteratively.
>
> You're right. And I knew that. ;)

:-))) This happens to all of us once in a while...

>> Well, yes.  Interestingly if you view a block as an anonymous
>> function (which is what it is basically) Enumerable#inject has the
>> same interface (apart from syntax) as foldr.  Arguments are an
>> initialization value and a binary function.
>
> If you wanted to define the type signature of inject, it would be
> quite difficult. You cannot omit the start value in Haskell foldr, for
> example. Ruby takes  a  rather pragmatic approach to typing, while
> Haskell has a much more pedantic one.

Yeah, I wouldn't want to go that much into the details. I also forgot to 
mention the receiver instance (it needs three things here: collection, start 
value, binary funtion).  Btw, IMHO those anonymous functions (aka "blocks") 
are among the most powerful features of Ruby.  I always think of this when I 
have to create an interface in Java just to get a callback...

> Yeah. I once made a quicksort in one lambda, when I was bored:
<snip/>

Nice!

> There are still Array operations used, but you could as well define
> cons pairs in terms of lambda, and then define lists of them. You
> could define a Y combinator to get rid of the quicksort assignment
> for the recursion, and so on. For learning and having fun this is
> great, but I guess it will get rather slow and be a memory hog, if
> you expect that to do real work.

I guess so, too.

Kind regards

    robert