Reverse a list

Bob Sneidar bobsneidar at iotecdigital.com
Mon Feb 9 11:01:44 EST 2015


Yes, but the second way is so much more sophisticated.

Bob S


On Feb 8, 2015, at 13:52 , J. Landman Gay <jacque at hyperactivesw.com<mailto:jacque at hyperactivesw.com>> wrote:

Just tinkering around on a lazy Sunday, and I thought I'd come up with a neat way to reverse a list without using the traditional clunky method:

function reverseSort pList
 repeat for each line l in pList
   put l & cr before tList
 end repeat
 return tList
end reverseSort

One of the best things I learned from a past LC converence came from Jerry Daniels who said "let the engine do it." It's almost always faster and more efficient. With that in mind I wrote this:

local sNum

function reverseText pList
 put the number of lines in pList into sNum
 sort lines of pList numeric by reverseSort(each)
 return pList
end reverseText

function reverseSort pTxt
 subtract 1 from sNum
 return sNum && pTxt
end reverseSort

Works great and I was proud. Then I did some timing tests and found out the two methods are very close to equivalent in timing, and on long lists, the first way is actually faster.

So much for improving on LC's text chunking speed. Pah.

--
Jacqueline Landman Gay         |     jacque at hyperactivesw.com<mailto:jacque at hyperactivesw.com>




More information about the use-livecode mailing list