Skip to content

Commit

Permalink
Added List:invoke(...) function.
Browse files Browse the repository at this point in the history
  • Loading branch information
Robert Zenz committed Aug 15, 2015
1 parent bd62a1e commit 0fdeb22
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 0 deletions.
26 changes: 26 additions & 0 deletions doc/modules/list.html
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,11 @@ <h2><a href="#Functions">Functions</a></h2>
<tr>
<td class="name" nowrap><a href="#List:index">List:index (item, equals)</a></td>
<td class="summary">Returns the index of the given item.</td>
</tr>
<tr>
<td class="name" nowrap><a href="#List:invoke">List:invoke (...)</a></td>
<td class="summary">If the List contains functions, this invokes all items with the given
parameters.</td>
</tr>
<tr>
<td class="name" nowrap><a href="#List:matching">List:matching (condition)</a></td>
Expand Down Expand Up @@ -294,6 +299,27 @@ <h3>Returns:</h3>



</dd>
<dt>
<a name = "List:invoke"></a>
<strong>List:invoke (...)</strong>
</dt>
<dd>
If the List contains functions, this invokes all items with the given
parameters.


<h3>Parameters:</h3>
<ul>
<li><span class="parameter">...</span>
The parameters to invoke the functions.
</li>
</ul>





</dd>
<dt>
<a name = "List:matching"></a>
Expand Down
30 changes: 30 additions & 0 deletions test/list.lua
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,36 @@ test.run("index", function()
test.equals(-1, list:index("f"))
end)

test.run("invoke", function()
local list = List:new()

local a_invoked = nil
local b_invoked = nil
local c_invoked = nil

list:add(function(parama, paramb, paramc)
a_invoked = (parama ~= nil)
and (paramb ~= nil)
and (paramc ~= nil)
end)
list:add(function(parama, paramb, paramc)
b_invoked = (parama ~= nil)
and (paramb ~= nil)
and (paramc ~= nil)
end)
list:add(function(parama, paramb, paramc)
c_invoked = (parama ~= nil)
and (paramb ~= nil)
and (paramc ~= nil)
end)

list:invoke(5, 6, 7)

test.equals(true, a_invoked)
test.equals(true, b_invoked)
test.equals(true, c_invoked)
end)

test.run("matching", function()
local list = List:new()
list:add("a", "b", "c", "d", "e")
Expand Down
11 changes: 11 additions & 0 deletions utils/list.lua
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,17 @@ function List:index(item, equals)
return -1
end

--- If the List contains functions, this invokes all items with the given
-- parameters.
--
-- @param ... The parameters to invoke the functions.
function List:invoke(...)
for index = self.base, self.counter - 1, 1 do
local item = self.items[index]
item(...)
end
end

--- Returns a List with all items that match the given condition.
--
-- @param condition The condition, a function that accepts one parameter,
Expand Down

0 comments on commit 0fdeb22

Please sign in to comment.