lambda callee

  • Thread starter Marcin MielżyÅ„ski
  • Start date
M

Marcin Mielżyński

Hi,

I wonder if something like this can be done without giving lambda a name:

(l=lambda{|a|a.zero? ? 1: a*l[a-1]})[10]

something similar like javascript arguments.callee comes to my mind..

lopex
 
P

Pierre Barbier de Reuille

Jeffrey Schwab a écrit :
Marcin said:
Hi,

I wonder if something like this can be done without giving lambda a name:

(l=lambda{|a|a.zero? ? 1: a*l[a-1]})[10]

something similar like javascript arguments.callee comes to my mind..

lopex


Skip the recursion. Instead of storing each successive value on the
call stack, store it using inject's accumulator.

(lambda {|a| (1..a).inject {|p,n| p * n } })[10]

Btw, for the record, the variable name l is horrid to begin with, and
interspersing it with the number 1 in a one-liner might make you go
cross-eyed. :)

Another solution (because the inject method is not *always* possible to
use) is the Y operator, which is very simply defined in Ruby :

def y(&f)
lambda { |*args| f.call f,*args }
end

And used with :

y { |f,a| a.zero? ? 1 : a*f[f,a-1]}[10]

As you can see, the block of the Y operator will get itself as first
argument.

Pierre
 

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,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top