[prev in list] [next in list] [prev in thread] [next in thread]
List: ruby-talk
Subject: Re: Need examples comparing Ruby to Python
From: Tobias Nurmiranta <spyck () lysator ! liu ! se>
Date: 2004-02-25 8:34:56
Message-ID: Pine.GSO.4.51L2.0402250910140.25026 () nazgul ! lysator ! liu ! se
[Download RAW message or body]
Hi,
On Wed, 25 Feb 2004, Jim Weirich wrote:
> The Python and Scheme examples are a bit different.
Yes, thats why I had a comment in the scheme code.
> The Scheme version in Ruby ...
>
> oneplus = lambda {|x| x+1 }
> def compose(f, g)
> lambda { |x| f[g[x]] }
> end
> compose(oneplus, oneplus) [1] #=> 3
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))
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.
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.
> The Python version of compose is more like this ...
>
> def compose(f, g, x)
> f[g[x]]
> end
My python interpreter (2.3.2) doesn't accept this code.
, Tobias
[prev in list] [next in list] [prev in thread] [next in thread]
Configure |
About |
News |
Add a list |
Sponsored by KoreLogic