[Elixir][Erlang]run functions like yield

These days, I challenge exercism.io to get used some languages. Especially, I use Erlang and Swift.

Then, I found some difference between Erlang and Elixir in aspect of syntax. So, I describe them as articles to remember them.

This time, I attached gist which is run function defined in variables.

Elixir run them with func.(), but Erlang tun them with Func().. We can give variables via func.(some_variables), Elixir, and Func(Some_variables)., Erang.

run functions defined in variables

Elixir

iex> f = fn -> IO.puts("hello") end
#Function<20.54118792/0 in :erl_eval.expr/5>
iex> f.()
hello
:ok
iex> f = fn x -> IO.puts("hello #{x}") end
#Function<6.54118792/1 in :erl_eval.expr/5>
iex> f.("world")
hello world
:ok

Erlang

> Fnc = fun() -> io:format("hello~n") end.
#Fun<erl_eval.20.54118792>
> Fnc().
hello
ok
> Fncx = fun(X) -> io:format("hello ~s~n", [X]) end.
#Fun<erl_eval.6.54118792>
> Fncx("world").
hello world
ok

These are like yield .

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.