Mercurial > prosody-hg
comparison util/array.lua @ 11200:bf8f2da84007
Merge 0.11->trunk
| author | Kim Alvefur <zash@zash.se> |
|---|---|
| date | Thu, 05 Nov 2020 22:31:25 +0100 |
| parents | 5777968301e8 |
| children | 3ae6fa901a8b |
comparison
equal
deleted
inserted
replaced
| 11199:6c7c50a4de32 | 11200:bf8f2da84007 |
|---|---|
| 8 | 8 |
| 9 local t_insert, t_sort, t_remove, t_concat | 9 local t_insert, t_sort, t_remove, t_concat |
| 10 = table.insert, table.sort, table.remove, table.concat; | 10 = table.insert, table.sort, table.remove, table.concat; |
| 11 | 11 |
| 12 local setmetatable = setmetatable; | 12 local setmetatable = setmetatable; |
| 13 local getmetatable = getmetatable; | |
| 13 local math_random = math.random; | 14 local math_random = math.random; |
| 14 local math_floor = math.floor; | 15 local math_floor = math.floor; |
| 15 local pairs, ipairs = pairs, ipairs; | 16 local pairs, ipairs = pairs, ipairs; |
| 16 local tostring = tostring; | 17 local tostring = tostring; |
| 17 local type = type; | 18 local type = type; |
| 38 local res = new_array(); | 39 local res = new_array(); |
| 39 return res:append(a1):append(a2); | 40 return res:append(a1):append(a2); |
| 40 end | 41 end |
| 41 | 42 |
| 42 function array_mt.__eq(a, b) | 43 function array_mt.__eq(a, b) |
| 44 if getmetatable(a) ~= array_mt or getmetatable(b) ~= array_mt then | |
| 45 -- Lua 5.3+ calls this if both operands are tables, even if metatables differ | |
| 46 return false; | |
| 47 end | |
| 43 if #a == #b then | 48 if #a == #b then |
| 44 for i = 1, #a do | 49 for i = 1, #a do |
| 45 if a[i] ~= b[i] then | 50 if a[i] ~= b[i] then |
| 46 return false; | 51 return false; |
| 47 end | 52 end |
| 127 return true; | 132 return true; |
| 128 end | 133 end |
| 129 end); | 134 end); |
| 130 end | 135 end |
| 131 | 136 |
| 132 function array_base.pluck(outa, ina, key) | 137 function array_base.pluck(outa, ina, key, default) |
| 133 for i = 1, #ina do | 138 for i = 1, #ina do |
| 134 outa[i] = ina[i][key]; | 139 local v = ina[i][key]; |
| 140 if v == nil then | |
| 141 v = default; | |
| 142 end | |
| 143 outa[i] = v; | |
| 135 end | 144 end |
| 136 return outa; | 145 return outa; |
| 137 end | 146 end |
| 138 | 147 |
| 139 function array_base.reverse(outa, ina) | 148 function array_base.reverse(outa, ina) |
