[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: cond*
From: |
Adam Porter |
Subject: |
Re: cond* |
Date: |
Sun, 17 Dec 2023 23:41:22 -0600 |
User-agent: |
Mozilla Thunderbird |
To finish the design of cond*, we need to find what features
needed to be added so that it is rare to encounter a pcase
that can't be replaced cleanly. Would people please tell me
about possible examples?
Well, one of the most common things I do with pcase is to destructure
alists, plists, and structs, often nested at multiple levels. For example:
(defun ement-ignore-user (user-id session &optional unignore-p)
"Ignore USER-ID on SESSION.
If UNIGNORE-P (interactively, with prefix), un-ignore USER."
(interactive (list (ement-complete-user-id)
(ement-complete-session)
current-prefix-arg))
;; NOTE: Destructures account-data slot of ement-session struct.
(pcase-let* (((cl-struct ement-session account-data) session)
;; NOTE: Destructures alist nested in another alist and
;; binds to a variable of a different name than the key.
((map ('content (map ('ignored_users ignored-users))))
(cl-find "m.ignored_user_list" account-data
:key (lambda (event) (alist-get 'type event))
:test #'equal)))
...))
(defun ement--room-in-space-p (room space)
"Return non-nil if ROOM is in SPACE on SESSION."
;; NOTE: Destructures slots in two structs to alternative variable
;; names, and destructures an alist key in each struct's `local'
;; slot to a variable of the same name.
(pcase-let* (((cl-struct ement-room (id parent-id) (local (map
children))) space)
((cl-struct ement-room (id child-id) (local (map
parents))) room))
...))
No alternative to pcase that omitted these features could take its place
for me. In a large package like Ement.el, where function after function
destructures similar objects in similar ways, the benefit of the
consistency, conciseness, and flexibility offered by pcase can't be
understated.
As well, pcase is easily extended with its built-in flexibility, e.g.
how easily support was added for the cl-type based pattern. It's a very
well-designed system.
Then there are pcase-lambda, pcase-dolist, and pcase-setq, all of which
use the same underlying facilities and patterns. It's a coherent
system: learning pcase opens up this world of useful forms. I would not
want to give up these powerful tools which make my programs better and
my programming more enjoyable.
- Re: cond*, (continued)
- Re: cond*, Philip Kaludercic, 2023/12/22
- Re: cond*, Richard Stallman, 2023/12/24
- Re: cond*, Philip Kaludercic, 2023/12/25
- Re: cond*, Richard Stallman, 2023/12/26
- Re: cond*, Philip Kaludercic, 2023/12/27
- Re: cond*, Richard Stallman, 2023/12/28
- RE: [External] : Re: cond*, Drew Adams, 2023/12/19
- Re: [External] : Re: cond*, Richard Stallman, 2023/12/20
- Re: [External] : Re: cond*, Richard Stallman, 2023/12/20
- cond*, Richard Stallman, 2023/12/17
- Re: cond*,
Adam Porter <=
- Re: cond*, Richard Stallman, 2023/12/20
- Re: Instead of pcase, Richard Stallman, 2023/12/15
- Re: Instead of pcase, Adam Porter, 2023/12/16
- Re: Instead of pcase, Richard Stallman, 2023/12/19
- Re: Instead of pcase, Adam Porter, 2023/12/20
- Re: Instead of pcase, Richard Stallman, 2023/12/22
- Re: Instead of pcase, Ihor Radchenko, 2023/12/25
- Re: Instead of pcase, Richard Stallman, 2023/12/26
- Re: Instead of pcase, Ihor Radchenko, 2023/12/28
- Re: Instead of pcase, Richard Stallman, 2023/12/29