Non-recursive !reduce
authorDavid Vázquez <davazp@gmail.com>
Fri, 3 May 2013 11:48:14 +0000 (12:48 +0100)
committerDavid Vázquez <davazp@gmail.com>
Fri, 3 May 2013 11:48:14 +0000 (12:48 +0100)
src/boot.lisp
src/utils.lisp

index a54291f..29d3dd2 100644 (file)
             (append (cdr list1) list2))))
 
 (defun append (&rest lists)
-  (!reduce #'append-two lists))
+  (!reduce #'append-two lists nil))
 
 (defun revappend (list1 list2)
   (while list1
     (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
   (+ (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)))
index 224fcb2..d586cdd 100644 (file)
       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)