~lthms/fairy.lisp

6c6324559013a27e535d590765a32b8ba56ce484 — Thomas Letan 6 years ago 4e89036
feature: Specify a keys equality when deleting a child (optional)
1 files changed, 13 insertions(+), 6 deletions(-)

M fairy.lisp
M fairy.lisp => fairy.lisp +13 -6
@@ 78,14 78,21 @@
(defmethod add-child ((el layer) child &key with-key)
  (setf (children el) (cons (cons with-key child) (children el))))

(defmethod get-child ((el layer) key)
  (cdr (assoc key (children el))))
(defmethod get-child ((el layer) key &key test)
  (if test
      (cdr (assoc key (children el) :test test))
      (cdr (assoc key (children el)))))

(defmethod (setf get-child) (value (el layer) key)
  (setf (cdr (assoc key (children el))) value))
(defmethod (setf get-child) (value (el layer) key &key test)
  (setf (if test
            (cdr (assoc key (children el) :test test))
            (cdr (assoc key (children el))))
        value))

(defmethod delete-child ((el layer) child)
  (setf (children el) (remove child (children el) :key #'cdr)))

(defmethod delete-child-with-key ((el layer) key)
  (setf (children el) (remove key (children el) :key #'car)))
(defmethod delete-child-with-key ((el layer) key &key test)
  (if test
      (setf (children el) (remove key (children el) :key #'car :test test))
      (setf (children el) (remove key (children el) :key #'car))))