- Install using this guide: https://elixir-lang.org/getting-started/introduction.html#installation
- Verify if the installation is done:
$elixir --version
- Run interactive shell:
$iex
- Execute a source file:
$elixir abc.exs
mix
is a the Elixir build tool:- To create a new project from scaffolding:
$ mix new {folder_name} --module {module_name}
- To compile a project, go into the project folder and:
$ mix compile
- Once project has been compiled, you can start a session inside:
$ iex -S mix
- You can do live debugging here, e.g.
KV.hello
- You can do live debugging here, e.g.
- To run test, go into the project folder and:
$ mix test
$ mix help
to have more informationmix deps.get
to install all dependencies (Ruby'sbundle install
)
- To create a new project from scaffolding:
- Get help in
iex
:h <module_name>
to get help, e.g.h Map
h <func>
to get information about the function, e.g.h Map.keys/1
i variable_name
to get information about that variable
IO.puts "Hello world"
to print onto consoleProcess.sleep(10)
to put the process to sleep for 10 millisecpid = spawn(ModuleName, :method, [])
to spawn a new processsend pid, message
to sendmessage
to the processflush
to flush all message to shell:observer.start
to start the ErlangVM' observer appIEx.Helpers.recompile
to reload code from insideiex
-
iex --sname quynh
to start a new shell with (short) name =quynh
-
iex --name [email protected]
to start a new shell with a long name -
From the shell, i.e.
iex
node
to get the name of node name of the VMNode.list
to get the list nodes that connect to the current oneNode.connect <name>
to connect the current shell to another node
-
:rpc.multical/3
to execute code in the cluster -
mix escript.build
to generate the command line -
- Start the service:
pg_ctl -D /usr/local/var/postgres start
- Stop the service:
pg_ctl -D /usr/local/var/postgres stop
- Version:
postgres -V
- Command line:
psql postgres
\du
: see what's installed on the machine\l
: list of databases\d
: list of tables
Process.info(self, :links)
to find all processes linked to the current oneProcess.link(pid)
to link the current process to thepid
oneProcess.alive?(pid)
whether a process is still aliveProcess.flag(:trap_exit, true)
to trap the exit signals from the current process
Ring of linked process:
iex -S mix
: Get into interactive consolepids = Ring.create_processes(10)
: Create 10 processesRing.link_processes(pids)
: Create a ring of linked processespids |> Enum.map(fn pid -> "#{inspect pid}: #{inspect Process.info(pid, :links)}" end)
: Inspec the ringpids |> Enum.shuffle |> List.first |> send(:crash)
: Crash a random process in the ringpids |> Enum.map(fn pid -> Process.alive?(pid) end)
: Check whether any process is still alive
Trapping exits:
Process.flag(:trap_exit, true)
: Trap the exit signals from the current processpid = spawn(fn -> receive do :crash -> 1/0 end end)
: Create a process that will crash on signalProcess.link(pid)
: Link two processessend(pid, :crash)
: Crash the target processflush
: To see the crash message the current process receive- Note that the current process still survice after the crash
3 ways for a process to terminate:
-
Naturally, no more code to run
pid = spawn(fn -> receive do :ok -> :ok end end)
: a process to wait for only one messageProcess.link(pid)
send(pid, :ok)
: execute the codeflush
: to see the message on the shell
-
Forcefully terminated
Process.flag(:trap_exit, true)
: trap the exit signalsProcess.exit(self, :whoops)
: raise exit signalflush
: to see the exit signal was trappedProcess.exit(self, **:kill**)
: send an untrappable signal, the shell will be crashed
-
Exception on a process, per the previous example
Elixir support the following data types:
- Value types
- Integer
- Float
- Atom
- Range
- Regex
- System types
- PID and port
- Reference
- Collection types
- Tuple
- List
- Maps
- Binaries
Utilities:
i
command iniex
: Print out information about a variable
Blitzy
command-line programRegistry
for message dispatching & pub/sub
- Install the latest version of Phoenix:
mix archive.install https://github.com/phoenixframework/archives/raw/master/phx_new.ez
mix phx.new
to create a new project, e.g.mix phx.new conduit --module Conduit --app conduit --no-brunch --no-html
mix phx.server
to run the Phoenix webserver, default URL:http://localhost:4000/
mix phx.routes
to show the defined routes in the application
##Dev Environment
- Erlang version:
erl -eval 'erlang:display(erlang:system_info(otp_release)), halt().' -noshell
- Elixir version:
elixir -v
asdf
for version manager:-
-