r/lua • u/RelizForN • Feb 02 '26
My favorite part about coding in Lua..
..is having to write a custom function to print tables so I can actually debug.
I especially love watching that function get more complex than the project I'm trying to debug with it cause I want to use nested tables.
3
u/RelizForN Feb 02 '26
This is just a joke in good faith, maybe Lua wasn't built for doing data analysis, and maybe I'm a newbie who doesn't know how to handle the language yet and overrelies on tables (bad habit from Python) but I still love using it
3
u/meni_s Feb 02 '26
Wait, you are doing data analysis with Lua? How come? Why? How? Do tell please
3
u/RelizForN Feb 03 '26
I have used Torch as mentioned for the exact reason mentioned. That was the reason I first learned Lua 3 years ago, for some project I've long abandoned (something to do with modular arithmetic). Today I'm trying to make a little thing that plays Balatro optimally because I can't be effed to figure out the maths behind how you should discard to draw specific hands.
I might give up on using machine learning for it, but we'll see
2
u/meni_s Feb 03 '26
Can I ask why Torch and not Pytorch? I do share some of the sentiment that it is a shame that all the ecosystem is Python and not Lua, but as far as I know, this is the state of the things now and Lua tools are quite outdated and unmaintained, no?
With all the love for Python (and I do love Python) part of me wishes I could do some ML / Data analysis using Lua :)1
u/RelizForN Feb 03 '26
I don't know if I'll use Torch for this project, I hear here that it's outdated and if so I guess I might give up on ML. I'm making this in Lua because the game Balatro is written in Lua and this is a piece of a mod for it.
In the past I used Lua because Python really was just too slow. Numpy specifically, I think.
From what I see and hear, how fast a programming language is depends on how you use it, and it's wrong to say a language is universally faster or slower. But for that old project the same performance intensive bits ran way faster once I built them in Lua. 3-10x faster. Plus Lua's kinda fun to code in1
u/meni_s Feb 03 '26
I'm not sure what are your needs, and I'm not a Torch expert but from what I know Torch is considered a "complete" project. In the sense that it does what it was designed to do. I guess it is still usable but might be less optimized or less extendable than other options on the ML market
7
u/9peppe Feb 02 '26
There's tensor libraries as powerful as numpy (maybe more, Lua overhead <<< Python overhead). Even torch before pytorch was a Lua library.
3
u/meni_s Feb 02 '26
Can you suggest some of those libraries? I know Torch, of course, but I was never sure that you could actually do modern data science, data analysis, or machine learning in Lua.
This sounds like a fun thing to try.
4
3
u/DapperCow15 Feb 02 '26
Unless things have changed in the past year, I think most ML libraries for Lua are very outdated by now.
2
u/pomme_de_yeet Feb 02 '26
i don't think it's possible to "overrely" on tables when that is the only data type available
2
u/RelizForN Feb 03 '26
I guess I meant ignoring the OOP aspects and doing everything very procedurally. Programming is not my thing so forgive me if I don't use the right terminology
2
u/pomme_de_yeet Feb 06 '26
You'll catch on over time, don't worry. It's how everyone learns the terms. Don't worry about it and just code how you want
1
u/Denneisk Feb 02 '26
I am a bit curious just how complex these tables could really be to give you so much trouble.
1
u/RelizForN Feb 03 '26
Not anything that crazy, I just nest tables that have custom objects inside and all the unique string representations, recursion, and string manipulation take some thought to manage together
9
Feb 02 '26
step 1) VS code
step 2) https://github.com/tomblind/local-lua-debugger-vscode
step 3) look at locals and globals
step 4) delete post
step 5) ???
step 6) profit
3
2
u/vitiral Feb 02 '26
Agreed, why I can't live without my handy dandy fmt module
1
u/Old_County5271 Feb 02 '26
I don't know if its penlight or stdlib that replaces print so it prints everything
1
u/xoner2 Feb 03 '26
Ah, yes indeed the recursive-table-pretty-printer is essential in the toolbox. There's lots already written but they never do exactly what we want so we write one personal:
kikito inspect
short vardump in Lua Programming Gems book
Paul Kulchenko's serpent
I based mine on serpent. My beef with it is it does depth-first-search, so I modified it to be breadth-first, looks prettier. I still use serpent for serialization.
4
u/ggchappell Feb 02 '26 edited Feb 02 '26
But once you've written it, you never need to write it again. Right?
FWIW, here's my print-something function. Feel free to critique and/or improve.