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
28-- @param[opt] o table that should be altered into a list (default: `{}`)
29-- @return the altered input table
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)
46--- Returns a (shallow) copy of the list.
48 local lst = setmetatable({}, getmetatable(self))
49 List.extend(lst, self)
53--- Adds the given list to the end of this list.
54-- @param list list to appended
55function List:extend (list)
57 self[#self + 1] = list[i]
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))
67 if pred(self[i], i) then
68 res[#res + 1] = self[i]
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)
83--- Returns the value and index of the first element for which the predicate
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
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)
107--- Insert an element into the list. Alias for `table.insert`.
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
115-- @param fn function which is applied to all list items.
116function List:map (fn)
117 local res = setmetatable({}, getmetatable(self))
119 res[i] = fn(self[i], i)
124--- Remove element from list (alias for `table.remove`)
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`)
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.
140setmetatable(List, ListMT)
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list