how to do nested loops of variable depth? Addendum

Boris_Gaertner at msg.de Boris_Gaertner at msg.de
Wed Dec 19 14:53:57 UTC 2001


Hallo Hans,

nice to hear that your problem is solved.

I rechecked my proposal and I think that I did a lot of
unnecessary assignments. Every time I saw that I reached
maximal recursion depth, I celebrated that event in a loop
that assigned values to all elements of the tuple. That is
overdoing: Every call of the method 
result:tupel:idx:allStreams: has the responsibility
to assign to exactly one index position of the tuple to
be constructed. Here is a revised version of that method
that emphasizes that responsibility and avoids unneeded
assignments. The gain in time is possibly not substantial,
but the gain in clarity is.

result: r tupel: tupel idx: myIdx allStreams: allStreams

   "  this method is recursive. Recursion runs from values 1 to allStreams 
size  of parameter myIdx. Each call of that method has the responsiblity 
to provide all possible values for one index position of the result 
tuples. That index position is given by the value of  myIdx."

 [tupel at: myIdx put: (allStreams at: myIdx) peek.
  myIdx = allStreams size
          ifTrue: [r addLast: tupel shallowCopy]
         ifFalse:  [self  result: r tupel: tupel idx: myIdx + 1 
allStreams: allStreams].
        (allStreams at: myIdx)
          next; 
             atEnd
 ] 
 whileFalse: [].
 (allStreams at: myIdx) reset.
 ^r


Cheers again
Boris Gärtner


mailto: Boris_Gaertner at msg.de
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.squeakfoundation.org/pipermail/squeak-dev/attachments/20011219/cb823cf8/attachment.htm


More information about the Squeak-dev mailing list