all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: "Pascal J. Bourguignon" <pjb@informatimago.com>
To: help-gnu-emacs@gnu.org
Subject: Re: I'd like to marry while and mapcar...
Date: Fri, 06 Feb 2015 17:52:45 +0100	[thread overview]
Message-ID: <87386jouci.fsf@kuiper.lan.informatimago.com> (raw)
In-Reply-To: barmar-B9B31B.11251906022015@88-209-239-213.giganet.hu

Barry Margolin <barmar@alum.mit.edu> writes:

> In article <mailman.19396.1423229779.1147.help-gnu-emacs@gnu.org>,
>  Eli Zaretskii <eliz@gnu.org> wrote:
>
>> > From: Marcin Borkowski <mbork@wmi.amu.edu.pl>
>> > Date: Fri, 06 Feb 2015 13:56:48 +0100
>> > 
>> > 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.
>> 
>> Can't you use 'throw' from within the function called by mapcar?
>
> That would allow you to terminate the loop, but how will it return the 
> list of the results? The function doesn't get a reference to the list of 
> results, so what value would you throw?

And worse: with mapcar, you'd have to duplicate the result list
building.  You can avoid it with mapc, but you need to return it both
when the list is exhausted and in the early exit, which is clearly not
pretty:


    (defun map-while (pred-fun list)
      (catch 'result
        (let ((results '()))
          (mapc (lambda (x) 
                  (let ((result (funcall pred-fun x)))
                    (if result
                        (push result results)
                        (throw 'result (nreverse results))))) 
                list)
          (nreverse results))))

    (map-while 'oddp '(1 3 5 4 6 8))
    --> (t t t)

-- 
__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	other threads:[~2015-02-06 16:52 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 [this message]
2015-02-06 22:49 ` Drew Adams
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=87386jouci.fsf@kuiper.lan.informatimago.com \
    --to=pjb@informatimago.com \
    --cc=help-gnu-emacs@gnu.org \
    /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.