From 00a9a11f273afe1bfd44272f462ae6756a923d2f Mon Sep 17 00:00:00 2001 From: Ryan Gonzalez Date: Mon, 7 Aug 2017 18:04:47 -0500 Subject: [PATCH] Reimplement variadic if without recursion (closes #842) --- hy/core/bootstrap.hy | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/hy/core/bootstrap.hy b/hy/core/bootstrap.hy index 4ecb243e3..8ed64aabb 100644 --- a/hy/core/bootstrap.hy +++ b/hy/core/bootstrap.hy @@ -10,11 +10,14 @@ "if with elif" (setv n (len args)) (if* n - (if* (= n 1) - (get args 0) - `(if* ~(get args 0) - ~(get args 1) - (if ~@(cut args 2)))))) + (do + (setv args (list args)) + (setv else-branch (if* (% n 2) (.pop args) `None)) + (.reverse args) + (reduce (fn [current-else [then-branch cond]] + `(if* ~cond ~then-branch ~current-else)) + (zip (cut args None None 2) (cut args 1 None 2)) + else-branch)))) (defmacro macro-error [location reason] "error out properly within a macro"