From: Intransition Date: 2010-04-08T15:01:24+09:00 Subject: Re: Is it possible, a fully general Enumerable#recursive ? On Apr 7, 11:51 pm, David Masover wrote: > On Wednesday 07 April 2010 04:49:15 pm Intransition wrote: > > > For the last couple of days I've been trying to write an Enumerable > > method called #recursive. Rather than create a bunch of methods like > > #recursive_each, #recursive_map, #recursive_sort, etc. I figured that > > it should be possible to create a single #recursive method that > > returned an Enumerator, > > Yes! > > > or barring that a Functor, > > What's a Functor in this context? We already have proc objects, which is > traditionally what "functor" means... Functor is defined as: class Functor private(*instance_methods.select { |m| m !~ /(^__|^binding$)/ }) def initialize(&function) @function = function end def to_proc @function end def method_missing(op, *args, &blk) @function.call(op, *args, &blk) end end #method_missing is what makes it different from a Proc. > > This works for #each and #map but not #sort. > > #sort isn't a method of Enumerable, it's a method of Array. It seems to be according to ri. > > Can anyone think of a better solution? > > Probably, but I'm not sure what it would look like. What's your use case -- > how would I actually use this to, say, generate an infinite stream of > fibbonacci numbers? I don't think anyone can generate that seeing that it's infinite and all ;-) > I guess I don't really see why you'd define it in terms of an enumerable (in > that case, you probably want inject). Perhaps Array is the more appropriate place for it.