diff --git a/utils.lisp b/utils.lisp index 66f4cd8..aad2f17 100644 --- a/utils.lisp +++ b/utils.lisp @@ -7,7 +7,7 @@ (defparameter *-utils-lisp* t) (defparameter *-utils-depends* '(#:cl-ppcre)) -(defparameter *-utils-version* "0.1.3") +(defparameter *-utils-version* "0.1.4") ;;; -- Handle internal exporting -- @@ -63,6 +63,7 @@ ;;; --- actual (exported) code goes here --- + (defun where (expr items) (mapcan #'(lambda (x) (when (funcall expr x) (list x))) @@ -82,8 +83,8 @@ (loop for line = (funcall read-line input nil) while line do (funcall fi line))) -(defmacro strcat (&rest str) - `(apply #'concatenate (cons 'string ,@str))) +(defun strcat (&rest str) + (apply #'concatenate (cons 'string str))) (defmacro until (stmt) `(let ((ret nil)) @@ -124,6 +125,65 @@ `(let ((vv ,val)) (if (funcall ,test vv) vv ,or))) +(defun get-all-symbols () + (let ((lst '())) + (do-all-symbols (s lst) + (push s lst)) + lst)) + +(defun symbol-match (scan &optional (symbols nil)) + (let ((symbols (val-if-or symbols #'true + (get-all-symbols)))) + (where #'(lambda (x) (cl-ppcre:scan scan (write-to-string x))) + symbols))) + +(defun index (i max) + (if (< i 0) + (index (+ max i) max) + (mod i max))) + +(defun slice (seq start end) + "only works with lists i guess" + (let ((start (index start (length seq))) + (end (index end (length seq)))) + (rplacd (nthcdr end seq) nil) + (nthcdr start seq))) + +(defun flatten-top-level (li) + (mapcan #'(lambda (x) + (if (atom x) (list x) x)) + li)) + +(defun flatten (li) + (mapcan #'(lambda (x) + (if (atom x) + (list x) + (flatten x))) + li)) + +(defun strjoin (delim &rest strs) + (let ((strs (flatten-top-level strs))) + (apply #'strcat + (slice + (mapcan #'(lambda (x) + (list x delim)) + strs) + 0 -2)))) + +(defun export-struct (struct &optional (symbols nil)) + (mapc #'export (symbol-match (strcat "^" (write-to-string struct) "-") symbols))) + +(defun -export*-struct (structs &optional (symbols nil)) + (mapc #'export (symbol-match + (strcat + "^(" + (strjoin "|" (mapcar #'write-to-string structs)) + ")-") + symbols))) + +(defmacro export*-struct (&rest structs) + `(-export*-struct '(,@structs))) + (defmacro popto (li val &key (test #'eql)) "pop() list
  • until (car li) is equal to , return elements pop()ed in new list" `(loop while (not (funcall ,test (car ,li) ,val))