Comparison

util/array.lua @ 7019:abcd5ec3ee41

util.array: Fix minory style issues
author Kim Alvefur <zash@zash.se>
date Wed, 23 Dec 2015 08:32:13 +0100
parent 6777:5de6b93d0190
child 7020:6ab9c691c4c6
comparison
equal deleted inserted replaced
7018:5c3d4254d415 7019:abcd5ec3ee41
35 35
36 setmetatable(array, { __call = new_array }); 36 setmetatable(array, { __call = new_array });
37 37
38 -- Read-only methods 38 -- Read-only methods
39 function array_methods:random() 39 function array_methods:random()
40 return self[math_random(1,#self)]; 40 return self[math_random(1, #self)];
41 end 41 end
42 42
43 -- These methods can be called two ways: 43 -- These methods can be called two ways:
44 -- array.method(existing_array, [params [, ...]]) -- Create new array for result 44 -- array.method(existing_array, [params [, ...]]) -- Create new array for result
45 -- existing_array:method([params, ...]) -- Transform existing array into result 45 -- existing_array:method([params, ...]) -- Transform existing array into result
46 -- 46 --
47 function array_base.map(outa, ina, func) 47 function array_base.map(outa, ina, func)
48 for k,v in ipairs(ina) do 48 for k, v in ipairs(ina) do
49 outa[k] = func(v); 49 outa[k] = func(v);
50 end 50 end
51 return outa; 51 return outa;
52 end 52 end
53 53
54 function array_base.filter(outa, ina, func) 54 function array_base.filter(outa, ina, func)
55 local inplace, start_length = ina == outa, #ina; 55 local inplace, start_length = ina == outa, #ina;
56 local write = 1; 56 local write = 1;
57 for read=1,start_length do 57 for read = 1, start_length do
58 local v = ina[read]; 58 local v = ina[read];
59 if func(v) then 59 if func(v) then
60 outa[write] = v; 60 outa[write] = v;
61 write = write + 1; 61 write = write + 1;
62 end 62 end
63 end 63 end
64 64
65 if inplace and write <= start_length then 65 if inplace and write <= start_length then
66 for i=write,start_length do 66 for i = write, start_length do
67 outa[i] = nil; 67 outa[i] = nil;
68 end 68 end
69 end 69 end
70 70
71 return outa; 71 return outa;
78 t_sort(outa, ...); 78 t_sort(outa, ...);
79 return outa; 79 return outa;
80 end 80 end
81 81
82 function array_base.pluck(outa, ina, key) 82 function array_base.pluck(outa, ina, key)
83 for i=1,#ina do 83 for i = 1, #ina do
84 outa[i] = ina[i][key]; 84 outa[i] = ina[i][key];
85 end 85 end
86 return outa; 86 return outa;
87 end 87 end
88 88
106 end 106 end
107 107
108 --- These methods only mutate the array 108 --- These methods only mutate the array
109 function array_methods:shuffle(outa, ina) 109 function array_methods:shuffle(outa, ina)
110 local len = #self; 110 local len = #self;
111 for i=1,#self do 111 for i = 1, #self do
112 local r = math_random(i,len); 112 local r = math_random(i, len);
113 self[i], self[r] = self[r], self[i]; 113 self[i], self[r] = self[r], self[i];
114 end 114 end
115 return self; 115 return self;
116 end 116 end
117 117
118 function array_methods:append(array) 118 function array_methods:append(array)
119 local len,len2 = #self, #array; 119 local len, len2 = #self, #array;
120 for i=1,len2 do 120 for i = 1, len2 do
121 self[len+i] = array[i]; 121 self[len+i] = array[i];
122 end 122 end
123 return self; 123 return self;
124 end 124 end
125 125
145 --- These methods always create a new array 145 --- These methods always create a new array
146 function array.collect(f, s, var) 146 function array.collect(f, s, var)
147 local t = {}; 147 local t = {};
148 while true do 148 while true do
149 var = f(s, var); 149 var = f(s, var);
150 if var == nil then break; end 150 if var == nil then break; end
151 t_insert(t, var); 151 t_insert(t, var);
152 end 152 end
153 return setmetatable(t, array_mt); 153 return setmetatable(t, array_mt);
154 end 154 end
155 155