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

Elixir in Elixir

Elixir in Elixir

Jay Hayes

March 15, 2018
Tweet

More Decks by Jay Hayes

Other Decks in Programming

Transcript

  1. if

  2. def

  3. |>

  4. quote do: “foo” # “foo” quote do: :foo # :foo

    quote do: 1 # 1 quote do: [1] # [1] quote do: {1,2} # {1,2} quote do: [a: 1] # [a: 1]
  5. num = 42 expr = quote do: 1 + num

    # => {:+, [], [1, {:num, [], :Elixir}]}
  6. num = 42 expr = quote do: 1 + num

    # => {:+, [], [1, {:num, [], :Elixir}]} Code.eval_quoted(expr) # ERROR! `num` is undefined
  7. num = 42 expr = quote do: 1 + unquote(num)

    # => {:+, [], [1, 42]} Code.eval_quoted(expr) # => {43, []}
  8. num = 42 expr = “1 + #{num}” # =>

    “1 + 42” eval expr # => 43
  9. one = quote do: 1 + 1 # => {:+,

    [], [1, 1]} two = quote do: 2 + 2 # => {:+, [], [2, 2]} sum = quote do: unquote(one) + unquote(two) # => {:+, [], [ # {:+, [], [1, 1]}, # {:+, [], [2, 2]} # ]}
  10. if

  11. case … do x when x in [false, nil] ->

    … _ -> … end cond cases
  12. defmacro if(c, do: tc, else: fc) do quote do case

    unquote(c) do x when x in [false, nil] -> unquote(fc) _ -> unquote(tc) end end end
  13. defmacro if(c, do: tc, else: fc) do quote do case

    unquote(c) do x when x in [false, nil] -> unquote(fc) _ -> unquote(tc) end end end cond
  14. defmacro if(c, do: tc, else: fc) do quote do case

    unquote(c) do x when x in [false, nil] -> unquote(fc) _ -> unquote(tc) end end end cases
  15. defmacro if(c, do: tc, else: fc) do quote do case

    unquote(c) do x when x in [false, nil] -> unquote(fc) _ -> unquote(tc) end end end
  16. defmacro if(c, do: tc, else: fc) do quote do case

    unquote(c) do x when x in [false, nil] -> unquote(fc) _ -> unquote(tc) end end end cond
  17. defmacro if(c, do: tc, else: fc) do quote do case

    unquote(c) do x when x in [false, nil] -> unquote(fc) _ -> unquote(tc) end end end cases
  18. defmacro if(c, do: tc, else: fc) do quote do case

    unquote(c) do x when x in [false, nil] -> unquote(fc) _ -> unquote(tc) end end end
  19. case do x when x in [false, nil] -> _

    -> end IO.puts(“Pass!”) IO.puts(“Fail.”) foo > 0
  20. :elixir_module.compile(Lolwat, {:def, [context: Elixir, import: Kernel], [{:lol, [context: Elixir], Elixir},

    [do: :wat]]}, [], %{__struct__: Macro.Env, aliases: [], context: nil, context_modules: [Mex], export_vars: nil, file: "iex", function: nil, functions: [Mex: [expand_all: 2], "IEx.Helpers": [c: 1, c: 2, cd: 1, clear: 0, flush: 0, h: 0, i: 1, l: 1, ls: 0, ls: 1, nl: 1, nl: 2, pid: 1, pid: 3, pwd: 0, r: 1, recompile: 0, respawn: 0, v: 0, v: 1], Kernel: [!=: 2, !==: 2, *: 2, +: 1, +: 2, ++: 2, -: 1, -: 2, --: 2, /: 2, <: 2, <=: 2, ==: 2, ===: 2, =~: 2, >: 2, >=: 2, abs: 1, apply: 2, apply: 3, binary_part: 3, bit_size: 1, byte_size: 1, div: 2, elem: 2, exit: 1, function_exported?: 3, get_and_update_in: 3, get_in: 2, hd: 1, inspect: 1, inspect: 2, is_atom: 1, is_binary: 1, is_bitstring: 1, is_boolean: 1, is_float: 1, is_function: 1, is_function: 2, is_integer: 1, is_list: 1, is_map: 1, is_number: 1, is_pid: 1, is_port: 1, is_reference: 1, is_tuple: 1, length: 1, macro_exported?: 3, make_ref: 0, map_size: 1, max: 2, min: 2, node: 0, node: 1, not: 1, pop_in: 2, put_elem: 3, put_in: 3, rem: 2, round: 1, self: 0, send: 2, spawn: 1, spawn: 3, spawn_link: 1, spawn_link: 3, spawn_monitor: 1, spawn_monitor: 3, struct: 1, struct: 2, struct!: 1, struct!: 2, throw: 1, tl: 1, trunc: 1, tuple_size: 1, update_in: 3]], lexical_tracker: nil, line: 2, macro_aliases: [], macros: [Mex: [mex: 1], "IEx.Helpers": [b: 1, h: 1, import_file: 1, import_file: 2, import_file_if_available: 1, import_if_available: 1, import_if_available: 2, s: 1, t: 1], Kernel: [!: 1, &&: 2, ..: 2, <>: 2, @: 1, alias!: 1, and: 2, binding: 0, binding: 1, def: 1, def: 2, defdelegate: 2, defexception: 1, defimpl: 2, defimpl: 3, defmacro: 1, defmacro: 2, defmacrop: 1, defmacrop: 2, defmodule: 2, defoverridable: 1, defp: 1, defp: 2, defprotocol: 2, defstruct: 1, destructure: 2, get_and_update_in: 2, if: 2, in: 2, is_nil: 1, match?: 2, or: 2, pop_in: 1, put_in: 2, raise: 1, raise: 2, reraise: 2, reraise: 3, sigil_C: 2, sigil_D: 2, sigil_N: 2, sigil_R: 2, sigil_S: 2, sigil_T: 2, sigil_W: 2, sigil_c: 2, sigil_r: 2, sigil_s: 2, sigil_w: 2, to_char_list: 1, to_charlist: 1, to_string: 1, unless: 2, update_in: 2, use: 1, use: 2, var!: 1, var!: 2, |>: 2, ||: 2]], module: nil, requires: [IEx.Helpers, Kernel, Kernel.Typespec, Mex], vars: []})