@@ -11,11 +11,16 @@ examples. This README will serve as my notes, therefore you shouldn't take them
11
11
value as the notes will make sense for me as I cherry pick a sentence or an analogy.
12
12
Moreover, don't expect direct quotes, changing the sentences vocbulary helps me personally.
13
13
14
- <details >
15
- <summary >Chapter 1 - The language of Macros</summary >
14
+ ### Contents
16
15
17
- <details >
18
- <summary >What are Macros?</summary >
16
+ - [ Chapter 1 - The Language of Macros] ( #chapter-1-the-language-of-macros )
17
+ - [ What are Macros?] ( #what-are-macros )
18
+ - [ The Abstract Syntax Tree] ( #the-abstract-syntax-tree )
19
+ - [ Trying It All Together] ( #trying-it-all-together )
20
+
21
+ ### Chapter 1 - The Language of Macros
22
+
23
+ #### What are Macros?
19
24
20
25
- Macros are code that write code.
21
26
- Elixir itself is made with macros, as a result you can extend the language itself
@@ -72,36 +77,38 @@ iex> quote do: div(10, 2)
72
77
In most languages, we would have to parse a string expression into something digestible by our program. With Elixir, we can access
73
78
the representation of expressions directly with macros."
74
79
75
- [ First macro - ` math.exs ` ] ( math.exs )
76
-
77
- <!-- <details>
78
- <summary> [First macro - `math.exs`](math.exs) </summary>
79
- ```elixir
80
- defmodule Math do
81
- @moduledoc false
82
-
83
- defmacro say({:+, _, [lhs, rhs]}) do
84
- quote do
85
- lhs = unquote(lhs)
86
- rhs = unquote(rhs)
87
- result = lhs + rhs
88
- IO.puts("#{lhs} plus #{rhs} is #{result}")
89
- result
90
- end
91
- end
80
+ [ First macro - ` math.exs ` ] ( math.exs )
92
81
93
- defmacro say({:*, _, [lhs, rhs]}) do
94
- quote do
95
- lhs = unquote(lhs)
96
- rhs = unquote(rhs)
97
- result = lhs * rhs
98
- IO.puts("#{lhs} times #{rhs} is #{result}")
99
- result
100
- end
82
+ <details >
83
+ <summary >math.exs</summary >
84
+
85
+ ``` elixir
86
+ defmodule Math do
87
+ @moduledoc false
88
+
89
+ defmacro say ({:+ , _ , [lhs, rhs]}) do
90
+ quote do
91
+ lhs = unquote (lhs)
92
+ rhs = unquote (rhs)
93
+ result = lhs + rhs
94
+ IO .puts (" #{ lhs } plus #{ rhs } is #{ result } " )
95
+ result
101
96
end
97
+ end
98
+
99
+ defmacro say ({:* , _ , [lhs, rhs]}) do
100
+ quote do
101
+ lhs = unquote (lhs)
102
+ rhs = unquote (rhs)
103
+ result = lhs * rhs
104
+ IO .puts (" #{ lhs } times #{ rhs } is #{ result } " )
105
+ result
102
106
end
103
- ```
104
- </details> -->
107
+ end
108
+ end
109
+ ```
110
+
111
+ </details >
105
112
106
113
Note when you use this in iex you need to first ` c "math.exs" ` then ` require Math ` but i've included it in [ .iex.exs] ( .iex.exs ) to save time.
107
114
Automagically adding these when you open iex with ` iex math.exs ` .
@@ -147,7 +154,3 @@ Yes that's correct because we are dealing with ASTs not the data it represents;
147
154
Much like interpolation from Ecto and the difference between ` "Hello world" ` and ` "Hello #{world} ` .
148
155
149
156
Back to the ` math.exs ` example.
150
-
151
- </details >
152
-
153
- </details >
0 commit comments