@@ -24,6 +24,9 @@ local List = {
24 _VERSION = "1.0.0"
24 _VERSION = "1.0.0"
27+--- Create a new list.
28+-- @param[opt] o table that should be altered into a list (default: `{}`)
29+-- @return the altered input table
27function List:new (o)
30function List:new (o)
28 o = o or {}
31 o = o or {}
29 setmetatable(o, self)
32 setmetatable(o, self)
@@ -47,17 +50,30 @@ function List:clone ()
47 return lst
50 return lst
50−--- Checks if the list has an item equal to the given needle.
53+--- Adds the given list to the end of this list.
51−-- @param needle item to search for
54+-- @param list list to appended
52−-- @param init index at which the search is started
55+function List:extend (list)
53−-- @return true if a list item is equal to the needle, false otherwise
56+ for i = 1, #list do
54−function List:includes (needle, init)
57+ self[#self + 1] = list[i]
55− return not (List.find(self, needle, init) == nil)
58+ end
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))
67+ if pred(self[i], i) then
68+ res[#res + 1] = self[i]
58--- Returns the value and index of the first occurrence of the given item.
74--- Returns the value and index of the first occurrence of the given item.
59-- @param needle item to search for
75-- @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)
61-- @return first item equal to the needle, or nil if no such item exists.
77-- @return first item equal to the needle, or nil if no such item exists.
62-- @return index of that element
78-- @return index of that element
63function List:find (needle, init)
79function List:find (needle, init)
@@ -67,7 +83,7 @@ end
67--- Returns the value and index of the first element for which the predicate
83--- Returns the value and index of the first element for which the predicate
68--- holds true.
84--- holds true.
69-- @param pred the predicate function
85-- @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)
71-- @return first item for which `test` succeeds, or nil if no such item exists.
87-- @return first item for which `test` succeeds, or nil if no such item exists.
72-- @return index of that element
88-- @return index of that element
73function List:find_if (pred, init)
89function List:find_if (pred, init)
@@ -80,16 +96,16 @@ function List:find_if (pred, init)
80 return nil
96 return nil
83−--- Adds the given list to the end of this list.
99+--- Checks if the list has an item equal to the given needle.
84−-- @param list list to appended
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
85−function List:extend (list)
103+function List:includes (needle, init)
86− for i = 1, #list do
104+ return not (List.find(self, needle, init) == nil)
87− self[#self + 1] = list[i]
91−--- Returns a copy of the current list by applying the given function to all
107+--- Returns a copy of the current list by applying the given function to
92−-- elements.
108+-- all elements.
93-- @param fn function which is applied to all list items.
109-- @param fn function which is applied to all list items.
94function List:map (fn)
110function List:map (fn)
95 local res = setmetatable({}, getmetatable(self))
111 local res = setmetatable({}, getmetatable(self))
@@ -99,19 +115,6 @@ function List:map (fn)
99 return res
115 return res
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))
108− if pred(self[i], i) then
109− res[#res + 1] = self[i]
115-- Set metatable with __call metamethod. This allows the use of `List`
118-- Set metatable with __call metamethod. This allows the use of `List`
116-- as a constructor function.
119-- as a constructor function.
117local ListMT = {
120local ListMT = {