Skip to content

Commit

Permalink
Added Iterable:count method
Browse files Browse the repository at this point in the history
  • Loading branch information
SinisterRectus committed Aug 3, 2017
1 parent a6b2f7d commit ef898a6
Showing 1 changed file with 38 additions and 15 deletions.
53 changes: 38 additions & 15 deletions libs/iterables/Iterable.lua
Original file line number Diff line number Diff line change
Expand Up @@ -73,29 +73,52 @@ function Iterable:random()
end
end

function Iterable:count(fn)
local n = 0
for _ in self:findAll(fn) do
n = n + 1
end
return n
end

local function sorter(a, b)
local t1, t2 = type(a), type(b)
if t1 == 'string' and t2 == 'string' then
local n1 = tonumber(a)
if n1 then
if t1 == 'string' then
if t2 == 'string' then
local n1 = tonumber(a)
if n1 then
local n2 = tonumber(b)
if n2 then
return n1 < n2
end
end
return a:lower() < b:lower()
elseif t2 == 'number' then
local n1 = tonumber(a)
if n1 then
return n1 < b
end
return a:lower() < tostring(b)
end
elseif t1 == 'number' then
if t2 == 'number' then
return a < b
elseif t2 == 'string' then
local n2 = tonumber(b)
if n2 then
return n1 < n2
return a < n2
end
return tostring(a) < b:lower()
end
return a:lower() < b:lower()
elseif t1 == 'number' and t2 == 'number' then
return a < b
else
local m1 = getmetatable(a)
if m1 and m1.__lt then
local m2 = getmetatable(b)
if m2 and m2.__lt then
return a < b
end
end
local m1 = getmetatable(a)
if m1 and m1.__lt then
local m2 = getmetatable(b)
if m2 and m2.__lt then
return a < b
end
return tostring(a) < tostring(b)
end
return tostring(a) < tostring(b)
end

function Iterable:select(...)
Expand Down

0 comments on commit ef898a6

Please sign in to comment.