Skip to content

Instantly share code, notes, and snippets.

@Qqwy
Last active July 19, 2017 03:15
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/519888d8ee6d3529251aeb188398091d to your computer and use it in GitHub Desktop.
Save Qqwy/519888d8ee6d3529251aeb188398091d to your computer and use it in GitHub Desktop.
How exclamation marks are supposed to be used in Elixir
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
@Qqwy
Copy link
Author

Qqwy commented Jul 18, 2017

Usage instructions:

# Usage: 
import Scream

scream!! 42
scream!!!!!!!!!!!!!! 'I like trains and I cannot lie'

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