the Elixir language and the Erlang VM ▸ A tour of some of the most interesting features of the language ▸ A diversion into the Elixir development environment
▸ @mo in the NI Tech Slack ▸ Professional programmer since 2001 ▸ Dabbled in a few languages BASIC |> C |> PHP |> C++ |> Java |> Objective-C |> Swift |> Elixir
has a Ruby heritage ▸ Elixir has a distinctly Ruby style def, do, end, puts, and lots of :atoms ▸ Elixir is popular with Rubyists for experimentation ▸ But it’s not just for Rubyists!
has a Ruby heritage ▸ Elixir has a distinctly Ruby style def, do, end, puts, and lots of :atoms ▸ Elixir is popular with Rubyists for experimentation ▸ But it’s not just for Rubyists!
has a Ruby heritage ▸ Elixir has a distinctly Ruby style def, do, end, puts, and lots of :atoms ▸ Elixir is popular with Rubyists for experimentation ▸ But it’s not just for Rubyists!
has a Ruby heritage ▸ Elixir has a distinctly Ruby style def, do, end, puts, and lots of :atoms ▸ Elixir is popular with Rubyists for experimentation ▸ But it’s not just for Rubyists!
this example we want a special case where we are summing two number that are equal def sum_two_numbers( a, b ) when a == b do a * 2 end def sum_two_numbers( a, b ) do a + b end
defmodule Summing do end end ‣ This doesn’t imply a special relationship - it is purely visual defmodule SimpleMath do end defmodule SimpleMath.Summing do end
= {a, b, c} = {1, 2, 3} ▸ Results in a = 1, b = 2 and c = 3 ▸ Pattern matching is used throughout Elixir ▸ Particularly important for ▸ function selection ▸ case statements ▸ message passing and more
available, but not used extensively ▸ Elixir code relies more on case statements if (a == 1) { // Swift return true } else if (a == 2 && b > 1) { return true } else { return false } case {a, b} do # Elixir {1, _} -> true {2, x} when x > 2 -> true _ -> false end
process an :increment message def loop(state) do receive do :increment -> new_state = state + 1 loop(new_state) end end ▸ The updated state is ready for the start of each message received
▸ Conditional functions help def recursive_function(value) when value is_map() do end def recursive_function(value) when value is_list() do end def recursive_function(value) do end
Mac - brew install elixir ▸ Linux - yum install elixir or apt-get install elixir ▸ Windows - an official installer exists — https://repo.hex.pm/elixir-websetup.exe ▸ Source ▸ git clone https://github.com/elixir-lang/elixir.git ▸ cd elixir ▸ make clean test
can be run directly $ elixir your_script.exs ▸ The Mix tool is the best way to manage bigger applications and can: ▸ build and test ▸ manage dependencies ▸ create new projects or resources within projects
can be run directly $ elixir your_script.exs ▸ The Mix tool is the best way to manage bigger applications and can: ▸ build and test ▸ manage dependencies ▸ create new projects or resources within projects
REPL is called Iex (eye-ee-ex, not Lex) $ iex Erlang/OTP 19 [erts-8.2] [source] [64-bit] [smp:4:4] [async-threads:10] [hipe] [kernel-poll:false] [dtrace] Interactive Elixir (1.4.0) - press Ctrl+C to exit (type h() ENTER for help) iex(1)> ▸ You can load your project directly into Iex: $ iex -S mix
don’t need to be precompiled $ elixir your_script.exs ▸ This does the following: ▸ Compiles the script into bytecode in memory ▸ Starts the BEAM with the bytecode ▸ Cleans everything up when the script is done
Non-trivial command line tools can be bundled as an escript ▸ Requires a change to mix.exs to include this configuration: escript: [main_module: EntryPointModuleName] ▸ On launch the function main/1 in that module will receive the arguments ▸ Build using mix escript.build ‣ https://hexdocs.pm/mix/master/Mix.Tasks.Escript.Build.html
processes need to define an application callback function: mod: {ApplicationEntryPoint, []} ▸ This executes the start/2 function in the ApplicationEntryPoint module ▸ To launch: $ mix run
use ExUnit.Case # brings in test functionality doctest Module # module should be doc-tested test "the truth" do # the start of a test assert 1 + 1 == 2 # a test assertion end end ‣ Doctests
doing something” do test “it does this” do end test “it does that too” do end end ‣ Tests should be named as .exs and are run using mix test ‣ Doctests
(https://hex.pm) defp deps do [{:cowboy, "~> 1.0.3"}, {:plug, "~> 1.0”}] end ▸ Hex is pulled in and invoked by Mix mix deps.get ▸ Documentation available at https://hexdocs.pm
(https://hex.pm) defp deps do [{:cowboy, "~> 1.0.3"}, {:plug, "~> 1.0”}] end ▸ Hex is pulled in and invoked by Mix mix deps.get ▸ Documentation available at https://hexdocs.pm
and functional programming language that compiles down to bytecode suitable for execution on the Erlang VM. ▸ It relies on the many years of development put into the Erlang VM to provide a scalable, distributed and fault-tolerable execution environment. ▸ As a developer, I really enjoying working with it. ▸ I think you might too.
site ▸ elixir-slackin.herokuapp.com — Community Slack ▸ nitech.herokuapp.com — NI Tech Slack - #elixir ▸ www.belfastelixir.org — the site for this meetup ▸ elixirsips.com — lots of training material ▸ @mauricerkelly — @belfastelixir ▸ manning.com/books/the-little-elixir-and-otp-guidebook — Little Elixir & OTP Guidebook