Re: Re: A Reap Sow question

• To: mathgroup at smc.vnet.net
• Subject: [mg65237] Re: [mg65216] Re: [mg65193] A Reap Sow question
• From: Andrzej Kozlowski <akoz at mimuw.edu.pl>
• Date: Sun, 19 Mar 2006 03:19:17 -0500 (EST)
• References: <200603170513.AAA09771@smc.vnet.net> <4EF26559-3634-461D-9E5E-6522C06A6751@mimuw.edu.pl> <200603181140.GAA09775@smc.vnet.net>
• Sender: owner-wri-mathgroup at wolfram.com

```Hi János,

Well, who am I to try to give advice to Trurl? Besides, I have to be
careful, I would not want my advice to be responsible for the
creation of the second stupidest machine in the universe.
Anyway, without knowing any details it would seem from your
description that what he (or you) need is probably Nest or perhaps
NestWhile rather than Reap and Sow, or perhaps a combination of the
two. Note in particular the fourth argument in NestWhile, which makes
it possible to apply the test function to all the results obtained so
far. Here is a simple example of using NestWhile with Sow and Reap:
we want to start with 2 and proceed in steps of 1 collecting number
until the number of primes we have collected is less than the number
of non-primes, and we want only the primes as the output. Here is one
way to do this:

Last[Reap[NestWhile[(If[PrimeQ[#1], Sow[#1]]; #1 + 1) & , 2,
Count[{##1}, _?PrimeQ] >= Floor[Length[{##1}]]/2 & , All]]]

{{2, 3, 5, 7}}

I think you should be able to use Nest or NestWhile to "to pull back
some earlier members of the list,  because they are needed to create
the new members of the list" but this is the most I can say without
more information. Of course if I knew how to make a demon of the
second kind I could get all the information without asking but
thermodynamics has always been one of the weak spots in my knowledge.

Andrzej Kozlowski

On 18 Mar 2006, at 12:40, János wrote:

> Hi Andrzej,
>
> Yes, you are right.  I thought of the i++ and that was so satisfying
> for me that I did not write it down :)  It is like attending to go
> through the Universe to get the magic key to the Princess's heart,
> but really never blast off from the ground, because I already did
> steal the only key to it from the secret room.
>
> Well, me being a "paleface", let me reformulate my thoughts.
>
> We all know that there is a  Cornu spiral connecting the houses of
> the two world known constructors Trurl and Klapaucius.  Turl is
> making his evolving time capsules and as soon as he does one, he sets
> it on its feet and gives a gentle bang on the back to start marching
> on the road to the direction of Klapaucius's house.  Of course Trurl
> does not want to anger Klapaucius with cookie cutter time capsules -
> that is why he calls them the "evolving" ones -, so he Sow some extra
> parts into them before sending them on their way.   As the capsules
> are marching on the road - first around his house a few times - Trurl
> periodically reaches out to them to get back the now evolved extra
> parts to put into the newly assembled evolving time capsule.
> At the end of the road Klapaucius is Reaping all the time capsules
> and he is putting them through a rigorous quality inspection, because
> he knows that when Trurl decides to create evolving time capsules,
> some Big Bangs can happen if one is not careful enough.
>
> So, my situation is similar to Trurl's.  Right now I am appending to
> a list  and as I am adding more and more items to it by using
> AppendTo, I have to pull back some earlier members of the list,
> because they are needed to create the new members of the list.  This
> method works satisfactory for the first 60 - 80 members of the list
> and my G5 duo core constructor - although it is NOTHING compared to
> Trurl - is happy to churn out them in less than a minute.  As soon as
> I have about 110 items, it takes half an hour to put one new item
> into it.  So I was thinking to give up on AppendTo and use Reap Sow
> to collect the list elements, but on the same time I needed to look
> into the list for earlier elements, because I needed them for
> construction.
>
> With the best,
>
> János
>
>
> On Mar 17, 2006, at 5:03 AM, Andrzej Kozlowski wrote:
>
>>
>> On 17 Mar 2006, at 06:13, János wrote:
>>
>>>
>>> Let say I have a construct like
>>>
>>> collection=Reap[i=1; While[i<=10,Sow[i]];]
>>
>> Looks like one of those journeys that take infinitely long yet
>> never leave the starting point. Again something out of Lem? ;-)
>> Presumably you meant:
>>
>> collection = First[Last[Reap[i = 1; While[i <= 10, Sow[i] ;i++]]]]
>>
>> {1,2,3,4,5,6,7,8,9,10}
>>
>>>
>>> Is it possible to put a condition on Sow referring back to
>>> collection ?  Something like:
>>>
>>> collection=Reap[i=1; While[i<=10,If[Length[First[Last[collection]] ]
>>>> 5,,Sow[i] ] ];]
>>
>>
>> It seems obvious that you can't  since "collection" won't have any
>> value until you Reap what you have Sown.
>>>
>>> If not, - I have not seen any indication in Help either way - , what
>>> technique can be used to get a similar effect without resorting to
>>> global lists or arrays?
>>>
>>
>> What do you mean by " a similar effect"? Is this a "similar effect":
>>
>> collection = First[Last[
>>     Reap[i = 1; While[i <= 10, If[i <= 5, Sow[i]]; i++]]]];
>>
>> {1,2,3,4,5}
>>
>> More seriously, probably you can make use of tags in Sow and Reap
>> as in
>>
>>
>> Flatten[Last[  Reap[i = 1; While[i <= 10,  Sow[i,i]; i++],_?
>> (#<=5&)]]]
>>
>> {1,2,3,4,5}
>>
>> but that really depends on what you want to achieve which I can't
>> make out from your question.
>>
>> Andrzej Kozlowski
>>
>>
>>
>
>
>
> --------------------------------------------------
> Lunatics always attain their goals better than normal people do. It
> happens because there are no moral boundaries for them: no shame, no
> truthfulness, no conscience, and not even fear.    <Leo Tolstoy>
>

```

• Prev by Date: Re: Unevaluated values of a[[i]]+b[[j]]
• Next by Date: Re: BinomialDistribution
• Previous by thread: Re: A Reap Sow question
• Next by thread: run time variability