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

Simple Recipe to Build a Theme App in React Native Recently, I have been working on a React Native application that demands a changeable theme based on the user selection. I wanted to do this in a simp...
First do no harm: 30 Principles that helped me avoid Fly-by Archit... Imagine a fly-by architecture review. An architect walks in, looks over, glosses over, though his binoculars. He provide comments that are often too g...
Stop measuring R&D planning VS execution. Start measuring team... Most of you know me as a product person, but in the past year-and-a-half, I’ve been leading our R&D team @Bizzabo. Since taking the lead, I’ve sea...
5 Programming Languages Every Master Developer Should Learn I have read somewhere that a programmer should learn a new programming language every year ( I think it’s code complete, not sure, though), but if yo...
Spread the love

Posted by News Monkey