Module:HashUtil

From Langrisser Wiki
Jump to: navigation, search

Lua metamodule containing higher-order functions for non-sequence tables.

  • t: Table which contains arbitrary key-value pairs.
  • f: Function which accepts a value from a table as its first argument and the value's corresponding key as its second argument.
  • pr: Predicate which accepts a value from a table as its first argument and the value's corresponding key as its second argument. Only truthiness of the return value is considered.
  • op: Function which accepts two table elements.

Generation[edit source]

  • from_lists (ks, vs)
Creates a table from a list of keys and a list of values. Later duplicate keys take precedence.
  • from_pairs (s, g)
Creates a table by calling g on each element of the sequence s and using its first two return values as a key-value pair.
  • generate (s, g)
Returns {[s[1]] = g(s[1]), [s[2]] = g(s[2]), ..., [s[#s]] = g(s[#s])}.

Counting[edit source]

  • any (t, pr)
Returns true if pr is true for any element of t.
  • all (t, pr)
Returns true if pr is true for all elements of t.
  • none (t, pr)
Returns true if pr is true for none of the elements of t.
  • count (t, x)
Returns the number of elements of t that compare equal to x.
  • count_if (t, pr)
Returns the number of elements of t that satisfy pr.

Searching[edit source]

  • find (t, x)
Returns the key-value pair of any of the table's elements that compares equal to x, or nil if none can be found.
  • find_if (t, pr)
Returns the key-value pair of any of the table's elements that satisfies pr, or nil if none can be found.

Transformation[edit source]

  • map (t, f)
Returns a new table with the results of calling f on each element of t.
  • map_self (t, f)
Replaces every element of t with the result of calling f on it. Returns t.
  • zip (t1, t2, op)
Returns a new table with the results of calling op on each element of t1 and the corresponding element of t2 with the same key.
  • select (t, pr)
Returns a new table containing only the elements of t that satisfy pr.
  • merge (t1, t2)
Returns a new table containing key-value pairs from both tables. t2 takes precedence.
  • merge_self (t1, t2)
Adds all key-value pairs from t2 to t1. Returns t1.
  • invert (t)
Returns a new table with keys becoming values and values becoming keys. Duplicate keys are overwritten in an arbitrary order.
  • keys (t)
Returns a sequence containing the keys of the table in an arbitrary order.
  • values (t)
Returns a sequence containing the values of the table in an arbitrary order.
  • to_lists (t)
Returns keys(t), values(t). The orders from the two sequences are guaranteed to be the same.

Other[edit source]

  • sorted_pairs (t)
Returns a generator which iterates through the key-value pairs of t, sorted by keys with <.
Equivalent to sorted_pairs(t, function (v1, v2, k1, k2) return k1 < k2 end).
  • sorted_pairs (t, g)
Returns a generator which iterates through the key-value pairs of t, sorted by g, which accepts the values of two pairs to compare followed by their corresponding keys.

local pairs = pairs

local from_lists = function (ks, vs)
  local z = {}
  for i = 1, #ks do
    z[ks[i]] = vs[i]
  end
  return z
end

local from_pairs = function (s, g)
  local z = {}
  for i = 1, #s do
    local k, v = g(s[i])
    z[k] = v
  end
  return z
end

local generate = function (s, g)
  local z = {}
  for i = 1, #s do
    z[s[i]] = g(s[i])
  end
  return z
end

local any = function (t, pr)
  for k, v in pairs(t) do
    if pr(v, k) then
      return true
    end
  end
  return false
end

local all = function (t, pr)
  for k, v in pairs(t) do
    if not pr(v, k) then
      return false
    end
  end
  return true
end

local none = function (t, pr)
  for k, v in pairs(t) do
    if pr(v, k) then
      return false
    end
  end
  return true
end

local count = function (t, x)
  local z = 0
  for _, v in pairs(t) do
    if v == x then
      z = z + 1
    end
  end
  return z
end

local count_if = function (t, pr)
  local z = 0
  for k, v in pairs(t) do
    if pr(v, k) then
      z = z + 1
    end
  end
  return z
end

local find = function (t, x)
  for k, v in pairs(t) do
    if v == x then
      return k, v
    end
  end
end

local find_if = function (t, pr)
  for k, v in pairs(t) do
    if pr(v, k) then
      return k, v
    end
  end
end

local map = function (t, f)
  local z = {}
  for k, v in pairs(t) do
    z[k] = f(v, k)
  end
  return z
end

local map_self = function (t, f)
  for k, v in pairs(t) do
    t[k] = f(v, k)
  end
  return t
end

local zip = function (t1, t2, op)
  local z = {}
  for k, v in pairs(t1) do
    z[k] = op(v, t2[k])
  end
  return z
end

local select = function (t, pr)
  local z = {}
  for k, v in pairs(t) do
    if pr(v, k) then
      z[k] = v
    end
  end
  return z
end

local merge = function (t1, t2)
  local z = {}
  for k, v in pairs(t1) do
    z[k] = v
  end
  for k, v in pairs(t2) do
    z[k] = v
  end
  return z
end

local merge_self = function (t1, t2)
  for k, v in pairs(t2) do
    t1[k] = v
  end
  return t1
end

local invert = function (t)
  local z = {}
  for k, v in pairs(t) do
    z[v] = k
  end
  return z
end

local keys = function (t)
  local z = {}
  for k in pairs(t) do
    z[#z + 1] = k
  end
  return z
end

local values = function (t)
  local z = {}
  for _, v in pairs(t) do
    z[#z + 1] = v
  end
  return z
end

local to_lists = function (t)
  local ks = {}
  local vs = {}
  for k, v in pairs(t) do
    ks[#ks + 1] = k
    vs[#vs + 1] = v
  end
  return ks, vs
end

local sorted_pairs = function (t, f)
  local ks = keys(t)
  if f then
    table.sort(ks, function (k1, k2)
      return f(t[k1], t[k2], k1, k2)
    end)
  else
    table.sort(ks)
  end
  local i = 0
  return function ()
    i = i + 1
    if i <= #ks then
      return ks[i], t[ks[i]]
    end
  end
end

return {
  from_lists = from_lists,
  from_pairs = from_pairs,
  generate = generate,

  any = any,
  all = all,
  none = none,
  count = count,
  count_if = count_if,

  find = find,
  find_if = find_if,

  map = map,
  map_self = map_self,
  zip = zip,
  select = select,
  merge = merge,
  merge_self = merge_self,
  invert = invert,
  keys = keys,
  values = values,
  to_lists = to_lists,

  sorted_pairs = sorted_pairs,
}