* Check whether an object is an alist @ 2024-09-16 10:29 Heime 2024-09-17 8:45 ` Heime 0 siblings, 1 reply; 22+ messages in thread From: Heime @ 2024-09-16 10:29 UTC (permalink / raw) To: Heime via Users list for the GNU Emacs text editor Is there a function that checks whether an object is an alist. Have only found listp. ^ permalink raw reply [flat|nested] 22+ messages in thread
* Re: Check whether an object is an alist 2024-09-16 10:29 Check whether an object is an alist Heime @ 2024-09-17 8:45 ` Heime 2024-09-17 9:38 ` Manuel Giraud via Users list for the GNU Emacs text editor 0 siblings, 1 reply; 22+ messages in thread From: Heime @ 2024-09-17 8:45 UTC (permalink / raw) To: Heime; +Cc: Heime via Users list for the GNU Emacs text editor Sent with Proton Mail secure email. On Monday, September 16th, 2024 at 10:29 PM, Heime <heimeborgia@protonmail.com> wrote: > Is there a function that checks whether an object is an alist. > Have only found listp. Because I may have to come up with a solution, I have made this one. Am I missing anything ? (defun torium-alist-p (obj) "Return t if object OBJ is an association list (alist)." (and (listp obj) (not (null obj)) (every (lambda (x) (and (consp x) (not (null x)))) obj))) ^ permalink raw reply [flat|nested] 22+ messages in thread
* Re: Check whether an object is an alist 2024-09-17 8:45 ` Heime @ 2024-09-17 9:38 ` Manuel Giraud via Users list for the GNU Emacs text editor 2024-09-17 9:46 ` tomas 0 siblings, 1 reply; 22+ messages in thread From: Manuel Giraud via Users list for the GNU Emacs text editor @ 2024-09-17 9:38 UTC (permalink / raw) To: Heime; +Cc: Heime via Users list for the GNU Emacs text editor Heime <heimeborgia@protonmail.com> writes: > Sent with Proton Mail secure email. > > On Monday, September 16th, 2024 at 10:29 PM, Heime <heimeborgia@protonmail.com> wrote: > >> Is there a function that checks whether an object is an alist. >> Have only found listp. > > Because I may have to come up with a solution, I have made this one. > Am I missing anything ? > > (defun torium-alist-p (obj) > "Return t if object OBJ is an association list (alist)." > (and (listp obj) > (not (null obj)) > (every (lambda (x) > (and (consp x) > (not (null x)))) > obj))) Looks good to me. I think it should `cl-every' instead of `every' and also, maybe, nil is a valid empty alist. -- Manuel Giraud ^ permalink raw reply [flat|nested] 22+ messages in thread
* Re: Check whether an object is an alist 2024-09-17 9:38 ` Manuel Giraud via Users list for the GNU Emacs text editor @ 2024-09-17 9:46 ` tomas 2024-09-17 11:16 ` Heime 0 siblings, 1 reply; 22+ messages in thread From: tomas @ 2024-09-17 9:46 UTC (permalink / raw) To: help-gnu-emacs [-- Attachment #1: Type: text/plain, Size: 1111 bytes --] On Tue, Sep 17, 2024 at 11:38:46AM +0200, Manuel Giraud via Users list for the GNU Emacs text editor wrote: > Heime <heimeborgia@protonmail.com> writes: > > > Sent with Proton Mail secure email. > > > > On Monday, September 16th, 2024 at 10:29 PM, Heime <heimeborgia@protonmail.com> wrote: > > > >> Is there a function that checks whether an object is an alist. > >> Have only found listp. > > > > Because I may have to come up with a solution, I have made this one. > > Am I missing anything ? > > > > (defun torium-alist-p (obj) > > "Return t if object OBJ is an association list (alist)." > > (and (listp obj) > > (not (null obj)) > > (every (lambda (x) > > (and (consp x) > > (not (null x)))) > > obj))) > > Looks good to me. I think it should `cl-every' instead of `every' and > also, maybe, nil is a valid empty alist. Unless you'd like to have nil as a possible key (alists allow that, why not?): (setq al '((nil . "I am not") (t . "I am"))) (alist-get nil al) => "I am not" Cheers -- t [-- Attachment #2: signature.asc --] [-- Type: application/pgp-signature, Size: 195 bytes --] ^ permalink raw reply [flat|nested] 22+ messages in thread
* Re: Check whether an object is an alist 2024-09-17 9:46 ` tomas @ 2024-09-17 11:16 ` Heime 2024-09-17 11:23 ` tomas 0 siblings, 1 reply; 22+ messages in thread From: Heime @ 2024-09-17 11:16 UTC (permalink / raw) To: tomas; +Cc: help-gnu-emacs Sent with Proton Mail secure email. On Tuesday, September 17th, 2024 at 9:46 PM, tomas@tuxteam.de <tomas@tuxteam.de> wrote: > On Tue, Sep 17, 2024 at 11:38:46AM +0200, Manuel Giraud via Users list for the GNU Emacs text editor wrote: > > > Heime heimeborgia@protonmail.com writes: > > > > > Sent with Proton Mail secure email. > > > > > > On Monday, September 16th, 2024 at 10:29 PM, Heime heimeborgia@protonmail.com wrote: > > > > > > > Is there a function that checks whether an object is an alist. > > > > Have only found listp. > > > > > > Because I may have to come up with a solution, I have made this one. > > > Am I missing anything ? > > > > > > (defun torium-alist-p (obj) > > > "Return t if object OBJ is an association list (alist)." > > > (and (listp obj) > > > (not (null obj)) > > > (every (lambda (x) > > > (and (consp x) > > > (not (null x)))) > > > obj))) > > > > Looks good to me. I think it should `cl-every' instead of` every' and > > also, maybe, nil is a valid empty alist. > > > Unless you'd like to have nil as a possible key (alists allow that, > why not?): > > (setq al '((nil . "I am not") (t . "I am"))) > (alist-get nil al) > => "I am not" > > > Cheers > -- > t And we end up with (defun torium-alist-p (obj) "Return t if object OBJ is an association list (alist)." (and (listp obj) (cl-every (lambda (x) (or (null x) (consp x))) obj))) I am quite sure I do not have to check anything upon tho state of (car x). Agreed ? ^ permalink raw reply [flat|nested] 22+ messages in thread
* Re: Check whether an object is an alist 2024-09-17 11:16 ` Heime @ 2024-09-17 11:23 ` tomas 2024-09-17 13:51 ` Heime 0 siblings, 1 reply; 22+ messages in thread From: tomas @ 2024-09-17 11:23 UTC (permalink / raw) To: Heime; +Cc: help-gnu-emacs [-- Attachment #1: Type: text/plain, Size: 1581 bytes --] On Tue, Sep 17, 2024 at 11:16:24AM +0000, Heime wrote: > > > > > > Sent with Proton Mail secure email. > > On Tuesday, September 17th, 2024 at 9:46 PM, tomas@tuxteam.de <tomas@tuxteam.de> wrote: > > > On Tue, Sep 17, 2024 at 11:38:46AM +0200, Manuel Giraud via Users list for the GNU Emacs text editor wrote: > > > > > Heime heimeborgia@protonmail.com writes: > > > > > > > Sent with Proton Mail secure email. > > > > > > > > On Monday, September 16th, 2024 at 10:29 PM, Heime heimeborgia@protonmail.com wrote: > > > > > > > > > Is there a function that checks whether an object is an alist. > > > > > Have only found listp. > > > > > > > > Because I may have to come up with a solution, I have made this one. > > > > Am I missing anything ? > > > > > > > > (defun torium-alist-p (obj) > > > > "Return t if object OBJ is an association list (alist)." > > > > (and (listp obj) > > > > (not (null obj)) > > > > (every (lambda (x) > > > > (and (consp x) > > > > (not (null x)))) > > > > obj))) > > > > > > Looks good to me. I think it should `cl-every' instead of` every' and > > > also, maybe, nil is a valid empty alist. > > > > > > Unless you'd like to have nil as a possible key (alists allow that, > > why not?): > > > > (setq al '((nil . "I am not") (t . "I am"))) > > (alist-get nil al) > > => "I am not" Oh, wait. I just see you were checking the pair to be not null. Then it's easier: your above code seems right, just the (not (null x)) is superfluous, since (consp x) makes sure it is so. Cheers -- t [-- Attachment #2: signature.asc --] [-- Type: application/pgp-signature, Size: 195 bytes --] ^ permalink raw reply [flat|nested] 22+ messages in thread
* Re: Check whether an object is an alist 2024-09-17 11:23 ` tomas @ 2024-09-17 13:51 ` Heime 2024-09-17 13:58 ` tomas 0 siblings, 1 reply; 22+ messages in thread From: Heime @ 2024-09-17 13:51 UTC (permalink / raw) To: tomas; +Cc: help-gnu-emacs On Tuesday, September 17th, 2024 at 11:23 PM, tomas@tuxteam.de <tomas@tuxteam.de> wrote: > On Tue, Sep 17, 2024 at 11:16:24AM +0000, Heime wrote: > > > Sent with Proton Mail secure email. > > > > On Tuesday, September 17th, 2024 at 9:46 PM, tomas@tuxteam.de tomas@tuxteam.de wrote: > > > > > On Tue, Sep 17, 2024 at 11:38:46AM +0200, Manuel Giraud via Users list for the GNU Emacs text editor wrote: > > > > > > > Heime heimeborgia@protonmail.com writes: > > > > > > > > > Sent with Proton Mail secure email. > > > > > > > > > > On Monday, September 16th, 2024 at 10:29 PM, Heime heimeborgia@protonmail.com wrote: > > > > > > > > > > > Is there a function that checks whether an object is an alist. > > > > > > Have only found listp. > > > > > > > > > > Because I may have to come up with a solution, I have made this one. > > > > > Am I missing anything ? > > > > > > > > > > (defun torium-alist-p (obj) > > > > > "Return t if object OBJ is an association list (alist)." > > > > > (and (listp obj) > > > > > (not (null obj)) > > > > > (every (lambda (x) > > > > > (and (consp x) > > > > > (not (null x)))) > > > > > obj))) > > > > > > > > Looks good to me. I think it should `cl-every' instead of` every' and > > > > also, maybe, nil is a valid empty alist. > > > > > > Unless you'd like to have nil as a possible key (alists allow that, > > > why not?): > > > > > > (setq al '((nil . "I am not") (t . "I am"))) > > > (alist-get nil al) > > > => "I am not" > > > Oh, wait. I just see you were checking the pair to be not > null. Then it's easier: your above code seems right, just > the (not (null x)) is superfluous, since (consp x) makes > sure it is so. > > Cheers > -- > t Have modified the code to this (defun torium-alist-p (obj) "Return t if object OBJ is an association list (alist)." (and (listp obj) (every (lambda (x) (or (null x) (consp x))) obj))) I want to allow empty lists and empty keys. Or should I remove (null x) as well ? ^ permalink raw reply [flat|nested] 22+ messages in thread
* Re: Check whether an object is an alist 2024-09-17 13:51 ` Heime @ 2024-09-17 13:58 ` tomas 2024-09-17 14:32 ` Heime 0 siblings, 1 reply; 22+ messages in thread From: tomas @ 2024-09-17 13:58 UTC (permalink / raw) To: Heime; +Cc: help-gnu-emacs [-- Attachment #1: Type: text/plain, Size: 847 bytes --] On Tue, Sep 17, 2024 at 01:51:54PM +0000, Heime wrote: [...] > Have modified the code to this > > (defun torium-alist-p (obj) > "Return t if object OBJ is an association list (alist)." > (and (listp obj) > (every (lambda (x) > (or (null x) > (consp x))) > obj))) > > I want to allow empty lists and empty keys. Or should I remove (null x) > as well ? That depends on how strict you want to be. Have you read elisp's docs on association lists? Elisp tends to tolerate alists whose elements are not pairs. Other Lisps are stricter. Your function is a funny in-between (nil non-pairs are OK, but others are not). First try to understand what you are trying to do, then do. Note that, contrary to hashes, alists are not strictly defined. Cheers -- t [-- Attachment #2: signature.asc --] [-- Type: application/pgp-signature, Size: 195 bytes --] ^ permalink raw reply [flat|nested] 22+ messages in thread
* Re: Check whether an object is an alist 2024-09-17 13:58 ` tomas @ 2024-09-17 14:32 ` Heime 2024-09-17 15:20 ` tomas 2024-09-17 19:22 ` [External] : " Drew Adams 0 siblings, 2 replies; 22+ messages in thread From: Heime @ 2024-09-17 14:32 UTC (permalink / raw) To: tomas; +Cc: help-gnu-emacs On Wednesday, September 18th, 2024 at 1:58 AM, tomas@tuxteam.de <tomas@tuxteam.de> wrote: > On Tue, Sep 17, 2024 at 01:51:54PM +0000, Heime wrote: > > [...] > > > Have modified the code to this > > > > (defun torium-alist-p (obj) > > "Return t if object OBJ is an association list (alist)." > > (and (listp obj) > > (every (lambda (x) > > (or (null x) > > (consp x))) > > obj))) > > > > I want to allow empty lists and empty keys. Or should I remove (null x) > > as well ? > > > That depends on how strict you want to be. Have you read elisp's > docs on association lists? > > Elisp tends to tolerate alists whose elements are not pairs. Other > Lisps are stricter. Your function is a funny in-between (nil non-pairs > are OK, but others are not). > > First try to understand what you are trying to do, then do. Note that, > contrary to hashes, alists are not strictly defined. > > Cheers > -- > t Originally I tried to find something as alistp but did not find any. I would have stayed to what it would have allowed. But then I started writing my own. For what I am doing, there is always a key and value in each cell. But taking in consideration what others have been saying here about alists, I decided to be more flexible and allow empty alists and keys with nil. Then (null x) should not be there because it allows nil non-pairs. This is what I am trying to do with (defun torium-alist-p (obj) "Return t if object OBJ is an association list (alist). An alist can be empty and nil can act as a key." (and (listp obj) (every (lambda (x) (or (null x) (consp x))) obj))) ^ permalink raw reply [flat|nested] 22+ messages in thread
* Re: Check whether an object is an alist 2024-09-17 14:32 ` Heime @ 2024-09-17 15:20 ` tomas 2024-09-17 16:26 ` Heime 2024-09-17 19:22 ` [External] : " Drew Adams 1 sibling, 1 reply; 22+ messages in thread From: tomas @ 2024-09-17 15:20 UTC (permalink / raw) To: Heime; +Cc: help-gnu-emacs [-- Attachment #1: Type: text/plain, Size: 676 bytes --] On Tue, Sep 17, 2024 at 02:32:33PM +0000, Heime wrote: [...] > But taking in consideration what others have been saying here about alists, I > decided to be more flexible and allow empty alists and keys with nil. Then > (null x) should not be there because it allows nil non-pairs. OK. > This is what I am trying to do with > > (defun torium-alist-p (obj) > "Return t if object OBJ is an association list (alist). An alist > can be empty and nil can act as a key." > (and (listp obj) > (every (lambda (x) > (or (null x) ;; <== so this one goes out? > (consp x))) > obj))) Cheers -- t [-- Attachment #2: signature.asc --] [-- Type: application/pgp-signature, Size: 195 bytes --] ^ permalink raw reply [flat|nested] 22+ messages in thread
* Re: Check whether an object is an alist 2024-09-17 15:20 ` tomas @ 2024-09-17 16:26 ` Heime 2024-09-17 17:57 ` tomas 0 siblings, 1 reply; 22+ messages in thread From: Heime @ 2024-09-17 16:26 UTC (permalink / raw) To: tomas; +Cc: help-gnu-emacs On Wednesday, September 18th, 2024 at 3:20 AM, tomas@tuxteam.de <tomas@tuxteam.de> wrote: > On Tue, Sep 17, 2024 at 02:32:33PM +0000, Heime wrote: > > [...] > > > But taking in consideration what others have been saying here about alists, I > > decided to be more flexible and allow empty alists and keys with nil. Then > > (null x) should not be there because it allows nil non-pairs. > > > OK. > > > This is what I am trying to do with > > > > (defun torium-alist-p (obj) > > "Return t if object OBJ is an association list (alist). An alist > > can be empty and nil can act as a key." > > (and (listp obj) > > (every (lambda (x) > > (or (null x) ;; <== so this one goes out? > > (consp x))) > > obj))) > > > Cheers > -- > t Is (null x) the part that allows nil, a non-pair object ? ^ permalink raw reply [flat|nested] 22+ messages in thread
* Re: Check whether an object is an alist 2024-09-17 16:26 ` Heime @ 2024-09-17 17:57 ` tomas 2024-09-17 18:04 ` Heime 0 siblings, 1 reply; 22+ messages in thread From: tomas @ 2024-09-17 17:57 UTC (permalink / raw) To: Heime; +Cc: help-gnu-emacs [-- Attachment #1: Type: text/plain, Size: 630 bytes --] On Tue, Sep 17, 2024 at 04:26:19PM +0000, Heime wrote: [...] > Is (null x) the part that allows nil, a non-pair object ? Just ask Emacs: (cl-every (lambda (x) (or (null x) (consp x))) '((foo . bar) nil (baz . quux))) => t (cl-every (lambda (x) (consp x)) '((foo . bar) nil (baz . quux))) => nil Now, should you decide you prefer the second variant, there is a more concise way of writing it. This is left as an exercise for the reader. Cheers -- t [-- Attachment #2: signature.asc --] [-- Type: application/pgp-signature, Size: 195 bytes --] ^ permalink raw reply [flat|nested] 22+ messages in thread
* Re: Check whether an object is an alist 2024-09-17 17:57 ` tomas @ 2024-09-17 18:04 ` Heime 2024-09-17 18:28 ` tomas 0 siblings, 1 reply; 22+ messages in thread From: Heime @ 2024-09-17 18:04 UTC (permalink / raw) To: tomas; +Cc: help-gnu-emacs On Wednesday, September 18th, 2024 at 5:57 AM, tomas@tuxteam.de <tomas@tuxteam.de> wrote: > On Tue, Sep 17, 2024 at 04:26:19PM +0000, Heime wrote: > > [...] > > > Is (null x) the part that allows nil, a non-pair object ? > > > Just ask Emacs: > > (cl-every (lambda (x) > (or (null x) > (consp x))) > '((foo . bar) > nil > (baz . quux))) > > => t > > > (cl-every (lambda (x) > (consp x)) > '((foo . bar) > nil > (baz . quux))) > > => nil > > > Now, should you decide you prefer the second variant, there is > a more concise way of writing it. This is left as an exercise > for the reader. > > Cheers > -- > t (cl-every #'consp '((foo . bar) nil (baz . quux))) ^ permalink raw reply [flat|nested] 22+ messages in thread
* Re: Check whether an object is an alist 2024-09-17 18:04 ` Heime @ 2024-09-17 18:28 ` tomas 2024-09-17 19:20 ` Heime 0 siblings, 1 reply; 22+ messages in thread From: tomas @ 2024-09-17 18:28 UTC (permalink / raw) To: Heime; +Cc: help-gnu-emacs [-- Attachment #1: Type: text/plain, Size: 267 bytes --] On Tue, Sep 17, 2024 at 06:04:41PM +0000, Heime wrote: > On Wednesday, September 18th, 2024 at 5:57 AM, tomas@tuxteam.de <tomas@tuxteam.de> wrote: > > [...] as an exercise for the reader. > > (cl-every #'consp '((foo . bar) nil (baz . quux))) Yes. -- t [-- Attachment #2: signature.asc --] [-- Type: application/pgp-signature, Size: 195 bytes --] ^ permalink raw reply [flat|nested] 22+ messages in thread
* Re: Check whether an object is an alist 2024-09-17 18:28 ` tomas @ 2024-09-17 19:20 ` Heime 2024-09-17 19:35 ` tomas 0 siblings, 1 reply; 22+ messages in thread From: Heime @ 2024-09-17 19:20 UTC (permalink / raw) To: tomas; +Cc: help-gnu-emacs On Wednesday, September 18th, 2024 at 6:28 AM, tomas@tuxteam.de <tomas@tuxteam.de> wrote: > On Tue, Sep 17, 2024 at 06:04:41PM +0000, Heime wrote: > > > On Wednesday, September 18th, 2024 at 5:57 AM, tomas@tuxteam.de tomas@tuxteam.de wrote: > > > > [...] as an exercise for the reader. > > > > (cl-every #'consp '((foo . bar) nil (baz . quux))) > > > Yes. > -- > t Have done this small test, and even though there is a nil (or anything else, e.g. jarod), assoc still works on the other keys. This means that one can still be able to get correct values even if the object is not a complete alist. In a sense, if I am looking for keys in an object, even though it is not an alist in a strict sense, things might still work correctly. Is this the reason there does not exist an alistp ? Or because traversing the list would be expensive ? But if we want to be strict that an alist is a list of cons cells with pairs of values, then only t1 should be accepted. (let ( (t1 '((foo . bar) (alice . bob) (baz . qux))) (t2 '((foo . bar) nil (alice . bob) (baz . qux))) (t3 '((foo . bar) jarod (alice . bob) (baz . qux))) ) (message "%s" (assoc 'foo t1)) (message "%s" (assoc 'alice t2)) (message "%s" (assoc 'baz t3)) ) (foo . bar) (alice . bob) (baz . qux) ^ permalink raw reply [flat|nested] 22+ messages in thread
* Re: Check whether an object is an alist 2024-09-17 19:20 ` Heime @ 2024-09-17 19:35 ` tomas 2024-09-17 21:02 ` Heime 0 siblings, 1 reply; 22+ messages in thread From: tomas @ 2024-09-17 19:35 UTC (permalink / raw) To: Heime; +Cc: help-gnu-emacs [-- Attachment #1: Type: text/plain, Size: 976 bytes --] On Tue, Sep 17, 2024 at 07:20:22PM +0000, Heime wrote: > On Wednesday, September 18th, 2024 at 6:28 AM, tomas@tuxteam.de <tomas@tuxteam.de> wrote: > > > On Tue, Sep 17, 2024 at 06:04:41PM +0000, Heime wrote: > > > > > On Wednesday, September 18th, 2024 at 5:57 AM, tomas@tuxteam.de tomas@tuxteam.de wrote: > > > > > > [...] as an exercise for the reader. > > > > > > (cl-every #'consp '((foo . bar) nil (baz . quux))) > > > > > > Yes. > > -- > > t > > Have done this small test, and even though there is a nil (or anything > else, e.g. jarod), assoc still works on the other keys. That's what I was saying (and what Elisp's doc, which is recommended reading also says). There are different views on that, and Elisp itself is one of the less strict: it just ignores list elements which aren't assocs. There is some historical background to that. "Classical" Lisp "types" are just defined by the operations defined on them. Cheers -- t [-- Attachment #2: signature.asc --] [-- Type: application/pgp-signature, Size: 195 bytes --] ^ permalink raw reply [flat|nested] 22+ messages in thread
* Re: Check whether an object is an alist 2024-09-17 19:35 ` tomas @ 2024-09-17 21:02 ` Heime 2024-09-17 21:25 ` Heime 2024-09-17 22:01 ` Stephen Berman 0 siblings, 2 replies; 22+ messages in thread From: Heime @ 2024-09-17 21:02 UTC (permalink / raw) To: tomas; +Cc: help-gnu-emacs Sent with Proton Mail secure email. On Wednesday, September 18th, 2024 at 7:35 AM, tomas@tuxteam.de <tomas@tuxteam.de> wrote: > On Tue, Sep 17, 2024 at 07:20:22PM +0000, Heime wrote: > > > On Wednesday, September 18th, 2024 at 6:28 AM, tomas@tuxteam.de tomas@tuxteam.de wrote: > > > > > On Tue, Sep 17, 2024 at 06:04:41PM +0000, Heime wrote: > > > > > > > On Wednesday, September 18th, 2024 at 5:57 AM, tomas@tuxteam.de tomas@tuxteam.de wrote: > > > > > > > > [...] as an exercise for the reader. > > > > > > > > (cl-every #'consp '((foo . bar) nil (baz . quux))) > > > > > > Yes. > > > -- > > > t > > > > Have done this small test, and even though there is a nil (or anything > > else, e.g. jarod), assoc still works on the other keys. > > > That's what I was saying (and what Elisp's doc, which is recommended > reading also says). There are different views on that, and Elisp > itself is one of the less strict: it just ignores list elements which > aren't assocs. > > There is some historical background to that. "Classical" Lisp "types" > are just defined by the operations defined on them. > > Cheers > -- > t After changing to the following, I am getting validation for list element (1 2 3), even though it is not an association. (defun torium-alist-p (obj) "Return t if object OBJ is an association list (alist)." (and (listp obj) (cl-every (lambda (x) #'consp) obj))) ^ permalink raw reply [flat|nested] 22+ messages in thread
* Re: Check whether an object is an alist 2024-09-17 21:02 ` Heime @ 2024-09-17 21:25 ` Heime 2024-09-18 4:21 ` tomas 2024-09-17 22:01 ` Stephen Berman 1 sibling, 1 reply; 22+ messages in thread From: Heime @ 2024-09-17 21:25 UTC (permalink / raw) To: Heime; +Cc: tomas, help-gnu-emacs Sent with Proton Mail secure email. On Wednesday, September 18th, 2024 at 9:02 AM, Heime <heimeborgia@protonmail.com> wrote: > > > > > > Sent with Proton Mail secure email. > > > On Wednesday, September 18th, 2024 at 7:35 AM, tomas@tuxteam.de tomas@tuxteam.de wrote: > > > On Tue, Sep 17, 2024 at 07:20:22PM +0000, Heime wrote: > > > > > On Wednesday, September 18th, 2024 at 6:28 AM, tomas@tuxteam.de tomas@tuxteam.de wrote: > > > > > > > On Tue, Sep 17, 2024 at 06:04:41PM +0000, Heime wrote: > > > > > > > > > On Wednesday, September 18th, 2024 at 5:57 AM, tomas@tuxteam.de tomas@tuxteam.de wrote: > > > > > > > > > > [...] as an exercise for the reader. > > > > > > > > > > (cl-every #'consp '((foo . bar) nil (baz . quux))) > > > > > > > > Yes. > > > > -- > > > > t > > > > > > Have done this small test, and even though there is a nil (or anything > > > else, e.g. jarod), assoc still works on the other keys. > > > > That's what I was saying (and what Elisp's doc, which is recommended > > reading also says). There are different views on that, and Elisp > > itself is one of the less strict: it just ignores list elements which > > aren't assocs. > > > > There is some historical background to that. "Classical" Lisp "types" > > are just defined by the operations defined on them. > > > > Cheers > > -- > > t > > > After changing to the following, I am getting validation for list > element (1 2 3), even though it is not an association. > > (defun torium-alist-p (obj) > "Return t if object OBJ is an association list (alist)." > (and (listp obj) > (cl-every (lambda (x) #'consp) obj))) I remember. No requirements of lambda to run #'consp on obj Thus, again (defun torium-alist-p (obj) "Return t if object OBJ is an association list (alist)." (and (listp obj) (cl-every #'consp obj))) ^ permalink raw reply [flat|nested] 22+ messages in thread
* Re: Check whether an object is an alist 2024-09-17 21:25 ` Heime @ 2024-09-18 4:21 ` tomas 2024-09-18 4:52 ` Heime 0 siblings, 1 reply; 22+ messages in thread From: tomas @ 2024-09-18 4:21 UTC (permalink / raw) To: Heime; +Cc: help-gnu-emacs [-- Attachment #1: Type: text/plain, Size: 792 bytes --] On Tue, Sep 17, 2024 at 09:25:44PM +0000, Heime wrote: [...] > > After changing to the following, I am getting validation for list > > element (1 2 3), even though it is not an association. > > > > (defun torium-alist-p (obj) > > "Return t if object OBJ is an association list (alist)." > > (and (listp obj) > > (cl-every (lambda (x) #'consp) obj))) > > I remember. No requirements of lambda to run #'consp on obj Much more important: /why/ are you "getting a validation" for list '(1 2 3) with the above form and... > (defun torium-alist-p (obj) > "Return t if object OBJ is an association list (alist)." > (and (listp obj) > (cl-every #'consp obj))) ...(probably, you didn't tell us) not with this one? Why are they different? Cheers -- t [-- Attachment #2: signature.asc --] [-- Type: application/pgp-signature, Size: 195 bytes --] ^ permalink raw reply [flat|nested] 22+ messages in thread
* Re: Check whether an object is an alist 2024-09-18 4:21 ` tomas @ 2024-09-18 4:52 ` Heime 0 siblings, 0 replies; 22+ messages in thread From: Heime @ 2024-09-18 4:52 UTC (permalink / raw) To: tomas; +Cc: help-gnu-emacs On Wednesday, September 18th, 2024 at 4:21 PM, tomas@tuxteam.de <tomas@tuxteam.de> wrote: > On Tue, Sep 17, 2024 at 09:25:44PM +0000, Heime wrote: > > [...] > > > > After changing to the following, I am getting validation for list > > > element (1 2 3), even though it is not an association. > > > > > > (defun torium-alist-p (obj) > > > "Return t if object OBJ is an association list (alist)." > > > (and (listp obj) > > > (cl-every (lambda (x) #'consp) obj))) > > > > I remember. No requirements of lambda to run #'consp on obj > > > Much more important: /why/ are you "getting a validation" for list '(1 2 3) > with the above form and... > > > (defun torium-alist-p (obj) > > "Return t if object OBJ is an association list (alist)." > > (and (listp obj) > > (cl-every #'consp obj))) > > > ...(probably, you didn't tell us) not with this one? > > Why are they different? > > Cheers > -- > t Have solved the problem. Was not with that one, but that one solved the problem. Felicitations ^ permalink raw reply [flat|nested] 22+ messages in thread
* Re: Check whether an object is an alist 2024-09-17 21:02 ` Heime 2024-09-17 21:25 ` Heime @ 2024-09-17 22:01 ` Stephen Berman 1 sibling, 0 replies; 22+ messages in thread From: Stephen Berman @ 2024-09-17 22:01 UTC (permalink / raw) To: Heime; +Cc: tomas, help-gnu-emacs On Tue, 17 Sep 2024 21:02:03 +0000 Heime <heimeborgia@protonmail.com> wrote: [...] > After changing to the following, I am getting validation for list > element (1 2 3), even though it is not an association. It is according to (info "(elisp) Association Lists"): Both the values and the keys in an alist may be any Lisp objects. For example, in the following alist, the symbol ‘a’ is associated with the number ‘1’, and the string ‘"b"’ is associated with the _list_ ‘(2 3)’, which is the CDR of the alist element: ((a . 1) ("b" 2 3)) > (defun torium-alist-p (obj) > "Return t if object OBJ is an association list (alist)." > (and (listp obj) > (cl-every (lambda (x) #'consp) obj))) Steve Berman ^ permalink raw reply [flat|nested] 22+ messages in thread
* RE: [External] : Re: Check whether an object is an alist 2024-09-17 14:32 ` Heime 2024-09-17 15:20 ` tomas @ 2024-09-17 19:22 ` Drew Adams 1 sibling, 0 replies; 22+ messages in thread From: Drew Adams @ 2024-09-17 19:22 UTC (permalink / raw) To: Heime, tomas@tuxteam.de; +Cc: help-gnu-emacs@gnu.org > (defun torium-alist-p (obj) > (and (listp obj) > (every (lambda (x) > (or (null x) > (consp x))) > obj))) `listp' does exactly this: (or (null x) (consp x)). So your code just does this: (and (listp obj) (every #'listp obj)) ^ permalink raw reply [flat|nested] 22+ messages in thread
end of thread, other threads:[~2024-09-18 4:52 UTC | newest] Thread overview: 22+ messages (download: mbox.gz follow: Atom feed -- links below jump to the message on this page -- 2024-09-16 10:29 Check whether an object is an alist Heime 2024-09-17 8:45 ` Heime 2024-09-17 9:38 ` Manuel Giraud via Users list for the GNU Emacs text editor 2024-09-17 9:46 ` tomas 2024-09-17 11:16 ` Heime 2024-09-17 11:23 ` tomas 2024-09-17 13:51 ` Heime 2024-09-17 13:58 ` tomas 2024-09-17 14:32 ` Heime 2024-09-17 15:20 ` tomas 2024-09-17 16:26 ` Heime 2024-09-17 17:57 ` tomas 2024-09-17 18:04 ` Heime 2024-09-17 18:28 ` tomas 2024-09-17 19:20 ` Heime 2024-09-17 19:35 ` tomas 2024-09-17 21:02 ` Heime 2024-09-17 21:25 ` Heime 2024-09-18 4:21 ` tomas 2024-09-18 4:52 ` Heime 2024-09-17 22:01 ` Stephen Berman 2024-09-17 19:22 ` [External] : " Drew Adams
Code repositories for project(s) associated with this external index https://git.savannah.gnu.org/cgit/emacs.git https://git.savannah.gnu.org/cgit/emacs/org-mode.git This is an external index of several public inboxes, see mirroring instructions on how to clone and mirror all data and code used by this external index.