4Copyright © 2017 Albert Krewinkel
6Permission to use, copy, modify, and/or distribute this software for any purpose
7with or without fee is hereby granted, provided that the above copyright notice
8and this permission notice appear in all copies.
10THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
11REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
12FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
13INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
14OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
15TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
19--- Pandoc's List type and helper methods
20-- @classmod pandoc.List
21-- @author Albert Krewinkel
22-- @copyright © 2017 Albert Krewinkel
35function List:__call (o)
39--- Concatenates two lists.
40-- @param list second list concatenated to the first
41-- @return a new list containing all elements from list1 and list2
42function List:__concat (list)
43 local res = List.clone(self)
44 List.extend(res, list)
48--- Returns a (shallow) copy of the list.
50 local lst = setmetatable({}, getmetatable(self))
51 List.extend(lst, self)
55--- Checks if the list has an item equal to the given needle.
56-- @param needle item to search for
57-- @param init index at which the search is started
58-- @return true if a list item is equal to the needle, false otherwise
59function List:includes (needle, init)
60 return not (List.find(self, needle, init) == nil)
63--- Returns the value and index of the first occurrence of the given item.
64-- @param needle item to search for
65-- @param init index at which the search is started
66-- @return first item equal to the needle, or nil if no such item exists.
67-- @return index of that element
68function List:find (needle, init)
69 return List.find_if(self, function(x) return x == needle end, init)
72--- Returns the value and index of the first element for which the predicate
74-- @param pred the predicate function
75-- @param init index at which the search is started
76-- @return first item for which `test` succeeds, or nil if no such item exists.
77-- @return index of that element
78function List:find_if (pred, init)
79 init = (init == nil and 1) or (init < 0 and #self - init) or init
80 for i = init, #self do
81 if pred(self[i], i) then
88--- Adds the given list to the end of this list.
89-- @param list list to appended
90function List:extend (list)
92 self[#self + 1] = list[i]
96--- Returns a copy of the current list by applying the given function to all
98-- @param fn function which is applied to all list items.
100 local res = setmetatable({}, getmetatable(self))
102 res[i] = fn(self[i], i)
107--- Returns a new list containing all items satisfying a given condition.
108-- @param pred condition items must satisfy.
109-- @return a new list containing all items for which `test` was true.
110function List:filter (pred)
111 local res = setmetatable({}, getmetatable(self))
113 if pred(self[i], i) then
114 res[#res + 1] = self[i]
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