Lambda function defined inside a module

I was wondering if I define an anonymous function in a module would I be able to access it from outside of the module? As an example consider the following code:

defmodule Lambda do
    lambda = &(&1 * 3)

temp = Lambda.lambda.(10)

when I run this, I’ll get the following error:

** (UndefinedFunctionError) function Lambda.lambda/0 is undefined or private

Am I missing something or it’s because lambda.() is private?



That is because you have it defined at compile-time, not run-time. To be able to access it post-compile you need to put it in a function, so like:

defmodule Lambda do
  def lambda(), do: &(&1 * 3)

This puts it in a function definition, meaning it will exist at run-time instead of at compile-time.

Nope, rather it is because lambda does not exist once the module is compiled since you did not put it into any run-time context. :slight_smile:

1 Like