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: Pascal J. Bourguignon
Subject: Re: I'd like to marry while and mapcar...
Date: Fri, 06 Feb 2015 15:13:58 +0100
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/24.3 (gnu/linux)

Marcin Borkowski <mbork@wmi.amu.edu.pl> writes:

> Hello Emacsers and Elispers!
>
> What I'd need is kind of a marriage of while and mapcar: I'd like to run
> some function until it returns nil and make a list of all results it
> gives back until then.
>
> Mu use case is that I'm getting some info from a LaTeX file.  For
> instance, assume that I want to make a list of all files \include'd by
> a LaTeX document.  I've written a function `get-TeX-macro-arguments'
> which finds the next occurence of a given TeX command, moves point past
> it and returns its arguments; if it does not find any such occurrence,
> it returns nil.  So I can say something like this:
>
> (let (current-include (list-of-includes ()))
>   (while (setq current-include (get-TeX-macro-arguments "include"))
>     (setq list-of-includes (append list-of-includes current-include)))
>   list-of-includes)
>
> This, however, looks a bit, let's say, Fortran-ish;-); I'm after a more
> Lispy way.  (Of course, one step would be to use anaphoric while, but
> that doesn't help my main problem of ditching list-of-includes.)
>
> Yes, I know, I have too much time on my hands, and I bother about style.
> But I'm also curious whether there is any "standard" way, or whether
> I should indeed wrap something like the above code in a macro, or
> something.  (Or maybe just don't care...)

Nothing standard really.


The technique is simple: just write what you want to use:

 
    (map-while (function get-TeX-macro-arguments) list-of-includes)


How map-while is implemented is irrelevant, and an exercise left to the
readers (see other posts).


Well, I'm a reader too ;-)


    (require 'cl)
    (defun map-while (pred-fun list)
        (loop for item in list
              for result = (funcall pred-fun item)
              while result
              collect result))


-- 
__Pascal Bourguignon__                 http://www.informatimago.com/
“The factory of the future will have only two employees, a man and a
dog. The man will be there to feed the dog. The dog will be there to
keep the man from touching the equipment.” -- Carl Bass CEO Autodesk


reply via email to

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