hi
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 in
ruby,
in scheme it would be like
(define (make-adder n)
(lambda (x) (+ x n))
thanks in advance
hi
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 in
ruby,
in scheme it would be like
(define (make-adder n)
(lambda (x) (+ x n))
thanks in advance
Hi Burlsm,
I think the following is what you want:
def make_adder(n)
lambda { |x| x + n }
end
or the following new lambda syntax if you are using Ruby 1.9 or later:
def make_adder(n)
->(x) { x + n }
end
Hope that helps.
allenlooplee
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
On a sidenote, Ruby 1.9 also has this cool function:
f = ->(a,b){ a + b }
c = f.curry
b = c[1]
where b is a partially applied function, with a bound to 1.
b[6] #=> a + b = 7
then evaluates the whole function.
Regards,
Florian
P.S.: I still think it should be called #schönfinkeln.
On Nov 28, 2009, at 2:47 AM, Allen L. wrote:
def make_adder(n)
hi
thanks in advance
Florian G.
smtp: [email protected]
jabber: [email protected]
gpg: 533148E2
-----BEGIN PGP SIGNATURE-----
Version: GnuPG/MacGPG2 v2.0.12 (Darwin)
iEYEARECAAYFAksQ1iEACgkQyLKU2FMxSOIX+QCeN3vFLd0m1dIkxo+limomEs1s
1KYAniQhUT/DlL73ON63DSX/FrVTVIgN
=irHI
-----END PGP SIGNATURE-----
Burlsm wrote:
hi
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 in
ruby,in scheme it would be like
(define (make-adder n)
(lambda (x) (+ x n))thanks in advance
I noticed this link describing some scheme style closures in ruby
http://innig.net/software/ruby/closures-in-ruby.rb
in the ruby talk faq the other day.
-r
2009/11/28 Florian G. [email protected]:
b[6] #=> a + b = 7
then evaluates the whole function.
The cool thing is that the result of f.curry can be used to curry the
function as well as to evaluate it - depending on the number of
arguments:
irb(main):001:0> f = ->(a,b){ a + b }
=> #<Proc:0x101731c0@(irb):1 (lambda)>
irb(main):002:0> f[1,2]
=> 3
irb(main):003:0> c = f.curry
=> #Proc:0x10169d28
irb(main):004:0> c[1,2]
=> 3
irb(main):005:0> b = c[1]
=> #Proc:0x10159cc0
irb(main):006:0> b[2]
=> 3
irb(main):007:0>
P.S.: I still think it should be called #schönfinkeln.
LOL
Kind regards
robert
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.
Sponsor our Newsletter | Privacy Policy | Terms of Service | Remote Ruby Jobs