paint-brush
Elixir — A slice of Shinjuku in Erlangby@sakibsami
483 reads
483 reads

Elixir — A slice of Shinjuku in Erlang

by Sakib SamiSeptember 10th, 2018
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

<strong>Shinjuku</strong> is a dessert available at <strong>Secret Recipe</strong>, a Malaysian food chain. This is a must try if you ever visit Secret Recipe.
featured image - Elixir — A slice of Shinjuku in Erlang
Sakib Sami HackerNoon profile picture

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 Erlanghello() ->'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 Elixirdef anotherHello() do IO.puts(:erlang_test_cli.another_hello())**end

end**

Test


%%% Compilerebar3 compile


%%% Opening erlang shell with erlang_test project's binaryerl -pa _build/default/lib/erlang_test/ebin


%%% Calling load_elixir() to setup elixir dependencieserlang_test_app:load_elixir().


%%% Calling hello from erlang_test_cli module which calls Elixirerlang_test_cli:hello().



%%% OutputWelcome 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 dependenciesmix deps.get


# Compilemix compile


# Run elixir_test project's binary with elixir shellies -S mix


# Calling Erlang from elixirElixirTest.anotherHello()



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