Can anonymous function be called inside of iex

Hello, I am new to elixir and I am trying to call my anonymous function defined in my “ex” file like so:

package_data = fn
  (x, [y]) -> [x, y]
  (x, 5) -> x + 20
  (x, y) -> {x, y}
end

When I compile this file using “iex -S mix” and try calling “package_data.(5, 8)” I get “undefined variable “package_data””. Is this just how elixir behaves when using variables in iex, or am I doing something wrong?

That’s expected behaviour. When you compile files the code in them will be executed, but only modules will stick around to be used from other contexts.

3 Likes