closures

B

Burlsm

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
 
A

Allen Lee

[Note: parts of this message were removed to make it a legal post.]

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
 
F

Florian Gilcher

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On a sidenote, Ruby 1.9 also has this cool function:

f =3D ->(a,b){ a + b }
c =3D f.curry
b =3D c[1]

where b is a partially applied function, with a bound to 1.

b[6] #=3D> a + b =3D 7

then evaluates the whole function.

Regards,
Florian

P.S.: I still think it should be called #sch=F6nfinkeln.

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

- --
Florian Gilcher

smtp: (e-mail address removed)
jabber: (e-mail address removed)
gpg: 533148E2

-----BEGIN PGP SIGNATURE-----
Version: GnuPG/MacGPG2 v2.0.12 (Darwin)

iEYEARECAAYFAksQ1iEACgkQyLKU2FMxSOIX+QCeN3vFLd0m1dIkxo+limomEs1s
1KYAniQhUT/DlL73ON63DSX/FrVTVIgN
=3DirHI
-----END PGP SIGNATURE-----
 
R

Roger Pack

Burlsm said:
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
 
R

Robert Klemme

2009/11/28 Florian Gilcher said:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On a sidenote, Ruby 1.9 also has this cool function:

=A0f =3D ->(a,b){ a + b }
=A0c =3D f.curry
=A0b =3D c[1]

where b is a partially applied function, with a bound to 1.

=A0b[6] #=3D> a + b =3D 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 =3D ->(a,b){ a + b }
=3D> #<Proc:0x101731c0@(irb):1 (lambda)>
irb(main):002:0> f[1,2]
=3D> 3
irb(main):003:0> c =3D f.curry
=3D> #<Proc:0x10169d28>
irb(main):004:0> c[1,2]
=3D> 3
irb(main):005:0> b =3D c[1]
=3D> #<Proc:0x10159cc0>
irb(main):006:0> b[2]
=3D> 3
irb(main):007:0>
P.S.: I still think it should be called #sch=F6nfinkeln.

LOL

Kind regards

robert

--=20
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,143
Latest member
DewittMill
Top