1(cl:defpackage :chillax.utils
4 :fun :mkhash :hashget :strcat :dequote :at))
5(in-package :chillax.utils)
8(defmacro fun (&body body)
9 "This macro puts the FUN back in FUNCTION."
10 `(lambda (&optional _) (declare (ignorable _)) ,@body))
13(defun mkhash (&rest keys-and-values &aux (table (make-hash-table :test #'equal)))
14 "Convenience function for `literal' hash table definition."
15 (loop for (key val) on keys-and-values by #'cddr do (setf (gethash key table) val)
16 finally (return table)))
18(defun hashget (hash &rest keys)
19 "Convenience function for recursively accessing hash tables."
20 (reduce (lambda (h k) (gethash k h)) keys :initial-value hash))
22(define-compiler-macro hashget (hash &rest keys)
24 (let ((hash-sym (make-symbol "HASH"))
25 (key-syms (loop for i below (length keys)
26 collect (make-symbol (format nil "~:@(~:R~)-KEY" i)))))
27 `(let ((,hash-sym ,hash)
28 ,@(loop for key in keys for sym in key-syms
29 collect `(,sym ,key)))
30 ,(reduce (lambda (hash key) `(gethash ,key ,hash))
31 key-syms :initial-value hash-sym)))))
33(defun (setf hashget) (new-value hash key &rest more-keys)
34 "Uses the last key given to hashget to insert NEW-VALUE into the hash table
35returned by the second-to-last key.
36tl;dr: DWIM SETF function for HASHGET."
38 (setf (gethash (car (last more-keys))
39 (apply #'hashget hash key (butlast more-keys)))
41 (setf (gethash key hash) new-value)))
44(defun strcat (string &rest more-strings)
45 (apply #'concatenate 'string string more-strings))
47(defun dequote (string)
48 (let ((len (length string)))
49 (if (and (> len 1) (starts-with #\" string) (ends-with #\" string))
50 (subseq string 1 (- len 1))
56(defgeneric at (doc &rest keys))
57(defgeneric (setf at) (new-value doc key &rest more-keys))
59(defmethod at ((doc hash-table) &rest keys)
60 (apply #'hashget doc keys))
61(defmethod (setf at) (new-value (doc hash-table) key &rest more-keys)
62 (apply #'(setf hashget) new-value doc key more-keys))
64(defmethod at ((doc list) &rest keys)
65 (reduce (lambda (alist key)
66 (cdr (assoc key alist :test #'equal)))
67 keys :initial-value doc))
68(defmethod (setf at) (new-value (doc list) key &rest more-keys)
70 (setf (cdr (assoc (car (last more-keys))
71 (apply #'at doc key (butlast more-keys))
74 (setf (cdr (assoc key doc :test #'equal)) new-value)))
77(defun @ (doc &rest keys)
78 (apply #'at doc keys))
79(defun (setf @) (new-value doc key &rest more-keys)
80 (apply #'(setf at) new-value doc key more-keys))
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list