Functional Shenanigans: map, filter, partition, reduce - two ways

While functional style is cool and has good use cases, Lua is probably not a good one for it. At least, not Roblox Lua. Granted that it does work, you might end up with less than readable code and there will be performance drops (which vary depending on what you do).

Looking specifically at performance, Roblox Lua gives up a crucial tail call optimization which makes tail calls a bits less efficient, not to mention calls in Lua are probably already one of the slowest operations. “Slow” here is relative. Sure, it’s a few ms, but I heavily discourage heavy functional use within performance dependent code. Every time function blah() is encountered, Lua has to allocate and make a new Closure instance of the function, and every time a {blah...} is encountered, Lua has to allocate and make a new table.

Generally, Lua is not compile-time optimized enough for this style of code to make it into production being used everywhere without some sort issue. I’d stick to imperative/procedural for most things.

9 Likes