Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Higher order ruby

Reply
Thread Tools

Higher order ruby

 
 
André Thieme
Guest
Posts: n/a
 
      01-18-2009
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é
--
 
Reply With Quote
 
 
 
 
Gregory Brown
Guest
Posts: n/a
 
      01-18-2009
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

 
Reply With Quote
 
 
 
 
Brian Candler
Guest
Posts: n/a
 
      01-19-2009
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/.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Question regarding Higher-Order-Programming in Python Mark Fink Python 3 12-22-2010 09:45 PM
Anyone playing with higher order messaging in ruby? Christoffer Lernö Ruby 17 03-16-2007 11:06 PM
Higher-order messaging in Ruby Victor \Zverok\ Shepelev Ruby 0 10-11-2006 05:37 PM
Accessing higher security level from higher security level nderose@gmail.com Cisco 0 07-11-2005 10:20 PM
Higher-Order Programming in C Jonathan Bartlett C Programming 4 04-04-2005 01:40 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57