Skip to content

Commit

Permalink
added count routine
Browse files Browse the repository at this point in the history
  • Loading branch information
tredfern committed May 20, 2021
1 parent 8a3c946 commit de7494e
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 0 deletions.
12 changes: 12 additions & 0 deletions docs/moonpie.tables.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,18 @@ moonpie.tables

The tables utilities provides various mini-functions for helpful operations.

tables.count(set, func)
Returns the count of items that match the comparison function passed in.

.. code-block:: lua
local set = { 1, 2, 3, 4, 5, 6 }
local compare = function(v) return v % 2 == 0 end
print(tables.count(set, compare))
-- 3
tables.deepCompare(tbl1, tbl2, ignoreMT)
Tests the values in the the 2 tables to see if they look the same without having to be the same table instance.

Expand Down
14 changes: 14 additions & 0 deletions moonpie/tables/count.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
-- Copyright (c) 2021 Trevor Redfern
--
-- This software is released under the MIT License.
-- https://opensource.org/licenses/MIT

return function(set, func)
if func == nil then return #set end

local c = 0
for _, v in ipairs(set) do
if func(v) then c = c + 1 end
end
return c
end
19 changes: 19 additions & 0 deletions moonpie/tables/count_spec.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
-- Copyright (c) 2021 Trevor Redfern
--
-- This software is released under the MIT License.
-- https://opensource.org/licenses/MIT

describe("moonpie.tables.count", function()
local count = require "moonpie.tables.count"

it("returns the number of items matching a function", function()
local set = { 1, 2, 3, 4, 5, 6 }

assert.equals(3, count(set, function(i) return i % 2 == 0 end))
end)

it("just returns the number of items if no function provided", function()
local set = { 1, 2, 3, 4, 5, 6 }
assert.equals(6, count(set))
end)
end)
1 change: 1 addition & 0 deletions moonpie/tables/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ return {
assign = require "moonpie.tables.assign",
concatArray = require "moonpie.tables.concat_array",
copyKeys = require "moonpie.tables.copy_keys",
count = require "moonpie.tables.count",
countBy = require "moonpie.tables.count_by",
countKeys = require "moonpie.tables.count_keys",
deepCompare = require "moonpie.tables.deep_compare",
Expand Down
1 change: 1 addition & 0 deletions moonpie/tables/init_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ describe("moonpie.tables", function()
assert.is_function(tables.assign)
assert.is_function(tables.concatArray)
assert.is_function(tables.copyKeys)
assert.is_function(tables.count)
assert.is_function(tables.countBy)
assert.is_function(tables.countKeys)
assert.is_function(tables.deepCompare)
Expand Down

0 comments on commit de7494e

Please sign in to comment.