Migrate toplevel code emitted as result of literals
[jscl.git] / src / utils.lisp
index f6d8120..5cb702c 100644 (file)
@@ -16,6 +16,8 @@
 ;; You should have received a copy of the GNU General Public License
 ;; along with JSCL.  If not, see <http://www.gnu.org/licenses/>.
 
+(/debug "loading utils.lisp!")
+
 (defvar *newline* "
 ")
 
   `(setq ,variable (concat ,variable (progn ,@form))))
 
 ;;; This couple of helper functions will be defined in both Common
-;;; Lisp and in Ecmalisp.
+;;; Lisp and in JSCL
 (defun ensure-list (x)
   (if (listp x)
       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 ""))
-  (!reduce (lambda (s o) (concat s separator o))  
-           (cdr list) 
-           :initial-value (car list))) 
+  (if (null list)
+      ""
+      (!reduce (lambda (s o) (concat s separator o))
+               (cdr list)
+               (car list))))
 
 (defun join-trailing (list &optional (separator ""))
   (if (null list)
 
 (defun float-to-string (x)
   #+jscl (float-to-string x)
-  #+common-lisp (format nil "~f" x))
+  #-jscl (format nil "~f" x))
+
+(defun satisfies-test-p (x y &key key (test #'eql) testp (test-not #'eql) test-not-p)
+  (when (and testp test-not-p)
+    (error "Both test and test-not are set"))
+  (let ((key-val (if key (funcall key y) y))
+        (fn (if test-not-p (complement test-not) test)))
+    (funcall fn x key-val)))