Higher order ruby

Discussion in 'Ruby' started by zslevi, Jan 14, 2009.

  1. zslevi

    zslevi Guest

    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.
    zslevi, Jan 14, 2009
    #1
    1. Advertising

  2. zslevi

    zslevi Guest

    I even tried

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

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

    But it doesn't work either.
    zslevi, Jan 14, 2009
    #2
    1. Advertising

  3. On Wed, Jan 14, 2009 at 11:09 AM, zslevi <> 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.
    Jesús Gabriel y Galán, Jan 14, 2009
    #3
  4. foo.call(3).call(4) should work

    blambeau

    On Wed, Jan 14, 2009 at 11:09 AM, zslevi <> 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.
    >
    >
    LAMBEAU Bernard, Jan 14, 2009
    #4
  5. zslevi

    zslevi Guest

    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?
    zslevi, Jan 14, 2009
    #5
  6. 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]
    --
    Posted via http://www.ruby-forum.com/.
    Brian Candler, Jan 14, 2009
    #6
  7. On Wed, Jan 14, 2009 at 11:14 AM, 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?


    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.
    Jesús Gabriel y Galán, Jan 14, 2009
    #7
  8. zslevi <> 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)

    --
    __Pascal Bourguignon__
    Pascal J. Bourguignon, Jan 14, 2009
    #8
  9. zslevi

    Tom Link Guest

    On Jan 14, 11:09=A0am, zslevi <> wrote:
    > 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
    Tom Link, Jan 14, 2009
    #9
  10. 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
    William James, Jan 15, 2009
    #10
  11. William James schrieb:
    > 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


    Javascript:
    3+4


    André
    --
    André Thieme, Jan 18, 2009
    #11
  12. On Thu, Jan 15, 2009 at 5:54 AM, William James <> 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"



    --
    Technical Blaag at: http://blog.majesticseacreature.com
    Non-tech stuff at: http://metametta.blogspot.com
    "Ruby Best Practices" Book now in O'Reilly Roughcuts:
    http://rubybestpractices.com
    Gregory Brown, Jan 18, 2009
    #12
  13. 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
    --
    Posted via http://www.ruby-forum.com/.
    Brian Candler, Jan 19, 2009
    #13
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jonathan Bartlett

    Higher-Order Programming in C

    Jonathan Bartlett, Mar 31, 2005, in forum: C Programming
    Replies:
    4
    Views:
    287
    Jonathan Bartlett
    Apr 4, 2005
  2. Mark Fink
    Replies:
    3
    Views:
    267
    Arnaud Delobelle
    Dec 22, 2010
  3. Nickolay Kolev

    Higher Order Functions

    Nickolay Kolev, Jul 31, 2005, in forum: Ruby
    Replies:
    5
    Views:
    129
    Nickolay Kolev
    Aug 8, 2005
  4. Victor \Zverok\ Shepelev

    Higher-order messaging in Ruby

    Victor \Zverok\ Shepelev, Oct 11, 2006, in forum: Ruby
    Replies:
    0
    Views:
    106
    Victor \Zverok\ Shepelev
    Oct 11, 2006
  5. Christoffer Lernö
    Replies:
    17
    Views:
    243
    Robert Dober
    Mar 16, 2007
Loading...

Share This Page