On ECL, exclude function that is incompatible with ECL
[kmrcl.git] / lists.lisp
1 ;;;; -*- Mode: Lisp; Syntax: ANSI-Common-Lisp; Base: 10 -*-
2 ;;;; *************************************************************************
3 ;;;; FILE IDENTIFICATION
4 ;;;;
5 ;;;; Name:          lists.lisp
6 ;;;; Purpose:       Functions for lists for KMRCL package
7 ;;;; Programmer:    Kevin M. Rosenberg
8 ;;;; Date Started:  Apr 2000
9 ;;;;
10 ;;;; $Id$
11 ;;;;
12 ;;;; This file, part of KMRCL, is Copyright (c) 2002 by Kevin M. Rosenberg
13 ;;;;
14 ;;;; KMRCL users are granted the rights to distribute and use this software
15 ;;;; as governed by the terms of the Lisp Lesser GNU Public License
16 ;;;; (http://opensource.franz.com/preamble.html), also known as the LLGPL.
17 ;;;; *************************************************************************
18
19 (in-package #:kmrcl)
20
21 (defun mklist (obj)
22   "Make into list if atom"
23   (if (listp obj) obj (list obj)))
24
25 (defun map-and-remove-nils (fn lst)
26   "mao a list by function, eliminate elements where fn returns nil"
27   (let ((acc nil))
28     (dolist (x lst (nreverse acc))
29       (let ((val (funcall fn x)))
30         (when val (push val acc))))))
31
32 (defun filter (fn lst)
33   "Filter a list by function, eliminate elements where fn returns nil"
34   (let ((acc nil))
35     (dolist (x lst (nreverse acc))
36       (when (funcall fn x)
37         (push x acc)))))
38
39 (defun appendnew (l1 l2)
40   "Append two lists, filtering out elem from second list that are already in first list"
41   (dolist (elem l2 l1)
42     (unless (find elem l1)
43       (setq l1 (append l1 (list elem))))))
44
45 (defun remove-from-tree-if (pred tree &optional atom-processor)
46   "Strip from tree of atoms that satistify predicate"
47   (if (atom tree)
48       (unless (funcall pred tree)
49         (if atom-processor
50             (funcall atom-processor tree)
51           tree))
52     (let ((car-strip (remove-from-tree-if pred (car tree) atom-processor))
53           (cdr-strip (remove-from-tree-if pred (cdr tree) atom-processor)))
54       (cond
55        ((and car-strip (atom (cadr tree)) (null cdr-strip))
56         (list car-strip))
57        ((and car-strip cdr-strip)
58         (cons car-strip cdr-strip))
59        (car-strip
60         car-strip)
61        (cdr-strip
62         cdr-strip)))))
63
64 (defun find-tree (sym tree)
65   "Finds an atom as a car in tree and returns cdr tree at that positions"
66   (if (or (null tree) (atom tree))
67       nil
68     (if (eql sym (car tree))
69         (cdr tree)
70       (aif (find-tree sym (car tree))
71           it
72         (aif (find-tree sym (cdr tree))
73             it
74             nil)))))
75
76 (defun flatten (lis)
77   (cond ((atom lis) lis)
78         ((listp (car lis))
79          (append (flatten (car lis)) (flatten (cdr lis))))
80         (t (append (list (car lis)) (flatten (cdr lis))))))
81
82 ;;; Keyword functions
83
84 ;; ECL doesn't allow FOR clauses after UNTIL.
85 #-ecl
86 (defun remove-keyword (key arglist)
87   (loop for sublist = arglist then rest until (null sublist)
88         for (elt arg . rest) = sublist
89         unless (eq key elt) append (list elt arg)))
90
91 (defun remove-keywords (key-names args)
92   (loop for ( name val ) on args by #'cddr
93         unless (member (symbol-name name) key-names
94                        :key #'symbol-name :test 'equal)
95         append (list name val)))
96
97 (defun mapappend (func seq)
98   (apply #'append (mapcar func seq)))
99
100 (defun mapcar-append-string-nontailrec (func v)
101   "Concatenate results of mapcar lambda calls"
102   (aif (car v)
103        (concatenate 'string (funcall func it)
104                     (mapcar-append-string-nontailrec func (cdr v)))
105        ""))
106
107
108 (defun mapcar-append-string (func v &optional (accum ""))
109   "Concatenate results of mapcar lambda calls"
110   (aif (car v)
111        (mapcar-append-string
112         func
113         (cdr v)
114         (concatenate 'string accum (funcall func it)))
115        accum))
116
117 (defun mapcar2-append-string-nontailrec (func la lb)
118   "Concatenate results of mapcar lambda call's over two lists"
119   (let ((a (car la))
120         (b (car lb)))
121     (if (and a b)
122       (concatenate 'string (funcall func a b)
123                    (mapcar2-append-string-nontailrec func (cdr la) (cdr lb)))
124       "")))
125
126 (defun mapcar2-append-string (func la lb &optional (accum ""))
127   "Concatenate results of mapcar lambda call's over two lists"
128   (let ((a (car la))
129         (b (car lb)))
130     (if (and a b)
131         (mapcar2-append-string func (cdr la)  (cdr lb)
132                                (concatenate 'string accum (funcall func a b)))
133       accum)))
134
135 (defun append-sublists (list)
136   "Takes a list of lists and appends all sublists"
137   (let ((results (car list)))
138     (dolist (elem (cdr list) results)
139       (setq results (append results elem)))))
140
141
142 ;; alists and plists
143
144 (defun alist-elem-p (elem)
145   (and (consp elem) (atom (car elem)) (atom (cdr elem))))
146
147 (defun alistp (alist)
148   (when (listp alist)
149     (dolist (elem alist)
150       (unless (alist-elem-p elem)
151         (return-from alistp nil)))
152     t))
153
154 (defmacro update-alist (akey value alist &key (test '#'eql) (key '#'identity))
155   "Macro to support below (setf get-alist)"
156   (let ((elem (gensym "ELEM-"))
157         (val (gensym "VAL-")))
158     `(let ((,elem (assoc ,akey ,alist :test ,test :key ,key))
159            (,val ,value))
160        (cond
161         (,elem
162          (setf (cdr ,elem) ,val))
163         (,alist
164          (setf (cdr (last ,alist)) (list (cons ,akey ,val))))
165          (t
166           (setf ,alist (list (cons ,akey ,val)))))
167        ,alist)))
168
169 (defun get-alist (key alist &key (test #'eql))
170   (cdr (assoc key alist :test test)))
171
172 (defun (setf get-alist) (value key alist &key (test #'eql))
173   "This won't work if the alist is NIL."
174   (update-alist key value alist :test test)
175   value)
176
177 (defun alist-plist (alist)
178   (apply #'append (mapcar #'(lambda (x) (list (car x) (cdr x))) alist)))
179
180 (defun plist-alist (plist)
181   (do ((alist '())
182        (pl plist (cddr pl)))
183       ((null pl) alist)
184     (setq alist (acons (car pl) (cadr pl) alist))))
185
186 (defmacro update-plist (pkey value plist &key (test '#'eql))
187   "Macro to support below (setf get-alist)"
188   (let ((pos (gensym)))
189     `(let ((,pos (member ,pkey ,plist :test ,test)))
190        (if ,pos
191            (progn
192              (setf (cadr ,pos) ,value)
193              ,plist)
194          (setf ,plist (append ,plist (list ,pkey ,value)))))))
195
196
197 (defun unique-slot-values (list slot &key (test 'eql))
198   (let ((uniq '()))
199     (dolist (item list (nreverse uniq))
200       (let ((value (slot-value item slot)))
201         (unless (find value uniq :test test)
202           (push value uniq))))))
203
204
205