unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
* What's the canonical way to check "alistp"?
@ 2018-03-01 15:21 Kaushal Modi
  2018-03-01 15:29 ` Kaushal Modi
                   ` (2 more replies)
  0 siblings, 3 replies; 6+ messages in thread
From: Kaushal Modi @ 2018-03-01 15:21 UTC (permalink / raw)
  To: Help Gnu Emacs mailing list

Hello,

I wish something like alistp existed..

What's the best way to check if every element in a list is also a list?

Thanks.
-- 

Kaushal Modi


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

* Re: What's the canonical way to check "alistp"?
  2018-03-01 15:21 What's the canonical way to check "alistp"? Kaushal Modi
@ 2018-03-01 15:29 ` Kaushal Modi
  2018-03-01 15:33   ` Dmitry Gutov
  2018-03-01 17:06 ` Drew Adams
       [not found] ` <mailman.9955.1519924176.27995.help-gnu-emacs@gnu.org>
  2 siblings, 1 reply; 6+ messages in thread
From: Kaushal Modi @ 2018-03-01 15:29 UTC (permalink / raw)
  To: Help Gnu Emacs mailing list

On Thu, Mar 1, 2018 at 10:21 AM Kaushal Modi <kaushal.modi@gmail.com> wrote:

> What's the best way to check if every element in a list is also a list?
>

I don't know if there's a more concise way.. but this seems like a good way:

(if (eq 0 (cl-count-if (lambda (el) (not (listp el))) MY-VAR)) ..)
-- 

Kaushal Modi


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

* Re: What's the canonical way to check "alistp"?
  2018-03-01 15:29 ` Kaushal Modi
@ 2018-03-01 15:33   ` Dmitry Gutov
  2018-03-01 16:36     ` Kaushal Modi
  0 siblings, 1 reply; 6+ messages in thread
From: Dmitry Gutov @ 2018-03-01 15:33 UTC (permalink / raw)
  To: Kaushal Modi, Help Gnu Emacs mailing list

On 3/1/18 5:29 PM, Kaushal Modi wrote:
> On Thu, Mar 1, 2018 at 10:21 AM Kaushal Modi <kaushal.modi@gmail.com> wrote:
> 
>> What's the best way to check if every element in a list is also a list?
>>
> 
> I don't know if there's a more concise way.. but this seems like a good way:
> 
> (if (eq 0 (cl-count-if (lambda (el) (not (listp el))) MY-VAR)) ..)

(seq-every-p #'consp alist) if we're checking for an alist.

Use #'listp otherwise.




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

* Re: What's the canonical way to check "alistp"?
  2018-03-01 15:33   ` Dmitry Gutov
@ 2018-03-01 16:36     ` Kaushal Modi
  0 siblings, 0 replies; 6+ messages in thread
From: Kaushal Modi @ 2018-03-01 16:36 UTC (permalink / raw)
  To: Dmitry Gutov; +Cc: Help Gnu Emacs mailing list

On Thu, Mar 1, 2018 at 10:34 AM Dmitry Gutov <dgutov@yandex.ru> wrote:

>
> (seq-every-p #'consp alist) if we're checking for an alist.
>
> Use #'listp otherwise.


Thanks!
-- 

Kaushal Modi


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

* RE: What's the canonical way to check "alistp"?
  2018-03-01 15:21 What's the canonical way to check "alistp"? Kaushal Modi
  2018-03-01 15:29 ` Kaushal Modi
@ 2018-03-01 17:06 ` Drew Adams
       [not found] ` <mailman.9955.1519924176.27995.help-gnu-emacs@gnu.org>
  2 siblings, 0 replies; 6+ messages in thread
From: Drew Adams @ 2018-03-01 17:06 UTC (permalink / raw)
  To: Kaushal Modi, Help Gnu Emacs mailing list

> What's the best way to check if every element in a list is also a list?

Also a _list_ or also a cons? Do you want (alistp '(()))
to return true?  I'm guessing no.

(defun alistp (xs)
  "Return non-nil iff XS is a list and each element is a cons."
  (and (listp xs)
       (catch 'alistp
         (dolist (x  xs) (unless (consp x) (throw 'alistp nil)))
         t)))

(The code should not bother to check each element if it
finds one that is not a cons.)



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

* Re: What's the canonical way to check "alistp"?
       [not found] ` <mailman.9955.1519924176.27995.help-gnu-emacs@gnu.org>
@ 2018-03-05 19:17   ` Robert L.
  0 siblings, 0 replies; 6+ messages in thread
From: Robert L. @ 2018-03-05 19:17 UTC (permalink / raw)
  To: help-gnu-emacs

On 3/1/2018, Drew Adams wrote:

> > What's the best way to check if every element in a list is also a list?
> 
> Also a list or also a cons? Do you want (alistp '(()))
> to return true?  I'm guessing no.
> 
> (defun alistp (xs)
>   "Return non-nil iff XS is a list and each element is a cons."
>   (and (listp xs)
>        (catch 'alistp
>          (dolist (x  xs) (unless (consp x) (throw 'alistp nil)))
>          t)))
> 
> (The code should not bother to check each element if it
> finds one that is not a cons.)

(defun alistp (xs)
  (and (listp xs) (while (consp (car xs)) (pop xs)))
  (not xs))

-- 
[Amazon bans history book after it received 300 5-star reviews.]
http://www.tomatobubble.com/worldwarii.html
http://archive.org/details/nolies


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

end of thread, other threads:[~2018-03-05 19:17 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2018-03-01 15:21 What's the canonical way to check "alistp"? Kaushal Modi
2018-03-01 15:29 ` Kaushal Modi
2018-03-01 15:33   ` Dmitry Gutov
2018-03-01 16:36     ` Kaushal Modi
2018-03-01 17:06 ` Drew Adams
     [not found] ` <mailman.9955.1519924176.27995.help-gnu-emacs@gnu.org>
2018-03-05 19:17   ` Robert L.

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