git.fiddlerwoaroof.com
Browse code

feat: add `keys` and `split-at`

Edward Langley authored on 23/04/2023 17:12:36
Showing 3 changed files
... ...
@@ -4,6 +4,15 @@
4 4
   `(with-accessors ,(ensure-mapping accessors) ,object
5 5
      ,@body))
6 6
 
7
+(defmacro keys ((op &rest args))
8
+  (multiple-value-bind (positional keywords) (split-at  '&key args)
9
+    `(,op
10
+       ,@positional
11
+       ,@(mapcan (lambda (_1)
12
+                   (list (alexandria:make-keyword _1)
13
+                         _1))
14
+                 (cdr keywords)))))
15
+
7 16
 (defmacro new (class &rest initializer-syms)
8 17
   (multiple-value-bind (required optional rest) (parse-ordinary-lambda-list initializer-syms)
9 18
     (when optional
... ...
@@ -354,3 +354,14 @@
354 354
         (when ,flag-sym
355 355
           (setf ,flag-sym nil)
356 356
           (go start)))))
357
+
358
+(defun split-at (el list &key (test #'eql) (key nil))
359
+  (if key
360
+      (loop for it on list
361
+            until (funcall test (funcall key (car it)) el)
362
+            collect (car it) into head
363
+            finally (return (values head it)))
364
+      (loop for it on list
365
+            until (funcall test (car it) el)
366
+            collect (car it) into head
367
+            finally (return (values head it)))))
... ...
@@ -33,7 +33,7 @@
33 33
            #:define-cluser-entrypoint #:new #:make-constructor #:dive
34 34
            #:empty-hash-table-like #:v-assoc #:defclass+
35 35
            #:closing #:inits #:retry-once #:hashtable-slot-mixin #:hsm-doc #:adjoinf
36
-           #:it))
36
+           #:it :keys))
37 37
 
38 38
 
39 39
 (defpackage :fwoar.lisputils.shortcuts