all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Drew Adams <drew.adams@oracle.com>
To: Marcin Borkowski <mbork@wmi.amu.edu.pl>,
	Help Gnu Emacs mailing list <help-gnu-emacs@gnu.org>
Subject: RE: I'd like to marry while and mapcar...
Date: Fri, 6 Feb 2015 14:49:18 -0800 (PST)	[thread overview]
Message-ID: <bbd9d428-1385-4b4a-bc44-a32516e2f0b6@default> (raw)
In-Reply-To: <87vbjfdwq7.fsf@wmi.amu.edu.pl>

FWIW, what you wrote in the first place, Marcin, is pretty
much what I would do.  Call it Fortranesque, if you like.
But it's classic Lispiness, IMO.  Lisp is not Haskell.

This is what I would do:

(let ((includes  ())
      include)
  (while (setq include  (get-TeX-macro-arguments "include"))
    (push include includes))
  (setq includes  (nreverse includes)))

I wouldn't bother with `(cl-)loop' or `mapcar' or `mapc' here.

Since you have a function that gets the next thingie you want,
and it returns nil when there are no more, there is no reason
at all to use `catch'+`throw' here.

`catch'+`throw' is useful when, e.g., you have an existing
list that you iterate over (e.g., using `dolist'), and you
do not want to continue iterating over the rest of that
list as soon as you know that you no longer need to.

Here is a typical, end-the-looping-early use of `catch'+`throw':

(defun take (n xs)
  "Return a new list containing only the first N elements of list XS.
If N is greater than the length of XS, return (a copy of) XS."
  (let ((takes  ()))
    (catch 'take
      (dolist (x  xs)
        (when (>= (length takes) n) (throw 'take takes))
        (push x takes)))
    (setq takes  (nreverse takes))))

Or if you don't want the inefficiency of evaluating `length'
at each iteration, add a counter:

(defun take (n xs)
  "..."
  (let ((takes  ())
        (len    0))
    (catch 'take
      (dolist (x  xs)
        (when (>= len n) (throw 'take takes))
        (push x takes)
        (cl-incf len)))
    (setq takes  (nreverse takes))))



  parent reply	other threads:[~2015-02-06 22:49 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-02-06 12:56 I'd like to marry while and mapcar Marcin Borkowski
2015-02-06 13:18 ` Rasmus
2015-02-06 13:53   ` Marcin Borkowski
2015-02-06 14:14     ` Rasmus
2015-02-06 13:36 ` Eli Zaretskii
2015-02-06 13:50   ` Marcin Borkowski
2015-02-06 13:44 ` Doug Lewan
     [not found] ` <mailman.19396.1423229779.1147.help-gnu-emacs@gnu.org>
2015-02-06 16:25   ` Barry Margolin
2015-02-06 16:52     ` Pascal J. Bourguignon
2015-02-06 22:49 ` Drew Adams [this message]
2015-02-07 21:09   ` Robert Thorpe
2015-02-07 23:37 ` Thien-Thi Nguyen
     [not found] ` <mailman.19494.1423352465.1147.help-gnu-emacs@gnu.org>
2015-02-08  0:28   ` Emanuel Berg
     [not found] <mailman.19392.1423227427.1147.help-gnu-emacs@gnu.org>
2015-02-06 14:13 ` Pascal J. Bourguignon
2015-02-06 14:54 ` Joost Kremers
2015-02-06 17:47   ` Joost Kremers

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=bbd9d428-1385-4b4a-bc44-a32516e2f0b6@default \
    --to=drew.adams@oracle.com \
    --cc=help-gnu-emacs@gnu.org \
    --cc=mbork@wmi.amu.edu.pl \
    /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.