Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Elixir - Not Just For Rubyists

Elixir - Not Just For Rubyists

An introduction to the Elixir language presented at the Belfast Elixir Group.

Maurice Kelly

February 02, 2017
Tweet

More Decks by Maurice Kelly

Other Decks in Programming

Transcript

  1. INTRO WHAT ARE WE HERE FOR? ▸ An overview of

    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
  2. INTRO WHO THE F**K IS THIS GUY? ▸ Maurice Kelly

    ▸ @mo in the NI Tech Slack ▸ Professional programmer since 2001 ▸ Dabbled in a few languages BASIC
 |> C
 |> PHP
 |> C++
 |> Java
 |> Objective-C
 |> Swift
 |> Elixir
  3. INTRO WHAT DID RUBYISTS EVER DO TO YOU? ▸ 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!
  4. INTRO WHAT DID RUBYISTS EVER DO TO YOU? ▸ 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!
  5. INTRO WHAT DID RUBYISTS EVER DO TO YOU? ▸ 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!
  6. INTRO WHAT DID RUBYISTS EVER DO TO YOU? ▸ 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!
  7. WAFFLE FUNCTIONS ▸ Functions are the building block for Elixir

    code int sumTwoNumbers( int a, int b ) { // Java
 return a + b;
 } def sum_two_numbers( a, b ) do # Elixir
 a + b
 end
  8. WAFFLE FUNCTIONS ▸ Functions can also contain conditions ▸ In

    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
  9. WAFFLE MODULES ▸ Elixir groups functionality by modules defmodule SimpleMath

    do
 def sum_two_numbers( a, b ) do
 a + b
 end
 end
  10. WAFFLE MODULES ▸ Modules can be nested defmodule SimpleMath do


    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
  11. WAFFLE IMMUTABILITY ▸ Everything is immutable var array = [“One”,

    “Two”, “Three”] // Swift
 array = array + “Four”
 print(array) // [“One”, “Two”, “Three”, “Four”] array = ["One", "Two", “Three"] # Elixir
 array ++ ["Four"]
 array # ["One", "Two", “Three"] array = array ++ [“Four”] # Elixir
 array # [“One”, “Two”, “Three”, “Four”]
  12. WAFFLE PATTERN MATCHING ▸ Based on the match operator —

    = {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
  13. WAFFLE PATTERN MATCHING - CASE STATEMENTS ▸ if statements are

    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
  14. WAFFLE PATTERN MATCHING - FUNCTIONS def sum_two_numbers( a, b )

    when a == b do
 a * 2
 end def sum_two_numbers( a, b ) do
 a + b
 end ‣ Can also match specific values: def sum_two_numbers( a, 0 ) do
 a
 end
  15. WAFFLE PIPELINE OPERATOR ▸ An operator to take lifeless code

    and make it sleeker, shinier, and more free flowing. initial_string = “HELLO WORLD!” # Ruby
 first_step = lowercase(initial_string)
 second_step = titlecase(first_step)
 result = trim(second_step, “!”) initial_string = “HELLO WORLD!” # Elixir
 first_step = String.downcase(initial_string)
 second_step = String.capitalize(first_step)
 result = String.trim_trailing(second_step, "!")
  16. WAFFLE PIPELINE OPERATOR ▸ An operator to take lifeless code

    and make it sleeker, shinier, and more free flowing. initial_string = “HELLO WORLD!” # Ruby
 first_step = lowercase(initial_string)
 second_step = titlecase(first_step)
 result = trim(second_step, “!”) result = “HELLO WORLD!” # Elixir
 |> String.downcase
 |> String.capitalize
 |> String.trim_trailing(“!”)
  17. WAFFLE STATEFUL PROCESSES ▸ The only state in Elixir is

    that which you pass around ▸ Erlang processes can act as containers for state ▸ Messages can be used to “modify” the state within a process
  18. WAFFLE STATEFUL PROCESSES ▸ In this example we try to

    process an :increment message receive do
 :increment ->
 new_state = state + 1
 end ▸ This only processes one message at a time
  19. WAFFLE STATEFUL PROCESSES ▸ In this example we try to

    process an :increment message def loop() do
 receive do
 :increment ->
 new_state = state + 1
 loop()
 end
 end ▸ The state doesn’t go anywhere
  20. WAFFLE STATEFUL PROCESSES ▸ In this example we try to

    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
  21. WAFFLE RECURSION ▸ Elixir dispenses with while and for loops

    ▸ 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
  22. WAFFLE RECURSION ▸ As does tail call optimisation def recursive_function(value)

    do
 ... recursive_function(new_value)
 end ‣ The last expression in the body must be a recursive call
  23. ALTERNATIVE FACTS HOW DO I GET STARTED WITH ELIXIR? ▸

    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
  24. ALTERNATIVE FACTS HOW DO I BUILD ELIXIR APPLICATIONS? ▸ Scripts

    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
  25. ALTERNATIVE FACTS HOW DO I BUILD ELIXIR APPLICATIONS? ▸ Scripts

    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
  26. ALTERNATIVE FACTS DOES IT HAVE A REPL? ▸ The Elixir

    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
  27. ALTERNATIVE FACTS HOW DO I RUN MY SCRIPTS ▸ Scripts

    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
  28. ALTERNATIVE FACTS HOW DO I RUN MY COMMAND-LINE TOOL? ▸

    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
  29. ALTERNATIVE FACTS HOW DO I RUN MY SERVER? ▸ Long-lived

    processes need to define an application callback function: mod: {ApplicationEntryPoint, []} ▸ This executes the start/2 function in the ApplicationEntryPoint module ▸ To launch: $ mix run
  30. ALTERNATIVE FACTS WHAT ABOUT TESTING? ▸ ExUnit defmodule ModuleTest do


    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
  31. ALTERNATIVE FACTS WHAT ABOUT TESTING? describe “when the app is

    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
  32. ALTERNATIVE FACTS SO HOW DO I MANAGE DEPENDENCIES? ▸ Hex

    (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
  33. ALTERNATIVE FACTS SO HOW DO I MANAGE DEPENDENCIES? ▸ Hex

    (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
  34. HAPPY FINISH WRAPPING UP ▸ Elixir is a modern, concurrent

    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.
  35. HAPPY FINISH LINKS AND RESOURCES ▸ elixir-lang.org — main project

    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