Last active
July 19, 2017 03:15
-
-
Save Qqwy/519888d8ee6d3529251aeb188398091d to your computer and use it in GitHub Desktop.
How exclamation marks are supposed to be used in Elixir
This file contains hidden or 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 Scream do | |
defmacro scream!(ast) do | |
{exclams, res} = count_exclams(ast, 0) | |
case exclams do | |
_ when exclams < 5 -> | |
quote do "You say: #{inspect(unquote(res))}." end | |
_ when exclams < 10 -> | |
quote do "You yell: #{inspect(unquote(res))}!" end | |
_ when exclams < 15 -> | |
quote do "You scream: #{inspect(unquote(res))}!!" end | |
_ -> | |
quote do "YOU SCREAM FROM THE BOTTOM OF YOUR LUNGS: #{inspect(unquote(res))}!!!" end | |
end | |
end | |
defp count_exclams({:!, _, [inner]}, accum) do | |
count_exclams(inner, accum + 1) | |
end | |
defp count_exclams(other, accum) do | |
{accum, other} | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage instructions: