Thaddeus L Olczyk wrote:
>
> Is there a statement somthing like collect-if in the loop construct?
>
> For example:
> (loop x in list
> collect-if (not (= (property x) 0))
> (process x))
MAPCAR applied to REMOVE-IF-NOT (or REMOVE) is the closest you can get.
That's not a CL standard loop, btw. That would be:
(loop for x in list
unless (= (property x) 0)
collect (process x))
Paul
"Paul F. Dietz" <·····@dls.net> writes:
> Thaddeus L Olczyk wrote:
> >
> > Is there a statement somthing like collect-if in the loop construct?
> >
> > For example:
> > (loop x in list
> > collect-if (not (= (property x) 0))
> > (process x))
>
> MAPCAR applied to REMOVE-IF-NOT (or REMOVE) is the closest you can get.
I'd think that MAPCAN would be closer to the non-loop version of what
he was looking for:
(mapcan #'(lambda (x) (when (not (= (property x)) 0)
(list x)))
list)
--
/|_ .-----------------------.
,' .\ / | No to Imperialist war |
,--' _,' | Wage class war! |
/ / `-----------------------'
( -. |
| ) |
(`-. '--.)
`. )----'
Thaddeus L Olczyk <······@interaccess.com> writes:
> Is there a statement somthing like collect-if in the loop construct?
>
> For example:
> (loop x in list
> collect-if (not (= (property x) 0))
> (process x))
(defun test-it (liste)
(loop for item in liste
when (>= item 0) collect item into result
finally (return result)))
Regards
Friedrich
Friedrich Dominicus <·····@q-software-solutions.com> wrote in message news:<··············@fbigm.here>...
> (defun test-it (liste)
> (loop for item in liste
> when (>= item 0) collect item into result
> finally (return result)))
There's no need to make the result explicit. E.g.
(defun test-it (list)
(loop for item in list
when (>= item 0) collect item))
··········@mailandnews.com (Software Scavenger) writes:
> Friedrich Dominicus <·····@q-software-solutions.com> wrote in message news:<··············@fbigm.here>...
>
> > (defun test-it (liste)
> > (loop for item in liste
> > when (>= item 0) collect item into result
> > finally (return result)))
>
> There's no need to make the result explicit. E.g.
>
> (defun test-it (list)
> (loop for item in list
> when (>= item 0) collect item))
Well you're right but nothing prevents me doing it more explicitly.
Regards
Friedrich
Thaddeus L Olczyk wrote:
> Is there a statement somthing like collect-if in the loop construct?
>
> For example:
> (loop x in list
> collect-if (not (= (property x) 0))
> (process x))
(loop for x from 1 to 5
if (oddp x)
collect x)
(1 3 5)
Study the ANS for more about what you can do with loop.
Compound loop clauses can be tricky and it is easy
to write something that doesn't do what you think it
should. When I write a nontrivial loop, I generally
quickly check the macroexpansion using my development
environment tools to make sure it's correct. In
general, cnoditional clauses should be used sparingly,
and especially on nested conditionals.