Logo

dev-resources.site

for different kinds of informations.

Polyglot: Lua (Part 1)

Published at
1/13/2025
Categories
programming
beginners
lua
learning
Author
nicholassynovic
Author
15 person written this
nicholassynovic
open
Polyglot: Lua (Part 1)

In my previous post, I talked about the reasons why I want to learn more programming language, the Lua programming language, and the developer tooling for Lua. Now it's time to actually code in Lua!

For this post, I'll be completing several basic Rosetta Code tasks. Nothing crazy, but enough to get me familiar with the language and its syntax. As Lua has a fairly minimal and straight forward syntax, I'll post the code snippets and output here, but I won't explain the implementation. For the complete source code, you can see my GitHub repository here.

GitHub Template

I created a GitHub Template to bootstrap my Lua projects going forward. You can find it here. As I find tooling to improve my Lua experience, I'll update the template.

Rosetta Code Problems

Integer Arithmetic

Outcome: Taught me how to take in user input and function declarations

local function sum(a, b)
    return a + b
end

local function difference(a, b)
    return a - b
end

local function product(a, b)
    return a * b
end

local function int_quotient(a, b)
    return a // b -- Rounds to negative infinity
end

local function remainder(a, b)
    return a % b
end

local function exponentiation(a, b)
    return a ^ b
end

local function main()
    io.write("First number: ") -- Use with io.read for single line input
    local a = io.read("n") -- Captures user input

    io.write("Second number: ")
    local b = io.read("n")

    print("===")

    print("Sum: " .. sum(a, b)) -- ".." syntax used to concatenate
    print("Difference: " .. difference(a, b))
    print("Product: " .. product(a, b))
    print(
        "Integer Quotient (rounds to negative infinity): " .. int_quotient(a, b)
    )
    print("Remainder" .. remainder(a, b))
    print("Exponentiation: " .. exponentiation(a, b))
end

main()
Enter fullscreen mode Exit fullscreen mode

String Length Comparison

Outcome: Learned that all objects (including arrays) are tables, how to sort tables, and how to index over them with a for loop

local function main()
    io.write("First string: ")
    local a = io.read("l")

    io.write("Second string: ")
    local b = io.read("l")

    io.write("Third string: ")
    local c = io.read("l")

    print("===")

    local strings = { a, b, c } -- Loads strings into an array (implemented as a table)

    table.sort(strings, function(foo, bar)
        return #foo > #bar
    end) -- Sort array based on string length
    for _, s in ipairs(strings) do
        print(#s, s) -- Print string size then string content
    end
end

main()
Enter fullscreen mode Exit fullscreen mode

Conclusions

Lua wasn't that hard to get a basic grasp of. While yes, I did not cover aspects such as loops, control flow, or binary operations, reading the manual and book provided enough context for me to grasp the core concepts.

I'd like to thank the Rosetta Code community for their problems and solutions. Without them it would be far more difficult for me to understand these core language features.

lua Article's
30 articles in total
Favicon
Polyglot: Lua (Part 1)
Favicon
Polyglot: Lua (Part 0)
Favicon
Rust-like Iteration in Lua
Favicon
Lua as your favorite Programming Language
Favicon
How to configure neovim's Lua LS for Love2D
Favicon
4G module update for the power failure alarm device with call telephone function
Favicon
Managing LSPs in Neovim: Enable/Disable for the Entire Session
Favicon
Austria RP Roblox
Favicon
FiveM RAT Analysis
Favicon
So... I created yet another Neovim plugin
Favicon
When (not) to write an Apache APISIX plugin
Favicon
Using xLua in Unity
Favicon
Doing Horrible Things in Haskell
Favicon
Supercharge Your Neovim Workflow with project-cli-commands.nvim
Favicon
Extensible Control of Reaper via OSC and Scripts
Favicon
Buscadores personalizados con Mini.Pick
Favicon
Kong Plugin Development: Local Development and Installation on Your Laptop/VM
Favicon
Running Lua C modules in a pure Lua environment (1)
Favicon
5 Lesser-Known Programming Languages That Are Easy to Learn and Highly Effective
Favicon
pico 8 game missing flag game by nassim hadadd
Favicon
How to Code with Lua on ESP32 with XEdge32
Favicon
Five ways to pass parameters to Apache APISIX
Favicon
libcert: Implementing S/MIME PKI in Lua (2)
Favicon
libcert: Implementing S/MIME PKI in Lua (1)
Favicon
Kong plugin development with breakpoint debugging
Favicon
Implementing the Idempotency-Key specification on Apache APISIX
Favicon
Complete and Quick Guide to Lua 🌑
Favicon
Understanding If Statements in Lua
Favicon
Understanding Loops in Lua
Favicon
Understanding Lua's lfs Module

Featured ones: