Hacker News new | ask | show | jobs
by neelesh 6802 days ago
The python implementation looks overly complicated. I was thinking that the ruby and python implementations would be very similar. Here it goes

def foo(n):

    return lambda i:n+i
Tried it (Python 2.5) and it works.
2 comments

That should read:

    def foo(n):
        n = [n]
        def bar(i):
            n[0] += i
            return n[0]
        return bar
Python really needs a scoping syntax to allow assignment to closed variables.
Actually it doesn't work - calling the returned lambda should increment n - your implementation doesn't.
That holds true for the given ruby implementation as well, since n is incremented only in the scope of foo and not in the calling scope.

def foo (n)

    lambda {|i| n += i } 
end

n=5

a = foo(n)

puts a.call(3) #prints 8

puts n # still prints 5. n is not incremented

I agree that the python one is strictly not according to the problem definition, but for all practical purposes both python equivalents are the same, aren't they? or am I missing something?