cevado

cevado

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?

Most Liked

hauleth

hauleth

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
OvermindDL1

OvermindDL1

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

*hint*hint*

cevado

cevado

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

Where Next?

Popular in Questions Top

senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
New
Werner
Hi, I’m using Ubuntu 18.04 and after updating to OTP-24.0 yesterday i have this warning when I run “mix local.hex”: 14:57:30.512 [warn] ...
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
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
script
If I have a string “1000 cfu/ml” . I want to remove the characters and / and space . So the string is like this "1000" What is the ...
New
vac
Hi, I'm quite new in Elixir and I'm trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and ...
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
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New

Other popular topics Top

srinivasu
How to handle excepions in elixir? Suppose i have A, B, C ,D, E modules. and each module has get() function. A.get() method will call th...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 35421 110
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
chensan
I have a User schema with a :from_id field set to type :string: defmodule TweetBot.Repo.Migrations.CreateUsers do use Ecto.Migration ...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
beno
I will often find my self writing things similar to: case some_value do nil -> something() "" -> something() _ -> someth...
New
magnetic
Hey :wave:t3: Elixir community, I’ve been learning Elixir, and working on some side projects. My editor of choice is VSCode, and althoug...
New

We're in Beta

About us Mission Statement