mirror of
https://github.com/TangentFoxy/lume.git
synced 2024-11-19 07:04:24 +00:00
Exposed internal isarray()
function as lume.isarray()
This commit is contained in:
parent
a20a39c8ee
commit
f226cf2e64
19
lume.lua
19
lume.lua
@ -40,12 +40,8 @@ local iscallable = function(x)
|
|||||||
return mt and mt.__call ~= nil
|
return mt and mt.__call ~= nil
|
||||||
end
|
end
|
||||||
|
|
||||||
local isarray = function(x)
|
|
||||||
return (type(x) == "table" and x[1] ~= nil) and true or false
|
|
||||||
end
|
|
||||||
|
|
||||||
local getiter = function(x)
|
local getiter = function(x)
|
||||||
if isarray(x) then
|
if lume.isarray(x) then
|
||||||
return ipairs
|
return ipairs
|
||||||
elseif type(x) == "table" then
|
elseif type(x) == "table" then
|
||||||
return pairs
|
return pairs
|
||||||
@ -142,6 +138,11 @@ function lume.weightedchoice(t)
|
|||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
function lume.isarray(x)
|
||||||
|
return (type(x) == "table" and x[1] ~= nil) and true or false
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
function lume.push(t, ...)
|
function lume.push(t, ...)
|
||||||
local n = select("#", ...)
|
local n = select("#", ...)
|
||||||
for i = 1, n do
|
for i = 1, n do
|
||||||
@ -151,11 +152,11 @@ function lume.push(t, ...)
|
|||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
function lume.remove(t, x)
|
function lume.remove(t, x)
|
||||||
local iter = getiter(t)
|
local iter = getiter(t)
|
||||||
for i, v in iter(t) do
|
for i, v in iter(t) do
|
||||||
if v == x then
|
if v == x then
|
||||||
if isarray(t) then
|
if lume.isarray(t) then
|
||||||
table.remove(t, i)
|
table.remove(t, i)
|
||||||
break
|
break
|
||||||
else
|
else
|
||||||
@ -381,7 +382,7 @@ function lume.count(t, fn)
|
|||||||
if fn(v) then count = count + 1 end
|
if fn(v) then count = count + 1 end
|
||||||
end
|
end
|
||||||
else
|
else
|
||||||
if isarray(t) then
|
if lume.isarray(t) then
|
||||||
return #t
|
return #t
|
||||||
end
|
end
|
||||||
for _ in iter(t) do count = count + 1 end
|
for _ in iter(t) do count = count + 1 end
|
||||||
@ -487,7 +488,7 @@ end
|
|||||||
function lume.combine(...)
|
function lume.combine(...)
|
||||||
local n = select('#', ...)
|
local n = select('#', ...)
|
||||||
if n == 0 then return noop end
|
if n == 0 then return noop end
|
||||||
if n == 1 then
|
if n == 1 then
|
||||||
local fn = select(1, ...)
|
local fn = select(1, ...)
|
||||||
if not fn then return noop end
|
if not fn then return noop end
|
||||||
assert(iscallable(fn), "expected a function or nil")
|
assert(iscallable(fn), "expected a function or nil")
|
||||||
|
Loading…
Reference in New Issue
Block a user