Can I reference a lambda from within itself using Ruby?

I want to be able to call an anonymous lambda from within itself using Ruby. Consider the following recursive block (returns a factorial). I know I can assign it to a variable, and that variable is within the scope of the lambda:

fac = lambda { |n| n == 1 ? 1 : n * fac.call(n - 1) }
fac.call(5)

But, I want to be able to do the following (for no practical reason as of yet, I'm just interested in exploring the language some more):

(lambda { |n| n == 1 ? 1 : n * self.call(n - 1) }).call(5)

I know that won't work, because self is the main object. Am I doing it wrong? Am I trying to do something that's not possible -- and if not, is this because of some theoretical restriction or is it simply not implemented in Ruby?

In the following example, the lambda is still anonymous, but it has a reference. (Does that pass for anonymous?)

(l = lambda { l.call }).call

(Thanks to Niklas B. for pointing out the error in my original answer; I had only tested it in IRB and it worked there).

This of course ends in a SystemStackError: stack level too deep error, but it demonstrates the purpose.

Is it possible to reference a Proc/lambda within itself? : ruby, x = lambda do self end x.call. I was hoping invoking this Proc would have it return itself, but it looks like the self keyword is getting resolved in the context of the� What is a Lambda? A lambda is a way to define a block & its parameters with some special syntax. You can save this lambda into a variable for later use. The syntax for defining a Ruby lambda looks like this: say_something = -> { puts "This is a lambda" } You can also use the alternative syntax: lambda instead of ->.

It seems that anonymous function really doesn't have any reference. You can check it by callee

lambda{ __callee__ }.call #=> nil

And without reference you can't call this function. I can propose to you only a little more clean variant:

(fac = lambda{ |n| n==1 ? 1 : n*fac.call(n-1) }).call(5)

Ruby Blocks, Procs & Lambdas - The Ultimate Guide!, Ruby blocks are anonymous functions that can be passed into methods. Blocks are enclosed in a do / end statement or between brackets {} , and they can Procs return from the current method, while lambdas return from the lambda itself. They don't carry the actual values, but a reference to them, so if the variables� There are at least three ways to invoke a lambda in Ruby. Perhaps this seems a little odd to you. It did to me at first. And to be honest the l.(arg) syntax still baffles me. But the l[arg] syntax is pretty interesting. It means that to a limited extent you can use lambdas in places where you'd normally use an array or a hash.

fact = -> (x){ x < 2 ? 1 : x*fact.(x-1)}

minimal function

Weird Ruby Part 4: Code Pods (Blocks, Procs, and Lambdas), Weird Ruby Part 4: Code Pods (Blocks, Procs, and Lambdas) function closure) is a function or reference to a function together with a referencing environment – a Blocks can be created a couple different ways in Ruby: The proc created the return in the same context where the proc itself was created,� Using the AWS SDK. Lambda functions are already configured to use the AWS SDK for Ruby, so no gems need to be installed before we can use the library. To reference the SDK, add a require statement to the top of your lambda_function.rb file. The below code shows the require statement at the top of the lambda_function.rb file: require "aws-sdk-s3"

In addition to KL-7's comment, here's a Y combinator solution:

lambda { |f|
  lambda { |x| x.call(x) }.call(
  lambda { |x| f.call( lambda { |v| x.call(x).call(v) } ) } )
}.call(
  lambda { |f|
    lambda { |n| n == 0 ? 1 : n * f.call(n - 1) }
  }
).call(5) #=> 120

You would normally split these:

y = lambda { |f|
  lambda { |x| x.call(x) }.call(
  lambda { |x| f.call( lambda { |v| x.call(x).call(v) } ) } )
}

fac = y.call(
  lambda { |f| lambda { |n| n == 0 ? 1 : n * f.call(n - 1) } }
)

fac.call(5) #=> 120

Note that although fac is being assigned, it is not used within the lambda.

I'd use Ruby's -> syntax and .() instead of .call():

y = ->(f) {
  ->(x) { x.(x) }.(
  ->(x) { f.(->(v) { x.(x).(v) }) } )
}

fac = y.(->(f) {
  ->(n) { n == 0 ? 1 : n * f.(n - 1) }
})

fac.(5) #=> 120

The y invocation can be simplified a bit by using curry:

y = ->(f) {
  ->(x) { x.(x) }.(
  ->(x) { f.curry.(->(v) { x.(x).(v) }) } )
}

fac = y.(
  ->(f, n) { n == 0 ? 1 : n * f.(n - 1) }
)

fac.(5) #=> 120

Method and Proc - Ruby Reference, Unbound methods are a reference to the method at the time it was objectified: In non-lambda procs, return means exit from embracing method (and will Since return and break exits the block itself in lambdas, lambdas cannot be orphaned. If you already have a background in programming, you might have already come across the word lambda. A lambda is also commonly referred to as an anonymous function. To create a lambda in Ruby, you can use the following syntax: lambda = lambda {} Alternatively you can use this syntax: lambda = ->() {} However, if you create a new lambda in IRB

Blocks, Procs and Lambda Types � Sorbet, Troubleshooting � FAQ � Error Reference We may add support for optional or keyword parameters in the future, if there is demand. Note that the yield itself in the method body doesn't need to change at all. Since every Ruby method can only accept one block, both Ruby and Sorbet are able to connect the yield call to the� -> exists since at least 2.0, so it shouldn't be a compatibility issue to use it. From the point of view of a Ruby VM, not having to handle #lambda magically changing the semantics of the passed block would be great, because it's really awkward and hard to check if the block is literal or not (considering lambda can be alias-ed).

Best practices for working with AWS Lambda functions, If your function relies on a mutable state that can't be stored in memory within the code in your Lambda function, wherein the function automatically calls itself� For convenience, you can use the same previously used instance, but this time to compile Ruby for use with Lambda. You can also create a new instance using the same instructions. Setting up Ruby on the instance

Functional programming in Ruby, Short introduction to blocks, procs, lambdas and closures. That doesn't mean we cannot do some functional programming in Ruby. It does not evaluate function arguments unless their values are needed for evaluation of the function itself. In Ruby during closure we remember references to variable not the value of it. You can run Ruby code in AWS Lambda. Lambda provides runtimes for Ruby that execute your code to process events. Your code runs in an environment that includes the AWS SDK for Ruby, with credentials from an AWS Identity and Access Management (IAM) role that you manage. Lambda supports the following Ruby runtimes.

Comments
  • Are you familiar with Y combinator? It might be not the best practical solution, but from theoretical point of view it's very interesting. If you don't, have a look at this article. Be careful, it might blow your brain out.
  • It would really be a lot cleaner to just create a named function for that.
  • Yeah, my original version did wrap the lambda in parentheses and I ran a .call() on there. This isn't really a question of code brevity, though, it's more about just how deep down the functional rabbit hole Ruby can go. KL-7 has a comment above linking to an article describing the Y combinator which is a very interesting read.
  • Almost. fact = -> (x){ x < 2 ? 1 : x*fact[x-1]} has one less non-whitespace character.
  • @pjs Most of the time parentheses are preferred over square brackets. Simply because square brackets mostly denote access to data (array, hash, struct, etc). Using parentheses clearly shows that you're calling a method/proc/lambda. Square bracket usage isn't wrong, but might mislead the unaware code reader.