Skip to content

Instantly share code, notes, and snippets.

@ivan
Last active October 22, 2016 18:27
Show Gist options
  • Save ivan/d50f831a946c50a3335ef354c86ad6f2 to your computer and use it in GitHub Desktop.
Save ivan/d50f831a946c50a3335ef354c86ad6f2 to your computer and use it in GitHub Desktop.
defmodule LangUtil do
@doc ~S"""
For use in a pipeline like so:
s
|> append_if(c.section, "Section: #{c.section}\n")
|> append_if(true, "Description: #{c.short_description}\n")
|> append_if(c.long_description, prefix_every_line(c.long_description, " ") <> "\n")
`expression` is not evaluated unless evaluation of `clause` is truthy. This avoids
blowing up on nils and other unexpected values.
"""
defmacro append_if(acc, clause, expression) do
quote do
if unquote(clause) do
unquote(acc) <> unquote(expression)
else
unquote(acc)
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment