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

Introduction to Elixir

Introduction to Elixir

Lightning talk given at work.

Mitchell Hanberg

June 01, 2018
Tweet

More Decks by Mitchell Hanberg

Other Decks in Programming

Transcript

  1. ELIXIR ABOUT ME ▸ I am on the Agronauts (John

    Deere) team on the 4th floor. ▸ Graduated from Purdue University in May 2016. ▸ Started at SEP in June 2016. ▸ GitHub: @mhanberg
  2. ELIXIR WHAT IS ELIXIR? ▸ Influenced by Ruby, Erlang, and

    Clojure ▸ A dynamic, functional, and compiled language designed for building scalable and maintainable applications. ▸ Leverages the Erlang VM, known for running low-latency, distributed and fault-tolerant systems, while also being successfully used in web development and the embedded software domain.
  3. ELIXIR WHAT IS ERLANG? ▸ Developed at Ericsson in 1986

    for use in their telephone network/platform. ▸ Runs on a virtual machine called the BEAM (or Erlang VM). ▸ Functional, dynamic, used for being distributed, fault-tolerant systems. ▸ Whatsapp (bought by Facebook for $19B) is built on Erlang.
  4. times2 = fn(num) -> num * 2 end times2.(3) #

    6 Enum.map([1, 2, 3], fn(num) -> num * 2 end) # [ 2, 4, 6 ] Enum.map([1, 2, 3], times2) # [ 2, 4, 6 ] ELIXIR FIRST CLASS FUNCTIONS
  5. ELIXIR PATTERN MATCHING, FUNCTION DEFINITIONS def small_talk(:swanson, topic) do IO.puts

    "How's your #{topic} doing?" end def small_talk(:fuller, _t) do IO.puts "Mr. Director, how goes the Directing?" end small_talk(:swanson, "Fantasy Canadian Football") # How's your Fantasy Canadian Football doing? small_talk(:fuller, "Doesn't matter what I put here") # Mr. Director, how goes the Directing?
  6. ELIXIR PATTERN MATCHING, VALUE DECONSTRUCTION status = {:ok, "All good

    here!"} {:ok, msg} = status msg # "All good here!" {:error, msg} = status # ** (MatchError) no match of right hand side value: {:ok, "All good there!"} lunch_spots = ["Cancun", "Bub's", "Chipotle"] [first | the_rest] = lunch_spots first # "Cancun" the_rest # ["Bub's", "Chipotle"] The match operator "="
  7. ELIXIR IMMUTABILITY num = 10 num = 11 # Reassigning

    is valid dict = %{SEP: "A Software Product Design & Development Company"} dict[:SEP] = "Sucks!!" # Compilation error! dict = Map.put(dict, :Mitch, "All Star Developer") # %{ # SEP: "A Software Product Design & Development Company", # Mitch: "All Star Developer" # }
  8. ELIXIR RECURSION ▸ Looping is done using recursion. ▸ The

    BEAM utilizes Tail Call Recursion. ▸ The Enum module abstracts away enumerating over lists, with functions like Enum.map and Enum.reduce. ▸ Pattern Matching makes recursion super easy to grok.
  9. ELIXIR RECURSION def print_n_times(0, msg) do IO.puts msg end def

    print_n_times(n, msg) do IO.puts msg print_n_times(n -1, msg) end
  10. ELIXIR CONCURRENCY ▸ Everything inside the BEAM runs in a

    "thread". ▸ Able to handle millions of threads running simultaneously. ▸ Utilizes the Erlang framework, OTP (Open Telecom Platform) ▸ Standard library comes with an abstraction called a "GenServer"
  11. ELIXIR GENSERVER ▸ "General Server" ▸ Client - Server concept,

    processes send messages to each other and respond appropriately. ▸ Convenient way to encapsulate state. ▸ Similar to the "Actor Pattern", which some think of as "True Object Oriented" programming.
  12. ELIXIR GENSERVER defmodule Stack do use GenServer # Callbacks def

    handle_call(:pop, _from, [h | t]) do {:reply, h, t} end def handle_cast({:push, item}, state) do {:noreply, [item | state]} end end # Start the server {:ok, pid} = GenServer.start_link(Stack, [:hello]) # This is the client GenServer.call(pid, :pop) #=> :hello GenServer.cast(pid, {:push, :world}) #=> :ok GenServer.call(pid, :pop) #=> :world
  13. ELIXIR ECOSYSTEM ▸ Rich ecosystem with many aspects built right

    into the core of the language. ▸ Mix - Build tool ▸ ExUnit - Unit Testing Framework ▸ ExDoc - Documentation Generation Framework ▸ Hex - Package Manager for both Erlang and Elixir
  14. ELIXIR INTEROPERABILITY WITH ERLANG ▸ All standard Erlang libraries are

    available ▸ Some concepts aren’t available in the Elixir standard library because they are already in Erlang. ▸ Erlang modules are represented by lowercase atoms, such as :math or :timer. ▸ Gives access to Erlang Term Storage (or ETS)
  15. ELIXIR POPULAR PROJECTS ▸ Phoenix - Web Framework ▸ First

    class support for Web Sockets/Channels ▸ Ecto - Query Builder/ORM ▸ Plug - Composable modules for web applications ▸ Nerves - Framework for embedded devices.
  16. ELIXIR WANT TO LEARN MORE? ▸ https://elixir-lang.org/ ▸ www.elixirforum.com ▸

    Join us on slack in #elixir! ▸ Bi-monthly meet-up, Indy Elixir (www.indyelixir.org) ▸ Come bother me at Code & Coffee on Thursdays @ 7am ▸ Follow my blog www.mitchellhanberg.com