From b7d3022b7718ba87a1867f56dc20b9c78976c161 Mon Sep 17 00:00:00 2001 From: Colin Jones Date: Tue, 8 Feb 2011 07:46:48 -0600 Subject: [PATCH] Allow r-infix to work with more than 2 operands. --- src/koans/macros.clj | 21 ++++++++++++--------- src/path_to_answer_sheet.clj | 2 +- 2 files changed, 13 insertions(+), 10 deletions(-) diff --git a/src/koans/macros.clj b/src/koans/macros.clj index 644430f..709fbac 100644 --- a/src/koans/macros.clj +++ b/src/koans/macros.clj @@ -10,14 +10,17 @@ __ )) (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)) - )) + (cond (not (seq? form)) + form + (= 1 (count form)) + `(r-infix ~(first form)) + :else + (let [operator (second form) + first-arg (first form) + others (drop 2 form)] + `(~operator + (r-infix ~first-arg) + (r-infix ~others))))) (meditations @@ -37,5 +40,5 @@ (= __ (macroexpand '(infix-better ( 10 + (2 * 3))))) "Really, you dont understand recursion until you understand recursion" - (= __ (r-infix (10 + (2 * 3))))) + (= __ (r-infix (10 + (2 * 3) + (4 * 5))))) diff --git a/src/path_to_answer_sheet.clj b/src/path_to_answer_sheet.clj index 9bd4e08..80f3dc5 100644 --- a/src/path_to_answer_sheet.clj +++ b/src/path_to_answer_sheet.clj @@ -141,7 +141,7 @@ ''(+ 9 1) ''(* 10 2) ''(+ 10 (2 * 3)) - 16]} + 36]} }) (defn replace-with [s k replacements]