Elixir is a functional, concurrent, general-purpose programming language that runs on the Erlang virtual machine (BEAM).
Elixir also provides a productive tooling (Mix
build tool, interactive shell)and an extensible design (scalability, fault-tolerance).
Instead of thinking of object, you think of little easy to test process that you spawn and kill when needed.
Go to elixir-lang.org and install elixir following the instruction, on MacOS with brew:
brew install elixir
Elixir has an interactive prompt called iex
that you can try on the commad line. To quit the iex
use [command] + [c]
then [a]
.
To run a script try with:
elixir hello.exs
To compile your module you should use elixirc
such as
elixirc hello.exs
Mix
is a development tool for Elixir that help kick start an Elixir project:
mix new demo
Before running a project you might want to update all dependency using:
mix deps.get
And a project will be generated. To test your mix project try using:
mix test
However is you want to use your project functions in the iex
, go in your root folder and run:
# The -S is for source
iex -S mix
It is adviced to follow the Mix structure in order to organise your files, ie:
- The package script should be in
lib
- The test files of the scripts in
test
- Configuration settings in
config
For the other folder, usually they are populated dynamically:
deps
is for installed dependencies_build
is the compiled sources of your projectdoc
is for documentation that can be generated with ExDoc
mix docs
The package can be installed
by adding the package name to your list of dependencies in mix.exs
:
def deps do
[
{:demo, "~> 0.1.0"}
]
end
Package can be either download from repository or directly fron Hex
Mix projects are hosted on Hex. To get started, you will need to install hex
using:
mix local.hex
Then you will need an account:
mix hex.user register
Then follow the procedure to add the correct metadata to your mix.exs
so that you can finally publish it:
mix hex.publish
There are no multiple comments line. A comments starts like in python with #
# Will print Hello world
IO.puts "Hello world"
What is the difference between .ex
and .exs
:
- The
ex
is compiled to the erlang.beam
code - The
.exs
is more of a script that is compiled just-in-time.