Reverse a list
alex at tweedly.net
Tue Feb 17 23:33:14 CET 2015
On 17/02/2015 13:25, Dave Cragg wrote:
> Thanks for that.
> Disbelieving soul that I am (sorry), I puzzled for a while over the results of these two versions.
> I had to use a pencil and paper to track what was in t and what the engine was referring to after the "x" and "y" inserts. Then it became clear.
> I feel a better person.
pencil and paper is good for your soul :-)
Though it would be kinda cool to do a quick LC simulation showing
visible "animation" of the variable and index as it goes through the loop.
That's top of my list for what to do the next time I have to go on a
long train journey ....
>> On 16 Feb 2015, at 23:08, Alex Tweedly <alex at tweedly.net> wrote:
>> That's not quite correct. It doesn't do a single initial complete scan of the whole variable and keep all the pointers. What it does is (more like) keep track of how far it has currently processed, and then when it needs the next line, it scans from that remembered position forward until the next CR - and then remembers that as the current position. (Oh, and btw, it does remember how many chars were in the initial value, and uses that).
>> The fact that it does incremental scan rather than one initial scan can be seen from the following (totally useless :-) bit of code :
>> on mouseup
>> put "abc" & CR & "def" & CR & "ghi" & CR into t
>> repeat for each line L in t
>> put the number of chars in L && L &CR after msg
>> if L = "abc" then
>> put "x" into line 2 of t
>> put "qwert" after line 3 of t
>> end if
>> end repeat
>> end mouseup
>> An initial scan would have produced :
>> 3 abc
>> 3 x
>> 3 ijq
>> But we actually get:
>> 3 abc
>> 1 x
>> 6 ghiqwe
>> It is significant that it doesn't do a complete scan, because that would be a cost penalty at the start of the loop, and so could make it unnecessarily expensive to do some operations. Again that can be seen from the following (take out of context) snippet of not quite real code ....
>> put "millions and millions of lines of data" into tVar
>> repeat for each line L in tvar
>> if some condition then exit repeat
>> end repeat
>> If there were an initial scan, then you would do that whole costly scan even though you could actually exit the loop very early; because it's an incremental scan, you don't need to worry about that (so long as you are sure you will exit soon).
>> -- Alex.
> use-livecode mailing list
> use-livecode at lists.runrev.com
> Please visit this url to subscribe, unsubscribe and manage your subscription preferences:
More information about the use-livecode