clem 0.4.1, ch-asdf 0.2.8, ch-util 0.2.2, lift 1.3.1, darcs ignored, smarkup 0.3.3
[CommonLispStat.git] / external / ch-util / src / lists.cl
blobdab0f82c64abce609c887514a34fcd8d34e6ab58
1 ;;;
2 ;;; lists.cl -- various lisp list utilities that make my life easier
3 ;;;
4 ;;; Author: Cyrus Harmon <ch-lisp@bobobeach.com>
5 ;;;
7 (in-package :ch-util)
9 ;;; Miscellaneous list utilities
11 (defun insert-before (new old list)
12 (labels ((build-list (old c &optional newlist)
13 (if c
14 (if (eq old (car c))
15 (append (reverse (cdr c)) (cons (car c) (cons new newlist)))
16 (build-list old (cdr c) (cons (car c) newlist)))
17 (cons new newlist))))
18 (reverse (build-list old list))))
20 (defun insert-before-all (new old list)
21 (labels ((build-list (old c &optional newlist)
22 (if c
23 (if (eq old (car c))
24 (build-list old (cdr c) (cons (car c) (cons new newlist)))
25 (build-list old (cdr c) (cons (car c) newlist)))
26 newlist)))
27 (reverse (build-list old list))))
29 (defun flatten (l)
30 (mapcan #'(lambda (x)
31 (cond ((null x) nil)
32 ((atom x) (list x))
33 (t (flatten x))))
34 l))
36 (defun properties (plist)
37 "Returns a list of the names properties present in plist, but
38 not the value of the properties."
39 (loop for prop in plist by #'cddr collect prop))
41 (defun remove-nulls (list)
42 (loop for l in list append (when l (list l))))
44 (defun ensure-list (l)
45 (cond ((listp l) l)
46 ((atom l) (list l))))