comparison lisp/cl-extra.el @ 4998:b46c89ccbed3

Merge.
author Aidan Kehoe <kehoea@parhasard.net>
date Sat, 06 Feb 2010 12:28:19 +0000
parents 8800b5350a13
children 6aba0daedb7c
comparison
equal deleted inserted replaced
4994:76af7fc13e81 4998:b46c89ccbed3
73 ;; XEmacs addition: enhanced numeric type coercions 73 ;; XEmacs addition: enhanced numeric type coercions
74 ((and-fboundp 'coerce-number 74 ((and-fboundp 'coerce-number
75 (memq type '(integer ratio bigfloat)) 75 (memq type '(integer ratio bigfloat))
76 (coerce-number x type))) 76 (coerce-number x type)))
77 ;; XEmacs addition: bit-vector coercion 77 ;; XEmacs addition: bit-vector coercion
78 ((eq type 'bit-vector) (if (bit-vector-p x) x 78 ((or (eq type 'bit-vector)
79 (apply 'bit-vector (append x nil)))) 79 (eq type 'simple-bit-vector))
80 (if (bit-vector-p x) x (apply 'bit-vector (append x nil))))
80 ;; XEmacs addition: weak-list coercion 81 ;; XEmacs addition: weak-list coercion
81 ((eq type 'weak-list) 82 ((eq type 'weak-list)
82 (if (weak-list-p x) x 83 (if (weak-list-p x) x
83 (let ((wl (make-weak-list))) 84 (let ((wl (make-weak-list)))
84 (set-weak-list-list wl (if (listp x) x (append x nil))) 85 (set-weak-list-list wl (if (listp x) x (append x nil)))
85 wl))) 86 wl)))
87 ((and
88 (consp type)
89 (or (eq (car type) 'vector)
90 (eq (car type) 'simple-array)
91 (eq (car type) 'simple-vector))
92 (cond
93 ((equal (cdr-safe type) '(*))
94 (coerce x 'vector))
95 ((equal (cdr-safe type) '(bit))
96 (coerce x 'bit-vector))
97 ((equal (cdr-safe type) '(character))
98 (coerce x 'string)))))
86 ((typep x type) x) 99 ((typep x type) x)
87 (t (error "Can't coerce %s to type %s" x type)))) 100 (t (error "Can't coerce %s to type %s" x type))))
88 101
89 102
90 ;;;;; Predicates. 103 ;;;;; Predicates.
210 ;; ;; bit-vectors and strings are only equalp if they're 223 ;; ;; bit-vectors and strings are only equalp if they're
211 ;; ;; zero-length: 224 ;; ;; zero-length:
212 ;; (and (equal "" y) (equal #* x))))) 225 ;; (and (equal "" y) (equal #* x)))))
213 ;; (t (equal x y))))))) 226 ;; (t (equal x y)))))))
214 227
215 ;;; Control structures. 228 ;; XEmacs; #'map, #'mapc, #'mapl, #'maplist, #'mapcon, #'some and #'every
216 229 ;; are now in C, together with #'map-into, which was never in this file.
217 (defun cl-mapcar-many (cl-func cl-seqs)
218 (if (cdr (cdr cl-seqs))
219 (let* ((cl-res nil)
220 (cl-n (apply 'min (mapcar 'length cl-seqs)))
221 (cl-i 0)
222 (cl-args (copy-sequence cl-seqs))
223 cl-p1 cl-p2)
224 (setq cl-seqs (copy-sequence cl-seqs))
225 (while (< cl-i cl-n)
226 (setq cl-p1 cl-seqs cl-p2 cl-args)
227 (while cl-p1
228 (setcar cl-p2
229 (if (consp (car cl-p1))
230 (prog1 (car (car cl-p1))
231 (setcar cl-p1 (cdr (car cl-p1))))
232 (aref (car cl-p1) cl-i)))
233 (setq cl-p1 (cdr cl-p1) cl-p2 (cdr cl-p2)))
234 (push (apply cl-func cl-args) cl-res)
235 (setq cl-i (1+ cl-i)))
236 (nreverse cl-res))
237 (let ((cl-res nil)
238 (cl-x (car cl-seqs))
239 (cl-y (nth 1 cl-seqs)))
240 (let ((cl-n (min (length cl-x) (length cl-y)))
241 (cl-i -1))
242 (while (< (setq cl-i (1+ cl-i)) cl-n)
243 (push (funcall cl-func
244 (if (consp cl-x) (pop cl-x) (aref cl-x cl-i))
245 (if (consp cl-y) (pop cl-y) (aref cl-y cl-i)))
246 cl-res)))
247 (nreverse cl-res))))
248
249 (defun map (cl-type cl-func cl-seq &rest cl-rest)
250 "Map a function across one or more sequences, returning a sequence.
251 TYPE is the sequence type to return, FUNC is the function, and SEQS
252 are the argument sequences."
253 (let ((cl-res (apply 'mapcar* cl-func cl-seq cl-rest)))
254 (and cl-type (coerce cl-res cl-type))))
255
256 (defun maplist (cl-func cl-list &rest cl-rest)
257 "Map FUNC to each sublist of LIST or LISTS.
258 Like `mapcar', except applies to lists and their cdr's rather than to
259 the elements themselves."
260 (if cl-rest
261 (let ((cl-res nil)
262 (cl-args (cons cl-list (copy-sequence cl-rest)))
263 cl-p)
264 (while (not (memq nil cl-args))
265 (push (apply cl-func cl-args) cl-res)
266 (setq cl-p cl-args)
267 (while cl-p (setcar cl-p (cdr (pop cl-p)) )))
268 (nreverse cl-res))
269 (let ((cl-res nil))
270 (while cl-list
271 (push (funcall cl-func cl-list) cl-res)
272 (setq cl-list (cdr cl-list)))
273 (nreverse cl-res))))
274
275 ;; XEmacs change: in Emacs, this function is named cl-mapc.
276 (defun mapc (cl-func cl-seq &rest cl-rest)
277 "Like `mapcar', but does not accumulate values returned by the function."
278 (if cl-rest
279 (apply 'map nil cl-func cl-seq cl-rest)
280 ;; XEmacs change: in the simplest case we call mapc-internal,
281 ;; which really doesn't accumulate any results.
282 (mapc-internal cl-func cl-seq))
283 cl-seq)
284
285 ;; XEmacs addition: FSF compatibility
286 (defalias 'cl-mapc 'mapc)
287
288 (defun mapl (cl-func cl-list &rest cl-rest)
289 "Like `maplist', but does not accumulate values returned by the function."
290 (if cl-rest
291 (apply 'maplist cl-func cl-list cl-rest)
292 (let ((cl-p cl-list))
293 (while cl-p (funcall cl-func cl-p) (setq cl-p (cdr cl-p)))))
294 cl-list)
295
296 (defun mapcan (cl-func cl-seq &rest cl-rest)
297 "Like `mapcar', but nconc's together the values returned by the function."
298 (apply 'nconc (apply 'mapcar* cl-func cl-seq cl-rest)))
299
300 (defun mapcon (cl-func cl-list &rest cl-rest)
301 "Like `maplist', but nconc's together the values returned by the function."
302 (apply 'nconc (apply 'maplist cl-func cl-list cl-rest)))
303
304 (defun some (cl-pred cl-seq &rest cl-rest)
305 "Return true if PREDICATE is true of any element of SEQ or SEQs.
306 If so, return the true (non-nil) value returned by PREDICATE."
307 (if (or cl-rest (nlistp cl-seq))
308 (catch 'cl-some
309 (apply 'map nil
310 (function (lambda (&rest cl-x)
311 (let ((cl-res (apply cl-pred cl-x)))
312 (if cl-res (throw 'cl-some cl-res)))))
313 cl-seq cl-rest) nil)
314 (let ((cl-x nil))
315 (while (and cl-seq (not (setq cl-x (funcall cl-pred (pop cl-seq))))))
316 cl-x)))
317
318 (defun every (cl-pred cl-seq &rest cl-rest)
319 "Return true if PREDICATE is true of every element of SEQ or SEQs."
320 (if (or cl-rest (nlistp cl-seq))
321 (catch 'cl-every
322 (apply 'map nil
323 (function (lambda (&rest cl-x)
324 (or (apply cl-pred cl-x) (throw 'cl-every nil))))
325 cl-seq cl-rest) t)
326 (while (and cl-seq (funcall cl-pred (car cl-seq)))
327 (setq cl-seq (cdr cl-seq)))
328 (null cl-seq)))
329 230
330 (defun notany (cl-pred cl-seq &rest cl-rest) 231 (defun notany (cl-pred cl-seq &rest cl-rest)
331 "Return true if PREDICATE is false of every element of SEQ or SEQs." 232 "Return true if PREDICATE is false of every element of SEQ or SEQs."
332 (not (apply 'some cl-pred cl-seq cl-rest))) 233 (not (apply 'some cl-pred cl-seq cl-rest)))
333 234