Skip to main content
7 votes
Accepted

Inverting a boolean in Lua

a more elegant way ? Simply assign the negation, unconditionally: not foo This works in Lua, and in essentially every other language. ...
J_H's user avatar
  • 42.2k
7 votes
Accepted

Complex number class in Lua

Possible improvements: localise functions of the math library, and sometimes, self.r and ...
Alexander Mashin's user avatar
5 votes

3D torus in Lua

You could organize it by using Lua's object oriented tricks to make a class, methods etc. I need to learn more math to cleanup the code you posted but here is a sample Vector3 class I wrote with ...
0xKate's user avatar
  • 521
4 votes
Accepted

Recursive descent parser for simple arithmetic expressions grammar

Style Your placement of the parameters in a function definition isn't consistent. The typical convention, across languages, is no space between the name and the (. ...
Curtis F's user avatar
  • 334
4 votes

Generating a Lua script for a tile map maker

maybe you can push some of your code into a text file and use it to generate code? template.txt: ...
Martin Frank's user avatar
  • 3,033
4 votes
Accepted

3D torus in Lua

You've named the radii as r and r2, which to me would read as \$ r \$ and \$ r ^ 2 \$; if they are different radii values, you ...
hjpotter92's user avatar
  • 8,921
4 votes
Accepted

RandomNumber wrapper in Lua

The main issue: side effects The main issue is that you are using a random which there is only a single seed, shared between any code running on the platform. This is a questionable design by Lua (and ...
Maarten Bodewes's user avatar
3 votes
Accepted

Trailing delimiter string splicing, implemented for Lua in C

Unexpected output Your code returns the original string if it consists of a single delimiter character: rtrim(".", ".") --> "." I ...
Martin R's user avatar
  • 24.2k
3 votes

Get list of buffers from current neovim instance

The code in your if and else branches is identical. table.insert(buffers, buffer) I prefer ...
Steve Ward's user avatar
3 votes
Accepted

Get list of buffers from current neovim instance

The only case when you don't want a buffer included is when options.listed is truthy and is_listed is falsy In every other ...
kikito's user avatar
  • 186
3 votes

Convert Lua UINT to hh:mm:ss

Try also function convert_to_time(tenths) return os.date("%H:%M:%S",os.time({sec=tenths//10, hour=0, min=0, day=1, month=1, year=2000})) end This code exploits ...
lhf's user avatar
  • 191
3 votes
Accepted

Convert Lua UINT to hh:mm:ss

A much simpler way is: ...
Roland Illig's user avatar
  • 21.9k
3 votes
Accepted

Convert an integer to 4 bytes without bitshift operators

There's not a ton that can be improved here, but: lut should be made const Due to implicit promotion, ...
Reinderien's user avatar
  • 71.1k
3 votes
Accepted

Currying in Lua

Overall a solid approach. Here's a few options to consider though: First of all, you could eliminate the separate curry function and just use the ...
DarkWiiPlayer's user avatar
3 votes
Accepted

Run UNIT TEST automatically from NeoVim

auto shortcuts -- Set the Key <leader>du to run this command There's a couple of comments in this standard format. Hopefully a Makefile or other script ...
J_H's user avatar
  • 42.2k
2 votes
Accepted

Simple terminal-based Lua task-tracking/calendar

In the main while loop, you have io.write('luacal> ') local command = io.read() for which you already have a separate function: ...
hjpotter92's user avatar
  • 8,921
2 votes
Accepted

Shutdown-Script for Don't Starve Together Dedicated Server

table.getn has been deprecated for more than 2 years now. Why iterate only until n-1? I'd prefer using the ...
hjpotter92's user avatar
  • 8,921
2 votes
Accepted

Faster way of converting a 1-d table to predefined userdata in Lua

You have a duplication of the allocation and init part in the 2 functions, instead you can create a helper that will create the usertable, return a pointer to it and leave it on top of the stack: <...
ratchet freak's user avatar
2 votes

Censoring words in lua

If you care about performance, you should have a look at LPeg. It makes it way easier to handle large numbers of string matching and substitution rules. For example, you could define patterns and ...
DarkWiiPlayer's user avatar
2 votes

function to print any type in Lua?

This code doesn't cover the case of cyclic tables, but that's fine, we'll view that as out-of-scope. The style guide recommends that Variables holding values or objects are typically lowercase This ...
J_H's user avatar
  • 42.2k
2 votes

Try to not repeat function in Lua

Not a completely review, but this should work as intended: ...
Tobias Grothe's user avatar
2 votes

Try to not repeat function in Lua

Maybe a recursive function? It seems to work: ...
user3204810's user avatar
2 votes

<Programming in Lua - 4th> exercise 2.2 - place 8 queens

Regarding avoid shallow copy, I wrote another version that achieve it via: Swap before recursion. Do recursion Swap back after recursion. Thus there is only 1 ...
Eric's user avatar
  • 151
1 vote

function to print any type in Lua?

if there is a more succinct way to achieve this No. if there is a standard way to print nested tables Your way is the standard way. if there are any cases the function doesn't cover You can not ...
ESkri's user avatar
  • 111
1 vote

I wrote my first pathfinding algorithm

Having a "global" board (the variable is local, but there can only ever be one in the file) has a high potential for future headache; instead I'd just pass the board as the first argument to ...
DarkWiiPlayer's user avatar
1 vote

I wrote my first pathfinding algorithm

You can always return the condition expression itself instead of using if: ...
Coal's user avatar
  • 111
1 vote

Finite state machine for chasing and attacking a target

Am I doing the transitions right with SetState(state)? (I feel like I can just handle the transitions inside each States update method. Maybe there's a better way but I'm not sure.) You allow a ...
dfhwze's user avatar
  • 14.2k
1 vote
Accepted

Building a 64 × 64 particle accelerator frame in Minecraft with a computercraft turtle

First of all, use more local. It will save you a lot of headache in the future. Instead of the three place functions, you could just do ...
DarkWiiPlayer's user avatar
1 vote

Collision detection for moving 2D objects

I'm not experienced in LUA, so I can't comment on the code style itself. But I can on the algorithm and implementation. Bugs: I'm fairly confident this should be ...
AJNeufeld's user avatar
  • 35.3k

Only top scored, non community-wiki answers of a minimum length are eligible