git.iko.soy/typeable/pandoc
on 171187a help sign in
109 lines 2.9 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---
20-- Lua functions for pandoc scripts.
21--
22-- @author Albert Krewinkel
23-- @copyright © 2017 Albert Krewinkel
24-- @license MIT
26------------------------------------------------------------------------
27-- Metatable for lists
28-- @type List
29local List = {
30 _VERSION = "0.1.0"
33function List:new (o)
34 o = o or {}
35 setmetatable(o, self)
36 self.__index = self
37 return o
38end
40function List:__call (o)
41 return self:new(o)
42end
44--- Concatenates two lists.
45function List:__concat (list)
46 local res = List.clone(self)
47 List.extend(res, list)
48 return res
49end
51--- Returns a (shallow) copy of the list.
52function List:clone ()
53 local lst = setmetatable({}, getmetatable(self))
54 List.extend(lst, self)
55 return lst
56end
58--- Appends the given list to the end of this list.
59function List:includes (needle)
60 for i = 1, #self do
61 if self[i] == needle then
62 return true
63 end
64 end
65 return false
66end
68--- Returns the value and index of the first occurrence of the given item.
69-- @param needle item to search for
70-- @return first item equal to the needle, or nil if no such item exists.
71-- @return index of that element
72function List:find (needle, init)
73 return List.find_if(self, function(x) return x == needle end, init)
74end
76--- Returns the value and index of the first element for which test returns true.
77-- @param test the test function
78-- @param init index at which the search is started
79-- @return first item for which `test` succeeds, or nil if no such item exists.
80-- @return index of that element
81function List:find_if (test, init)
82 init = (init == nil and 1) or (init < 0 and #self - init) or init
83 for i = init, #self do
84 if test(self[i], i) then
85 return self[i], i
86 end
87 end
88 return nil
89end
91--- Add the given list to the end of this list.
92-- @param list list to appended
93function List:extend (list)
94 for i = 1, #list do
95 self[#self + 1] = list[i]
96 end
97end
99-- Returns a copy of the current list by applying the given function to all
100-- elements.
101function List:map (fn)
102 local res = setmetatable({}, getmetatable(self))
103 for i = 1, #self do
104 res[i] = fn(self[i])
105 end
106 return res
107end
109return List