dubesoftware

dubesoftware

Can anonymous functions be imported into other modules?

Hi all,

I’m learning Elixir/OTP and am stumped by a certain behaviour. I’m declaring an anonymous function in a module, then when I attempt to call that function from another module, I get an UndefinedFunctionError error. I can alias the anonymous function’s module, but cannot even import the function using the syntax import Module, only: [anon_func]. Please see the code below. What gives?

Module that defines anonymous function:
defmodule Servy.PowerNapper do power_nap = fn -> time = :rand.uniform(10_000) :timer.sleep(time) time end end

and the caller module
`
defmodule Servy.PowerNapperClient do
alias Servy.PowerNapper

parent = self()
spawn(fn → send(parent, {:slept, PowerNapper.power_nap.()}) end)

receive do
{:slept, time} → IO.puts “Slept #{time} ms”
end
end
`

Most Liked

al2o3cr

al2o3cr

Nope, that’s not what that does. power_nap is a local variable inside of the do block but it doesn’t stick around after the module is compiled and it isn’t visible outside.

Also, an linguistic nitpick: an “anonymous function” would be a function without a name - but the intent of the code you posted is to name the function PowerNapper.power_nap!

What specifically were you hoping to get with this approach, compared to doing a standard def power_nap do in Servy.PowerNapper?


Related:

defmodule Servy.PowerNapperClient do
  alias Servy.PowerNapper

  parent = self()
  spawn(fn -> send(parent, {:slept, PowerNapper.power_nap.()}) end)

self() here will be the Elixir compiler! The receive will happen while PowerNapperClient is being compiled.

cjbottaro

cjbottaro

My guy… triple backticks…

The only things “exported” out of a module are public functions, i.e. anything defined by def.

* Public types too
** Public submodules also
*** Now I’m questioning all of reality… what else??

Where Next?

Popular in Questions Top

bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 record...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
Jim
As a follow up to my earlier question: I have the code compiling and running but not getting a successful login from the rest server. ...
New
dokuzbir
Hello, I am trying to convert my lists to string without losing brackets.For start i have 3 map. They look like these buyer = %{ id: ...
New
polypush135
As many of you may have realized by now (sorry for all the posts here) I’ve been working on a db problem where I’m trying to aggregate a ...
New
johnnyicon
Hi all, I've just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I'm trying to use Postg...
New
chewm
Hi guys, nice to meet you to the whole forum, I’m new here, I’m trying to configure visual studio code for elixir, right now the intellis...
New
Mooodi
Given a string, how can I get access to its character by index? Enum.at("my_string", 2) doesn't work. Or rather, not char, but a substr...
New

Other popular topics Top

rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list....
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod -- where is this set? Thanks.
New
Tee
can someone please explain to me how Enum.reduce works with maps
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
yawaramin
In the Dialyzer docs ( http://erlang.org/doc/man/dialyzer.html#requesting-or-suppressing-warnings-in-source-files ), there is a way to tu...
New
AstonJ
You’re a programmer, so you don’t need spoon feeding with the conventional drivel about “this is an integer.” No. You need to know what’s...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
lucidguppy
I have a super simple question about elixir - how would I take a file like this foo bar baz and output a new file that enumerates th...
New

We're in Beta

About us Mission Statement