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

Showcase your libraries, tools and other projects that help your fellow love users.
User avatar
YoungNeer
Party member
Posts: 118
Joined: Wed May 15, 2019 7:49 am

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

Post 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)
Last edited by YoungNeer on Thu Jul 25, 2019 7:01 am, edited 2 times in total.
My Github- your contribution is highly appreciated
User avatar
raidho36
Party member
Posts: 2063
Joined: Mon Jun 17, 2013 12:00 pm

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

Post 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.
User avatar
ivan
Party member
Posts: 1911
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

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

Post 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.
Last edited by ivan on Sat Dec 11, 2021 8:23 am, edited 1 time in total.
User avatar
4vZEROv
Party member
Posts: 126
Joined: Wed Jan 02, 2019 8:44 pm

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

Post by 4vZEROv »

You already reinvented the wheel :)

https://github.com/Yonaba/Moses
User avatar
pgimeno
Party member
Posts: 3548
Joined: Sun Oct 18, 2015 2:58 pm

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

Post 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.
User avatar
YoungNeer
Party member
Posts: 118
Joined: Wed May 15, 2019 7:49 am

Reply to ivan

Post 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."
My Github- your contribution is highly appreciated
User avatar
zorg
Party member
Posts: 3441
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

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

Post 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
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
User avatar
YoungNeer
Party member
Posts: 118
Joined: Wed May 15, 2019 7:49 am

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

Post 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)
My Github- your contribution is highly appreciated
User avatar
Pyuu
Prole
Posts: 22
Joined: Mon Jul 11, 2016 1:19 am

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

Post 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!)
User avatar
ivan
Party member
Posts: 1911
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

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

Post 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.
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Google [Bot] and 70 guests