From: Robert Klemme Date: 2006-03-19T21:33:57+09:00 Subject: Re: Functor Opinion Trans wrote: > So here's the Functor class: > > class Functor < BasicObject > def initialize(*obj, &func) > @obj = obj > @func = func > end > def method_missing(op, *args) > if @obj.empty? > @func.call(op, *args) > else > @func.call(op, *(@obj + args)) > end > end > end > > Anyone who has seen the older version of this cllas will notice that I > have added the *obj parameter. This allows the Functor to act like a > simple delegator too. But strictly speaking it isn't at all necessary > since one could just use the object(s) passed to the initialize > directly in the block. Eg. > > foo = 1 > Functor.new(foo) { |op, obj, *args| obj.send(op, *args) } > > is the exact same as > > foo = 1 > Functor.new { |op, *args| foo.send(op, *args) } > > The only time this really is a little more handy is when passing > 'self', because of the ambiguity between contexts. Eg. > > Functor.new(self) { |op, obj, *args| obj.send(op, *args) } > > vs. > > slf = self > Functor.new { |op, *args| slf.send(op, *args) } > > Not a big deal but there it is. Is there any other reason anyone can > think of that the *obj parameter is acctually needed or more > convenient vs. direct reference in the block itself? If not, is it > worth keeping, or should I just get rid of the *obj parameter > altogether? > > Settling that issue, I'd like to request that the Functor class be > made part of Ruby's standard library. It is simple, generic and > generally useful --I have had use for it myself on more than a few > occasions now across multiple projects. I'm not the one who ultimately will decide whether something goes into Ruby's standard lib or not. But I have doubts in this case. I cannot recognize a clear cut task of this class; it's very generic and doesn't really do much apart from propagating (delegating) arbitrary method calls. I'm not even sure that the name is appropriately chosen. If one looks at the definition in Wikipedia at http://en.wikipedia.org/wiki/Function_object a functor needs at least the syntactic looks of a function. In the case of Ruby, implementing #[] and / or #call seems to be a minimum requirement. Can you elaborate a bit more the benefits and use cases of this class? Thanks! Kind regards robert