-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.lisp
68 lines (56 loc) · 1.95 KB
/
util.lisp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
(in-package :synergy)
(defun chomp (str)
(string-trim '(#\Space #\Newline #\Backspace #\Tab
#\Linefeed #\Page #\Return #\Rubout)
str))
(defmacro set-assoc (location key value)
`(let ((pair (assoc ,key ,location :test #'equal)))
(if pair
(setf (cdr pair) ,value)
(setf ,location (append ,location (list (cons ,key ,value)))))))
(defun rm-assoc (alist key &optional (cmp #'eq))
(let ((pos (loop
for x from 0
for (name . v) in alist
if (funcall cmp name key)
return x)))
(when pos
(cond ((zerop pos) (setf (car alist) (cadr alist)
(cdr alist) (cddr alist)))
(t (let ((cell (nthcdr (1- pos) alist)))
(setf (cdr cell) (nthcdr (1+ pos) alist))))))
alist))
(defmacro mvlet* (bindings &body body)
(if (null bindings)
`(progn ,@body)
(let* ((binding (first bindings))
(variables (butlast binding))
(expr (car (last binding))))
`(multiple-value-bind ,variables ,expr
(mvlet* ,(rest bindings) ,@body)))))
;; (defmacro with-alist-slots (slot-key-pairs alist &body body)
;; `(symbol-macrolet
;; ,(mapcar (lambda (slot-key-pair)
;; `(,(car slot-key-pair) (cdr (assoc ,(cadr slot-key-pair) ,alist :test #'equal))))
;; slot-key-pairs)
;; ,@body))
(defmacro with-alist-slots (slot-key-pairs alist &body body)
(let ((temp-alist (gensym "TEMP-ALIST-")))
`(let ((,temp-alist ,alist))
(symbol-macrolet
,(mapcar (lambda (slot-key-pair)
`(,(car slot-key-pair) (cdr (assoc ,(cadr slot-key-pair) ,temp-alist :test #'equal))))
slot-key-pairs)
,@body))))
(defun cat (&rest args)
(format nil "~{~A~}" args))
(defun splice (lst &rest args)
(labels ((splice-on-symbol (list symbol splice-list)
(loop for item in list
append (if (eq item symbol)
splice-list
(list item)))))
(if args
(destructuring-bind (symbol value . rest) args
(apply #'splice (splice-on-symbol lst symbol value) rest))
lst)))