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

Elixir - Part 1

Elixir - Part 1

A talk about Elixir and OTP given at Codeaholics in Hong Kong on August 12th 2015

Matthew Rudy Jacobs

August 12, 2015
Tweet

More Decks by Matthew Rudy Jacobs

Other Decks in Technology

Transcript

  1. -module(roman). -export([to_roman/1]). to_roman(0) -> []; to_roman(X) when X >= 1000

    -> [$M | to_roman(X - 1000)]; to_roman(X) when X >= 100 -> digit(X div 100, $C, $D, $M) ++ to_roman(X rem 100); to_roman(X) when X >= 10 -> digit(X div 10, $X, $L, $C) ++ to_roman(X rem 10); to_roman(X) when X >= 1 -> digit(X, $I, $V, $X). digit(1, X, _, _) -> [X]; digit(2, X, _, _) -> [X, X]; digit(3, X, _, _) -> [X, X, X]; digit(4, X, Y, _) -> [X, Y]; digit(5, _, Y, _) -> [Y]; digit(6, X, Y, _) -> [Y, X]; digit(7, X, Y, _) -> [Y, X, X]; digit(8, X, Y, _) -> [Y, X, X, X]; digit(9, X, _, Z) -> [X, Z]. Complex? Ugly?
  2. %%% erlang -module(recursve). -export([fac/1]). fac(N) when N == 0 ->

    1; fac(N) when N > 0 -> N*fac(N-1). # Ruby module Recursive def fac(n) return 1 if n == 0 fail ArgumentError unless n > 0 n*fac(n-1) end end
  3. # Elixir defmodule Recursive do def fac(n) when n ==

    0, do: 1 def fac(n) when n > 0 do n*fac(n-1) end end
  4. # Elixir - with tail call defmodule Recursive do def

    fac(0), do: 1 def fac(n) when n > 0 do fac(n, 1) end defp fac(1, acc), do: acc defp fac(n, acc) do fac(n-1, acc*n) end end
  5. defmodule Math do def sum([head|tail], acc) do sum(tail, head +

    acc) end def sum_list([], acc) do acc end end
  6. Gateway Todos Users Auth SERVICES OVER HTTP Mailer Search •HTTP

    •Ports •Monitoring •Scaling •Logging Issues
  7. Gateway Todos Users Auth A SINGLE OTP SYSTEM Mailer Search

    •HTTP •Ports •Monitoring •Scaling •Logging Issues