Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- defmodule LangUtil do
- @doc ~S"""
- For use in a pipeline like so:
- {s, &Kernel.<>/2}
- |> oper_if(c.section, "Section: #{c.section}\n")
- |> oper_if(true, "Description: #{c.short_description}\n")
- |> oper_if(c.long_description, prefix_every_line(c.long_description, " ") <> "\n")
- |> elem(0)
- `expression` is not evaluated unless evaluation of `clause` is truthy. This avoids
- blowing up on nils and other unexpected values.
- """
- defmacro oper_if(state, clause, expression) do
- quote do
- {acc, operator} = unquote(state)
- result = if unquote(clause) do
- operator.(acc, unquote(expression))
- else
- acc
- end
- {result, operator}
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement