diff --git a/sys/apis/map.lua b/sys/apis/map.lua index 4fc3bc8..9e4ab93 100644 --- a/sys/apis/map.lua +++ b/sys/apis/map.lua @@ -3,6 +3,10 @@ local Util = require('util') local Map = { } +-- TODO: refactor +Map.merge = Util.merge +Map.shallowCopy = Util.shallowCopy + function Map.removeMatches(t, values) local function matchAll(entry) for k, v in pairs(values) do @@ -25,7 +29,7 @@ function Map.prune(t, fn) for _,k in pairs(Util.keys(t)) do local v = t[k] if type(v) == 'table' then - t[k] = Util.prune(v, fn) + t[k] = Map.prune(v, fn) end if not fn(t[k]) then t[k] = nil @@ -34,4 +38,12 @@ function Map.prune(t, fn) return t end +function Map.size(list) + local length = 0 + for _ in pairs(list) do + length = length + 1 + end + return length +end + return Map diff --git a/sys/apis/util.lua b/sys/apis/util.lua index 8b3d26b..16d00b4 100644 --- a/sys/apis/util.lua +++ b/sys/apis/util.lua @@ -277,8 +277,9 @@ function Util.filter(it, f) end function Util.reduce(t, fn, acc) + acc = acc or 0 for _, v in pairs(t) do - fn(acc, v) + acc = fn(acc, v) end return acc end