unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
* bug#62068: 29.0.60; map-elt and map-insert for nested structures
@ 2023-03-09  8:16 Augusto Stoffel
  2023-03-10  1:18 ` Michael Heerdegen
  0 siblings, 1 reply; 5+ messages in thread
From: Augusto Stoffel @ 2023-03-09  8:16 UTC (permalink / raw)
  To: 62068; +Cc: Basil L. Contovounesios

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





^ permalink raw reply	[flat|nested] 5+ messages in thread

* bug#62068: 29.0.60; map-elt and map-insert for nested structures
  2023-03-09  8:16 bug#62068: 29.0.60; map-elt and map-insert for nested structures Augusto Stoffel
@ 2023-03-10  1:18 ` Michael Heerdegen
  2023-03-10  7:09   ` Augusto Stoffel
  0 siblings, 1 reply; 5+ messages in thread
From: Michael Heerdegen @ 2023-03-10  1:18 UTC (permalink / raw)
  To: Augusto Stoffel; +Cc: Basil L. Contovounesios, 62068

Augusto Stoffel <arstoffel@gmail.com> writes:

> 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.
>
> (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)))

Isn't this more or less the same as `map-nested-elt'?

Michael.





^ permalink raw reply	[flat|nested] 5+ messages in thread

* bug#62068: 29.0.60; map-elt and map-insert for nested structures
  2023-03-10  1:18 ` Michael Heerdegen
@ 2023-03-10  7:09   ` Augusto Stoffel
  2023-03-11  3:22     ` Michael Heerdegen
  0 siblings, 1 reply; 5+ messages in thread
From: Augusto Stoffel @ 2023-03-10  7:09 UTC (permalink / raw)
  To: Michael Heerdegen; +Cc: Basil L. Contovounesios, 62068

On Fri, 10 Mar 2023 at 02:18, Michael Heerdegen wrote:

> Augusto Stoffel <arstoffel@gmail.com> writes:
>
>> 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.
>>
>> (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)))
>
> Isn't this more or less the same as `map-nested-elt'?

How did I miss that?  In any case the more interesting bit is the other
function, which should then be renamed to `map-nested-insert'.

Now, `map-nested-elt' has an inconsistency regarding the DEFAULT
argument which needs to be fixed.

(map-nested-elt '(a nil) '(a) 1)
1
(map-nested-elt '((a . nil)) '(a) 1)
1
etc.

While in the other hand:
(map-elt '(a nil) 'a 1)
nil
(map-elt '((a . nil)) 'a 1)
nil
etc.

Also inconsistent with the rest of Emacs:
(let ((m (make-hash-table)))
  (puthash 'a nil m)
  (gethash 'a m 1))
nil
(alist-get 'a '((a . nil)) 1)
nil






^ permalink raw reply	[flat|nested] 5+ messages in thread

* bug#62068: 29.0.60; map-elt and map-insert for nested structures
  2023-03-10  7:09   ` Augusto Stoffel
@ 2023-03-11  3:22     ` Michael Heerdegen
  2023-03-11  7:51       ` Augusto Stoffel
  0 siblings, 1 reply; 5+ messages in thread
From: Michael Heerdegen @ 2023-03-11  3:22 UTC (permalink / raw)
  To: Augusto Stoffel; +Cc: Basil L. Contovounesios, Nicolas Petton, 62068

Augusto Stoffel <arstoffel@gmail.com> writes:

> How did I miss that?  In any case the more interesting bit is the other
> function, which should then be renamed to `map-nested-insert'.

Is that something one will really need often (honest question, I'm
really curious)?  If yes, I wonder if we should make it work as a
generalized variable.

> Now, `map-nested-elt' has an inconsistency regarding the DEFAULT
> argument which needs to be fixed.
>
> (map-nested-elt '(a nil) '(a) 1)
> 1
> (map-nested-elt '((a . nil)) '(a) 1)
> 1
> etc.
>
> While in the other hand:
> (map-elt '(a nil) 'a 1)
> nil
> (map-elt '((a . nil)) 'a 1)
> nil
> etc.
> [...]

That's a good point indeed.  However, `map-nested-elt' exists since the
beginning of the library in 2015, so it could be that existing code
relies on the behavior (maybe that's why you wanted to add a new
function?).  But it's a terrible inconsistency.

CC'ing also Nicolas.

Should we maybe just add Augusto's version and deprecate the existing
function?


Thanks,

Michael.





^ permalink raw reply	[flat|nested] 5+ messages in thread

* bug#62068: 29.0.60; map-elt and map-insert for nested structures
  2023-03-11  3:22     ` Michael Heerdegen
@ 2023-03-11  7:51       ` Augusto Stoffel
  0 siblings, 0 replies; 5+ messages in thread
From: Augusto Stoffel @ 2023-03-11  7:51 UTC (permalink / raw)
  To: Michael Heerdegen; +Cc: Basil L. Contovounesios, Nicolas Petton, 62068

On Sat, 11 Mar 2023 at 04:22, Michael Heerdegen wrote:

> Augusto Stoffel <arstoffel@gmail.com> writes:
>
>> How did I miss that?  In any case the more interesting bit is the other
>> function, which should then be renamed to `map-nested-insert'.
>
> Is that something one will really need often (honest question, I'm
> really curious)?

I think it's very useful when you need it :-).  Actually, I wouldn't use
map-elt in any code, because I always know what type I'm dealing with.
I would just turn to this library for special maneuvers that the basic
API doesn't offer.

>  If yes, I wonder if we should make it work as a
> generalized variable.

Maybe, but that's a separate story.  First and most importantly, in my
opinion, the library should work right on values (as opposed to places).

>> Now, `map-nested-elt' has an inconsistency regarding the DEFAULT
>> argument which needs to be fixed.
>>
>> (map-nested-elt '(a nil) '(a) 1)
>> 1
>> (map-nested-elt '((a . nil)) '(a) 1)
>> 1
>> etc.
>>
>> While in the other hand:
>> (map-elt '(a nil) 'a 1)
>> nil
>> (map-elt '((a . nil)) 'a 1)
>> nil
>> etc.
>> [...]
>
> That's a good point indeed.  However, `map-nested-elt' exists since the
> beginning of the library in 2015, so it could be that existing code
> relies on the behavior (maybe that's why you wanted to add a new
> function?).

(No, the reason was just pure oversight.  Maybe "elt" is a weird word?
Why isn't it `map-get' and `map-nested-get'?)

>  But it's a terrible inconsistency.

There are more consistency issues in this library, see bug#62067 and
bug#62117.  Emacs has its quirks and one of the selling points of map.el
is to be a more consistent layer on top of the basic APIs, so I'd say
this need to be fixed.





^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2023-03-11  7:51 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-03-09  8:16 bug#62068: 29.0.60; map-elt and map-insert for nested structures Augusto Stoffel
2023-03-10  1:18 ` Michael Heerdegen
2023-03-10  7:09   ` Augusto Stoffel
2023-03-11  3:22     ` Michael Heerdegen
2023-03-11  7:51       ` Augusto Stoffel

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).