From: Jim Weirich Date: 2004-02-25T20:43:59+09:00 Subject: Re: Need examples comparing Ruby to Python Tobias Nurmiranta said: > Ah interesting, so one can use f[x] instead of f.call(x), by defining [] > in Method. But if you want to call compose with defined functions instead > of lambdas, you have to use: > compose(method(:f1), method(:f2)) It would be more accurate to say: "But if you want to call compose with defined methods instead of functions, you have to get a callable version of the method by using ..." You see, Ruby doesn't have functions. Not really. What it does have are objects that look sufficiently like functions that we can can use them in functional ways. These "functional objects" (or "functors") can be called by sending a "call" message to the object. The "[]" convention is just a little syntax sugar for the call message. > I guess that this syntax "overhead" is due to that we want to be able to > call function f with "f" instead of "f()". Otherwise functions could be > passed as arguments more easily, with one syntax to call them. More accurately, the syntax "f()" is reserved for sending the message "f" to self, not for function calls. > Given "oneplus = lambda {|x| x + 1}" wouldn't it be possible to call the > function with "oneplus(1)", implemented so that the interpreter checks if > it is a Method instance, and then calls the method. Then you wouldn't have > to have several ways to call a function. There are no functions in Ruby, only functors. And there is only one way to call a functor ... by sending it a message. I think this goes to the heart of the difference between Python and Ruby. Pythonists see functions as fundamental and build objects composed of functions callable through a dictionary mechanism. Rubyist see objects as fundamental and build functions through callable objects. >> The Python version of compose is more like this ... [...] > My python interpreter (2.3.2) doesn't accept this code. I should have said "The Ruby translation of the Python version of compose..." -- -- Jim Weirich jim@weirichhouse.org http://onestepback.org ----------------------------------------------------------------- "Beware of bugs in the above code; I have only proved it correct, not tried it." -- Donald Knuth (in a memo to Peter van Emde Boas)