Higher order ruby

Z

zslevi

foo = lambda {|x| lambda {|y| return x+y}}

foo.call(3)(4)

Why this doesn't work? I expected lamdas to behave like in SML.
 
Z

zslevi

I even tried

foo = lambda {|x| lambda return {|x,y| return x+y}}

(foo.call(3)).call(4)

But it doesn't work either.
 
J

Jesús Gabriel y Galán

foo = lambda {|x| lambda {|y| return x+y}}

foo.call(3)(4)

Why this doesn't work? I expected lamdas to behave like in SML.

When you call foo, or apply it with [], you get another lambda in return.
To call that lambda you have to use call again, or []:

irb(main):001:0> foo = lambda {|x| lambda {|y| return x+y}}
=> #<Proc:0xb7bb5f28@(irb):1>
irb(main):002:0> foo[3][4]
=> 7
irb(main):003:0> foo.call(3).call(4)
=> 7

Jesus.
 
Z

zslevi

foo = lambda {|x|
return lambda {|y| return x+y}}

puts (foo.call(3)).call(4)

It works finally, but it's just too verbose.
Can anyone suggest a shorter, programmer friendlier way of writing
this?
 
B

Brian Candler

zslevi said:
foo = lambda {|x|
return lambda {|y| return x+y}}

puts (foo.call(3)).call(4)

It works finally, but it's just too verbose.
Can anyone suggest a shorter, programmer friendlier way of writing
this?

foo[3][4] as shown above. And you don't need 'return' inside the lambda.

foo = lambda {|x| lambda {|y| x+y}}
foo[3][4]
 
J

Jesús Gabriel y Galán

foo = lambda {|x|
return lambda {|y| return x+y}}

puts (foo.call(3)).call(4)

It works finally, but it's just too verbose.
Can anyone suggest a shorter, programmer friendlier way of writing
this?

As said before you don't need the returns, and you don't need so many
parentheses.
Can you let us know if this works for you?

irb(main):004:0> foo = lambda {|x| lambda {|y| x + y}}
=> #<Proc:0xb7b9c758@(irb):4>
irb(main):005:0> foo.call(3).call(4)
=> 7
irb(main):006:0> puts foo.call(3).call(4)
7
=> nil
irb(main):007:0> puts foo[3][4]
7
=> nil

Jesus.
 
P

Pascal J. Bourguignon

zslevi said:
foo = lambda {|x|
return lambda {|y| return x+y}}

puts (foo.call(3)).call(4)

It works finally, but it's just too verbose.
Can anyone suggest a shorter, programmer friendlier way of writing
this?

What about:

irb(main):038:0> (foo = (lambda {|x| (lambda {|y| (x + y)})}))
#<Proc:0x00007f797ea420f0@(irb):38>
irb(main):039:0> (funcall (funcall foo,3),4)
7

(See the "functional programming" thread).

or you could write:

(def uncurry(f,arg)
(funcall f,arg)
end)

and then:

(uncurry (uncurry foo,3),4)
 
T

Tom Link

foo =3D lambda {|x| lambda {|y| return x+y}}

foo.call(3)(4)

In ruby 1.9, you could also do:
foo.(3).(4)

You also have a curry method:

bar =3D lambda {|x,y| x + y}
bar.curry[3].(4)

-tom
 
W

William James

zslevi said:
foo = lambda {|x|
return lambda {|y| return x+y}}

puts (foo.call(3)).call(4)

It works finally, but it's just too verbose.
Can anyone suggest a shorter, programmer friendlier way of writing
this?

Can you see that "proc" is shorter than "lambda"?

foo = proc{|x| proc{|y| x+y}}
==>#<Proc:0x02824eb4@(irb):20>
foo[3][4]
==>7
 
A

André Thieme

William said:
zslevi said:
foo = lambda {|x|
return lambda {|y| return x+y}}

puts (foo.call(3)).call(4)

It works finally, but it's just too verbose.
Can anyone suggest a shorter, programmer friendlier way of writing
this?

Can you see that "proc" is shorter than "lambda"?

foo = proc{|x| proc{|y| x+y}}
==>#<Proc:0x02824eb4@(irb):20>
foo[3][4]
==>7

Javascript:
3+4


André
--
 
G

Gregory Brown

Can you see that "proc" is shorter than "lambda"?

foo = proc{|x| proc{|y| x+y}}
==>#<Proc:0x02824eb4@(irb):20>
foo[3][4]
==>7

But also not the same thing on Ruby 1.9, so it's important to be
careful about this generally, even if it works fine with either in the
above example.
Gets Printed
=> nil=> "1.9.1"
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,900
Latest member
Nell636132

Latest Threads

Top