• Welcome to the Kancolle Wiki!
  • If you have any questions regarding site content, account registration, etc., please visit the KanColle Wiki Discord

Difference between revisions of "Module:Core"

From Kancolle Wiki
Jump to navigation Jump to search
m
Line 15: Line 15:
 
     end
 
     end
 
   end
 
   end
   return false
+
   return nil
 +
end
 +
 
 +
function Module.findBy(tbl, fn)
 +
  for k, v in pairs(tbl) do
 +
    if fn(v, k) then
 +
      return v, k
 +
    end
 +
  end
 +
  return nil
 
end
 
end
  
 
return Module
 
return Module

Revision as of 21:41, 7 February 2019

Documentation for this module may be created at Module:Core/doc

local Module = {}

-- * Function functions.

function Module.id(x)
  return x
end

-- * Collection functions.

function Module.find(tbl, v_, k_)
  for _, v in pairs(tbl) do
    if k_ and v and v[k_] == v_ or not k_ and v == v_ then
      return v
    end
  end
  return nil
end

function Module.findBy(tbl, fn)
  for k, v in pairs(tbl) do
    if fn(v, k) then
      return v, k
    end
  end
  return nil
end

return Module