[prev in list] [next in list] [prev in thread] [next in thread] 

List:       ruby-talk
Subject:    Re: Need examples comparing Ruby to Python
From:       Florian Gross <flgr () ccan ! de>
Date:       2004-02-25 22:14:47
Message-ID: c1j6i7$1j0pk4$1 () ID-7468 ! news ! uni-berlin ! de
[Download RAW message or body]

Tobias Nurmiranta schrieb:

> Hi,

Moin!

> 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.

Yes, but it wouldn't be possible to call it without parentheses and 
there would be other semantic differences.

I think it's either optional parentheses or function object access via name.

>>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.

Yup, you will have to backport it. I think he just used Ruby to show the 
different semantics of the original posters LISP and Python versions. 
(The LISP version returns a function which can be called to get the 
result and the Python version directly returns the result.)

Backported it looks like this:

def compose(f, g, x): # the result version
   return f(g(x))

def compose_callable(f, g): # the function version
   return lambda x: f(g(x))

Regards,
Florian Gross

[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic