Fix comment
[jscl.git] / src / utils.lisp
1 ;;; utils.lisp ---
2
3 ;; Copyright (C) 2012, 2013 David Vazquez
4 ;; Copyright (C) 2012 Raimon Grau
5
6 ;; JSCL is free software: you can redistribute it and/or
7 ;; modify it under the terms of the GNU General Public License as
8 ;; published by the Free Software Foundation, either version 3 of the
9 ;; License, or (at your option) any later version.
10 ;;
11 ;; JSCL is distributed in the hope that it will be useful, but
12 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
13 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 ;; General Public License for more details.
15 ;;
16 ;; You should have received a copy of the GNU General Public License
17 ;; along with JSCL.  If not, see <http://www.gnu.org/licenses/>.
18
19 (/debug "loading utils.lisp!")
20
21 (defmacro concatf (variable &body form)
22   `(setq ,variable (concat ,variable (progn ,@form))))
23
24 ;;; This couple of helper functions will be defined in both Common
25 ;;; Lisp and in JSCL
26 (defun ensure-list (x)
27   (if (listp x)
28       x
29       (list x)))
30
31 (defun !reduce (func list initial-value)
32   (let ((result initial-value))
33     (dolist (element list result)
34       (setq result (funcall func result element)))))
35
36 ;;; Concatenate a list of strings, with a separator
37 (defun join (list &optional (separator ""))
38   (if (null list)
39       ""
40       (!reduce (lambda (s o) (concat s separator o))
41                (cdr list)
42                (car list))))
43
44 (defun join-trailing (list &optional (separator ""))
45   (if (null list)
46       ""
47       (concat (car list) separator (join-trailing (cdr list) separator))))
48
49 (defun mapconcat (func list)
50   (join (mapcar func list)))
51
52 (defun vector-to-list (vector)
53   (let ((list nil)
54         (size (length vector)))
55     (dotimes (i size (reverse list))
56       (push (aref vector i) list))))
57
58 (defun list-to-vector (list)
59   (let ((v (make-array (length list)))
60         (i 0))
61     (dolist (x list v)
62       (aset v i x)
63       (incf i))))
64
65 (defmacro awhen (condition &body body)
66   `(let ((it ,condition))
67      (when it ,@body)))
68
69 (defun integer-to-string (x)
70   (cond
71     ((zerop x)
72      "0")
73     ((minusp x)
74      (concat "-" (integer-to-string (- 0 x))))
75     (t
76      (let ((digits nil))
77        (while (not (zerop x))
78          (push (mod x 10) digits)
79          (setq x (truncate x 10)))
80        (mapconcat (lambda (x) (string (digit-char x)))
81                   digits)))))
82
83 (defun float-to-string (x)
84   #+jscl (float-to-string x)
85   #-jscl (format nil "~f" x))
86
87 (defun satisfies-test-p (x y &key key (test #'eql) testp (test-not #'eql) test-not-p)
88   (when (and testp test-not-p)
89     (error "Both test and test-not are set"))
90   (let ((key-val (if key (funcall key y) y))
91         (fn (if test-not-p (complement test-not) test)))
92     (funcall fn x key-val)))
93
94
95 (defun interleave (list element &optional after-last-p)
96   (unless (null list)
97     (with-collect
98       (collect (car list))
99       (dolist (x (cdr list))
100         (collect element)
101         (collect x))
102       (when after-last-p
103         (collect element)))))