From: Robert Klemme Date: 2010-04-13T05:25:04+09:00 Subject: Re: Is it possible, a fully general Enumerable#recursive ? On 04/12/2010 07:00 PM, Caleb Clausen wrote: >> On Apr 12, 11:08 am, Robert Klemme wrote: >> >>> I don't really understand why this thread seems to be so complicated. >>> I suspect that either I am missing something or not understanding >>> properly, or other contributors to this thread are missing something. >>> :-) > > I find this subject extremely confusing. Colin's answer to my last > email is still causing me puzzlement. :-) > On 4/12/10, Intransition wrote: >> I think I see what the confusion is. Your version of recursive looses >> all the nested structure of the original. And so works fine for most >> cases, as long that is what one wants/expects. I've been trying to do >> it such that the nested structure stays intact. e.g. >> >> [3,2,1,[6,5,4,[9,8,7]].recursive.sort #=> [1,2,3,[4,5,6,[7,8,9]] >> >> Your method produces: >> >> [3,2,1,[6,5,4,[9,8,7]].recursive.sort #=> [1,2,3,4,5,6,7,8,9] >> >> The former proves much more difficult. > > Yeah, Intransition's way is what I understood to be the point of this > thread.... Probably because he is the OP. :-) > Intransition, how would you handle this: > [9,8,7,[6,5,4,[3,2,1]].recursive.sort #=> [1,2,3,[4,5,6,[7,8,9]] > > Clearly possible, but I have trouble seeing how you're going to make > that happen easily. The question is: is this desirable? How do you compare a collection with individual values etc.? You also cannot easily provide a block to #sort because then you need might have to do complex type differentiations etc. > I have often found that too much recursion is confusing. This whole > problem is going to be a lot easier if it's structured as an external > iterator first and foremost. I have an extern iterator library > (sequence) maybe I can figure out how to do this within that. (I don't > really have a good grip on how Enumerator works, myself...) I also start getting doubts whether all this is a good idea - either my way or Tom's way. My way suffers from loss of structure information as well as it requires compatible types as leaves on all levels. His way makes sorting difficult since you do not have a uniform type any more. It's probably better to provide classes for the nesting on a case by case basis. Kind regards robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/