From 0c0ab4e45772af361b3c3581d3215605256e8ee4 Mon Sep 17 00:00:00 2001 From: Jaskirat Date: Tue, 8 Feb 2011 17:21:11 +0530 Subject: [PATCH] Added macros.clj and updated the path_to_answer_sheet & path_to_enlightenment for the same. --- src/koans/macros.clj | 47 +++++++++++++++++++++++++++++++++++ src/path_to_answer_sheet.clj | 10 ++++++++ src/path_to_enlightenment.clj | 3 ++- 3 files changed, 59 insertions(+), 1 deletion(-) create mode 100644 src/koans/macros.clj diff --git a/src/koans/macros.clj b/src/koans/macros.clj new file mode 100644 index 0000000..a72d835 --- /dev/null +++ b/src/koans/macros.clj @@ -0,0 +1,47 @@ +"Macros are also just functions!" +(defmacro hello [x] + (str "Hello, " x)) + + "Create a list and use it to be eval-ed at run time" +(defmacro infix [form] + (list (second form) (first form) (nth form 2))) + +"Sometimes it is easier to handcraft it ..." +(defmacro infix-better [form] + `(~(second form) + __ + __ )) + +"Macro in a macro until you get a stack overflow! ;)" +(defmacro r-infix [form] + (list (second form) + (if (instance? clojure.lang.PersistentList (first form)) + `(r-infix ~(first form)) + (first form)) + (if (instance? clojure.lang.PersistentList (nth form 2)) + `(r-infix ~(nth form 2)) + (nth form 2)) + )) + + +(meditations + "Macros are just function eval-ed at compile time" + (= "Hello, Macros!" (hello "Macros!")) + + "Can I haz some infix?" + (= __ (infix (9 + 1))) + + "Remember these are nothing but code transformations" + (= __ (macroexpand '(infix (9 + 1)))) + + "You can do better than that, hand crafting ftw" + (= __ (macroexpand '(infix-better (10 * 2)))) + + "Things dont always work as you would like them to... " + (= __ (macroexpand '(infix-better ( 10 + (2 * 3))))) + + "Really, you dont understand recursion until you understand recursion" + (= __ (r-infix (10 + (2 * 3)))) + + ) + diff --git a/src/path_to_answer_sheet.clj b/src/path_to_answer_sheet.clj index 63844ad..3ea42e9 100644 --- a/src/path_to_answer_sheet.clj +++ b/src/path_to_answer_sheet.clj @@ -133,6 +133,16 @@ ] "___" ['(comp exclamator exclamator exclamator) '(fn [x] (+ 20 x))]} + + "macros" {"__" ['~(first form) + '~(nth form 2) + 10 + ''(+ 9 1) + ''(* 10 2) + ''(+ 10 (2 * 3)) + 16 + ] + } }) (defn replace-with [s k replacements] diff --git a/src/path_to_enlightenment.clj b/src/path_to_enlightenment.clj index 9941bae..f70bf52 100644 --- a/src/path_to_enlightenment.clj +++ b/src/path_to_enlightenment.clj @@ -29,7 +29,8 @@ "creating_functions" "recursion" "destructuring" - "refs"]) + "refs" + "macros"]) (defn require-version [[required-major required-minor]] (let [{:keys [major minor]} *clojure-version*]