git.iko.soy/typeable/pandoc
on lowmem help sign in
typeable/pandoc/data/pandoc.List.lua
142 lines 4.5 kB history raw
1--[[
2List.lua
4Copyright © 2017–2020 Albert Krewinkel
6Permission to use, copy, modify, and/or distribute this software for any
7purpose with or without fee is hereby granted, provided that the above
8copyright notice and this permission notice appear in all copies.
10THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. ]]
18--- Pandoc's List type and helper methods
19-- @classmod pandoc.List
20-- @author Albert Krewinkel
21-- @copyright © 2017–2020 Albert Krewinkel
22-- @license MIT
23local List = {
24 _VERSION = "1.0.0"
27--- Create a new list.
28-- @param[opt] o table that should be altered into a list (default: `{}`)
29-- @return the altered input table
30function List:new (o)
31 o = o or {}
32 setmetatable(o, self)
33 self.__index = self
34 return o
35end
37--- Concatenates two lists.
38-- @param list second list concatenated to the first
39-- @return a new list containing all elements from list1 and list2
40function List:__concat (list)
41 local res = List.clone(self)
42 List.extend(res, list)
43 return res
44end
46--- Returns a (shallow) copy of the list.
47function List:clone ()
48 local lst = setmetatable({}, getmetatable(self))
49 List.extend(lst, self)
50 return lst
51end
53--- Adds the given list to the end of this list.
54-- @param list list to appended
55function List:extend (list)
56 for i = 1, #list do
57 self[#self + 1] = list[i]
58 end
59end
61--- Returns a new list containing all items satisfying a given condition.
62-- @param pred condition items must satisfy.
63-- @return a new list containing all items for which `test` was true.
64function List:filter (pred)
65 local res = setmetatable({}, getmetatable(self))
66 for i = 1, #self do
67 if pred(self[i], i) then
68 res[#res + 1] = self[i]
69 end
70 end
71 return res
72end
74--- Returns the value and index of the first occurrence of the given item.
75-- @param needle item to search for
76-- @param[opt] init index at which the search is started (default: 1)
77-- @return first item equal to the needle, or nil if no such item exists.
78-- @return index of that element
79function List:find (needle, init)
80 return List.find_if(self, function(x) return x == needle end, init)
81end
83--- Returns the value and index of the first element for which the predicate
84--- holds true.
85-- @param pred the predicate function
86-- @param[opt] init index at which the search is started (default: 1)
87-- @return first item for which `test` succeeds, or nil if no such item exists.
88-- @return index of that element
89function List:find_if (pred, init)
90 init = (init == nil and 1) or (init < 0 and #self - init) or init
91 for i = init, #self do
92 if pred(self[i], i) then
93 return self[i], i
94 end
95 end
96 return nil
97end
99--- Checks if the list has an item equal to the given needle.
100-- @param needle item to search for
101-- @param[opt] init index at which the search is started; defaults to 1.
102-- @return true if a list item is equal to the needle, false otherwise
103function List:includes (needle, init)
104 return not (List.find(self, needle, init) == nil)
105end
107--- Insert an element into the list. Alias for `table.insert`.
108-- @param list list
109-- @param[opt] pos position at which the new element is to be inserted
110-- @param value value to insert
111List.insert = table.insert
113--- Returns a copy of the current list by applying the given function to
114-- all elements.
115-- @param fn function which is applied to all list items.
116function List:map (fn)
117 local res = setmetatable({}, getmetatable(self))
118 for i = 1, #self do
119 res[i] = fn(self[i], i)
120 end
121 return res
122end
124--- Remove element from list (alias for `table.remove`)
125-- @param list list
126-- @param[opt] pos position of the element to be removed (default: #list)
127-- @return the removed element
128List.remove = table.remove
130--- Sort list in-place (alias for `table.sort`)
131-- @param list list
132-- @param[opt] comp comparison function; default to `<` operator.
133List.sort = table.sort
135-- Set metatable with __call metamethod. This allows the use of `List`
136-- as a constructor function.
137local ListMT = {
138 __call = List.new
140setmetatable(List, ListMT)
142return List