Advertisement
Guest User

Untitled

a guest
Dec 9th, 2016
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.70 KB | None | 0 0
  1. defmodule LangUtil do
  2. @doc ~S"""
  3. For use in a pipeline like so:
  4.  
  5. {s, &Kernel.<>/2}
  6. |> oper_if(c.section, "Section: #{c.section}\n")
  7. |> oper_if(true, "Description: #{c.short_description}\n")
  8. |> oper_if(c.long_description, prefix_every_line(c.long_description, " ") <> "\n")
  9. |> elem(0)
  10.  
  11. `expression` is not evaluated unless evaluation of `clause` is truthy. This avoids
  12. blowing up on nils and other unexpected values.
  13. """
  14. defmacro oper_if(state, clause, expression) do
  15. quote do
  16. {acc, operator} = unquote(state)
  17. result = if unquote(clause) do
  18. operator.(acc, unquote(expression))
  19. else
  20. acc
  21. end
  22. {result, operator}
  23. end
  24. end
  25. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement