Reverse a list

Ben Rubinstein benr_mc at cogapp.com
Mon Feb 16 14:51:48 EST 2015


On 15/02/2015 02:28, Mike Kerner wrote:
> I just read the dictionary entry (again), and I would say that it is not at
> all clear that there would appear to be an ENORMOUS difference.  For
> starters, you have to read waaaaaaaay down to find the mention, it isn't
> really called out with a NOTE or anything else to draw one's attention to
> it, and it is definitely understated.  Even mentioning "order of magnitude"
> would be better (although it would appear to be an understatement).  I
> literally had no idea until I ran into this, by accident, and was
> exchanging notes with Peter.  The difference is staggering, and it really
> should be made much more obvious.

I think that we need to be clear that it's not true that "repeat with" is 
massively faster than "repeat for".  They're not comparable, they're doing 
different things.

What _is_ true is that if you want to iterate over the chunks of a variable, 
the most efficient way to do that is using the "repeat for each" construction. 
  But there's nothing inherently faster in the loop form, it's what you do 
with it that counts.  If you wanted to print the squares of the first 100 
integers, "repeat with n = 1 to 100" is the way to go - don't make a list of 
those 100 integers so that you can use "repeat for each" on it.

I'm not saying the documentation is perfect, far from it.  But what it says on 
this topic is accurate, i.e. "*if* you want to perform an action on each chunk 
in a container. This form is much faster than ... with".

On 14/02/2015 22:00, Mike Kerner wrote:
> You would think that Edinburgh would think about tweaking an algorithm,
> since REPEAT WITH seems to be a special case of REPEAT FOR, and you can
> generate the REPEAT WITH behavior by wrapping the REPEAT FOR...

That's exactly what (Scott Raney) did, back in the mists of time.  He wrote 
extra code to make iterating over the chunks of a variable exceptionally fast; 
and added syntax for the programmer to indicate that they wanted to use this 
algorithm (which has other disadvantages).

REPEAT WITH is certainly not a special case of REPEAT FOR EACH (note 'repeat 
for' is actually something else); it's closer to the other way round, that is 
REPEAT FOR EACH could be regarded as a specialisation for a common case 
otherwise handled by REPEAT WITH.

Ben




More information about the use-livecode mailing list