加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

Lua适合比C/C++好用的例子

发布时间:2020-12-15 00:24:23 所属栏目:大数据 来源:网络整理
导读:我正在嵌入Lua并将其用作一个荣耀的智能配置文件.但是,我觉得我失去了一些东西,因为人们对于Lua的使用感到吃惊. 例如,我可以很容易地解释为什么你可以使用shell脚本而不是C来显示这个例子(诚然,boost regexp是overkill): #include dirent.h #include stdio.
我正在嵌入Lua并将其用作一个荣耀的智能配置文件.但是,我觉得我失去了一些东西,因为人们对于Lua的使用感到吃惊.

例如,我可以很容易地解释为什么你可以使用shell脚本而不是C来显示这个例子(诚然,boost regexp是overkill):

#include <dirent.h> 
#include <stdio.h> 
#include <boost/regex.hpp>

int main(int argc,char * argv[]) {
    DIR           *d;
    struct dirent *dir;
    boost::regex re(".*.cpp$");
    if (argc==2) d = opendir(argv[1]); else d = opendir(".");
if (d) {
    while ((dir = readdir(d)) != NULL) {
            if (boost::regex_match(dir->d_name,re)) printf("%sn",dir->d_name);
    }

    closedir(d);
}

return(0);

并将其与:

for foo in *.cpp; do echo $foo; done;

有没有什么例子,你可以给在Lua,可以让它’点击’为我?

编辑:也许我的问题是,我不知道Lua还不够流畅地使用它,因为我发现编写C代码更容易.

EDIT2:

一个例子是C和Lua中的玩具因子程序:

#include <iostream>

int fact (int n){
    if (n==0) return 1; else
    return (n*fact(n-1));
}

int main (){
    int input;
    using namespace std;
    cout << "Enter a number: " ;
    cin >> input;
    cout << "factorial: " << fact(input) << endl;
    return 0;
}

LUA:

function fact (n)
    if n==0 then
        return 1
    else 
        return n * (fact(n-1))
    end
end

print ("enter a number")
a = io.read("*number")
print ("Factorial: ",fact(a))

在这里,程序看起来是一样的,但是在include,namespace和main()声明中可以清除一些.也删除变量声明和强类型.

现在有人说这是加大一个更大的计划的优势,还是有更多的呢?这与bash示例的方式不同.

解决方法

使用Lua等脚本语言具有许多其他优点.

Lua对C的几个优点:

由于高层次的性质,开发时间往往较短,如您的例子.>它不需要重新编译来改变行为.>可以在非开发机器上更改行为.原型开发非常快速,简单,因为您只需在运行时调整逻辑.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读