Building a Better Functor

D

David A. Black

Hi --

Coming from Javascript (where every instance is trivially extensible

Aw, come from Ruby already! :)
directly) that seems somewhat cumbersome.

Whoops, I snipped the part you were talking about... but aren't they
in Ruby too?

o = Object.new
def o.x; puts "I'm a new method!"; end
Do many people write things like:

class Object
def instance_class
class<<self; self; end
end
end

in a common library they re-use frequently? Is there an RCR (or has there
been discussion) about adding a simple method like that to the language, so
you can simply do:

f = Foo.new
f.instance_class.class_eval{ ... }

Yes; I submitted such an RCR, for Kernel#singleton_class. (I know
about the naming issues, but I'd rather not deviate from what Matz
uses at a given time.) See http://www.rcrchive.net/rcr/show/231
Or (what I'm really looking for) perhaps it might be nice to have a
define_method method that worked directly for instances. Hrm, but what would
the syntax be? Perhaps that's not such a good idea.

I think that Kernel#singleton_class would work for that purpose, and
would also completely transform people's understanding of the whole
class/singleton-class duality.


David
 
R

Robert Klemme

from that alone it's obvious that it's a bit more complicated.
Indeed JavaScript supports object orientation based on prototypes
rather than classes. This is an idea borrowed by a Smalltalk-like
language called Self and other sources of inspirations.

Don't underestimate JavaScript though or prototype-based inheritance.

I didn't intend to. It's just that Ruby has classes while JS has not.
Most things that can be done with classes can be done with prototypes.
Some things are straightforward with prototypes: delegation, addition
of a few methods or properties to one or a few instances.

The obvious thing with JavaScript is that it lacks syntactic sugar to
make prototype construction more legible and distinguishable. Most
JavaScript code out there are lame and don't use object programming.
But the real point is that writing object code in JavaScript can be
pretty complicated and likely much less legible than in Ruby.

Btw:

class JavaScript
def method_missing(s,*a,&b)
if /^(.*)=$/ =~ s.to_s
class <<self;self;end.class_eval do
define_method($1,&a[0])
end
else
super
end
end
end
hello
world
=> nil

:)

Kind regards

robert
 
J

John W. Long

Trans said:
Hi--

Just FYI. I do not think this is what one calls a Functor exactly,
since the object is independent of the method ( please see
http://jakarta.apache.org/commons/sandbox/functor/ ).

. . .

f = Functor.new { |op, x| x.send(op, x) }
f + 1 #=> 2
f + 2 #=> 4
f + 3 #=> 6
f * 1 #=> 1
f * 2 #=> 2
f * 3 #=> 9

. . .

Interesting. So how exactly would you define a functor? What you have
shown above seems to be an object that knows how to do some kind of
operation using a user-defined method. Is this how you would define a
functor?
 
T

Trans

Interesting. So how exactly would you define a functor?
What you have shown above seems to be an object that
knows how to do some kind of operation using a
user-defined method. Is this how you would define a functor?

A good question. By standard definition, Method objects and Proc
objects are Functors. Since Ruby already has these I'm simply defining
a Functor as a "MetaMethod" for the sake of a distinction. By this I
mean an object the represents a method which can act on another method.
Function composition, represented as a 1st class object, for instance,
would also be a Functor of this sort.


T.
 
J

John W. Long

Trans said:
A good question. By standard definition, Method objects and Proc
objects are Functors. Since Ruby already has these I'm simply defining
a Functor as a "MetaMethod" for the sake of a distinction. By this I
mean an object the represents a method which can act on another method.
Function composition, represented as a 1st class object, for instance,
would also be a Functor of this sort.

And a functor, by definition only handles one method? Which is why what
I have written is not a functor?
 
T

Trans

And a functor, by definition only handles one method? Which
is why what. I have written is not a functor?

Yes. But that not to say what you have written isn't close (and damn
cool). I just wouldn't call it a Functor per se. Certainly, your first
example is very close, since it only stores a single block, so I see
why you'd call it a Functor. But it's the block inside that's actually
the Functor. You're creating more of a Functor Delegator. A minor
distinction no doubt. Maybe not even worth considering. But I thought
I'd point it out.

T.
 
M

Mathieu Bouchard

I don't - I prefere singleton_class or instance_class. :) It seems it's
about time that matz settles this once and forever by choosing a name and
adding a method to Object in the next release.

The Module class already has an instance-method called
"singleton_methods".

I recall that at one point I was beginning to call a singleton class a
s-class, a singleton-method a s-method, an instance method an i-method, an
instance variable an i-var, especially because that kind of shortcut was
being helpful in designing RubyAST (an internal format for ruby compilers
i designed a few years ago).

In retrospect i like calling them with those short names, because i think
the long names are not that well-chosen, or that i think they are just too
long :-/

The best/worst offender is: if you define a s-method on a class object x,
and you make a class object y that inherits from x, then why the heck
would it be callable from y _AND_ called "singleton", at the same time ?

proxy_class is *not* a good name. it's already used for something else on
a very related topic, in a way that could be confusing (although not that
many people would complain). See chapter 19 of Pickaxe-2000.

_____________________________________________________________________
Mathieu Bouchard -=- Montréal QC Canada -=- http://artengine.ca/matju
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top