Page 1 of 2

iTable: Extra table functions for LÖVE so you don't reinvent the wheel

Posted: Wed Jul 17, 2019 4:31 pm
by YoungNeer
iTable is a very very simple but useful helper library for Lua which adds extra table functionality

By default Lua doesn't support many table operations even important ones such as indexOf and slice, etc. So this library is to fill that gap.
For full documentation refer to the github link,
https://github.com/YoungNeer/lovelib/tree/master/itable
Currently the functions supported are:-

Code: Select all

	table.slice(tbl, fromIndex, toIndex, skip)
	--make a table from 'fromIndex' to 'toIndex' and skip 'skip-1' elements continually

	table.subset(tbl, fromIndex, toIndex)
	--same as table.slice just doesn't allow skipping any element between edges
	
	table.firstIndexOf(tbl,element)
	--returns the index of the first occurence of the element in given table 

	table.lastIndexOf(tbl,element)
	--returns the index of the first occurence of the element in given table 

	table.indexOf(tbl,element)
	--same as table.firstIndexOf
	
	table.removeAll(tbl,element)
	--removes all occurences of an element from table
	
	table.removeDuplicates(tbl,element)
	--removes duplicate occurences of an element from table
	
	table.removeAllDuplicates(tbl)
	--removes all the duplicate elements from a table
	
	table.reverse(tbl)
	--reverses table (in-place)
	
	table.random(tbl)
	--gets a random element from given table
	
	table.mix(tbl)
	--shuffles table (in-place)
	
	table.shuffle(tbl)
	--shuffles table (returns the shuffled table without affecting original table)
	
	table.copy(tbl)
	--returns a COPY of table tbl
	
	table.range(from,to,skip)
	--returns a table of elements 'from' to 'to'  and skips 'skip-1' elements alternatively (3 overloads)
	
	table.append(tbl,value1,value2,...,valuen)
	--Push value1,...,valuen to the end of the table
	
	table.merge(main_tbl,tbl1,tbl2,...,tbln)
	--Push elements of tbl1 to the end of main_tbl, and so on (for linear array)
	
	table.join(main_tbl,tbl1,tbl2,...,tbln)
	--Push elements of tbl1 to the end of main_tbl, and so on (for hash-table)
	
	table.divide(tbl,n)
	--divides a linear table into n or n+1 tables

	table.subdivide(tbl,n)
	--divides a hash-table into n or n+1 tables
	
	table.isort(tbl,funcn)
	--sorts table tbl a/c to rule function 'funcn' and returns the sorted table (not in-place)

Re: iTable: Extra table functions for LÖVE so you don't reinvent the wheel

Posted: Wed Jul 17, 2019 5:13 pm
by raidho36
Yay, more javascript in Lua. It creates new strings in asserts which it calls every time - thanks but no thanks. The language could be a little more professional, too.

Re: iTable: Extra table functions for LÖVE so you don't reinvent the wheel

Posted: Wed Jul 17, 2019 5:18 pm
by ivan
I've been using a similar library for a long time. I have posted it on bitbucket so you can take a look:
https://github.com/2dengine/table.lua
Here is what I've learned:
- you don't need to assert that the first argument is a table, Lua will report that as an error anyways. If you insist on printing your own error messages you have to avoid intermediate strings like raidho pointed out.
- firstIndexOf and lastIndexOf should assert that the search value is non-nil (you will get some silent bugs otherwise)
- you don't need table.push_back or push_front, that's the same as table.insert
- from what I can see, table.isort is "inplace" because tmp=tbl doesn't make a copy
- "different than table.merge in the sense that it supports only hashtables not arrays" - not quite, "pairs" iterates both numeric and non-numeric keys. I'm pretty sure "for _,i in pairs{...} do" is a mistake
- you are missing some basic operations like table.copy and table.reverse. For example, if you had a table copy function the user can easily do table.isort
- your code could be "optimized" using locals and by avoiding the # operator. I'm not saying that it's slow, but a lib like this needs to be as efficient as possible
Hope this helps.

Re: iTable: Extra table functions for LÖVE so you don't reinvent the wheel

Posted: Wed Jul 17, 2019 7:00 pm
by 4vZEROv
You already reinvented the wheel :)

https://github.com/Yonaba/Moses

Re: iTable: Extra table functions for LÖVE so you don't reinvent the wheel

Posted: Thu Jul 18, 2019 1:35 am
by pgimeno

Code: Select all

--push a value at the front
table.push_front=function(tbl,value) tableassert(tbl,"push_front") tbl[1]=value end
That's not a push, that's a replacement of the first element.

Reply to ivan

Posted: Thu Jul 18, 2019 1:14 pm
by YoungNeer
Well it is true table.isort is in-place and there is no point in tmp=tbl cause tmp would just be a reference to tbl. But the point is that table.isort returns and table.sort doesn't which can be useful *sometimes* such as in this case

Code: Select all

self.highscores=table.subset(table.isort(csvfile:readFile('highscores.lst',':'),function (a,b) return tonumber(a[2])>tonumber(b[2]) end),1,10)
Actually this library was made to fit my custom needs for Arkanoid (https://love2d.org/forums/viewtopic.php?f=14&t=86887) and sorry to every-one shouting on their computer screens "hey that's stupid."

Re: iTable: Extra table functions for LÖVE so you don't reinvent the wheel

Posted: Thu Jul 18, 2019 1:20 pm
by zorg
Don't apologize, rather, you should just weigh in the information that others shared and, if you think the information is good, learn from it. :3

Re: iTable: Extra table functions for LÖVE so you don't reinvent the wheel

Posted: Thu Jul 18, 2019 3:08 pm
by YoungNeer
zorg wrote: Thu Jul 18, 2019 1:20 pm Don't apologize, rather, you should just weigh in the information that others shared and, if you think the information is good, learn from it. :3
Well I AM learning and I am learning a LOT since I joined the community. I believe that day is not far when I will have my master game created which I can be proud of and which will defy all criticism (though I must admit I kinda like criticism)

Re: iTable: Extra table functions for LÖVE so you don't reinvent the wheel

Posted: Fri Jul 19, 2019 7:06 pm
by Pyuu
I went through Github and combed through the library and made a few changes. You can see the pull request here: https://github.com/YoungNeer/lovelib/pull/4

If you're interested in how to do these fixes yourself please take a minute to look through the commits :D I'm not very good at explaining things, so sorry if things are a bit odd to look at.

You might be interested in looking into the table.insert function. It already has provided functionality for things like table.push_front... and since it's a native function I believe it's written in C and not Lua (meaning it'll be faster? correct me if I'm wrong please!)

Re: iTable: Extra table functions for LÖVE so you don't reinvent the wheel

Posted: Sat Jul 20, 2019 10:25 pm
by ivan
Pyuu, in "lastIndexOf" you are doing a weird reverse iteration "for i=1, tbl_size do local true_i = tbl_size - i + 1".
You could just write "for i=tbl_size,1,-1" which is both clearer and faster.
Also, you don't need to write "return nil" at the end of any function.