Last active
October 22, 2016 18:27
-
-
Save ivan/d50f831a946c50a3335ef354c86ad6f2 to your computer and use it in GitHub Desktop.
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 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