In this talk, we’ll walk through the process of writing and publishing an Elixir library from scratch. Along the way, we’ll discover a handful of Elixir features such as: Mix, Documentation, Doctests, Typespecs, and Hex.
Introduction to Elixir Elixir’s build tool Background Mix Docs Types Conclusion Docstrings and Doctests Summary and further learning Introduction to Typespecs
What is Elixir? ● Language that compiles to Erlang ● Built on top of the famed Erlang VM (“nine 9s of reliability”) ○ Traditionally used for telecommunications by Ericsson ○ WhatsApp, Facebook Messenger, Riot Games, RabbitMQ, Riak
Features of Elixir ● A pleasant, modern syntax similar to Ruby ● Immutable ● Pattern Matching ● Gradual types with Typespecs ● First-class documentation ● Metaprogramming through Macros ● Concurrency abstractions (Actor model and OTP) ● Interop with Erlang
Introduction to Elixir Elixir’s build tool Background Mix Docs Types Conclusion Docstrings and Doctests Summary and further learning Introduction to Typespecs
defmodule SimpleStatistics.Mixfile do use Mix.Project def project do [app: :simple_statistics, deps: deps] end defp deps do [{:ex_doc, "~> 0.11", only: :dev}, {:dialyxir, "~> 0.3", only: [:dev]}] end end Mix.exs defines our project metadata
Introduction to Elixir Elixir’s build tool Background Mix Docs Types Conclusion Docstrings and Doctests Summary and further learning Introduction to Typespecs
Document with Docstrings # lib/simple_statistics/mean.ex defmodule SimpleStatistics.Mean do @doc ~S""" The mean is the sum of all values over the number of values. """ def mean([]), do: nil ... end
# test/simple_statistics_test.ex defmodule SimpleStatisticsTest do use ExUnit.Case test "the truth" do assert 1 + 1 == 2 end end Write unit tests in test/ with ExUnit
Show examples with Doctests @doc ~S""" The mean is the sum of all values over the number of values. ## Examples iex> SimpleStatistics.Mean.mean([]) nil iex> SimpleStatistics.Mean.mean([1,2,3,4,5]) 3.0 iex> SimpleStatistics.Mean.mean([1.5,-2.1,3,4.5,5]) 2.38 """
Introduction to Elixir Elixir’s build tool Background Mix Docs Types Conclusion Docstrings and Doctests Summary and further learning Introduction to Typespecs
Typespecs @spec add(number, number) :: number def add(x, y) do x + y end @spec mean(nonempty_list(number)) :: float() def mean(list) do Enum.sum(list) / Kernel.length(list) end
Introduction to Elixir Elixir’s build tool Background Mix Docs Types Conclusion Docstrings and Doctests Summary and further learning Introduction to Typespecs