unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
From: Augusto Stoffel <arstoffel@gmail.com>
To: 62068@debbugs.gnu.org
Cc: "Basil L. Contovounesios" <contovob@tcd.ie>
Subject: bug#62068: 29.0.60; map-elt and map-insert for nested structures
Date: Thu, 09 Mar 2023 09:16:30 +0100	[thread overview]
Message-ID: <875ybafig1.fsf@gmail.com> (raw)

I would like to suggest the following two functions to deal with nested
maps.  Let me know if you would like to see a patch.

--8<---------------cut here---------------start------------->8---
(defun map-elt-in (map keys &optional default)
  "Look up a nested sequence of KEYS in MAP and return its associated value.
KEYS is any sequence type supported by `seq'.  If a key is not
found in any intermediate step, return DEFAULT, which defaults to
nil."
  (catch 'map--break
    (seq-reduce (lambda (m k)
                  (let ((v (map-elt m k 'map--default)))
                    (if (eq v 'map--default)
                        (throw 'map--break default)
                      v)))
                keys
                map)))
--8<---------------cut here---------------end--------------->8---

Examples:

(map-elt-in '(x (y (z 1))) '(x y))
=> (z 1)
(map-elt-in '(x (y (z 1))) '(x y z))
=> 1
(map-elt-in '(x (y (z 1))) '(x y zz))
=> nil
(map-elt-in '(x (y (z 1))) '(x yy zz) 99)
=> 99

--8<---------------cut here---------------start------------->8---
(defun map-insert-in (map keys value &optional default-type)
  "Return a new map like MAP except that it the nested KEYS with VALUE.
This does not modify MAP.  KEYS is any sequence type supported by
`seq'.  If intermediate values are missing, they are created with
DEFAULT-TYPE."
  (when (seq-empty-p keys)
    (error "Keys sequence must not be empty."))
  (named-let recur ((map map) (keys keys))
    (let* ((k (seq-first keys))
           (ks (seq-rest keys))
           (new (if (seq-empty-p ks)
                   (map-insert map k value)
                 (map-insert map k (recur (map-elt map k) ks)))))
      (if (and (not map) default-type) (map-into new default-type) new))))
--8<---------------cut here---------------end--------------->8---

Examples:
(map-insert-in nil '(x y z) 1)
=> ((x (y (z . 1))))

(map-insert-in nil '(x y z) 1 'plist)
=> (x (y (z 1)))

(map-insert-in '(x 2 y (z 3)) '(y a b) 1 'plist)
=> (y (a (b 1) z 3) x 2 y (z 3))
;; Looks funny, but see bug#62067





             reply	other threads:[~2023-03-09  8:16 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-03-09  8:16 Augusto Stoffel [this message]
2023-03-10  1:18 ` bug#62068: 29.0.60; map-elt and map-insert for nested structures Michael Heerdegen
2023-03-10  7:09   ` Augusto Stoffel
2023-03-11  3:22     ` Michael Heerdegen
2023-03-11  7:51       ` Augusto Stoffel

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=875ybafig1.fsf@gmail.com \
    --to=arstoffel@gmail.com \
    --cc=62068@debbugs.gnu.org \
    --cc=contovob@tcd.ie \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).