@@ -24,6 +24,9 @@ local List = {
2424 _VERSION = "1.0.0"
2525}
2626
27+--- Create a new list.
28+-- @param[opt] o table that should be altered into a list (default: `{}`)
29+-- @return the altered input table
2730function List:new (o)
2831 o = o or {}
2932 setmetatable(o, self)
@@ -47,17 +50,30 @@ function List:clone ()
4750 return lst
4851end
4952
50−--- Checks if the list has an item equal to the given needle.
51−-- @param needle item to search for
52−-- @param init index at which the search is started
53−-- @return true if a list item is equal to the needle, false otherwise
54−function List:includes (needle, init)
55− return not (List.find(self, needle, init) == nil)
53+--- Adds the given list to the end of this list.
54+-- @param list list to appended
55+function List:extend (list)
56+ for i = 1, #list do
57+ self[#self + 1] = list[i]
58+ end
59+end
60+
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.
64+function 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
5672end
5773
5874--- Returns the value and index of the first occurrence of the given item.
5975-- @param needle item to search for
60−-- @param init index at which the search is started
76+-- @param[opt] init index at which the search is started (default: 1)
6177-- @return first item equal to the needle, or nil if no such item exists.
6278-- @return index of that element
6379function List:find (needle, init)
@@ -67,7 +83,7 @@ end
6783--- Returns the value and index of the first element for which the predicate
6884--- holds true.
6985-- @param pred the predicate function
70−-- @param init index at which the search is started
86+-- @param[opt] init index at which the search is started (default: 1)
7187-- @return first item for which `test` succeeds, or nil if no such item exists.
7288-- @return index of that element
7389function List:find_if (pred, init)
@@ -80,16 +96,16 @@ function List:find_if (pred, init)
8096 return nil
8197end
8298
83−--- Adds the given list to the end of this list.
84−-- @param list list to appended
85−function List:extend (list)
86− for i = 1, #list do
87− self[#self + 1] = list[i]
88− end
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
103+function List:includes (needle, init)
104+ return not (List.find(self, needle, init) == nil)
89105end
90106
91−--- Returns a copy of the current list by applying the given function to all
92−-- elements.
107+--- Returns a copy of the current list by applying the given function to
108+-- all elements.
93109-- @param fn function which is applied to all list items.
94110function List:map (fn)
95111 local res = setmetatable({}, getmetatable(self))
@@ -99,19 +115,6 @@ function List:map (fn)
99115 return res
100116end
101117
102−--- Returns a new list containing all items satisfying a given condition.
103−-- @param pred condition items must satisfy.
104−-- @return a new list containing all items for which `test` was true.
105−function List:filter (pred)
106− local res = setmetatable({}, getmetatable(self))
107− for i = 1, #self do
108− if pred(self[i], i) then
109− res[#res + 1] = self[i]
110− end
111− end
112− return res
113−end
114−
115118-- Set metatable with __call metamethod. This allows the use of `List`
116119-- as a constructor function.
117120local ListMT = {