summaryrefslogtreecommitdiff
path: root/data
diff options
context:
space:
mode:
authorAlbert Krewinkel <albert@zeitkraut.de>2017-12-01 17:12:56 +0100
committerAlbert Krewinkel <albert@zeitkraut.de>2017-12-01 17:12:56 +0100
commit8473a151c5685f8ceb515abf6000ab4fb7a3911c (patch)
tree0c9fe0651e83d5e35f5b0e966174780d4f117b4f /data
parent171187a4527497701b3c77bd56cea2d770d4e3b0 (diff)
List.lua: add missing fixes as discussed in #4099
The changes were missing due to an error while using git.
Diffstat (limited to 'data')
-rw-r--r--data/List.lua55
-rw-r--r--data/pandoc.lua2
2 files changed, 34 insertions, 23 deletions
diff --git a/data/List.lua b/data/List.lua
index db6233c86..b68ff5119 100644
--- a/data/List.lua
+++ b/data/List.lua
@@ -16,16 +16,11 @@ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
THIS SOFTWARE.
]]
----
--- Lua functions for pandoc scripts.
---
+--- Pandoc's List type and helper methods
+-- @classmod pandoc.List
-- @author Albert Krewinkel
-- @copyright © 2017 Albert Krewinkel
-- @license MIT
-
-------------------------------------------------------------------------
--- Metatable for lists
--- @type List
local List = {
_VERSION = "0.1.0"
}
@@ -42,6 +37,8 @@ function List:__call (o)
end
--- Concatenates two lists.
+-- @param list second list concatenated to the first
+-- @return a new list containing all elements from list1 and list2
function List:__concat (list)
local res = List.clone(self)
List.extend(res, list)
@@ -55,40 +52,40 @@ function List:clone ()
return lst
end
---- Appends the given list to the end of this list.
-function List:includes (needle)
- for i = 1, #self do
- if self[i] == needle then
- return true
- end
- end
- return false
+--- Checks if the list has an item equal to the given needle.
+-- @param needle item to search for
+-- @param init index at which the search is started
+-- @return true if a list item is equal to the needle, false otherwise
+function List:includes (needle, init)
+ return not (List.find(self, needle, init) == nil)
end
--- Returns the value and index of the first occurrence of the given item.
-- @param needle item to search for
+-- @param init index at which the search is started
-- @return first item equal to the needle, or nil if no such item exists.
-- @return index of that element
function List:find (needle, init)
return List.find_if(self, function(x) return x == needle end, init)
end
---- Returns the value and index of the first element for which test returns true.
--- @param test the test function
+--- Returns the value and index of the first element for which the predicate
+--- holds true.
+-- @param pred the predicate function
-- @param init index at which the search is started
-- @return first item for which `test` succeeds, or nil if no such item exists.
-- @return index of that element
-function List:find_if (test, init)
+function List:find_if (pred, init)
init = (init == nil and 1) or (init < 0 and #self - init) or init
for i = init, #self do
- if test(self[i], i) then
+ if pred(self[i], i) then
return self[i], i
end
end
return nil
end
---- Add the given list to the end of this list.
+--- Adds the given list to the end of this list.
-- @param list list to appended
function List:extend (list)
for i = 1, #list do
@@ -96,12 +93,26 @@ function List:extend (list)
end
end
--- Returns a copy of the current list by applying the given function to all
+--- Returns a copy of the current list by applying the given function to all
-- elements.
+-- @param fn function which is applied to all list items.
function List:map (fn)
local res = setmetatable({}, getmetatable(self))
for i = 1, #self do
- res[i] = fn(self[i])
+ res[i] = fn(self[i], i)
+ end
+ return res
+end
+
+--- Returns a new list containing all items satisfying a given condition.
+-- @param pred condition items must satisfy.
+-- @return a new list containing all items for which `test` was true.
+function List:filter (pred)
+ local res = setmetatable({}, getmetatable(self))
+ for i = 1, #self do
+ if pred(self[i], i) then
+ res[#res + 1] = self[i]
+ end
end
return res
end
diff --git a/data/pandoc.lua b/data/pandoc.lua
index 6eafde90a..74263b1fd 100644
--- a/data/pandoc.lua
+++ b/data/pandoc.lua
@@ -26,7 +26,7 @@ local M = {
_VERSION = "0.3.0"
}
-local List = require 'List'
+local List = require 'pandoc.List'
------------------------------------------------------------------------
-- The base class for pandoc's AST elements.