From cae73e8875a24258961b29e84d48d9794cb5be03 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20V=C3=A1zquez?= Date: Fri, 3 May 2013 12:48:14 +0100 Subject: [PATCH] Non-recursive !reduce --- src/boot.lisp | 6 +++--- src/utils.lisp | 16 +++++++--------- 2 files changed, 10 insertions(+), 12 deletions(-) diff --git a/src/boot.lisp b/src/boot.lisp index a54291f..29d3dd2 100644 --- a/src/boot.lisp +++ b/src/boot.lisp @@ -254,7 +254,7 @@ (append (cdr list1) list2)))) (defun append (&rest lists) - (!reduce #'append-two lists)) + (!reduce #'append-two lists nil)) (defun revappend (list1 list2) (while list1 @@ -282,7 +282,7 @@ (setq assignments (reverse assignments)) ;; `(let ,(mapcar #'cdr assignments) - (setq ,@(!reduce #'append (mapcar #'butlast assignments)))))) + (setq ,@(!reduce #'append (mapcar #'butlast assignments) nil))))) (defmacro do (varlist endlist &body body) `(block nil @@ -590,7 +590,7 @@ (+ (get-unix-time) 2208988800)) (defun concat (&rest strs) - (!reduce #'concat-two strs :initial-value "")) + (!reduce #'concat-two strs "")) (defun values-list (list) (values-array (list-to-vector list))) diff --git a/src/utils.lisp b/src/utils.lisp index 224fcb2..d586cdd 100644 --- a/src/utils.lisp +++ b/src/utils.lisp @@ -29,20 +29,18 @@ x (list x))) -(defun !reduce (func list &key initial-value) - (if (null list) - initial-value - (!reduce func - (cdr list) - :initial-value (funcall func initial-value (car list))))) +(defun !reduce (func list initial-value) + (let ((result initial-value)) + (dolist (element list result) + (setq result (funcall func result element))))) ;;; Concatenate a list of strings, with a separator (defun join (list &optional (separator "")) (if (null list) "" - (!reduce (lambda (s o) (concat s separator o)) - (cdr list) - :initial-value (car list)))) + (!reduce (lambda (s o) (concat s separator o)) + (cdr list) + (car list)))) (defun join-trailing (list &optional (separator "")) (if (null list) -- 1.7.10.4