Skip to content

Commit

Permalink
Focus the problem and clean up naming in macros
Browse files Browse the repository at this point in the history
  • Loading branch information
trptcolin committed Apr 9, 2018
1 parent faa02b5 commit 809af93
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 12 deletions.
4 changes: 1 addition & 3 deletions resources/koans.clj
Original file line number Diff line number Diff line change
Expand Up @@ -264,8 +264,6 @@
(drop 2 form)
"Hello, Macros!"
10
'(+ 9 1)
'(* 10 2)
'(+ 10 (2 * 3))]}]
'(+ 9 1)]}]

]
18 changes: 9 additions & 9 deletions src/koans/24_macros.clj
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,23 @@
(defmacro infix [form]
(list (second form) (first form) (nth form 2)))

(defmacro infix-better [form]
(defmacro infix-concise [form]
`(~(second form) ; Note the syntax-quote (`) and unquote (~) characters!
__
__ ))
__))

(defmacro r-infix [form]
(defmacro recursive-infix [form]
(cond (not (seq? form))
__
(= 1 (count form))
`(r-infix ~(first form))
`(recursive-infix ~(first form))
:else
(let [operator (second form)
first-arg (first form)
others __]
`(~operator
(r-infix ~first-arg)
(r-infix ~others)))))
(recursive-infix ~first-arg)
(recursive-infix ~others)))))

(meditations
"Macros are like functions created at compile time"
Expand All @@ -36,10 +36,10 @@
(= __ (macroexpand '(infix (9 + 1))))

"You can do better than that - hand crafting FTW!"
(= __ (macroexpand '(infix-better (10 * 2))))
(= '(* 10 2) (macroexpand '(infix-concise (10 * 2))))

"Things don't always work as you would like them to... "
(= __ (macroexpand '(infix-better ( 10 + (2 * 3)))))
(= '(+ 10 (2 * 3)) (macroexpand '(infix-concise (10 + (2 * 3)))))

"Really, you don't understand recursion until you understand recursion"
(= 36 (r-infix (10 + (2 * 3) + (4 * 5)))))
(= 36 (recursive-infix (10 + (2 * 3) + (4 * 5)))))

0 comments on commit 809af93

Please sign in to comment.