Mercurial > prosody-hg
comparison util/set.lua @ 9488:a96a2fbcc6c0
util.set: Add freeze metamethod
| author | Kim Alvefur <zash@zash.se> |
|---|---|
| date | Fri, 12 Oct 2018 00:51:56 +0200 |
| parents | 4f0f5b49bb03 |
| children | 13d5fb74648f c358537c0878 |
comparison
equal
deleted
inserted
replaced
| 9487:4e38121a349d | 9488:a96a2fbcc6c0 |
|---|---|
| 19 end | 19 end |
| 20 | 20 |
| 21 local items_mt = {}; | 21 local items_mt = {}; |
| 22 function items_mt.__call(items, _, k) | 22 function items_mt.__call(items, _, k) |
| 23 return next(items, k); | 23 return next(items, k); |
| 24 end | |
| 25 | |
| 26 function set_mt:__freeze() | |
| 27 local a, i = {}, 1; | |
| 28 for item in self._items do | |
| 29 a[i], i = item, i+1; | |
| 30 end | |
| 31 return a; | |
| 24 end | 32 end |
| 25 | 33 |
| 26 local function new(list) | 34 local function new(list) |
| 27 local items = setmetatable({}, items_mt); | 35 local items = setmetatable({}, items_mt); |
| 28 local set = { _items = items }; | 36 local set = { _items = items }; |
