emacs-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

RE: seq-some-p and nil


From: Drew Adams
Subject: RE: seq-some-p and nil
Date: Tue, 8 Sep 2015 12:48:23 -0700 (PDT)

> > Was there something wrong with the suggestion to return,
> > as the non-nil value, a cons (ELEMENT . VALUE)?
> >
> > (Where ELEMENT is the sequence element that satisfies the
> > predicate, and VALUE is the return value of the predicate
> > for that element.)
> >
> > That gives you "some" element that satisfies the predicate
> > (the first such).  And it gives you the result of the test.
> > Each of these can be useful, depending on the context.
> 
> It would work, and I believe Scheme has a similar function, but I don't
> want seq-some to have this extra complexity.  It could be another
> function though, just like in Scheme.

What extra complexity?  It creates one cons cell which has as its
car and cdr things that presumably already exist.

I realize you are dealing with general sequences, and that the
sequence code can be complex, but the cons part of it, as the return
value, should be simple, no?  Isn't it true for all sequence types
supported, that the car and cdr will exist after the predicate is
called, and so they can just be pointed to by the cons?

For lists only, this is what I use, FWIW:

(defun zz-some (predicate list)
  "Return non-nil if PREDICATE applied to some element of LIST is true.
The value returned is a cons, (ELEMENT . VALUE), where ELEMENT is the
first list element that satisfies PREDICATE and VALUE is the value of
PREDICATE applied to ELEMENT."
  (let (elt val)
    (catch 'zz-some
      (while list
        (when (setq val  (funcall predicate (setq elt  (pop list))))
          (throw 'zz-some (cons elt val))))
      nil)))



reply via email to

[Prev in Thread] Current Thread [Next in Thread]