This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule Hygiene do | |
defmacro interference do | |
quote do: var!(a) + var!(b) + var!(c) | |
end | |
end | |
defmodule HygieneTest do | |
def go do | |
require Hygiene | |
a = 10 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
iex(14)> f = function do | |
...(14)> (do: anything) -> anything | |
...(14)> (_) -> :nothing | |
...(14)> end | |
#Fun<erl_eval.6.82930912> | |
iex(15)> f.(do: 42) | |
42 | |
iex(16)> f.(do | |
...(16)> answer = 42 | |
...(16)> answer |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
iex(8)> Regex.run %r/^~(\d)+\.\.[0 ]B$/, "~4..0B" | |
["~4..0B", "4"] | |
iex(9)> Regex.run %r/^~(\d)+\.\.[0 ]B$/, "~4..0B", capture: :groups | |
** (ArgumentError) regex was not compiled with g | |
/Users/alco/Documents/git/elixir/lib/elixir/lib/regex.ex:134: Regex.run/3 | |
erl_eval.erl:569: :erl_eval.do_apply/6 | |
src/elixir.erl:138: :elixir.eval_forms/3 | |
iex(9)> Regex.run %r/^~(\d)+\.\.[0 ]B$/g, "~4..0B", capture: :groups | |
[] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule L do | |
# lc x inlist [0, 1], y inlist [:a, :b], do: {x, y} | |
defmacro do_list(list) do | |
args = 1..length(list) | |
|> Enum.map(fn x -> binary_to_atom(<<?a + x :: utf8>>) end) | |
|> Enum.map(fn x -> {x, [], :Elixir} end) # this creates AST nodes for variables | |
pairs = Enum.zip(args, list) | |
|> Enum.map(fn {v, lst} -> | |
quote do |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
iex(33)> Code.eval_quoted(quote do: [unquote_splicing([{4, "show/:id"}])]) | |
{[{4, "show/:id"}], []} | |
iex(34)> Code.eval_quoted(quote do: [unquote_splicing([{4, "show/:id", 1, 2}])]) | |
** (CompileError) nofile: invalid quoted expression: {4, "show/:id", 1, 2} | |
(elixir) src/elixir_exp.erl:408: :elixir_exp.expand_arg/2 | |
(elixir) src/elixir_exp.erl:384: :elixir_exp.expand_list/4 | |
(elixir) src/elixir_exp.erl:358: :elixir_exp.expand/2 | |
(elixir) src/elixir.erl:150: :elixir.quoted_to_erl/3 | |
(elixir) src/elixir.erl:134: :elixir.eval_forms/4 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ elixir macros-why-error.exs | |
false | |
** (UndefinedFunctionError) undefined function: MyMacro.unless/2 | |
MyMacro.unless(false, [do: :ok]) | |
(elixir) src/elixir_lexical.erl:17: :elixir_lexical.run/2 | |
(elixir) lib/code.ex:301: Code.require_file/2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule User do | |
defstruct firstname: "Ryan", lastname: "Levick" | |
def full_name(user) do | |
user.firstname <> " " <> user.lastname | |
end | |
end | |
user = %User{} | |
User.full_name(user) => |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule View do | |
defrecord Page, [templates: []] | |
defmacro __using__(_) do | |
template = """ | |
<%= for template <- page.templates do %> | |
<%= template.id %> :: | |
<% end %> | |
<%= if true do %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule SimpleDynamoDeploy.Mixfile do | |
use Mix.Project | |
def project do | |
[ app: :simple_dynamo_deploy, | |
version: "0.0.1", | |
build_per_environment: true, | |
dynamos: [SimpleDynamoDeploy.Dynamo], | |
compilers: [:elixir, :dynamo, :app], | |
deps: deps ] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defrecord User, name: "", email: "" | |
defmodule Amnesia do | |
@compile {:parse_transform, :qlc} | |
def create_schema do | |
create_table User | |
end |
OlderNewer