git.fiddlerwoaroof.com
Browse code

feat(utils): add generic FUNCTIONALIZE to turn values into functions

Edward authored on 31/12/2020 06:03:56
Showing 3 changed files
... ...
@@ -7,6 +7,23 @@
7 7
          data-lens:compress-runs data-lens:combine-matching-lists
8 8
          data-lens:juxt data-lens:element data-lens:sorted))
9 9
 
10
+(defgeneric functionalize (it)
11
+  (:method ((it hash-table))
12
+    (lambda (key &optional default)
13
+      (gethash key it default)))
14
+  (:method ((it vector))
15
+    (lambda (idx &optional default)
16
+      (let ((present-p (and (>= idx 0)
17
+                            (< idx (length it)))))
18
+        (values (if present-p
19
+                    (aref it idx)
20
+                    default)
21
+                present-p))))
22
+  (:method ((it symbol))
23
+    (fdefinition it))
24
+  (:method ((it function))
25
+    it))
26
+
10 27
 ;;; TODO: consider making this wrap defalias?
11 28
 (defmacro shortcut (name function &body bound-args)
12 29
   `(eval-when (:load-toplevel :compile-toplevel :execute)
... ...
@@ -18,8 +18,7 @@
18 18
            #:compress-runs #:combine-matching-lists #:sorted #:applicable-when
19 19
            #:of-length #:of-min-length #:of-max-length #:transform-head
20 20
            #:maximizing #:zipping #:applying #:splice-elt #:transform-elt #:denest
21
-           #:op #:defalias #:<> #:<>1 #:== #:•
22
-           ))
21
+           #:op #:defalias #:<> #:<>1 #:== #:• #:suffixp #:functionalize))
23 22
 
24 23
 (defpackage :data-lens.transducers.internals
25 24
   (:use :cl)
... ...
@@ -11,7 +11,9 @@
11 11
   (:method ((seq sequence) (func symbol) init)
12 12
     (reduce func seq :initial-value init))
13 13
   (:method (seq (func symbol) init)
14
-    (reduce-generic seq (symbol-function func) init))
14
+    (reduce-generic seq
15
+                    (symbol-function func)
16
+                    init))
15 17
   (:method ((seq hash-table) (func function) init)
16 18
     (let ((acc init))
17 19
       (maphash (lambda (k v)