Skip to content

Instantly share code, notes, and snippets.

@udzura
udzura / correct_macro.ex
Created April 13, 2012 01:01
test elixir macro var!!!!!
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
@manpages
manpages / i.ex
Created January 20, 2013 21:47
Let's talk about Elixir
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
@alco
alco / re.ex
Created September 4, 2013 09:23
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
[]
@alco
alco / macro_list.ex
Last active September 25, 2022 19:56
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
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
@zsoldosp
zsoldosp / elixir macros-why-error.exs
Created February 8, 2014 19:38
Different errors given for the same elixir code - different scoping between iex and elixir? If so, why?
$ 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
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) =>
defmodule View do
defrecord Page, [templates: []]
defmacro __using__(_) do
template = """
<%= for template <- page.templates do %>
<%= template.id %> ::
<% end %>
<%= if true do %>
@stevedomin
stevedomin / mix.exs
Last active September 25, 2022 19:55
simple_dynamo_deploy mix file
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 ]
defrecord User, name: "", email: ""
defmodule Amnesia do
@compile {:parse_transform, :qlc}
def create_schema do
create_table User
end