diff --git a/lens.lisp b/lens.lisp index d898688..b4a3564 100644 --- a/lens.lisp +++ b/lens.lisp @@ -7,6 +7,23 @@ data-lens:compress-runs data-lens:combine-matching-lists 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? (defmacro shortcut (name function &body bound-args) `(eval-when (:load-toplevel :compile-toplevel :execute) diff --git a/package.lisp b/package.lisp index f151bd1..4ee82d6 100644 --- a/package.lisp +++ b/package.lisp @@ -18,8 +18,7 @@ #:compress-runs #:combine-matching-lists #:sorted #:applicable-when #:of-length #:of-min-length #:of-max-length #:transform-head #:maximizing #:zipping #:applying #:splice-elt #:transform-elt #:denest - #:op #:defalias #:<> #:<>1 #:== #:• - )) + #:op #:defalias #:<> #:<>1 #:== #:• #:suffixp #:functionalize)) (defpackage :data-lens.transducers.internals (:use :cl) diff --git a/transducers.lisp b/transducers.lisp index df17a47..c83afa5 100644 --- a/transducers.lisp +++ b/transducers.lisp @@ -11,7 +11,9 @@ (:method ((seq sequence) (func symbol) init) (reduce func seq :initial-value 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) (let ((acc init)) (maphash (lambda (k v)