Advertisement
Guest User

Untitled

a guest
Jun 29th, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.25 KB | None | 0 0
  1. defmodule Macra do
  2. @arg_names ~w[a b c d e f g h i j k l m n o p q r s t u v w x y z]a
  3.  
  4. def take_arguments(many) do
  5. for arg_name <- Enum.take(@arg_names, many), do: {arg_name, [], Elixir}
  6. end
  7.  
  8. defmacro defreverse({:/, _, [{name, meta, _ }, arity]} , to: {function, call_meta, _ }) do
  9. args = take_arguments(arity)
  10. function_definition = { name, meta, args}
  11. call = {function, call_meta, Enum.reverse(args) }
  12.  
  13. quote do
  14. def unquote(function_definition), do: unquote(call)
  15. end
  16. end
  17.  
  18. defmacro defswap({:/, _, [{name, meta, _ }, arity]} , to: {function, call_meta, _ }) do
  19. args = take_arguments(arity)
  20. function_definition = { name, meta, args}
  21. {first, [second]} = Enum.split(args,arity-1)
  22. call = {function, call_meta, [second | first] }
  23.  
  24. quote do
  25. def unquote(function_definition), do: unquote(call)
  26. end
  27. end
  28.  
  29.  
  30. end
  31.  
  32. defmodule Test do
  33.  
  34. require Macra
  35. import Macra
  36.  
  37. def test(uno, due, tres,quatro) do
  38. IO.inspect uno
  39. IO.inspect due
  40. IO.inspect tres
  41. IO.inspect quatro
  42. end
  43.  
  44. defreverse test_rev/4, to: test
  45.  
  46. defswap swapped/4, to: test
  47.  
  48. end
  49. IO.inspect "Original"
  50. Test.test(1,2,3,4)
  51. IO.inspect "Reversed"
  52. Test.test_rev(1,2,3,4)
  53. IO.inspect "Swapped"
  54. Test.swapped(1,2,3,4)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement