unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
From: "Stephen J. Turnbull" <turnbull@sk.tsukuba.ac.jp>
To: Mark Oteiza <mvoteiza@udel.edu>
Cc: Nicolas Petton <nicolas@petton.fr>,
	Stefan Monnier <monnier@iro.umontreal.ca>,
	Drew Adams <drew.adams@oracle.com>,
	emacs-devel@gnu.org
Subject: Re: seq-some-p and nil
Date: Wed, 09 Sep 2015 13:28:48 +0900	[thread overview]
Message-ID: <878u8g1ain.fsf@uwakimon.sk.tsukuba.ac.jp> (raw)
In-Reply-To: <20150909021959.GA1494@holos>

Mark Oteiza writes:
 > On 08/09/15 at 01:50pm, Stefan Monnier wrote:
 > > >   (defun seq-some (pred seq)
 > > >     (funcall pred (seq-find (pred seq)))
 > > 
 > > But that fails for the case where the element found is nil.
 > 
 > Works as expected

[...]
 >   (seq-find 'null [1 2 nil])
 >   ;; => nil
 > 
 >   (seq-some 'null [1 2 nil])
 >   ;; => t
 > 
 > It's not a "semantic problem" or a "corner case", if one is really
 > curious if their sequence has a nil value, seq-find isn't the way to
 > find out.

You have a test coverage problem.

    (seq-find 'null [1 2])
    ;; => nil

    (seq-some 'null [1 2])
    ;; => t

I suppose this is the corner case Stefan meant, in the sense that when
seq-find "finds" (ie, "returns") nil it's impossible to distinguish
finding nil from not finding nil.

There are many ways to implement the distinction.  One is to create an
uninterned symbol and return that from seq-find-internal, then define
seq-find and seq-some in terms of seq-find-internal.  I suppose it's
more elegant to use a multiple-value technique (ie, return a cons
since Emacs doesn't have Common Lisp-style multiple values).  This
could be done in the -internal function or in the exported API as Drew
(IIRC) suggested.  You could use the get/find convention used
occasionally in Emacs, where the get-foo version signals an error and
find-foo returns nil.  Then find can be defined in terms of get with
an error handler for the not-found error.  (This is mostly used in
cases where the object is typically a blocker if not found though --
condition-case is relatively expensive.)  The find version can also
take an optional sentinel argument which is an object to return in the
not found case.

There are so many ways to get this right.  Please don't get it wrong.




  reply	other threads:[~2015-09-09  4:28 UTC|newest]

Thread overview: 54+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-09-03 15:50 seq-some-p and nil Simen Heggestøyl
2015-09-03 16:42 ` Stefan Monnier
2015-09-03 17:19   ` Artur Malabarba
2015-09-03 17:35     ` John Mastro
2015-09-03 17:50       ` Drew Adams
2015-09-03 19:36         ` Nicolas Petton
2015-09-03 19:45       ` Nicolas Petton
2015-09-06  1:17         ` Mark Oteiza
2015-09-06 20:48           ` Nicolas Petton
2015-09-06 21:46           ` Stefan Monnier
2015-09-07  6:11             ` Nicolas Petton
2015-09-07  8:06               ` Drew Adams
2015-09-07 15:19                 ` Stefan Monnier
2015-09-07 15:23                   ` Mark Oteiza
2015-09-07 17:08                   ` Drew Adams
2015-09-07 20:44                   ` Nicolas Petton
2015-09-07 22:27                     ` Stefan Monnier
2015-09-08  7:09                       ` Nicolas Petton
2015-09-08 12:36                         ` Stefan Monnier
2015-09-08 13:07                           ` Nicolas Petton
2015-09-08 16:49                             ` Stefan Monnier
2015-09-08 17:01                               ` David Kastrup
2015-09-08 17:08                                 ` Drew Adams
2015-09-08 19:02                                   ` Nicolas Petton
2015-09-08 19:48                                     ` Drew Adams
2015-09-09  7:56                                       ` Nicolas Petton
2015-09-09 13:26                                         ` Drew Adams
2015-09-09 17:45                                           ` Stefan Monnier
2015-09-09 17:53                                             ` Drew Adams
2015-09-09 20:24                                               ` Stefan Monnier
2015-09-09 20:46                                                 ` Drew Adams
2015-09-09 21:44                                           ` Nicolas Petton
2015-09-08 18:43                                 ` Stefan Monnier
2015-09-08 19:03                                   ` David Kastrup
2015-09-08 18:59                               ` Nicolas Petton
2015-09-08 19:06                                 ` David Kastrup
2015-09-08 20:33                                 ` Stefan Monnier
2015-09-09  7:57                                   ` Nicolas Petton
2015-09-08 13:21                           ` Nicolas Petton
2015-09-08 13:37                             ` Mark Oteiza
2015-09-08 17:50                               ` Stefan Monnier
2015-09-09  2:19                                 ` Mark Oteiza
2015-09-09  4:28                                   ` Stephen J. Turnbull [this message]
2015-09-09  4:57                                     ` Mark Oteiza
2015-09-07 20:45                   ` Nicolas Petton
2015-09-03 19:40 ` Nicolas Petton
2015-09-10 17:46   ` Simen Heggestøyl
2015-09-10 22:12     ` Nicolas Petton
2015-09-22 17:17       ` Simen Heggestøyl
2015-09-23  8:38         ` Nicolas Petton
2015-09-23  8:44           ` David Kastrup
2015-09-23  8:48           ` Simen Heggestøyl
2015-09-23 10:57             ` Nicolas Petton
2015-09-28 19:17         ` Nicolas Petton

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=878u8g1ain.fsf@uwakimon.sk.tsukuba.ac.jp \
    --to=turnbull@sk.tsukuba.ac.jp \
    --cc=drew.adams@oracle.com \
    --cc=emacs-devel@gnu.org \
    --cc=monnier@iro.umontreal.ca \
    --cc=mvoteiza@udel.edu \
    --cc=nicolas@petton.fr \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).