Skip to content

Instantly share code, notes, and snippets.

@lagenorhynque
Last active February 15, 2018 16:42
Show Gist options
  • Select an option

  • Save lagenorhynque/eb8fa8c2f37615cc79390c76989ed1be to your computer and use it in GitHub Desktop.

Select an option

Save lagenorhynque/eb8fa8c2f37615cc79390c76989ed1be to your computer and use it in GitHub Desktop.
iex(13)> defmodule MyUtil do
...(13)> defmacro my_if(test, do: body) do
...(13)> quote do
...(13)> if unquote(test), do: unquote(body)
...(13)> end
...(13)> end
...(13)> end
{:module, MyUtil,
<<70, 79, 82, 49, 0, 0, 4, 160, 66, 69, 65, 77, 65, 116, 85, 56, 0, 0, 0, 138,
0, 0, 0, 15, 13, 69, 108, 105, 120, 105, 114, 46, 77, 121, 85, 116, 105, 108,
8, 95, 95, 105, 110, 102, 111, 95, 95, ...>>, {:my_if, 2}}
iex(14)> require MyUtil
require MyUtil
MyUtil
iex(15)> MyUtil.my_if true do
...(15)> IO.puts "Hello!"
...(15)> 42
...(15)> end
Hello!
42
iex(16)> MyUtil.my_if false do
...(16)> IO.puts "Hello!"
...(16)> 42
...(16)> end
nil
user> (ns my-util)
nil
my-util> (defmacro my-when [test & body]
`(if ~test
(do ~@body)))
#'my-util/my-when
my-util> (my-when true
(println "Hello!")
42)
Hello!
42
my-util> (my-when false
(println "Hello!")
42)
nil
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment