X-Git-Url: http://repo.macrolet.net/gitweb/?a=blobdiff_plain;f=src%2Futils.lisp;h=d380e4c29b6b93c2b8ce6508c60c32f298462f49;hb=587d21c0521468b08f1e1b045f2d08a3a93873a5;hp=3cf0103bd67da676a246760bb9efdf848a57ea77;hpb=54de4d4abbeb7f99e5f5702ad93e815cae7a0b3b;p=jscl.git diff --git a/src/utils.lisp b/src/utils.lisp index 3cf0103..d380e4c 100644 --- a/src/utils.lisp +++ b/src/utils.lisp @@ -3,49 +3,46 @@ ;; Copyright (C) 2012, 2013 David Vazquez ;; Copyright (C) 2012 Raimon Grau -;; This program is free software: you can redistribute it and/or +;; JSCL is free software: you can redistribute it and/or ;; modify it under the terms of the GNU General Public License as ;; published by the Free Software Foundation, either version 3 of the ;; License, or (at your option) any later version. ;; -;; This program is distributed in the hope that it will be useful, but +;; JSCL is distributed in the hope that it will be useful, but ;; WITHOUT ANY WARRANTY; without even the implied warranty of ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU ;; General Public License for more details. ;; ;; You should have received a copy of the GNU General Public License -;; along with this program. If not, see . +;; along with JSCL. If not, see . -(defvar *newline* (string (code-char 10))) +(/debug "loading utils.lisp!") + +(defvar *newline* " +") (defmacro concatf (variable &body form) `(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 "")) - (cond - ((null list) - "") - ((null (cdr list)) - (car list)) - (t - (concat (car list) - separator - (join (cdr list) separator))))) + (if (null list) + "" + (!reduce (lambda (s o) (concat s separator o)) + (cdr list) + (car list)))) (defun join-trailing (list &optional (separator "")) (if (null list) @@ -88,4 +85,22 @@ (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))) + + +(defun interleave (list element &optional after-last-p) + (unless (null list) + (with-collect + (collect (car list)) + (dolist (x (cdr list)) + (collect element) + (collect x)) + (when after-last-p + (collect element)))))