translate: Is Lua worth learning

This commit is contained in:
Muggle Wei 2022-11-10 11:26:21 +08:00
parent c022dfa003
commit df71941e04

View File

@ -2,49 +2,49 @@
[#]: via: "https://opensource.com/article/22/11/lua-worth-learning"
[#]: author: "Seth Kenlon https://opensource.com/users/seth"
[#]: collector: "lkxed"
[#]: translator: " "
[#]: translator: "MuggleWei"
[#]: reviewer: " "
[#]: publisher: " "
[#]: url: " "
Is Lua worth learning?
Lua值得学习吗?
======
Lua is a fun and robust language, with progressive improvements made with each release, and an ever-growing developer base. Discover all of its possibilities.
Lua是一个有趣且强大的语言, 随着版本的推进, 功能愈发的强大, 开发者也在不断的增长. 这篇文章我们将探索一下它的可能性.
Lua is a scripting language used for procedural programming, functional programming, and even [object-oriented programming][1]. It uses a C-like syntax, but is dynamically typed, features automatic memory management and garbage collection, and runs by interpreting bytecode with a register-based virtual machine. This makes it a great language for beginners, but also a powerful tool for experienced programmers.
Lua是一个脚本语言, 它面向过程, 函数式编程, 甚至可以是[面向对象的][1]. 它使用类c的语法, 但却是动态类型, 具有自动内存管理和垃圾回收功能, 使用基于寄存器的虚拟机来解释字节码. 这些特点使得它对于初学者来说是个很好的语言, 同时也是经验丰富的程序员的强大工具.
Lua has been somewhat eclipsed from the public view by languages like [Python][2] and [JavaScript][3], but Lua has several advantages that make it popular in some major software projects. Lua is easily embedded within other languages, meaning that you can include Lua files in the code base of something written in (for instance) Java and it runs as if it were native Java code. It sounds like magic, but of course there are projects like [luaj][4] working to make it possible, and it's only possible because Lua is designed for it. It's partly because of this flexibility that you're likely to find Lua as the scripting language for video games, graphic applications, and more.
虽然与[Python][2]和[JavaScript][3]相比, Lua现在已经有点儿黯然失色了, 但是Lua拥有的一些优点使得它在许多的重大软件项目中很受欢迎. Lua很容易嵌入到其他语言当中, 这意味着你可以在(例如)Java编写的代码中包含Lua文件, 就像原生的Java代码一样运行. 这听起来就像魔法一般, 现在有许多项目如[luaj][4]使得其成为可能, 之所以可以实现, 正是因为Lua就是为此而设计的. 正因这部分的灵活性, 你可以在许多游戏, 图形应用的程序中发现Lua脚本的身影.
As with anything, it takes time to perfect, but Lua is easy (and fun) to learn. It's a consistent language, a friendly language with useful error messages, and there's lots of great support online. Ready to get started?
就像其他任何事情一样, 做到完美是需要时间的, 但是Lua是很易于学习(并且有趣)的语言. 它是一种一致性的语言, 一种带有有用的错误消息的友好的语言, 并且可以在网上轻松找到许多有用的资料. 那么就让我们开始吧!
### Installing Lua
### 安装Lua
On Linux, you can install Lua using your distribution's package manager. For instance, on Fedora, CentOS, Mageia, OpenMandriva, and similar distributions:
在Linux下, 你可以使用发行版自带的包管理来安装Lua. 例如, 在Fedora, CentOS, Mageia, OpenMandriva以及相似的发行版中:
```
$ sudo dnf install lua
```
On Debian and Debian-based systems:
在Debian以及基于Debian的系统中:
```
$ sudo apt install lua
```
For Mac, you can use [MacPorts][5] or [Homebrew][6].
对于Mac, 你可以使用[MacPorts][5] 或者 [Homebrew][6].
```
$ sudo port install lua
```
For Windows, install Lua using [Chocolatey][7].
对于Windows, 可以使用[Chocolatey][7]安装Lua.
To test Lua in an interactive interpreter, type `lua` in a terminal.
完成安装后, 可以在终端中输入`lua`来在交互式解释器中使用Lua.
### Functions
### 函数
As with many programming languages, Lua syntax generally involves a built-in function or keyword, followed by an argument. For instance, the `print` function displays any argument you provide to it.
如许多编程语言一样, Lua调用一个内建的函数或关键字, 后面跟着参数. 例如, `print`函数显示你传给它的所有参数.
```
$ lua
@ -54,16 +54,16 @@ Lua 5.4.2 Copyright (C) 1994-2020 Lua.org, PUC-Rio
hello
```
Lua's `string` library can manipulate words (called "strings" in programming.) For instance, to count the letters in a string, you use the `len` function of the `string` library:
Lua的`string`库可以操作单词(在编程中称为"字符串"). 例如, 统计字符串中的字母数量, 你可以使用`string`库中`len`函数:
```
> string.len('hello')
5
```
### Variables
### 变量
A variable allows you to create a special place in your computer's memory for temporary data. You can create variables in Lua by inventing a name for your variable, and then putting some data into it.
一个变量允许你在计算机内存中为临时的数据创建一个指定的空间. Lua中创建变量的方法是赋予变量一个名字, 接着将数据放入其中.
```
> foo = "hello world"
@ -74,17 +74,17 @@ hello world
3
```
### Tables
###
Second only to the popularity of variables in programming is the popularity of arrays. The word "array" literally means an arrangement, and that's all a programming array is. It's a specific arrangement of data, and because there is an arrangement, an array has the advantage of being structured. An array is often used to perform essentially the same purpose as a variable, except that an array can enforce an order to its data. In Lua, an array is called a `table`.
在编程中, 数组的使用频率仅次于变量的存在. "数组"这个词的字面意思就是一种排列, 而这就是程序中数组的意义了. 它是数据的一种排列, 因为有排列, 所有数组具有结构化的优势. 本只上数组通常用于和变量相同的目的, 只不过数组会给对其中的数据进行排序. 在Lua中, 数组被称为`表`.
Creating a table is like creating a variable, except that you set its initial content to two braces (`{}`):
创建表和创建变量类似, 区别仅在于它的初始化内容被设置为`{}`:
```
> mytable = {}
```
When you add data to a table, it's also a lot like creating a variable, except that your variable name always begins with the name of the table, and is separated with a dot:
当往表中增加数据时, 它就如同创建变量一样, 区别在于这里的变量之前总是以表名开头, 中间使用`.`来连接:
```
> mytable.foo = "hello world"
@ -95,17 +95,17 @@ hello world
3
```
### Scripting with Lua
### 使用Lua作为脚本
Running Lua in the terminal is great for getting instant feedback, but it's more useful to run Lua as a script. A Lua script is just a text file containing Lua code, which the Lua command can interpret and execute.
在终端交互环境中运行Lua可以得到良好的反馈, 但是将Lua作为脚本运行会更为有用. 一个Lua脚本就是包含Lua代码的文本文件, Lua命令可以解析并执行此文件.
The eternal question, when just starting to learn a programming language, is how you're supposed to know what to write. This article has provided a good start, but so far you only know two or three Lua functions. The key, of course, is in documentation. The Lua language isn't that complex, and it's very reasonable to refer to the [Lua documentation site][8] for a list of keywords and functions.
对于开始学习一门编程语言, 永恒的问题是你怎么知道该写什么. 这篇文章将提供一个不错的开端, 截至目前, 你仅知道了两三个Lua函数. 懂得查阅文档是很关键的. Lua并不是一个复杂的语言, 可以通过[Lua文档网站][8]很方便的获取关键字以及函数的用法.
Here's a practice problem.
下面是一个练习题.
Suppose you want to write a Lua script that counts words in a sentence. As with many programming challenges, there are many ways to go about this, but say the first relevant function you find in the Lua docs is `string.gmatch`, which can search for a specific character in a string. Words are usually separated by an empty space, so you decide that counting spaces + 1 ought to render a reasonably accurate count of the words they're separating.
假设你想编写一个Lua脚本来统计句子中的单词数量. 与众多的编程挑战一样, 有许多方法可以解决这个问题, 假设你在Lua文档中找到的第一个相关的函数是`string.gmatch`, 此函数可以搜索字符串中的特定字符. 单词通常通过空格分隔开来, 所以你决定计算空格数并加1来作为单词的数量.
Here's the code for that function:
下面是实现的代码
```
function wc(words,delimiter)
@ -118,20 +118,20 @@ function wc(words,delimiter)
end
```
These are the components of that sample code:
下面是这个样例代码的解释
- `function`: A keyword declaring the start of a function. A custom function works basically the same way as built-in functions (like `print` and `string.len`.)
- `words` and `delimiter`: Arguments required for the function to run. In the statement `print('hello')`, the word `hello` is an argument.
- `counter`: A variable set to 1.
- `for`: A loop using the `string.gmatch` function as it iterates over the `words` you've input into the function, and searches for the `delimiter` you've input.
- `count = count +1`: For each `delimiter` found, the value of `count` is re-set to its current value plus 1.
- `end`: A keyword ending the `for` loop.
- `return count`: This function outputs (or returns) the contents of the `count` variable.
- `end`: A keyword ending the function.
- `function`: 这是声明函数开始的关键字. 自定义函数的工作方式与内置函数(如`print`和`string.len`)基本相同
- `words`和`delimiter`: 这是函数运行所需的参数. 正如`print('hello')`当中, `hello`是一个参数
- `counter`: 一个变量, 且被初始化为1
- `for`: 在循环中使用`string.gmatch`作为迭代器遍历`words`, 并且在其中搜索`delimiter`
- `count = count +1`: 当搜索到了`delimiter`, 则对`count`进行自增1的操作
- `end`: `for`循环的结束关键字
- `return count`: 这个函数输出(或返回)`count`变量的内容
- `end`: 函数结束的关键字
Now that you've created a function all your own, you can use it. That's an important thing to remember about a function. It doesn't run on its own. It waits for you to call it in your code.
现在你已经创建了一个函数, 你可以使用它. 需要记住, 函数不会自动运行, 而是等待你在代码中调用它.
Type this sample code into a text file and save it as `words.lua`:
将下面的代码写入文件并保存为`words.lua`
```
function wc(words,delimiter)
@ -152,7 +152,7 @@ result = wc('can you find the bug? ', ' ')
print(result)
```
You've just created a Lua script. You can run it with Lua. Can you find the problem with this method of counting words?
你现在创建了一个Lua脚本. 你可以使用Lua运行它. 随后你会发现统计单词的结果有些问题
```
$ lua ./words.lua
@ -161,11 +161,11 @@ $ lua ./words.lua
6
```
You might notice that the count is incorrect for the final phrase because there's a trailing space in the argument. Lua correctly detected the space and tallied it into `count`, but the word count is incorrect because that particular space happens not to delimit a word. I leave it to you to solve that problem, or to find other ways in which this method isn't ideal. There's a lot of rumination in programming. Sometimes it's purely academic, and other times it's a question of whether an application works at all.
你也许已经注意到了最后一个句子的单词统计不正确, 因为在句子的最后带有一个额外的空格. Lua正确的检测到了空格, 并将其计入`count`中, 但是单子的统计是错误的, 因为有个空格并没有作为单词的分隔出现. 我把这个问题留给你来解决, 或者去发现其他方法, 即使这个方法不太理想. 编程中有很多需要思考的地方. 有时是纯粹学术性的思考, 有时也许是应用是否运训正常的思考.
### Learning Lua
### 学习Lua
Lua is a fun and robust language, with progressive improvements made with each release, and an ever-growing developer base. You can use Lua as a utilitarian language for personal scripts, or to advance your career, or just as an experiment in learning a new language. Give it a try, and see what Lua brings to the table.
Lua是一个有趣且强大的语言, 随着版本的推进, 功能愈发的强大, 开发者也在不断的增长. 你可以将Lua作为实用性的个人脚本使用, 或者在工作中使用, 或者仅仅是体验一下一个新的语言. 尝试一下, 看看Lua能给你带来什么吧.
--------------------------------------------------------------------------------
@ -173,7 +173,7 @@ via: https://opensource.com/article/22/11/lua-worth-learning
作者:[Seth Kenlon][a]
选题:[lkxed][b]
译者:[译者ID](https://github.com/译者ID)
译者:[MuggleWei](https://github.com/MuggleWei)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出