Elixir — A slice of Shinjuku in Erlang

Forex stock trading

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() ->
code:add_path("/usr/local/Cellar/elixir/1.7.2/lib/elixir/ebin/"),
code:add_path("/Users/sakib/ElixirProjects/elixir_test/_build/dev/lib/elixir_test/ebin"),
application:start(compiler),
application:start(elixir),
ok.

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.

-module(erlang_test_cli).
-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 do

def hello do
IO.puts("Welcome from Elixir.")
end

### Calling Erlang from Elixir
def anotherHello() do
IO.puts(:erlang_test_cli.another_hello())
end

end

Test

%%% 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
erlang_test_app:load_elixir().
%%% Calling hello from erlang_test_cli module which calls Elixir
erlang_test_cli:hello().
%%% Output
Welcome from Elixir.
ok

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
:erlang_test_cli.another_hello()

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"}
]
end

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
ElixirTest.anotherHello()
# Output
Hello from Erlang
:ok

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. ❤


Elixir — A slice of Shinjuku in Erlang was originally published in Hacker Noon on Medium, where people are continuing the conversation by highlighting and responding to this story.

Read about withdrawable no deposit bonus and make profit trading now