Comparison

util/array.lua @ 1372:3b13bb57002e

util.array: Make array:reverse() and array:shuffle() return the array to allow chaining
author Matthew Wild <mwild1@gmail.com>
date Sat, 20 Jun 2009 22:45:07 +0100
parent 1371:9e45bdf55353
child 1373:120275376bbb
comparison
equal deleted inserted replaced
1371:9e45bdf55353 1372:3b13bb57002e
40 local len = #self; 40 local len = #self;
41 for i=1,#self do 41 for i=1,#self do
42 local r = math.random(i,len); 42 local r = math.random(i,len);
43 self[i], self[r] = self[r], self[i]; 43 self[i], self[r] = self[r], self[i];
44 end 44 end
45 return self;
45 end 46 end
46 47
47 function array:reverse() 48 function array:reverse()
48 local len = #self-1; 49 local len = #self-1;
49 for i=len,1,-1 do 50 for i=len,1,-1 do
50 self:push(self[i]); 51 self:push(self[i]);
51 self:pop(i); 52 self:pop(i);
52 end 53 end
54 return self;
53 end 55 end
54 56
55 function array:append(array) 57 function array:append(array)
56 local len,len2 = #self, #array; 58 local len,len2 = #self, #array;
57 for i=1,len2 do 59 for i=1,len2 do