- Notifications
You must be signed in to change notification settings - Fork2
Open
Description
While CL has a concept of improper (dotted or circular) lists, very few standard functions support them. Today, while tinkering with my NIHuniversal let, I found myself in need of amapcar equivalent tosubst to call on adestructuring lambda list which can be dotted.
Here's a simple and relatively efficient version (needs a basic queue structure sinceloop can'tcollect infinally and doesn't give access to the raw tail cons cell anyway):
(defunmake-queue (&rest initial-contents) (let ((queue (consnil (copy-list initial-contents)))) (setf (car queue) (last queue)) queue))(declaim (inline push-queue))(defunpush-queue (obj queue) (let ((new-tail (list obj))) (setf (cdar queue) new-tail (car queue) new-tail)))(defunmapcar-dotted (funlist) (let ((queue (make-queue))) (do ((celllist (cdr cell))) ((atom cell) (progn (when cell (setf (cdar queue) (funcall fun cell))) (cdr queue))) (push-queue (funcall fun (car cell)) queue))))
Basic test:
(formatt"~@{~A~%~}" (mapcar-dotted#'identitynil) (mapcar-dotted#'identity'(1)) (mapcar-dotted#'identity'(123)) (mapcar-dotted#'identity'(12.3)));; NIL;; (1);; (1 2 3);; (1 2 . 3)
Metadata
Metadata
Assignees
Labels
No labels