From b9836a7f264b98265cb185826d15b75616096e1f Mon Sep 17 00:00:00 2001 From: Avril Date: Mon, 7 Jun 2021 15:14:52 +0100 Subject: [PATCH] added switch added split-string --- flan-utils.lisp | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/flan-utils.lisp b/flan-utils.lisp index d3999aa..139ea7a 100644 --- a/flan-utils.lisp +++ b/flan-utils.lisp @@ -576,6 +576,27 @@ ; --- others +(defmacro switch (value &body conds) + "Cond `eql' for value to each first element of `conds', with the result as the 2nd element. If `conds' is an atom, it is treated as the default condition" + (let* ((value-name (gensym)) + (exprs (mapcar #'(lambda (pair) + (if (atom pair) + `(t ,pair) + `((eql ,value-name ,(car pair)) ,(cadr pair)))) conds))) + `(let ((,value-name ,value)) + ,(cons 'cond exprs)))) + +(defun split-string (string &optional sep) + "Split a string by this seperator (or `:whitespace', if not provided" + (let* ((sep (or (switch sep + (nil "\\s") + nil) + (cl-ppcre:quote-meta-chars sep))) + (lst (cl-ppcre:split sep string))) + (values (where #'(lambda (str) (> (length str) 0)) lst) + sep))) + + (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))