Named anonymous functions in elixir

I’m curious to know if there is any reason for not allowing named anonymous functions in elixir as it’s possible in erlang. I’ve looked up here and at elixir mailing list and didn’t find discussions on that matter.

Erlang would allow me to do something like that:

fun Sum(T, 0) ->
        T
    Sum(T, N) ->
        Sum(T + N, N - 1)
end

while in elixir, to get something similar, I’d need to pass the function as a parameter.

fn
  _f, s, 0 -> s
  f, s, n -> f.(s + n, n - 1)
end

since it’s something available in Erlang, is there a reason to not allow the same thing in elixir?

1 Like

I’m pretty sure from long ago I remember it being talked about it being for backwards compat issues, specifically that Elixir’s minimum OTP version didn’t support it yet. I wonder if that’s still the case. Anyone happen to know what the minimum OTP version for Elixir is nowadays and what is the minimum OTP version that named anonymous functions are supported?

So, named anonymous functions were available in otp 17 afaik.
And elixir 1.13 supports otp 22 to 24.

1 Like

So it sounds like someone should start the proposal! ^.^

*hint*hint*

3 Likes

I believe there were proposals for that, but all were turned down, as these do not really make sense in Elixir. Erlang added named lambdas for sole purpose of being able to define recursive functions in shell. This is not a problem in Elixir shell, as you can define whole module from there with ease.

However, if you really want, then you can achieve that with some black-magic macros:

defmodule Foo do
  defmacro fn_rec(do: body) do
    {name, argc, body} =
      body
      |> Enum.map(fn
        {:'->', e1, [[{:when, e2, [{name, _, args}, guard]}], body]} ->
          {name, length(args), {:'->', e1, [[{:when, e2, args ++ [guard]}], body]}}

        {:'->', e1, [[{name, _, args}], body]} ->
          {name, length(args), {:'->', e1, [args, body]}}
      end)
      |> Enum.reduce(fn {name, argc, body}, {name, argc, acc} ->
        {name, argc, List.wrap(acc) ++ [body]}
      end)

    args = Macro.generate_arguments(argc, __CALLER__.module)
    func = {:fn, [], body}
    name = {name, [], Elixir}

    quote do
      tmp =
        fn f ->
          var!(unquote(name)) = fn unquote_splicing(args) -> f.(f).(unquote_splicing(args)) end
          unquote(func)
        end

      tmp.(tmp)
    end
  end

  def run do
    ack =
      fn_rec do
        a(0, n) -> n + 1
        a(m, 0) -> a.(m - 1, 1)
        a(m, n) -> a.(m - 1, a.(m, n - 1))
      end

    ack.(3, 4)
  end
end

IO.puts Foo.run
5 Likes

Do you remember if the discussions were in this forum or in the mailing list?
I’d like to go understand the reasons why it was rejected.

I did a little experimental back then. A bit hacky but somehow works.