Elixir — A slice of Shinjuku in Erlang

Shinjuku is a dessert available at Secret Recipe, a Malaysian food chain. This is a must try if you ever visit Secret Recipe.

Erlang & Elixir are widely used functional programming language. Elixir runs on Erlang VM. Sometimes it may happen that you want to use Erlang code in Elixir and vice-versa. Here is a detailed guide how you can do that.

For simplicity let’s assume we have two project one is in Erlang and another is in Elixir. We will call a hello function of Elixir from Erlang and another hello function of Erlang from Elixir. Also assume we have two modules ‘Erlang?—?erlang_test_cli’ and ‘Elixir?—?ElixirTest’.

Setup?—?To get started for Erlang we have to setup few things. One is Elixir standard library and another is Elixir binary. Have a look on below codebase. We have added elixir ebin and elixir binary using code:add_path() . Then we have started compiler module and elixir module. Now you are ready to go…

load_elixir() ->

Uses?—?Go through below code. In our Erlang module erlang_test_cli we are calling elixir module 'Elixir.ElixirTest':hello() . So to call elixir the format is 'Elixir.ElixirModuleName':functionName() . Lets check if it works.

-author("sakib").%% API
-export([hello/0, another_hello/0]).%%% Calling Elixir from Erlang
hello() ->
'Elixir.ElixirTest':hello().another_hello() ->
"Hello from Erlang".

Elixir Code,

defmodule ElixirTest dodef hello do
IO.puts("Welcome from Elixir.")
end### Calling Erlang from Elixir
def anotherHello() do



%%% Compile
rebar3 compile
%%% Opening erlang shell with erlang_test project's binary
erl -pa _build/default/lib/erlang_test/ebin
%%% Calling load_elixir() to setup elixir dependencies
%%% Calling hello from erlang_test_cli module which calls Elixir
%%% Output
Welcome from Elixir.

See we have to got result as expected. ?

Now time to check if we can use Erlang library in Elixir.

We have a Elixir project created with Mix. Mix is a Elixir build tool.

# Calling Erlang from elixir

If you want to call Erlang code from Elixir the format is :module_name.function_name() .

Setup?—?This is much more straightforward than Erlang. Just add Erlang project as a dependency in mix.

# Run "mix help deps" to learn about dependencies.
defp deps do
# {:dep_from_hexpm, "~> 0.3.0"},
# {:dep_from_git, git: "https://github.com/elixir-lang/my_dep.git", tag: "0.1.0"},
{:erlang_test, git: "https://github.com/s4kibs4mi/erlang_test.git"}

See we have added our erlang_test project as a dependency.

# Get dependencies
mix deps.get
# Compile
mix compile
# Run elixir_test project's binary with elixir shell
ies -S mix
# Calling Erlang from elixir
# Output
Hello from Erlang

See we have got our result as expected this time as well ?

It doesn’t matter whether you start your code with Erlang or Elixir. You can reuse it always.

Keep Coding & Go functional. ?

Facebook Comments

More Stuff

Meta Skills of a Software Engineer Learning Through Books on Correctness and Pattern Recognition Books on meta; on how to craft software well.Recently, a friend asked a question ab...
You have to choose between Software Delivered on T... Hopefully I caught your attention with the controversial (and admittedly hyperbolic) title. Good. Let me restate it in a slightly more elegant and les...
How I introduced an information radiator to my dis... Remote developerCompanies are increasingly hiring remote engineers to battle the talent shortage. Having been an engineering VP for the past 8 year...
Event Sourcing Using Writer Monad In previous posts (A Writer Monad for F#, How to log in Apache Spark, A Functional Approach), we have discussed the idea of using the Writer monad...
Spread the love

Posted by News Monkey