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

Writing and Publishing Elixir Libraries

Writing and Publishing Elixir Libraries

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.

Yos Riady

March 16, 2017
Tweet

More Decks by Yos Riady

Other Decks in Programming

Transcript

  1. At this moment, there are more than 7100 gems hosted

    on Rubygems.org. Hex.pm has 1455 packages.
  2. Introduction to Elixir Elixir’s build tool Background Mix Docs Types

    Conclusion Docstrings and Doctests Summary and further learning Introduction to Typespecs
  3. 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
  4. 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
  5. Introduction to Elixir Elixir’s build tool Background Mix Docs Types

    Conclusion Docstrings and Doctests Summary and further learning Introduction to Typespecs
  6. Use Mix to generate an Elixir project > mix new

    simple_statistics > cd simple_statistics
  7. Elixir project structure |-- _build |-- config/ |-- config.exs |--

    lib/ |-- simple_statistics.ex |-- test/ |-- simple_statistics_test.exs |-- test_helper.exs |-- mix.exs |-- mix.lock |-- README.md |-- .gitignore
  8. 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
  9. Write code in lib/ # lib/simple_statistics/mean.ex defmodule SimpleStatistics.Mean do def

    mean(list) do Enum.sum(list) / Kernel.length(list) end end
  10. Introduction to Elixir Elixir’s build tool Background Mix Docs Types

    Conclusion Docstrings and Doctests Summary and further learning Introduction to Typespecs
  11. 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
  12. # 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
  13. 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 """
  14. Run tests with Mix > mix test . Finished in

    0.07 seconds (0.07s on load, 0.00s on tests) 1 test, 0 failures
  15. Introduction to Elixir Elixir’s build tool Background Mix Docs Types

    Conclusion Docstrings and Doctests Summary and further learning Introduction to Typespecs
  16. 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
  17. Typespecs • Scalars: integer(), float(), pos_integer() • Lists: list(type), non_empty_list(type),

    [type] • Functions: (type1, type2 -> type) • Create your own types: @type t :: %ECS.Entity{ id: String.t, components: [ECS.Component] }
  18. Generate web-ready documentation with ex_doc # mix.exs defp deps do

    [{:ex_doc, "~> 0.11", only: :dev}, {:earmark, "~> 0.1", only: :dev}] end
  19. Introduction to Elixir Elixir’s build tool Background Mix Docs Types

    Conclusion Docstrings and Doctests Summary and further learning Introduction to Typespecs