I'm having a little trouble with closures and I'd like to know what the equivalent code for the canonical make-adder procedure would be in Ruby.
In scheme it would be like:
(define (make-adder n)
(lambda (x) (+ x n))
It's actually very close...
def make_addr n
lambda { |x| x + n }
end
t = make_addr 100
t.call 1
101
In 1.9 you can use...
def make_addr n
->(x) { x + n }
end
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With