From 4ede11a75f37bb35d384fc9ce95231671e18acf3 Mon Sep 17 00:00:00 2001 From: Albert Krewinkel Date: Sat, 11 Jan 2020 20:35:56 +0100 Subject: Lua: add methods `insert`, `remove`, and `sort` to pandoc.List The functions `table.insert`, `table.remove`, and `table.sort` are added to pandoc.List elements. They can be used as methods, e.g. local numbers = pandoc.List {2, 3, 1} numbers:sort() -- numbers is now {1, 2, 3} --- data/pandoc.List.lua | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) (limited to 'data') diff --git a/data/pandoc.List.lua b/data/pandoc.List.lua index f5b118965..b33c30876 100644 --- a/data/pandoc.List.lua +++ b/data/pandoc.List.lua @@ -104,6 +104,12 @@ function List:includes (needle, init) return not (List.find(self, needle, init) == nil) end +--- Insert an element into the list. Alias for `table.insert`. +-- @param list list +-- @param[opt] pos position at which the new element is to be inserted +-- @param value value to insert +List.insert = table.insert + --- 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. @@ -115,6 +121,17 @@ function List:map (fn) return res end +--- Remove element from list (alias for `table.remove`) +-- @param list list +-- @param[opt] pos position of the element to be removed (default: #list) +-- @return the removed element +List.remove = table.remove + +--- Sort list in-place (alias for `table.sort`) +-- @param list list +-- @param[opt] comp comparison function; default to `<` operator. +List.sort = table.sort + -- Set metatable with __call metamethod. This allows the use of `List` -- as a constructor function. local ListMT = { -- cgit v1.2.3