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

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

Post by YoungNeer »

Pyuu wrote: Fri Jul 19, 2019 7:06 pm 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!)
Yeah everyone was pointing at that "There's already table.insert function" and honestly saying I was unaware of that!! :rofl: But I appreciate all those people who took the time to correct me. That's pretty cool. And note I accepted your contribution but made a little changes to that - once I realised that Lua tables are passed by reference I thought it'd be great if we had a table.copy which copies a table - simple yet sweet and I also added a table.reverse and a table.range function. And of course I removed all those table.push functions which were effectively just clones of table.insert as the community pointed out.

A stupid note: (can skip if you don't have time)
And one last thing I would like to point out that- I'm a beginner in Lua/Love2D (Beginner in the sense that although I have some like one and a half months experience - yet my knowledge is very limited and I think maybe by making more libraries and more games I can increase my Lua and Love2D skills. Keep correcting me whenever you feel the need - that would increase my rate of learning (which is currently not very high) and once I have mastered shaders - believe me A LOT OF AWESOME GAMES will be created and uploaded on the "Games and Creations" forums and of course you'd be credited (Special thanks to Chris H. that is) - well that's just how great and grateful I am ;)

Sorry I kind of get carried away sometimes - but yeah thanks for your contribution!! I appreciate that
My Github- your contribution is highly appreciated
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 »

ivan wrote: Sat Jul 20, 2019 10:25 pm 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.
Yeah! Yours version is better and I have comitted the changes and note that now iTable doesn't contain any stupid push_back or push_front, etc. And some extra functions like table.range, table.reverse, table.copy and all the previous stupid code has been updated by Pyutaro. So please give it a go and I am looking forward to hear your opinion
My Github- your contribution is highly appreciated
User avatar
ivan
Party member
Posts: 1915
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 »

That's good to hear, Neer.
Keep reading other people's code and you are bound to improve.
BTW your reverse function looks like will cause an infinite loop when i<j.
Regarding table.copy => you are doing a "shallow" copy of just the numerically indexed elements.
For small tables this can be shorted to:

Code: Select all

return {unpack(tbl)}
Making a "deep" copy is typically more useful -
please refer the the previous link I posted for examples of both
table.copy and table.reverse, you are welcome to reuse that code.
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 »

ivan wrote: Sun Jul 21, 2019 1:54 pm BTW your reverse function looks like will cause an infinite loop when i<j.
Regarding table.copy => you are doing a "shallow" copy of just the numerically indexed elements.
please refer the the previous link I posted for examples of both
table.copy and table.reverse, you are welcome to reuse that code.
I don't know about how will table.reverse will cause an infinite loop :huh: (seems to be working for me)
And yes- I looked at your table.copy - and it's cool. Inspired from your _table.copy I reimplemented my own table.copy but I didn't use caching. I don't understand why do you need a helper function and an extra _cache variable. :huh:

Anyways inspired from your _table.shuffle, I added table.mix and table.shuffle in iTable library (nothing special in that) and some extra functions like table.random, table.max/table.min and table.removeAll
My Github- your contribution is highly appreciated
User avatar
ivan
Party member
Posts: 1915
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 »

Sorry I misread that, the reverse function is fine.
You need the cache when copying to avoid infinite recursion caused by cycles:
t={} t.a = t
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 »

ivan wrote: Tue Jul 23, 2019 12:16 pm Sorry I misread that, the reverse function is fine.
Never mind that. But since you are here. I'd like you to review my other libraries (tinyCSV and loveCC - Since Anima is currently undocumented I can't ask for that one)
My Github- your contribution is highly appreciated
User avatar
ivan
Party member
Posts: 1915
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 took a brief look and it's not a bad start but it still needs a lot of work. With tinycsv you need to remove the "love.filesystem" and possibly "io." dependencies. The color lib needs better direction since a lot of the code is unnecessary and not related to colors at all. You don't need the : operator in both cases. There are more efficient ways to convert between hex/rgb. Also, it good to separate code from data as much as possible, for example you have:

Code: Select all

local colors=require 'colorcodes'
colors:newColor("aliceblue","f0f8ff") --*very light blue
colors:newColor("antiquewhite",'#faebd7')
colors:newColor("aqua",'#00ffff') -- a blue shade
...
I would change that to:

Code: Select all

local data = {
aliceblue="f0f8ff",
antiquewhite='#faebd7',
aqua='#00ffff',
...
}
local colors=require 'colorcodes'
for k,v in pairs(data) do
  list[k] = colors:newColor(v)
end
That's much cleaner because the code and data are better separated.
Good luck.
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 »

ivan wrote: Sat Jul 20, 2019 10:25 pm 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.
Didn't know Lua had support for specifying i-- vs. i++. Thanks.
User avatar
zorg
Party member
Posts: 3465
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 »

Pyuu wrote: Wed Jul 24, 2019 6:00 pm Didn't know Lua had support for specifying i-- vs. i++. Thanks.
Lua supports iterating in any direction with for loops (including smaller or larger steps), but that's not the same as v-- or v++ (increment/decrement operators, like in C and C++), which it doesn't support.
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 »

ivan wrote: Wed Jul 24, 2019 3:46 am I took a brief look and it's not a bad start but it still needs a lot of work. With tinycsv you need to remove the "love.filesystem" and possibly "io." dependencies. The color lib needs better direction since a lot of the code is unnecessary and not related to colors at all. You don't need the : operator in both cases. There are more efficient ways to convert between hex/rgb. Also, it good to separate code from data as much as possible, for example you have:

Code: Select all

local colors=require 'colorcodes'
colors:newColor("aliceblue","f0f8ff") --*very light blue
colors:newColor("antiquewhite",'#faebd7')
colors:newColor("aqua",'#00ffff') -- a blue shade
...
I would change that to:

Code: Select all

local data = {
aliceblue="f0f8ff",
antiquewhite='#faebd7',
aqua='#00ffff',
...
}
local colors=require 'colorcodes'
for k,v in pairs(data) do
  list[k] = colors:newColor(v)
end
That's much cleaner because the code and data are better separated.
Good luck.
Thanks for that! Now loveCC is more powerful and now code is completely seperated from data (loveCC.lua is the actual library and palette.lua only returns a table of colors). What more now loveCC has over 100 colors, 140 to be precise!!! And selecting a color out of 140 colors is not a walk in the park so I made a small tool that comes with loveCC- Color Picker (https://love2d.org/forums/viewtopic.php ... 5ea36c9975) which lines up all the 140 colors in an organised way and you click on a color it's name is copied to clipboard - as simple as that.

And I would also like to point out that loveCC now has two new functions invert and invertColor which inverts the current and given color respectively. So you bet loveCC is now a beast compared to what it was a week ago.

And also I added some new features to Anima which might come in handy in many-games (especially visual novels and the likes of pokemon where you have this moving text animation). Please refer to that post - https://love2d.org/forums/viewtopic.php?f=5&t=86951
My Github- your contribution is highly appreciated
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 2 guests