* What to use instead of find-if?
@ 2008-12-01 19:54 Rupert Swarbrick
0 siblings, 0 replies; 5+ messages in thread
From: Rupert Swarbrick @ 2008-12-01 19:54 UTC (permalink / raw)
To: help-gnu-emacs
[-- Attachment #1: Type: text/plain, Size: 1159 bytes --]
--=-=-=
I was messing around last night and added a small feature to my local
copy of gnus. Cool. I reckon it'd be useful in general, so I was going
to post it to ding. Cool.
However
Looking back at the code, I realised I used cl's find-if (I know
somewhat more common lisp than elisp). And gnus doesn't (require
'cl). 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? This is a genuine question, by the way. I'm sure
I'm being thick not spotting a neat way to write this.
Rupert
--=-=-=
Content-Type: application/pgp-signature
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (GNU/Linux)
iJwEAQECAAYFAkk0QOwACgkQRtd/pJbYVoYc6AP8CbG17u+2UZ5hGcbnBS5C29u2
bQLnTpigHQHn1zXrIsdhDYqwSW9iB3zof+F/MQVVr728chiXTRiq8OlHdwktUmrR
7zJReKLxz6DKiw456ymuLLV7rnfU4O3u9l8CtTCecS85D6eDSSP6Ia5c5Z7dfTPc
IekoKQsg37B26FlVm0k=
=mS8x
-----END PGP SIGNATURE-----
--=-=-=--
[-- Attachment #2: Type: application/pgp-signature, Size: 314 bytes --]
^ permalink raw reply [flat|nested] 5+ messages in thread
* What to use instead of find-if?
@ 2008-12-01 19:56 Rupert Swarbrick
2008-12-01 21:24 ` Drew Adams
[not found] ` <mailman.1649.1228166673.26697.help-gnu-emacs@gnu.org>
0 siblings, 2 replies; 5+ messages in thread
From: Rupert Swarbrick @ 2008-12-01 19:56 UTC (permalink / raw)
To: help-gnu-emacs
[-- Attachment #1: Type: text/plain, Size: 775 bytes --]
I was messing around last night and added a small feature to my local
copy of gnus. Cool. I reckon it'd be useful in general, so I was going
to post it to ding. Cool.
However
Looking back at the code, I realised I used cl's find-if (I know
somewhat more common lisp than elisp). And gnus doesn't (require
'cl). 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? This is a genuine question, by the way. I'm sure
I'm being thick not spotting a neat way to write this.
Rupert
[-- Attachment #2: Type: application/pgp-signature, Size: 314 bytes --]
^ permalink raw reply [flat|nested] 5+ messages in thread
* RE: What to use instead of find-if?
2008-12-01 19:56 Rupert Swarbrick
@ 2008-12-01 21:24 ` Drew Adams
[not found] ` <mailman.1649.1228166673.26697.help-gnu-emacs@gnu.org>
1 sibling, 0 replies; 5+ messages in thread
From: Drew Adams @ 2008-12-01 21:24 UTC (permalink / raw)
To: 'Rupert Swarbrick', help-gnu-emacs
> 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))
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: What to use instead of find-if?
[not found] ` <mailman.1649.1228166673.26697.help-gnu-emacs@gnu.org>
@ 2008-12-03 0:29 ` Rupert Swarbrick
2008-12-03 0:58 ` Drew Adams
0 siblings, 1 reply; 5+ messages in thread
From: Rupert Swarbrick @ 2008-12-03 0:29 UTC (permalink / raw)
To: help-gnu-emacs
[-- Attachment #1: Type: text/plain, Size: 952 bytes --]
"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 #2: Type: application/pgp-signature, Size: 314 bytes --]
^ permalink raw reply [flat|nested] 5+ messages in thread
* RE: What to use instead of find-if?
2008-12-03 0:29 ` Rupert Swarbrick
@ 2008-12-03 0:58 ` Drew Adams
0 siblings, 0 replies; 5+ messages in thread
From: Drew Adams @ 2008-12-03 0:58 UTC (permalink / raw)
To: 'Rupert Swarbrick', help-gnu-emacs
> > 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?
Sorry, I can't answer that.
My guess is that it is very performant.
I know that I don't think twice about using it.
But then I don't think twice about a lot of things. ;-)
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2008-12-03 0:58 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2008-12-01 19:54 What to use instead of find-if? Rupert Swarbrick
-- strict thread matches above, loose matches on Subject: below --
2008-12-01 19:56 Rupert Swarbrick
2008-12-01 21:24 ` Drew Adams
[not found] ` <mailman.1649.1228166673.26697.help-gnu-emacs@gnu.org>
2008-12-03 0:29 ` Rupert Swarbrick
2008-12-03 0:58 ` Drew Adams
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).