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

This commit is contained in:
Edward
2020-12-30 22:03:56 -08:00
parent 1310d70f9c
commit 07b0c02806
3 changed files with 21 additions and 3 deletions

View File

@ -7,6 +7,23 @@
data-lens:compress-runs data-lens:combine-matching-lists data-lens:compress-runs data-lens:combine-matching-lists
data-lens:juxt data-lens:element data-lens:sorted)) data-lens:juxt data-lens:element data-lens:sorted))
(defgeneric functionalize (it)
(:method ((it hash-table))
(lambda (key &optional default)
(gethash key it default)))
(:method ((it vector))
(lambda (idx &optional default)
(let ((present-p (and (>= idx 0)
(< idx (length it)))))
(values (if present-p
(aref it idx)
default)
present-p))))
(:method ((it symbol))
(fdefinition it))
(:method ((it function))
it))
;;; TODO: consider making this wrap defalias? ;;; TODO: consider making this wrap defalias?
(defmacro shortcut (name function &body bound-args) (defmacro shortcut (name function &body bound-args)
`(eval-when (:load-toplevel :compile-toplevel :execute) `(eval-when (:load-toplevel :compile-toplevel :execute)

View File

@ -18,8 +18,7 @@
#:compress-runs #:combine-matching-lists #:sorted #:applicable-when #:compress-runs #:combine-matching-lists #:sorted #:applicable-when
#:of-length #:of-min-length #:of-max-length #:transform-head #:of-length #:of-min-length #:of-max-length #:transform-head
#:maximizing #:zipping #:applying #:splice-elt #:transform-elt #:denest #:maximizing #:zipping #:applying #:splice-elt #:transform-elt #:denest
#:op #:defalias #:<> #:<>1 #:== #: #:op #:defalias #:<> #:<>1 #:== #: #:suffixp #:functionalize))
))
(defpackage :data-lens.transducers.internals (defpackage :data-lens.transducers.internals
(:use :cl) (:use :cl)

View File

@ -11,7 +11,9 @@
(:method ((seq sequence) (func symbol) init) (:method ((seq sequence) (func symbol) init)
(reduce func seq :initial-value init)) (reduce func seq :initial-value init))
(:method (seq (func symbol) init) (:method (seq (func symbol) init)
(reduce-generic seq (symbol-function func) init)) (reduce-generic seq
(symbol-function func)
init))
(:method ((seq hash-table) (func function) init) (:method ((seq hash-table) (func function) init)
(let ((acc init)) (let ((acc init))
(maphash (lambda (k v) (maphash (lambda (k v)