git.iko.soy/typeable/pandoc
on fac3953 help sign in
120 lines 3.6 kB history raw
1--[[
2List.lua
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
16THIS SOFTWARE.
17]]
19--- Pandoc's List type and helper methods
20-- @classmod pandoc.List
21-- @author Albert Krewinkel
22-- @copyright © 2017 Albert Krewinkel
23-- @license MIT
24local List = {
25 _VERSION = "0.1.0"
28function List:new (o)
29 o = o or {}
30 setmetatable(o, self)
31 self.__index = self
32 return o
33end
35function List:__call (o)
36 return self:new(o)
37end
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)
45 return res
46end
48--- Returns a (shallow) copy of the list.
49function List:clone ()
50 local lst = setmetatable({}, getmetatable(self))
51 List.extend(lst, self)
52 return lst
53end
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)
61end
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)
70end
72--- Returns the value and index of the first element for which the predicate
73--- holds true.
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
82 return self[i], i
83 end
84 end
85 return nil
86end
88--- Adds the given list to the end of this list.
89-- @param list list to appended
90function List:extend (list)
91 for i = 1, #list do
92 self[#self + 1] = list[i]
93 end
94end
96--- Returns a copy of the current list by applying the given function to all
97-- elements.
98-- @param fn function which is applied to all list items.
99function List:map (fn)
100 local res = setmetatable({}, getmetatable(self))
101 for i = 1, #self do
102 res[i] = fn(self[i], i)
103 end
104 return res
105end
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))
112 for i = 1, #self do
113 if pred(self[i], i) then
114 res[#res + 1] = self[i]
115 end
116 end
117 return res
118end
120return List