From: ············@gmail.com
Subject: loop: finally collect
Date: 
Message-ID: <f008e09e-4ca6-4182-abea-4ba96fef324c@m44g2000hsc.googlegroups.com>
hello,

in a loop form, I would like to finalize a collect procedure.
So i could do something like:
  (loop for element in list
        collect element in tmp-collection
        finally (if blabla
                    (return tmp-collection)
                    (return (append tmp-collection (list 'foo))))))

Is it possible to avoid such a `finally' clause but still using a loop
form ?

-Nicolas

From: Volkan YAZICI
Subject: Re: loop: finally collect
Date: 
Message-ID: <dabb15cf-03b3-4984-b0e5-2b7bb4b167ed@e39g2000hsf.googlegroups.com>
Does this help:

(loop for x below 10
      collect x into xs
      when (> x 3) do (return xs)
      finally (return (mapcar #'oddp xs)))
=> (1 2 3 4)
From: ············@gmail.com
Subject: Re: loop: finally collect
Date: 
Message-ID: <5888e55f-bbf4-4466-bebf-50931d18e8db@k30g2000hse.googlegroups.com>
On May 26, 4:58 pm, Volkan YAZICI <·············@gmail.com> wrote:
> Does this help:
>
> (loop for x below 10
>       collect x into xs
>       when (> x 3) do (return xs)
>       finally (return (mapcar #'oddp xs)))
> => (1 2 3 4)

Well, not exactly: I'd like to work on large lists and using loop/
collect and mapcar would make the function to iterate twice on the
list.

-Nicolas
From: Ken Tilton
Subject: Re: loop: finally collect
Date: 
Message-ID: <483ab760$0$25030$607ed4bc@cv.net>
············@gmail.com wrote:
> hello,
> 
> in a loop form, I would like to finalize a collect procedure.
> So i could do something like:
>   (loop for element in list
>         collect element in tmp-collection
>         finally (if blabla
>                     (return tmp-collection)
>                     (return (append tmp-collection (list 'foo))))))
> 
> Is it possible to avoid such a `finally' clause but still using a loop
> form ?

We'll get to work on it. When is it due?

kt

-- 
http://smuglispweeny.blogspot.com/
http://www.theoryyalgebra.com/
ECLM rant: 
http://video.google.com/videoplay?docid=-1331906677993764413&hl=en
ECLM talk: 
http://video.google.com/videoplay?docid=-9173722505157942928&q=&hl=en
From: ············@gmail.com
Subject: Re: loop: finally collect
Date: 
Message-ID: <f7787b2a-0669-40f1-98d9-a0fce2c04625@27g2000hsf.googlegroups.com>
On May 26, 3:14 pm, Ken Tilton <···········@optonline.net> wrote:
>
> We'll get to work on it. When is it due?
>

Just left the school more than 15 years ago. I'm just looking for
 + readability and efficiency (while i know "premature optimization is
the root of all evil")
 + avoiding conventional pitfalls due to imperative programing deeply
anchored in mind.

I was just wondering how to use :collect in a loop after the list
iteration has been done.

-Nicolas
From: Ken Tilton
Subject: Re: loop: finally collect
Date: 
Message-ID: <483aec0e$0$25027$607ed4bc@cv.net>
············@gmail.com wrote:
> hello,
> 
> in a loop form, I would like to finalize a collect procedure.
> So i could do something like:
>   (loop for element in list
>         collect element in tmp-collection
>         finally (if blabla
>                     (return tmp-collection)
>                     (return (append tmp-collection (list 'foo))))))
> 
> Is it possible to avoid such a `finally' clause but still using a loop
> form ?

(append <loop-form> (when blalba (list 'foo)))

kt

-- 
http://smuglispweeny.blogspot.com/
http://www.theoryyalgebra.com/
ECLM rant: 
http://video.google.com/videoplay?docid=-1331906677993764413&hl=en
ECLM talk: 
http://video.google.com/videoplay?docid=-9173722505157942928&q=&hl=en
From: Pascal J. Bourguignon
Subject: Re: loop: finally collect
Date: 
Message-ID: <7cej7pgo8z.fsf@pbourguignon.anevia.com>
············@gmail.com writes:

> hello,
>
> in a loop form, I would like to finalize a collect procedure.
> So i could do something like:
>   (loop for element in list
>         collect element in tmp-collection
>         finally (if blabla
>                     (return tmp-collection)
>                     (return (append tmp-collection (list 'foo))))))
>
> Is it possible to avoid such a `finally' clause but still using a loop
> form ?

One way would be not to loop :for :in.

(mapcar (lambda (list)
          (flet ((blahblah (x) (oddp x)))
            (loop 
               :for element = (pop list)
               :collect element 
               :if (and (endp list) (not (blahblah element))) :collect 'foo 
               :while list)))
        '((1 2 3) (1 2 3 4)))
--> ((1 2 3) (1 2 3 4 FOO))


-- 
__Pascal Bourguignon__
From: Madhu
Subject: Re: loop: finally collect
Date: 
Message-ID: <m3hccl6nka.fsf@meer.net>
* ············@gmail.com <····································@m44g2000hsc.googlegroups.com> :
Wrote on Mon, 26 May 2008 02:22:20 -0700 (PDT):

| in a loop form, I would like to finalize a collect procedure.
| So i could do something like:
|   (loop for element in list
|         collect element in tmp-collection
|         finally (if blabla
|                     (return tmp-collection)
|                     (return (append tmp-collection (list 'foo))))))
|
| Is it possible to avoid such a `finally' clause but still using a loop
| form ?

It is stll not clear what youre after here. But the answer is
yes. Change what youre looping on, ane be VERY VERY CAREFUL about nested
conditionals in LOOP. These can trip up the loop newbie badly:

(loop for (element . rest) ON list
      collect element into tmp-collection
      when (endp rest)			; end of list
        if blablabla
         return tmp-collection      
        else
          collect 'foo into tmp-collection 
          and return tmp-collection)

Once that structure is clear, you can simplify it by removing the
collection variable:

(loop for (element . rest) on list
      collect element
      when (and (endp rest) (not blablabla))
      collect 'foo)

--
Madhu
From: ············@gmail.com
Subject: Re: loop: finally collect
Date: 
Message-ID: <acb0c567-b954-4d53-8abf-4a84233337c1@e53g2000hsa.googlegroups.com>
On 26 mai, 18:24, Madhu <·······@meer.net> wrote:
> * ············@gmail.com <····································@m44g2000hsc.googlegroups.com> :
> Wrote on Mon, 26 May 2008 02:22:20 -0700 (PDT):
>
> | in a loop form, I would like to finalize a collect procedure.
> | So i could do something like:
> |   (loop for element in list
> |         collect element in tmp-collection
> |         finally (if blabla
> |                     (return tmp-collection)
> |                     (return (append tmp-collection (list 'foo))))))
> |
> | Is it possible to avoid such a `finally' clause but still using a loop
> | form ?
>
> It is stll not clear what youre after here. But the answer is
> yes. Change what youre looping on, ane be VERY VERY CAREFUL about nested
> conditionals in LOOP. These can trip up the loop newbie badly:
>
> (loop for (element . rest) ON list
>       collect element into tmp-collection
>       when (endp rest)                  ; end of list
>         if blablabla
>          return tmp-collection
>         else
>           collect 'foo into tmp-collection
>           and return tmp-collection)
>
> Once that structure is clear, you can simplify it by removing the
> collection variable:
>
> (loop for (element . rest) on list
>       collect element
>       when (and (endp rest) (not blablabla))
>       collect 'foo)
>
> --
> Madhu

You get the (best ;) point.
From: Ken Tilton
Subject: Re: loop: finally collect
Date: 
Message-ID: <483b172c$0$11635$607ed4bc@cv.net>
············@gmail.com wrote:
> On 26 mai, 18:24, Madhu <·······@meer.net> wrote:
> 
>>* ············@gmail.com <····································@m44g2000hsc.googlegroups.com> :
>>Wrote on Mon, 26 May 2008 02:22:20 -0700 (PDT):
>>
>>| in a loop form, I would like to finalize a collect procedure.
>>| So i could do something like:
>>|   (loop for element in list
>>|         collect element in tmp-collection
>>|         finally (if blabla
>>|                     (return tmp-collection)
>>|                     (return (append tmp-collection (list 'foo))))))
>>|
>>| Is it possible to avoid such a `finally' clause but still using a loop
>>| form ?
>>
>>It is stll not clear what youre after here. But the answer is
>>yes. Change what youre looping on, ane be VERY VERY CAREFUL about nested
>>conditionals in LOOP. These can trip up the loop newbie badly:
>>
>>(loop for (element . rest) ON list
>>      collect element into tmp-collection
>>      when (endp rest)                  ; end of list
>>        if blablabla
>>         return tmp-collection
>>        else
>>          collect 'foo into tmp-collection
>>          and return tmp-collection)
>>
>>Once that structure is clear, you can simplify it by removing the
>>collection variable:
>>
>>(loop for (element . rest) on list
>>      collect element
>>      when (and (endp rest) (not blablabla))
>>      collect 'foo)
>>
>>--
>>Madhu
> 
> 
> You get the (best ;) point.

No, that is just what happens on c.l.l: all the language lawyers dance 
to whatever tune the noob specifies. The use of endp is just a clever 
way to humor your misbegotten unhappiness with having to use a finally 
clause to finalize the result and successfully obscures what you seem 
unable to deal with: your loop needs a post-loop finalization (tho see 
next).

Now Madhu (to his credit) has successfully obscurantized your code such 
that the finalization disappears and looks like a normal iteration step.

Meanwhile, if blalabla does not rely on state local to the loop, then 
the loop does not in fact need a finalization step and should not have 
one -- finish off the result outside the loop as I suggested and then I 
want the damn prize.

kt

-- 
http://smuglispweeny.blogspot.com/
http://www.theoryyalgebra.com/
ECLM rant: 
http://video.google.com/videoplay?docid=-1331906677993764413&hl=en
ECLM talk: 
http://video.google.com/videoplay?docid=-9173722505157942928&q=&hl=en
From: ············@gmail.com
Subject: Re: loop: finally collect
Date: 
Message-ID: <47082d60-e976-4127-8d94-779d31c5dc31@34g2000hsh.googlegroups.com>
On 26 mai, 22:02, Ken Tilton <···········@optonline.net> wrote:
> ············@gmail.com wrote:
> > On 26 mai, 18:24, Madhu <·······@meer.net> wrote:
>
> >>* ············@gmail.com <····································@m44g2000hsc.googlegroups.com> :
> >>Wrote on Mon, 26 May 2008 02:22:20 -0700 (PDT):
>
> >>| in a loop form, I would like to finalize a collect procedure.
> >>| So i could do something like:
> >>|   (loop for element in list
> >>|         collect element in tmp-collection
> >>|         finally (if blabla
> >>|                     (return tmp-collection)
> >>|                     (return (append tmp-collection (list 'foo))))))
> >>|
> >>| Is it possible to avoid such a `finally' clause but still using a loop
> >>| form ?
>
> >>It is stll not clear what youre after here. But the answer is
> >>yes. Change what youre looping on, ane be VERY VERY CAREFUL about nested
> >>conditionals in LOOP. These can trip up the loop newbie badly:
>
> >>(loop for (element . rest) ON list
> >>      collect element into tmp-collection
> >>      when (endp rest)                  ; end of list
> >>        if blablabla
> >>         return tmp-collection
> >>        else
> >>          collect 'foo into tmp-collection
> >>          and return tmp-collection)
>
> >>Once that structure is clear, you can simplify it by removing the
> >>collection variable:
>
> >>(loop for (element . rest) on list
> >>      collect element
> >>      when (and (endp rest) (not blablabla))
> >>      collect 'foo)
>
> >>--
> >>Madhu
>
> > You get the (best ;) point.
>
> No, that is just what happens on c.l.l: all the language lawyers dance
> to whatever tune the noob specifies. The use of endp is just a clever
> way to humor your misbegotten unhappiness with having to use a finally
> clause to finalize the result and successfully obscures what you seem
> unable to deal with: your loop needs a post-loop finalization (tho see
> next).

You're right, I'm new to Lisp. Shame on me (or not ?).

>
> Now Madhu (to his credit) has successfully obscurantized your code such
> that the finalization disappears and looks like a normal iteration step.

Do you really find this code obscurantized ?

> Meanwhile, if blalabla does not rely on state local to the loop, then
> the loop does not in fact need a finalization step and should not have
> one -- finish off the result outside the loop as I suggested and then I
> want the damn prize.
>

Since I really appreciate your help, I am confused with your answer.
Considering my poor english may also have been confusing, let me
expand the context of my question and why I feel Madhu answer fit my
needs:
I am looking for an efficiency/readibility compromise since I want to
deal with with very large lists. That said, using functions such as
append (what I did in the example I wrote on the original post) would
cause the list to be traversed 2 times, while collecting only once
could speed up a bit at low cost. Of course I could do that in a
single do statement and get even more obscurantized code. I don't find
Madhu's proposal so much obscurantized.

So please don't blame me on this: I am *learning* the language at this
time (today I've learn the :ON loop keyword) but would like to make
the result useable in real life.

Hope you'll still be of so much help for next post ;) c.l.l is my only
teacher (with books, right).


-Nicolas
From: Ken Tilton
Subject: Re: loop: finally collect
Date: 
Message-ID: <483b3dea$0$15182$607ed4bc@cv.net>
············@gmail.com wrote:
> On 26 mai, 22:02, Ken Tilton <···········@optonline.net> wrote:
> 
>>············@gmail.com wrote:
>>
>>>On 26 mai, 18:24, Madhu <·······@meer.net> wrote:
>>
>>>>* ············@gmail.com <····································@m44g2000hsc.googlegroups.com> :
>>>>Wrote on Mon, 26 May 2008 02:22:20 -0700 (PDT):
>>
>>>>| in a loop form, I would like to finalize a collect procedure.
>>>>| So i could do something like:
>>>>|   (loop for element in list
>>>>|         collect element in tmp-collection
>>>>|         finally (if blabla
>>>>|                     (return tmp-collection)
>>>>|                     (return (append tmp-collection (list 'foo))))))
>>>>|
>>>>| Is it possible to avoid such a `finally' clause but still using a loop
>>>>| form ?
>>
>>>>It is stll not clear what youre after here. But the answer is
>>>>yes. Change what youre looping on, ane be VERY VERY CAREFUL about nested
>>>>conditionals in LOOP. These can trip up the loop newbie badly:
>>
>>>>(loop for (element . rest) ON list
>>>>     collect element into tmp-collection
>>>>     when (endp rest)                  ; end of list
>>>>       if blablabla
>>>>        return tmp-collection
>>>>       else
>>>>         collect 'foo into tmp-collection
>>>>         and return tmp-collection)
>>
>>>>Once that structure is clear, you can simplify it by removing the
>>>>collection variable:
>>
>>>>(loop for (element . rest) on list
>>>>     collect element
>>>>     when (and (endp rest) (not blablabla))
>>>>     collect 'foo)
>>
>>>>--
>>>>Madhu
>>
>>>You get the (best ;) point.
>>
>>No, that is just what happens on c.l.l: all the language lawyers dance
>>to whatever tune the noob specifies. The use of endp is just a clever
>>way to humor your misbegotten unhappiness with having to use a finally
>>clause to finalize the result and successfully obscures what you seem
>>unable to deal with: your loop needs a post-loop finalization (tho see
>>next).
> 
> 
> You're right, I'm new to Lisp. Shame on me (or not ?).

I think "shame" might be a bit over the top. :)

> 
> 
>>Now Madhu (to his credit) has successfully obscurantized your code such
>>that the finalization disappears and looks like a normal iteration step.
> 
> 
> Do you really find this code obscurantized ?

Yes, but that is OK when optimizing so....

> Since I really appreciate your help, I am confused with your answer.
> Considering my poor english may also have been confusing, let me
> expand the context of my question and why I feel Madhu answer fit my
> needs:
> I am looking for an efficiency/readibility compromise since I want to
> deal with with very large lists. That said, using functions such as
> append (what I did in the example I wrote on the original post) would
> cause the list to be traversed 2 times, while collecting only once
> could speed up a bit at low cost.

...oh, OK, you do not even want an NCONC at the end, which would still 
have to run down the newly collected list to find the end.

Fine. Jeez. Exactly how long are these lists?! Something tells me if 
they are /that/ long that they will be a performance slam anyway.

kt

-- 
http://smuglispweeny.blogspot.com/
http://www.theoryyalgebra.com/
ECLM rant: 
http://video.google.com/videoplay?docid=-1331906677993764413&hl=en
ECLM talk: 
http://video.google.com/videoplay?docid=-9173722505157942928&q=&hl=en
From: ············@gmail.com
Subject: Re: loop: finally collect
Date: 
Message-ID: <593551fb-33fe-4863-ac11-bf6e2f25d5d9@x35g2000hsb.googlegroups.com>
On May 27, 12:46 am, Ken Tilton <···········@optonline.net> wrote:
> [...]
> Fine. Jeez. Exactly how long are these lists?! Something tells me if
> they are /that/ long that they will be a performance slam anyway.
>
> kt

A performance slam compared to what ? I have to deal with lists of
about thousands of cel^^conses, and have to perform a lot of search/
insertions/substitutions on them. Note I just want the first shot of
my first Lisp program to be useable; I'll deep into optimization later
if needed.

-Nicolas
From: Ken Tilton
Subject: Re: loop: finally collect
Date: 
Message-ID: <483c0757$0$11638$607ed4bc@cv.net>
············@gmail.com wrote:
> On May 27, 12:46 am, Ken Tilton <···········@optonline.net> wrote:
> 
>>[...]
>>Fine. Jeez. Exactly how long are these lists?! Something tells me if
>>they are /that/ long that they will be a performance slam anyway.
>>
>>kt
> 
> 
> A performance slam compared to what ?

Some other data strucrure? Array? Hash table? B-tree?

> I have to deal with lists of
> about thousands of cel^^conses, and have to perform a lot of search/
> insertions/substitutions on them.

Then it hardly matters had you done this once:

     (nconcc <loop-collect> (list 'foo)) ;; /not/ '(foo)!

...but the trick Madhu gave you was fun anyway.

> Note I just want the first shot of
> my first Lisp program to be useable; I'll deep into optimization later
> if needed.

Have fun. :)

kenny

-- 
http://smuglispweeny.blogspot.com/
http://www.theoryyalgebra.com/
ECLM rant: 
http://video.google.com/videoplay?docid=-1331906677993764413&hl=en
ECLM talk: 
http://video.google.com/videoplay?docid=-9173722505157942928&q=&hl=en
From: John Thingstad
Subject: Re: loop: finally collect
Date: 
Message-ID: <op.ubr6ugpyut4oq5@pandora.alfanett.no>
P� Mon, 26 May 2008 23:08:29 +0200, skrev <············@gmail.com>:


>
> Since I really appreciate your help, I am confused with your answer.
> Considering my poor english may also have been confusing, let me
> expand the context of my question and why I feel Madhu answer fit my
> needs:
> I am looking for an efficiency/readibility compromise since I want to
> deal with with very large lists. That said, using functions such as
> append (what I did in the example I wrote on the original post) would
> cause the list to be traversed 2 times, while collecting only once
> could speed up a bit at low cost. Of course I could do that in a
> single do statement and get even more obscurantized code. I don't find
> Madhu's proposal so much obscurantized.
>
> So please don't blame me on this: I am *learning* the language at this
> time (today I've learn the :ON loop keyword) but would like to make
> the result useable in real life.
>
> Hope you'll still be of so much help for next post ;) c.l.l is my only
> teacher (with books, right).
>
>
> -Nicolas

If they are so long wouldn't it be better to use a array?
Half the memory for fixnum's..

--------------
John Thingstad
From: Stanisław Halik
Subject: Re: loop: finally collect
Date: 
Message-ID: <g1jv8r$e0d$1@news2.task.gda.pl>
thus spoke ············@gmail.com:

>> Meanwhile, if blalabla does not rely on state local to the loop, then
>> the loop does not in fact need a finalization step and should not have
>> one -- finish off the result outside the loop as I suggested and then I
>> want the damn prize.

> That said, using functions such as append [...] would cause the list
> to be traversed 2 times, while collecting only once could speed up a
> bit at low cost.

Roll up your own tail-consing macro instead of being dependent on the
LOOP straitjacket. Or use Iterate which solves the problem of the
distinction between Lisp forms and LOOP keywords.

-- 
The great peril of our existence lies in the fact that our diet consists
entirely of souls. -- Inuit saying
From: Alex Mizrahi
Subject: Re: loop: finally collect
Date: 
Message-ID: <483eb6fd$0$90268$14726298@news.sunsite.dk>
 ne> I am looking for an efficiency/readibility compromise since I want to
 ne> deal with with very large lists. That said, using functions such as
 ne> append (what I did in the example I wrote on the original post) would
 ne> cause the list to be traversed 2 times, while collecting only once
 ne> could speed up a bit at low cost.

indeed, that would speed up _a bit_. list traversal is orders of maginitude
faster than consing list of same length, so performance difference is likely 
to
 be barely noticable. do you really want to obscure you code to gain 
fraction
 of percent of performance?

OTOH there will be huge difference if you use NCONC instead of APPEND --
NCONC won't do consing.