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

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

Re: What to use instead of find-if?


From: Rupert Swarbrick
Subject: Re: What to use instead of find-if?
Date: Wed, 03 Dec 2008 00:29:34 +0000
User-agent: Gnus/5.110011 (No Gnus v0.11) Emacs/23.0.60 (gnu/linux)

"Drew Adams" <drew.adams@oracle.com> writes:

>> So the code I'm thinking about does the following:
>>   (let ((blah (find-if (lambda (elem)
>>                          (whopping-great-predicatey-thing))
>>                        some-list)))
>>     (if blah (something using blah) (something else)))
>> 
>> Can anyone suggest a vaguely idiomatic way to do this using 
>> the built-in constructs of elisp?
>
> There are no doubt lots of ways to do it. Here's one:
>
> (defun my-find-if (pred xs)
>   (catch 'my-found
>     (dolist (x xs) (when (funcall pred x) (throw 'my-found x)))
>     nil))

Ahah. That's neat! (And is indeed the semantics I had in mind when I
wrote the original).

Now, this really isn't relevant to the original question, but I was just
wondering: what are the performance costs of (catch ) in elisp? For
example, should one think twice before using it in syntax highlighting
code or the like?

Rupert

Attachment: pgpszc1TFhv76.pgp
Description: PGP signature


reply via email to

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