A collection library to simplify sequential table operations
local Enumerable = require('__stdlib__/stdlib/enumerable')
Enumerable.create({1,2,3})
Vendor.Enumerable._data | Internal collection data |
Vendor.Enumerable:all(callback) | Returns true if callback returns truthy for all elements in the collection. |
Vendor.Enumerable:any(callback) | Returns true if callback returns truthy for any element in the collection. |
Vendor.Enumerable:collect() | |
Vendor.Enumerable:concat(other) | Append the contents of one table onto the end of the existing enumerable. |
Vendor.Enumerable:count(callback) | Return the number of items in the collection. |
Vendor.Enumerable:create(collection) | Enumerable constructor. |
Vendor.Enumerable:data() | Return the unwrapped collection data. |
Vendor.Enumerable:detect() | |
Vendor.Enumerable:each(callback) | Pass all elements in the collection to a callback. |
Vendor.Enumerable:empty() | Whether the collection has no elements. |
Vendor.Enumerable:find(callback) | Returns the first element in the collection where the callback returns true. |
Vendor.Enumerable:find_all() | |
Vendor.Enumerable:find_index(callback) | Find the position of the first item in collection for which the callback returns true. |
Vendor.Enumerable:first(n) | Return the first element or elements in the collection. |
Vendor.Enumerable:group_by(callback) | Groups elements into collections based on the result of the provided callback. |
Vendor.Enumerable:inject() | |
Vendor.Enumerable:last(n) | Return the last element or elements in the collection. |
Vendor.Enumerable:map(callback) | Pass all elements in collection to a callback. |
Vendor.Enumerable:max(callback) | Find the highest value in the enumerable instance. |
Vendor.Enumerable:min(callback) | Find the lowest value in the enumerable instance. |
Vendor.Enumerable:minmax(callback) | Find the highest and lowest values in the enumerable. |
Vendor.Enumerable:partition(callback) | Split enumerable into two groups, based on the true or false result of the callback. |
Vendor.Enumerable:pop() | Remove and return the last item from the collection. |
Vendor.Enumerable:push(...) | Add one or more items to the enumerable. |
Vendor.Enumerable:reduce(initial, callback) | Combine elements of enumerable by passing all items to a callback. |
Vendor.Enumerable:reject(callback) | Create a new collection with elements which the callback returns false. |
Vendor.Enumerable:select(callback) | Create a new collection with elements which the callback returns true. |
Vendor.Enumerable:shift() | Remove and return the first item from the collection. |
Vendor.Enumerable:sort(callback) | Sort the enumerable by optional callback in place. |
Vendor.Enumerable:to_table() | Create a shallow copy of the unwrapped collection data. |
Vendor.Enumerable:unshift(...) | Insert one or more items into the beginning of the collection. |
Internal collection data
Returns true if callback returns truthy for all elements in the collection.
Parameters:
items = Enumerable.create({10, 20, 30})
items:all(function(value, index) return value > 5 end)
-> true
items:all(function(value, index) return value < 25 end)
-> false
Returns true if callback returns truthy for any element in the collection.
Parameters:
items = Enumerable.create({10, 20, 30})
items:any(function(value, index) return value > 25 end)
-> true
items:any(function(value, index) return value > 30 end)
-> false
Append the contents of one table onto the end of the existing enumerable.
Parameters:
pets = Enumerable:create({'dog', 'cat'})
pets:concat({'turtle', 'wizard'})
-> pets now contains {'dog', 'cat', 'turtle', 'wizard'}
Return the number of items in the collection.
If a callback is given, count will return the number of elements for which the callback returns true
Parameters:collection = Enumerable.create({1,2,3})
collection:count()
-> 3
collection:count(function(value, index) return value % 2 == 0 end)
-> 1
Enumerable constructor.
If no collection is provided, a new empty table will be generated.
Parameters:collection = Enumerable.create({123})
Return the unwrapped collection data.
Returns:
collectionData = collection:to_table()
Pass all elements in the collection to a callback.
Parameters:
collection:each(function(value, index) ... end)
Whether the collection has no elements.
Returns:
collection = Enumerable.create()
if collection:empty() then
print('Collection is empty')
end
-> Collection is empty
Returns the first element in the collection where the callback returns true.
Parameters:
numbers = Enumerable.create({20, 30, 40})
numbers:find(function(value, index) return value > 25 end)
-> 30
Find the position of the first item in collection for which the callback returns true.
Parameters:
collection = Collection.create({0, 1, 2, 3, 4})
collection:findIndex(function(value, index) return value > 2 end)
-> 4
Return the first element or elements in the collection.
Parameters:
collection = Enumerable.create({1,2,3,4})
collection:first()
-> 1
collection:first(3)
-> {1,2,3}
Groups elements into collections based on the result of the provided callback.
Resulting table will have keys matching the returned value of the callback, and values as a table of elements which returned that value.
Parameters:numbers = Enumerable.create({1,2,3,4,5,6})
result = Enumerable.group_by(function(value, index) return value % 3 end)
result[0]
-> Enumerable containing {3, 6}
result[1]
-> Enumerable containing {2, 5}
result[2]
-> Enumerable containing {1, 4}
Return the last element or elements in the collection.
Parameters:
collection = Enumerable.create({1,2,3,4})
collection:last()
-> 4
collection:last(3)
-> {2, 3, 4}
Pass all elements in collection to a callback.
returns a new enumerable instance containing values returned by the callback.
Parameters:collection = Enumerable.create({1, 2, 3})
collection:map(function(value, index) return value* 2 end)
> Enumerable containing {2, 4, 6}
Find the highest value in the enumerable instance.
If a callback is provided, the return value will be used to determine the highest value.
Parameters:strings = Enumerable.create({'aaaaaa', 'bbb', 'c'})
strings:max()
> 'c'
strings:max(function(value) return #value end)
> 'aaaaa'
Find the lowest value in the enumerable instance.
If a callback is provided, the return value will be used to determine the lowest value.
Parameters:strings = Enumerable.create({'aaaaaa', 'bbb', 'c'})
strings:min()
> 'aaaaa'
strings:min(function(value) return #value end)
> 'c'
Find the highest and lowest values in the enumerable.
If a callback is provided, the return value will be used to determine the highest and lowest values.
Parameters:numbers = Enumerable.create({6,3,1,5,2,4})
lowest, highest = numbers:minmax()
> (1,6)
strings:max(function(value) return 10 - value end)
> (6, 1)
Split enumerable into two groups, based on the true or false result of the callback.
Aliases: find_all, detect
Parameters:numbers = Enumerable.create({1,2,3,4,5,6})
ven, odd = Enumerable:partition(function(value, index) return value % 2 == 1 end)
> even is a collection containing {2, 4, 6}
> odd is a collection containing {1, 3, 5}
Remove and return the last item from the collection.
Returns:
items = Enumerable.create({1,2,3})
items:pop()
> returns 3
> items now contains {1,2}
Add one or more items to the enumerable.
Parameters:
items = Enumerable.create({1,2,3})
items:push(4, 5)
> items contains {1,2,3,4,5}
Combine elements of enumerable by passing all items to a callback.
Values returned by the callback will be used as the accumulator value for subsequent callbacks.
Parameters:numbers = Enumerable.create({1,2,3})
numbers:reduce(function(accumulator, value) return (accumulator or 0) + value end)
-> 6
numbers:reduce(20, function(accumulator, value) return accumulator + value end)
-> 26
Create a new collection with elements which the callback returns false.
Parameters:
items = Enumerable.create({1,2,3,4,5,6})
odd = Enumerable:reject(function(value, index) return value % 2 == 0 end)
-> Enumerable containing {1,3,5}
Create a new collection with elements which the callback returns true.
Parameters:
items = Enumerable.create({1,2,3,4,5,6})
even = Enumerable:select(function(value, index) return value % 2 == 0 end)
-> Enumerable containing {2,4,6}
Remove and return the first item from the collection.
Returns:
items = Enumerable.create({1,2,3})
items:shift()
> returns 1
> items now contains {2,3}
Sort the enumerable by optional callback in place.
If a callback is not provided, data will be sorted in ascending order. If callback is provided, it will be passed two table elements, and should return true if the first element should appear first, otherwise false. See also: table.sort documentation
Parameters:numbers = Enumerable.create({2,1,3})
numbers:sort()
-> numbers now contains {1,2,3}
numbers:sort(function(a, b) return b < a end)
-> numbers now contains {3,2,1}
Create a shallow copy of the unwrapped collection data.
Returns:
clonedData = collection:to_table()
Insert one or more items into the beginning of the collection.
Parameters:
items = Enumerable.create({4,5,6})
items:unshift(1,2,3)
-> Items now contains {1,2,3,4,5,6}