@@ -11,10 +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 >
16
- ### Chapter 1 - The language of macros
17
- </summary >
14
+ ### Contents
15
+
16
+ - [ Chapter 1 - The Language of Macros] ( #heading )
17
+ - [ What are Macros?] ( #sub-heading )
18
+ - [ The Abstract Syntax Tree] ( #sub-heading-1 )
19
+ - [ Trying It All Together] ( sub-heading-2 )
20
+
21
+ ### Chapter 1 - The Language of Macros
22
+
23
+ #### What are Macros?
18
24
19
25
- Macros are code that write code.
20
26
- Elixir itself is made with macros, as a result you can extend the language itself
@@ -65,36 +71,38 @@ iex> quote do: div(10, 2)
65
71
In most languages, we would have to parse a string expression into something digestible by our program. With Elixir, we can access
66
72
the representation of expressions directly with macros."
67
73
68
- [ First macro - ` math.exs ` ] ( math.exs )
69
-
70
- <!-- <details>
71
- <summary> [First macro - `math.exs`](math.exs) </summary>
72
- ```elixir
73
- defmodule Math do
74
- @moduledoc false
75
-
76
- defmacro say({:+, _, [lhs, rhs]}) do
77
- quote do
78
- lhs = unquote(lhs)
79
- rhs = unquote(rhs)
80
- result = lhs + rhs
81
- IO.puts("#{lhs} plus #{rhs} is #{result}")
82
- result
83
- end
84
- end
74
+ [ First macro - ` math.exs ` ] ( math.exs )
85
75
86
- defmacro say({:*, _, [lhs, rhs]}) do
87
- quote do
88
- lhs = unquote(lhs)
89
- rhs = unquote(rhs)
90
- result = lhs * rhs
91
- IO.puts("#{lhs} times #{rhs} is #{result}")
92
- result
93
- end
76
+ <details >
77
+ <summary >math.exs</summary >
78
+
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
94
90
end
91
+ end
92
+
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
95
100
end
96
- ```
97
- </details> -->
101
+ end
102
+ end
103
+ ```
104
+
105
+ </details >
98
106
99
107
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.
100
108
Automagically adding these when you open iex with ` iex math.exs ` .
@@ -140,5 +148,3 @@ Yes that's correct because we are dealing with ASTs not the data it represents;
140
148
Much like interpolation from Ecto and the difference between ` "Hello world" ` and ` "Hello #{world} ` .
141
149
142
150
Back to the ` math.exs ` example.
143
-
144
- </details >
0 commit comments