Skip to content

Instantly share code, notes, and snippets.

@Qqwy
Created June 21, 2017 20:46
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Qqwy/79960701a31bbe75d44fc018b2ffdea1 to your computer and use it in GitHub Desktop.
Save Qqwy/79960701a31bbe75d44fc018b2ffdea1 to your computer and use it in GitHub Desktop.
AtAt: The pinnacle of Elixir MetaProgramming.
defmodule AtAt do
defmacro __using__(_opts) do
quote do
import AtAt
import Kernel, except: [@: 1]
end
end
defmacro @(val) do
case val do
{:@, _, [{name, context, args}]} ->
{count, inner} = count_ats({name, context, args}, 2)
quote bind_quoted: [count: count, inner: inner, escaped_inner: Macro.escape(inner)] do
IO.inspect("Wow! Running #{Macro.to_string(escaped_inner)} with #{count} ats!")
inner
end
_ -> quote do Kernel.@(unquote(val)) end
end
end
defp count_ats({:@, _, [{name, context, args}]}, accum) do
count_ats({name, context, args}, accum + 1)
end
defp count_ats(other, accum) do
{accum, other}
end
end
@Qqwy
Copy link
Author

Qqwy commented Jun 21, 2017

Usage instructions:

iex> use AtAt
iex> @@@@@@@@@@@@@@@@@@@@@@@self()
"Wow! Running self() with 23 ats!"
#PID<0.85.0>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment