DEV Community

Cover image for πŸŒ™ Lua: an introduction
Bruno
Bruno

Posted on

πŸŒ™ Lua: an introduction

You have probably programmed with other languages like JavaScript or Java, or C++, but found yourself with the question what is "Lua"?, or you would simply like to learn about it or learn a new language, am I right?

2d game

From video games to web applications, Lua has proven to be the perfect potion for creating dynamic and interactive content. So, grab your virtual wand and join me as we explore the wonders of Lua and discover why it's one of the most interesting languages in the programming realm!✨

πŸ€” What exactly is Lua?

Before we begin with the more technical explanation, there is a funny particularity about the name, because "lua" in Portuguese means "moon". πŸ˜„

moon and clouds

Now, the technical side: Lua is a lightweight, high-level, multi-paradigm programming language designed primarily for embedded systems and clients. It is dynamically typed, interpreted, and garbage-collected, making it easy to learn and use. It supports procedural, object-oriented, and functional programming styles, and provides a simple syntax that can be used to build complex systems. It also has a small footprint, making it well suited for use in embedded systems, where memory and CPU resources are limited. Additionally, Lua has a strong ecosystem of libraries and tools, making it a popular choice for game development, scripting, and other applications.

πŸ’Ύ How do you install Lua?

Installing Lua depends on your machine, first of all, but I have divided the installation guide into Windows, MacOS and Linux so you only have to scroll to yours and start.

Windows:

  1. Download the latest version of the Lua binary distribution from the official website (www.lua.org).

  2. Extract the contents of the downloaded archive to a directory of your choice.

  3. Add the path to the Lua bin directory to your system's PATH environment variable.

set PATH=%PATH%;<directory where Lua was extracted>\bin
Enter fullscreen mode Exit fullscreen mode
  1. Open a command prompt or terminal and run the command "lua" to verify the installation.

MacOS:

  1. Download the latest version of the Lua binary distribution from the official website (www.lua.org).

  2. Extract the contents of the downloaded archive to a directory of your choice.

  3. Open Terminal and run the following commands:

$ cd <directory where Lua was extracted>
$ sudo make install
Enter fullscreen mode Exit fullscreen mode
  1. Verify the installation by running the command "lua" in the terminal.

Linux:

  1. Download the latest version of the Lua source code from the official website (www.lua.org).

  2. Extract the contents of the downloaded archive to a directory of your choice.

  3. Open Terminal and run the following commands:

$ cd <directory where Lua was extracted>
$ sudo make linux
$ sudo make install
Enter fullscreen mode Exit fullscreen mode
  1. Verify the installation by running the command "lua" in the terminal.

There is another possible alternative here, by installing Lua using the package manager for your specific distribution (e.g. apt-get on Ubuntu). For example:

$ sudo apt-get install lua5.4
Enter fullscreen mode Exit fullscreen mode

In all cases, I recommend you check out the official documentation before jumping straight to installing: https://www.lua.org/manual/5.4/readme.html

πŸ‘·β€β™€οΈ Creating your first Lua script

Let's take the most basic of examples: writing a function. In Lua, you can define a function using the "function" keyword followed by the function name, its parameters (enclosed in parentheses), and its body (enclosed in curly braces). Sounds familiar, doesn't it? πŸ˜‰

function greet(greeting, name) 
   return greeting + ", " + name
end

message = greet("Hey", "Bruno")
pring(message) -- Hey, Bruno
Enter fullscreen mode Exit fullscreen mode

But, there's something new here: functions in Lua are first-class citizens, meaning they can be assigned to variables (as you can see with message = greet("Hey", "Bruno")), passed as arguments to other functions, and even returned as values from other functions. Here is an example use case: suppose you want to sort an array of numbers in ascending order. You can write a comparison function that returns true if the first argument is less than the second argument, and false otherwise. This comparison function can then be passed as an argument to the table.sort function, which sorts the array based on the results of the comparison function. Take a look at the code:

-- Define the comparison function
function compare(a, b) 
   return a < b
end

-- Define an array of numbers
numbers = {3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5}

-- Sort the array using the compare function
table.sort(numbers, compare)

-- Print the sorted array
for i, v in ipairs(numbers) do
  print(v)
end

-- Output:
-- 1
-- 1
-- 2
-- 3
-- 3
-- 4
-- 5
-- 5
-- 5
-- 6
-- 9
Enter fullscreen mode Exit fullscreen mode

In the above example, the comparison function compare() is a first-class citizen, meaning it can be assigned to a variable, passed as an argument to another function (table.sort), and even returned as a value from another function. This allows for a high degree of flexibility and reusability in the code, and makes it easy to write generic functions that can work with any comparison function. Cool, right? πŸ˜‰

✨ Thank you for reading!

Lua is a very powerful language and more articles will follow this one, so bear with me as we explore the realm of this language βœ¨πŸŒ™

moon gif

I'd like to thank you for reading my article and invite you to follow me on Dev.to, as well as my other platforms:

GitHub: https://github.com/bcostaaa01
Twitter: https://twitter.com/bruno2001costa
Enter fullscreen mode Exit fullscreen mode

I look forward to seeing you on my next one!

Until then,
Happy Coding!πŸ‘©β€πŸ’»

Top comments (0)