Forum: Ruby Higher order ruby

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
4a0ddce0e09e6d2faa47365af1c69bfc?d=identicon&s=25 zslevi (Guest)
on 2009-01-14 11:11
(Received via mailing list)
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.
E088bb5c80fd3c4fd02c2020cdacbaf0?d=identicon&s=25 Jesús Gabriel y Galán (Guest)
on 2009-01-14 11:14
(Received via mailing list)
On Wed, Jan 14, 2009 at 11:09 AM, zslevi <zslevi@gmail.com> wrote:
> 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.
F1938de600a3feacd015051492fdaf0f?d=identicon&s=25 LAMBEAU Bernard (Guest)
on 2009-01-14 11:15
(Received via mailing list)
foo.call(3).call(4) should work

blambeau
4a0ddce0e09e6d2faa47365af1c69bfc?d=identicon&s=25 zslevi (Guest)
on 2009-01-14 11:16
(Received via mailing list)
I even tried

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

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

But it doesn't work either.
4a0ddce0e09e6d2faa47365af1c69bfc?d=identicon&s=25 zslevi (Guest)
on 2009-01-14 11:16
(Received via mailing list)
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?
753dcb78b3a3651127665da4bed3c782?d=identicon&s=25 Brian Candler (candlerb)
on 2009-01-14 11:21
zslevi wrote:
> 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]
E088bb5c80fd3c4fd02c2020cdacbaf0?d=identicon&s=25 Jesús Gabriel y Galán (Guest)
on 2009-01-14 11:36
(Received via mailing list)
On Wed, Jan 14, 2009 at 11:14 AM, zslevi <zslevi@gmail.com> wrote:
> 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.
9e2504e0b74e5384af09ce8a660afac4?d=identicon&s=25 Pascal J. Bourguignon (Guest)
on 2009-01-14 12:27
(Received via mailing list)
zslevi <zslevi@gmail.com> writes:

> 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)
9b905791cbdbb1af35b65e02c3217e23?d=identicon&s=25 Tom Link (Guest)
on 2009-01-14 12:30
(Received via mailing list)
On Jan 14, 11:09 am, zslevi <zsl...@gmail.com> wrote:
> foo = 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 = lambda {|x,y| x + y}
  bar.curry[3].(4)

-tom
2ee1a7960cc761a6e92efb5000c0f2c9?d=identicon&s=25 William James (Guest)
on 2009-01-15 11:55
(Received via mailing list)
zslevi wrote:

> 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
9534252117299627ff3e99100f371304?d=identicon&s=25 André Thieme (Guest)
on 2009-01-18 03:45
(Received via mailing list)
William James schrieb:
>
> 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
31e038e4e9330f6c75ccfd1fca8010ee?d=identicon&s=25 Gregory Brown (Guest)
on 2009-01-18 04:09
(Received via mailing list)
On Thu, Jan 15, 2009 at 5:54 AM, William James <w_a_x_man@yahoo.com>
wrote:

> 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.

>> def foo
>>   proc { return }.call
>>   puts "Never gets printed"
>> end
=> nil
>> foo
=> nil
>> def bar
>>   lambda { return }.call
>>   puts "Gets Printed"
>> end
=> nil
>> bar
Gets Printed
=> nil
>> RUBY_VERSION
=> "1.9.1"
753dcb78b3a3651127665da4bed3c782?d=identicon&s=25 Brian Candler (candlerb)
on 2009-01-19 10:23
Gregory Brown wrote:
> 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.

Yep, it's best to avoid proc{} entirely, since in ruby 1.8 it's an alias
for lambda{}, but in ruby 1.9 it has changed to be an alias for
Proc.new{}.

For the full gory details see
http://innig.net/software/ruby/closures-in-ruby.rb
This topic is locked and can not be replied to.