all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Drew Adams <drew.adams@oracle.com>
To: Philipp Stephani <p.stephani2@gmail.com>, help-gnu-emacs@gnu.org
Subject: RE: return first element in list with certain property
Date: Mon, 20 Nov 2017 12:12:13 -0800 (PST)	[thread overview]
Message-ID: <ef79b941-b725-4664-8bb3-1053538c6038@default> (raw)
In-Reply-To: <CAArVCkQK47zz-ojnORq9866xUerABCFtVX1Eww3QWJnbtTr47g@mail.gmail.com>

>> cl-find-if
>
> That's it 100%

BTW, I haven't run any tests, but the `cl-find' code, which
is used also by functions such as `cl-find-if', apparently
traverses the list twice: Once when it calls `cl-position'
and a second time when it calls `elt'.

 (defun cl-find (cl-item cl-seq &rest cl-keys)
  (let ((cl-pos (apply 'cl-position cl-item cl-seq cl-keys)))
    (and cl-pos (elt cl-seq cl-pos))))

`cl-position' does this for a list - it cdrs down list CL-P:

(let ((cl-p (nthcdr cl-start cl-seq)))
  (or cl-end (setq cl-end 8000000))
      (let ((cl-res nil))
        (while (and cl-p (< cl-start cl-end)
                    (or (not cl-res) cl-from-end))
          (if (cl--check-test cl-item (car cl-p))
              (setq cl-res cl-start))
          (setq cl-p (cdr cl-p) cl-start (1+ cl-start)))
        cl-res))

Checking the C source for `elt' called on a list (admittedly,
somewhat old C source code, which is all I have at hand), it
does, in effect, (car (nthcdr n list)).  It cdrs down LIST.

Someone might want to profile the difference, for a long list
whose first occurrence for the sought item is near the end of
the list.  Maybe compare, for example, the use of `cl-find-if'
with something simple that traverses the list only once:

(catch '>1
  (dolist (x xs nil) (when (> x 1) (throw '>1 x))))

The idiom of traversing a list only once, throwing to a
`catch', is a pretty good one to learn, I think.  It's
straightforward and transparent, doing just what it says.

Granted, it doesn't shout "Return the first element > 1."

And it's a little more verbose than using a higher
abstraction such as `cl-find-if'.  Anyway, compare the
length of the code above with these - a difference, but
not huge:

(seq-find (apply-partially #'< 1) xs)
(seq-find (lambda (x) (> x 1)) xs)
(cl-find-if (lambda (x) (> x 1)) xs)
(cl-loop for x in xs when (> x 1) return x)
(cl-some (lambda (x) (and (> x 1) x)) xs)

Dunno whether the other functions, besides `cl-find-if',
traverse the list more than once.

Maybe the code defining `cl-find' should be tweaked to
avoid two traversals?  `cl-position' traverses only once,
and so does `elt'.  Maybe getting the position in the list
and then cdring down the list again to that position is
not the smartest way to do `cl-find-if' on a list.



  parent reply	other threads:[~2017-11-20 20:12 UTC|newest]

Thread overview: 54+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-11-19 20:43 return first element in list with certain property Emanuel Berg
2017-11-19 20:51 ` Eric Abrahamsen
2017-11-19 21:37 ` Philipp Stephani
2017-11-19 22:55   ` Emanuel Berg
2017-11-20 18:51     ` John Mastro
2017-11-20 19:21       ` Michael Heerdegen
2017-11-20 21:20         ` Emanuel Berg
2017-11-20 21:40           ` John Mastro
2017-11-20 22:19           ` Michael Heerdegen
2017-11-20 20:52       ` Emanuel Berg
2017-11-20 20:12   ` Drew Adams [this message]
2017-11-20 20:49     ` Eric Abrahamsen
2017-11-20 21:16       ` Emanuel Berg
2017-11-20 22:02         ` Eric Abrahamsen
2017-11-21  1:54           ` Emanuel Berg
2017-11-21  2:18             ` Emanuel Berg
2017-11-21 17:34               ` Michael Heerdegen
2017-11-21 20:10                 ` Emanuel Berg
2017-11-21 21:33                   ` Michael Heerdegen
2017-11-22 21:30                     ` Nicolas Petton
2017-11-22 21:31                   ` Nicolas Petton
2017-11-21 18:01             ` Philipp Stephani
2017-11-21 18:37               ` Michael Heerdegen
2017-11-21 19:26               ` Drew Adams
2017-11-21 20:17                 ` Emanuel Berg
2017-11-21 20:47                   ` Drew Adams
2017-11-21 20:14               ` Emanuel Berg
     [not found]               ` <mailman.4260.1511289435.27995.help-gnu-emacs@gnu.org>
2017-11-22  3:52                 ` James K. Lowden
2017-11-22  5:04                   ` Michael Heerdegen
2017-11-22 14:06                   ` Emanuel Berg
2017-11-22 14:52                   ` Rusi
2017-11-22 21:28             ` Nicolas Petton
2017-11-23  0:56               ` Emanuel Berg
2017-11-23  1:17                 ` Michael Heerdegen
2017-11-23  1:30                   ` Emanuel Berg
2017-11-25  2:57                 ` John Mastro
2017-11-25  3:54                   ` Robert Thorpe
2017-11-25  4:44                     ` Alexis
2017-11-25  7:10                     ` tomas
2017-11-25 17:11                       ` Emanuel Berg
2017-11-25  7:46                     ` Eli Zaretskii
2017-11-25  8:03                       ` tomas
2017-11-25 19:00                     ` John Mastro
2017-11-25 19:45                       ` Emanuel Berg
2017-11-27  3:44                         ` Michael Heerdegen
2017-11-20 22:59         ` Drew Adams
2017-11-21  1:50           ` Emanuel Berg
     [not found] ` <mailman.4093.1511127491.27995.help-gnu-emacs@gnu.org>
2018-03-06  0:31   ` Robert L.
2018-03-06  8:53     ` Emanuel Berg
2018-03-06  9:29       ` Emanuel Berg
     [not found] <mailman.4086.1511124258.27995.help-gnu-emacs@gnu.org>
2017-11-19 21:19 ` Marco Wahl
2017-11-19 22:51   ` Emanuel Berg
2017-11-21  4:00   ` Emanuel Berg
2017-11-19 21:20 ` Marco Wahl

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

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

  git send-email \
    --in-reply-to=ef79b941-b725-4664-8bb3-1053538c6038@default \
    --to=drew.adams@oracle.com \
    --cc=help-gnu-emacs@gnu.org \
    --cc=p.stephani2@gmail.com \
    /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 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.