From e46dea130e2eb36cb0cc1956a100a0497981dc42 Mon Sep 17 00:00:00 2001 From: Colin Okay Date: Tue, 1 Nov 2022 09:58:17 -0500 Subject: Add: pluck-nested and get-nested --- derrida.lisp | 16 ++++++++++++++++ package.lisp | 4 +++- 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/derrida.lisp b/derrida.lisp index c9bd495..442c890 100644 --- a/derrida.lisp +++ b/derrida.lisp @@ -86,3 +86,19 @@ And would return else collect `(,term (getf ,plist-var ',term))))) `(let ((,plist-var ,plist)) (symbol-macrolet ,macrolet-bindings ,@body)))) + + +(defun get-nested (plist-tree &rest indicators) + "PLIST-TREE is plist some of whose values are also + PLISTS. INDICATORS are keyes to the plists." + (if (or (null plist-tree) (null indicators)) + plist-tree + (apply #'get-nested (getf plist-tree (car indicators)) (cdr indicators)))) + +(defmacro pluck-nested (keypaths plist &body body) + "Pluck nested binds variables to paths into a plist tree." + (let ((tmp-plist (gensym "plist"))) + `(let ((,tmp-plist ,plist)) + (let ,(loop for (var . path) in keypaths + collect `(,var (get-nested ,tmp-plist ,@path))) + ,@body)))) diff --git a/package.lisp b/package.lisp index 62377cb..8484a30 100644 --- a/package.lisp +++ b/package.lisp @@ -3,4 +3,6 @@ (defpackage #:derrida (:use #:cl) (:export #:with-plist - #:with-alist)) + #:with-alist + #:get-nested + #:pluck-nested)) -- cgit v1.2.3