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.
...
7
votes
Accepted
Complex number class in Lua
Possible improvements:
localise functions of the math library, and sometimes, self.r and ...
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 ...
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 (. ...
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:
...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
3
votes
Accepted
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, ...
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 ...
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
...
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:
...
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 ...
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:
<...
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 ...
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 ...
2
votes
Try to not repeat function in Lua
Not a completely review, but this should work as intended:
...
2
votes
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 ...
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 ...
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 ...
1
vote
I wrote my first pathfinding algorithm
You can always return the condition expression itself instead of using if:
...
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 ...
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
...
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 ...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
lua × 107beginner × 14
performance × 12
lua-table × 12
game × 10
strings × 9
algorithm × 6
c × 6
object-oriented × 6
parsing × 5
c++ × 4
redis × 4
entity-component-system × 4
api × 3
opengl × 3
.net × 2
comparative-review × 2
regex × 2
random × 2
file-system × 2
serialization × 2
computational-geometry × 2
macros × 2
adventure-game × 2
ffi × 2