From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Augusto Stoffel Newsgroups: gmane.emacs.bugs Subject: bug#62068: 29.0.60; map-elt and map-insert for nested structures Date: Thu, 09 Mar 2023 09:16:30 +0100 Message-ID: <875ybafig1.fsf@gmail.com> Mime-Version: 1.0 Content-Type: text/plain Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="8188"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: Gnus/5.13 (Gnus v5.13) Cc: "Basil L. Contovounesios" To: 62068@debbugs.gnu.org Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Thu Mar 09 09:17:21 2023 Return-path: Envelope-to: geb-bug-gnu-emacs@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1paBSj-0001yC-4T for geb-bug-gnu-emacs@m.gmane-mx.org; Thu, 09 Mar 2023 09:17:21 +0100 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1paBSW-0002ts-65; Thu, 09 Mar 2023 03:17:09 -0500 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1paBSR-0002ta-C4 for bug-gnu-emacs@gnu.org; Thu, 09 Mar 2023 03:17:04 -0500 Original-Received: from debbugs.gnu.org ([209.51.188.43]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1paBSQ-0008Ig-Ts for bug-gnu-emacs@gnu.org; Thu, 09 Mar 2023 03:17:03 -0500 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1paBSQ-0004oi-7m; Thu, 09 Mar 2023 03:17:02 -0500 X-Loop: help-debbugs@gnu.org Resent-From: Augusto Stoffel Original-Sender: "Debbugs-submit" Resent-CC: contovob@tcd.ie, bug-gnu-emacs@gnu.org Resent-Date: Thu, 09 Mar 2023 08:17:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: report 62068 X-GNU-PR-Package: emacs X-Debbugs-Original-To: bug-gnu-emacs@gnu.org X-Debbugs-Original-Xcc: "Basil L. Contovounesios" Original-Received: via spool by submit@debbugs.gnu.org id=B.167834980418486 (code B ref -1); Thu, 09 Mar 2023 08:17:02 +0000 Original-Received: (at submit) by debbugs.gnu.org; 9 Mar 2023 08:16:44 +0000 Original-Received: from localhost ([127.0.0.1]:50834 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1paBS7-0004o5-Ld for submit@debbugs.gnu.org; Thu, 09 Mar 2023 03:16:44 -0500 Original-Received: from lists.gnu.org ([209.51.188.17]:37080) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1paBS5-0004nv-Sg for submit@debbugs.gnu.org; Thu, 09 Mar 2023 03:16:42 -0500 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1paBS5-0002si-GY for bug-gnu-emacs@gnu.org; Thu, 09 Mar 2023 03:16:41 -0500 Original-Received: from mail-ed1-x536.google.com ([2a00:1450:4864:20::536]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1paBRy-0008DB-Vv for bug-gnu-emacs@gnu.org; Thu, 09 Mar 2023 03:16:36 -0500 Original-Received: by mail-ed1-x536.google.com with SMTP id i34so3692302eda.7 for ; Thu, 09 Mar 2023 00:16:33 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; t=1678349792; h=mime-version:user-agent:message-id:date:subject:to:from:from:to:cc :subject:date:message-id:reply-to; bh=aRitDrsyUtCym2Psp9yyHod0c8oUhrgpseWMLQnYZSE=; b=OOkhRWSueX+RTubJhAeTzOcpEBGXlLJ31G7sP3kiH34R3Lpdrgs8rHlDlEyj2YYe+M LDDPBPBJUx7qgt/TpG+0Oy7xAFE9860OCrFnqmcsgZ084B8Nu8MH2wFJ9ZZy4fbBKsu6 /x7YfsoF4VmPFmGnoP9im+lBsZ0z/oV7Lk7FwmehcyRCX0B5bpUtPBI3F43j3rV2rxLc fWZRhu0VLNsQjHZIuhZZp8dM8jI/lOWrAQyPWBlERMSV6mDQNzCyR7CX4lSYyWruOTvK k/ZMu7+06/MvRZyFhgBZcd+x+7WCeb+2K9PDq8+QIy9O5yb2UaDwRQNNXYrULSg4eKuV 0W7w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1678349792; h=mime-version:user-agent:message-id:date:subject:to:from :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=aRitDrsyUtCym2Psp9yyHod0c8oUhrgpseWMLQnYZSE=; b=S/VIEpONjfee4DjWAb4qKWKtfpWeNv7F5KbHGIvvkM1n9Bcu7S9tb7HTP1iVH6rdlg e7dkWjNZW2Hh2amPjp+htrGhdJMfHrS1/7gRmFeQpuP4ucXAVF+LyuIompurR/5ixIhy vi9ERNkOtUgf1pgxUiN3C1gLE1+DyN/FtN+DhKPgYUZrGld/Egku7hQURcJR1mm3Rz/w Tf563jZaGaoEAKa5QlW9WkDlImw2AAx4U4MJEykO24uW92XdgaK4iv9JA2c8HGN1BKmy k/9TTXnpuiIvJIWWxyyz9lzc9xvg4waiKDiQpWW4JrpOqJmPWQRrpN11soPgPINyDOf+ iFlg== X-Gm-Message-State: AO0yUKUnBAI2QxunVOjSWqPhpaD4Co54jjVaSz554c6CTmo6ml41ZdWC l59nKpCc1bv7bCGN7YJMLrbbbHfQhHYmzA== X-Google-Smtp-Source: AK7set80NrQPuXAQw31fsBgeN7XwJ5AGNzfPBkJ8OkXlXXX9EKR+EATzRnAn/Bf1vE+ooKNWzT2sQw== X-Received: by 2002:a17:906:b050:b0:8b1:e791:faef with SMTP id bj16-20020a170906b05000b008b1e791faefmr20313873ejb.67.1678349792075; Thu, 09 Mar 2023 00:16:32 -0800 (PST) Original-Received: from ars3 ([2a02:8109:8ac0:56d0::8b3a]) by smtp.gmail.com with ESMTPSA id n6-20020a50cc46000000b004bcee7838c8sm9156444edi.75.2023.03.09.00.16.30 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 09 Mar 2023 00:16:31 -0800 (PST) Received-SPF: pass client-ip=2a00:1450:4864:20::536; envelope-from=arstoffel@gmail.com; helo=mail-ed1-x536.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: bug-gnu-emacs@gnu.org List-Id: "Bug reports for GNU Emacs, the Swiss army knife of text editors" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Original-Sender: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Xref: news.gmane.io gmane.emacs.bugs:257591 Archived-At: 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