Lists - A module for working with lists easier

This is a module I made for operating in Lists. The uses are very basic, with an custom documentation.
And also report if there is any errors in this module!

How to use
Simply create a ModuleScript named Lists and copy this text:

local module = {}
export type Parameters = {any}

--[[
Creates a new list.
]]
function module.NewList()
	return {}
end

--[[
Creates an list with values.
]]
function module.ListWithValues(... : any)
	return {...}
end

--[[
Similar to list[n].
Picks an value in the list according to index (n).
]]
function module:Part(list : {any}, n : number)
	return list[n]
end

--[[
This returns True if element is found in list, otherwise it returns False.
]]
function module:MemberQ(list : {any}, element : {any})
	for _, v in ipairs(list) do
		if v == element then
			return true
		end
	end
	return false
end

--[[
Flatten[list] - Flattens a nested list, removing all levels of nesting, and returns a single list.
Flatten[list, n] - Flattens the list to a depth of n. If n is 1, only the outermost level is flattened. If n is 2, the first two levels are flattened, and so on.
Flatten[list, {n, m}] - Flattens the list to the specified depth, starting from level n and stopping at level m.
]]
function module:Flatten(list : {any} , level : {number} | number)
	level = level or math.huge
	local result = {}

	local function flattenHelper(lst, depth)
		if depth > level then
			return
		end

		for _, v in ipairs(lst) do
			if type(v) == "table" then
				flattenHelper(v, depth + 1)
			else
				table.insert(result, v)
			end
		end
	end

	flattenHelper(list, 1)
	return result
end

--[[
Take[list, n] - Extracts the first n elements from the list.
Take[list, {start, end}] - Extracts elements starting from the start index to the end index.
Take[list, {start, end, step}] - Extracts elements starting from start, up to end, and with a step of step.
Take[list, n, m] - Extracts a sublist of length n starting from index m.
]]
function module:Take(list : {any}, ... : Parameters)
	local args = {...}
	local result = {}

	if #args == 1 then
		local n = args[1]
		for i = 1, n do
			table.insert(result, list[i])
		end
	elseif #args == 2 then
		local start, finish = args[1], args[2]
		for i = start, finish do
			table.insert(result, list[i])
		end
	elseif #args == 3 then
		local start, finish, step = args[1], args[2], args[3]
		for i = start, finish, step do
			table.insert(result, list[i])
		end
	end

	return result
end

--[[
Drop[list, n] - removes the first n elements from the list.
Drop[list, {n}] - removes the first n elements of the list.
Drop[list, {n, m}] -  removes elements starting from the n-th index to the m-th index.
]]
function module:Drop(list : {any}, ... : Parameters)
	local args = {...}
	local result = {}
	if #args == 1 then
		local n = args[1]
		for i = n+1, #list do
			table.insert(result, list[i])
		end
	elseif #args == 2 then
		local n, m = args[1], args[2]
		for i = n, m do
			table.insert(result, list[i])
		end
	elseif #args == 3 then
		local values = args[1]
		for _, v in ipairs(values) do
			table.insert(result, list[v])
		end
	end
	return result
end

--[[
Debugs list (which means prints the list).
]]
function module:DebugValues(list : {any})
	print(game.HttpService:JSONEncode(list))
end

--[[
Prints every value seperately in the list.
]]
function module:DebugValuesSeperately(list : {any})
	for i,v in pairs(list) do
		print(game.HttpService:JSONEncode(v))
	end
end

return module 

And then save it. Now create a script and do this:
local list = require(the_list_module_path)
(replace the_list_module_path with the actual path that leads to the module script)

And then thats all! The setup is complete. You can either work with it or explore functions / how do they work.

Im new to this forum, so any supports would be appreciated!

2 Likes

These methods are… quite useless to say the least. There’s no arguing that. They already exist in much simpler ways. In fact, this makes it harder to work with lists.

local list = module.NewList()
local list = {} -- Shorter, more flexible, more readable, less time
local list = module.ListWithValues(1, 2, 3)
local list = {1, 2, 3} -- Shorter, more readable, less time
local value = module:Part(list, 1)
local value = list[1] -- Shorter, more readable, less time
local i = module:MemberQ(list, "Apple")
local i = table.find(list, "Apple") -- Shorter... and I think you get it by now.

We’ve all been there, so don’t worry about it too much. It’s part of learning. In the case of making tables, less is more.

4 Likes

I see. I know these but I just made these in purpose for people who are beginners in scripting.