From: Thaddeus L Olczyk
Subject: Collect-if?
Date: 
Message-ID: <3bphjuo50dipv6q4ovds35c4gg9rq9gv12@4ax.com>
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))

From: Paul F. Dietz
Subject: Re: Collect-if?
Date: 
Message-ID: <3D38ED0C.EEF644F2@dls.net>
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
From: Thomas F. Burdick
Subject: Re: Collect-if?
Date: 
Message-ID: <xcvfzye6w30.fsf@famine.OCF.Berkeley.EDU>
"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!       |                        
    /       /      `-----------------------'                        
   (   -.  |                               
   |     ) |                               
  (`-.  '--.)                              
   `. )----'                               
From: Friedrich Dominicus
Subject: Re: Collect-if?
Date: 
Message-ID: <873cuf80tm.fsf@fbigm.here>
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
From: Software Scavenger
Subject: Re: Collect-if?
Date: 
Message-ID: <a6789134.0207200756.358c2759@posting.google.com>
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))
From: Friedrich Dominicus
Subject: Re: Collect-if?
Date: 
Message-ID: <87d6ti756k.fsf@fbigm.here>
··········@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
From: Steven M. Haflich
Subject: Re: Collect-if?
Date: 
Message-ID: <3D39641C.2050806@alum.mit.edu>
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.