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(1)> <<a :: utf8, b>> = <<1, "hello">> | |
** (MatchError) no match of right hand side value: <<1, 104, 101, 108, 108, 111>> | |
iex(1)> <<a :: utf8, b :: binary>> = <<1, "hello">> | |
<<1, 104, 101, 108, 108, 111>> |
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 M do | |
defp cases do | |
[quote do 1 -> 2 end, quote do 3 -> 4 end] | |
end | |
defmacrop make_case do | |
q = quote do | |
case var!(x) do | |
0 -> nil | |
unquote_splicing(cases) |
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 M do | |
defp cases do | |
[quote do 0 -> 0 end] | |
++ [quote do 1 -> 2 end, quote do 3 -> 4 end] |> :lists.flatten | |
end | |
defmacrop make_case do | |
IO.inspect cases | |
q = 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
defmodule N do | |
x = 1 | |
def foo(x), do: -x | |
y = x + 1 | |
IO.puts "The correct answer is #{y}" | |
def y(y), do: y*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
try do | |
exit :badfood | |
catch | |
type, reason -> | |
IO.inspect type | |
IO.inspect reason | |
end |
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 M do | |
@type mytype :: String.t | |
end | |
defmodule N do | |
@spec hi(M.mytype) :: M.mytype | |
def hi(x), do: x | |
end | |
defmodule Mac 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 B do | |
def bin_to_hex(data) do | |
for <<hi::4, lo::4 <- data>>, | |
into: <<>>, | |
do: <<hex_digit(hi), hex_digit(lo)>> | |
end | |
defp hex_digit(n) when n < 10, do: n + ?0 | |
defp hex_digit(n), do: n-10 + ?a | |
end |
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 Ex do | |
def extract(file) do | |
str = File.read!(file) |> String.to_char_list! | |
all_toks = scan_string(str, 0, []) | |
# now we have all the tokens, we just need to match each one and see if | |
# it's a -define() form | |
all_toks | |
end |
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 MacroM do | |
defmacro h_transform(mod) do | |
IO.puts "Applying h_transform" | |
IO.inspect(mod) | |
end | |
defmacro la_transform(mod) do | |
IO.puts "Applying la_transform" | |
IO.inspect(mod) | |
end |
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 M do | |
defmacro common_fields(extra \\ []) do | |
[color: nil, id: nil] ++ extra | |
end | |
end | |
defmodule Pawn do | |
import M | |
defstruct common_fields | |
end |
OlderNewer