help-gnu-emacs
[Top][All Lists]
Advanced

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

RE: I'd like to marry while and mapcar...


From: Drew Adams
Subject: RE: I'd like to marry while and mapcar...
Date: Fri, 6 Feb 2015 14:49:18 -0800 (PST)

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



reply via email to

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