Lisp: Accessing recursive hashes with syntactic sugar

蓝咒 提交于 2021-01-28 00:19:20

问题


I'm trying to build a function (or macro) to ease the getting and setting of data deep in a hash table (meaning, a hash within a hash, within a hash, etc). I don't think I can do it with a macro, and I'm not sure how to do it with eval. I'd like to be able to do the following:

(gethashdeep *HEROES* "Avengers" "Retired" "Tony Stark")

and have that return "Iron Man"

The hashes are all created with:

(setf hashtablename (make-hash-table :test 'equal))

and populated from there.

I can do the following, but would like to abstract it so I can programmatically pull a value from an arbitrary depth:

;;pulling from a hash that's 2 deep
(gethash "Tony Stark" (gethash "Avengers" *HEROES*))

update - my go at it:

(defun getdeephash (hashpath h k)
  (let* ((rhashpath (reverse hashpath))
    (hashdepth (list-length hashpath))
    (hashcommand (concatenate 'string "(gethash \"" k "\"")))
   (loop for i from 1 to hashdepth
      do (setf hashcommand (concatenate 'string hashcommand "(gethash \"" (nth (- i 1) rhashpath) "\"")))
      (setf hashcommand (concatenate 'string  hashcommand " "  h (make-string (- hashdepth 0) :initial-element #\Right_Parenthesis) ")"))
      (values hashcommand)))

回答1:


It's a one liner with the Access library:

(ql:quickload "access")

We define the *heroes* hash table (as in Xach's example):

(defun table (&rest keys-and-values &key &allow-other-keys)
  (let ((table (make-hash-table :test 'equal)))
    (loop for (key value) on keys-and-values by #'cddr
          do (setf (gethash key table) value))
    table))
TABLE

(defparameter *heroes*
  (table "Avengers"
         (table "Retired" (table "Tony Stark" "Iron Man")
                "Active" (table "Bruce Banner" "Hulk"))))

Usually we use access:access for a consistent access to diverse data structures (alist, plist, hash table, objects,…). For a nested access we use access:accesses (plural):

(access:accesses *heroes* "Avengers" "Retired" "Tony Stark")
"Iron Man"

Besides, we can setf it:

(setf (access:accesses *heroes* "Avengers" "Retired" "Tony Stark") "me")
"me"

It is a battle tested library, since it is the core of the Djula template library, one of the most downloaded Quicklisp libraries.

my blog post: https://lisp-journey.gitlab.io/blog/generice-consistent-access-of-data-structures-dotted-path/




回答2:


There's nothing fancy needed for nested table lookup.

One possible definition of gethash-deep:

(defun gethash-deep (value &rest keys)
  (if (or (endp keys)
          (not (hash-table-p value)))
      value
      (apply #'gethash-deep
             (gethash (first keys) value)
             (rest keys))))

Example use:

(defun table (&rest keys-and-values &key &allow-other-keys)
  (let ((table (make-hash-table :test 'equal)))
    (loop for (key value) on keys-and-values by #'cddr
          do (setf (gethash key table) value))
    table))

(defparameter *heroes*
  (table "Avengers"
         (table "Retired" (table "Tony Stark" "Iron Man")
                "Active" (table "Bruce Banner" "Hulk"))))

(gethash-deep *heroes* "Avengers" "Retired" "Tony Stark") => "Iron Man"
(gethash-deep *heroes* "Avengers" "Active" "Bruce Banner") => "Hulk"



回答3:


For an ad-hoc construct, you could use ->> from arrows:

(->> table
     (gethash "Avengers")
     (gethash "Retired")
     (gethash "Tony Stark"))

If you want to mix with other accessors (e. g. aref), you could use as-> or -<> instead to deal with the different argument orders.

If I wanted to implement it, I'd rely on the implicit checks and maybe use reduce:

(defun gethash-deep (hash-table &rest keys)
  (reduce (lambda (table key)
            (gethash key table))
          keys
          :initial-value hash-table))

Or loop:

(defun gethash-deep (table &rest keys)
  (loop :for k :in keys
        :for v := (gethash k table) :then (gethash k v)
        :finally (return v)))



回答4:


If all the keys exist this should work, the only reason gethash can't be used directly in the reduce is that it receives the inputs in the incorrect order to work well.

(defun gethashdeep (table &rest keys)
  (reduce #'(lambda (h k)
          (gethash k h)) (cdr keys)
          :initial-value (gethash (car keys) table)))

Of course it could be written as a macro and be setfable

(defmacro gethashdeep1 (table &rest keys)
  (reduce #'(lambda (h k) (list 'gethash k h)) (cdr keys)
          :initial-value (list 'gethash (car keys) table)))

Of course this has limitations and does not create hashes that do not exist.



来源:https://stackoverflow.com/questions/56589496/lisp-accessing-recursive-hashes-with-syntactic-sugar

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!