gpt4 book ai didi

lua - Lua 中 Underscore.lua 模块的用途是什么?

转载 作者:行者123 更新时间:2023-12-01 22:19:17 24 4
gpt4 key购买 nike

在一些源代码中,我看到了“Underscore.lua”模块的用法。

_ = require 'underscore'

描述如下:

Underscore.lua is a Lua library that provides a set of utility functions for dealing with iterators, arrays, tables, and functions. It's api and documentation are heavily inspired by Underscore.js. It is idiomatic in Lua to use the underscore character for throw away variables so you can simply assign it to another variable name...

文档中提供的示例让我有些困惑。例如,以下操作的目的是什么?

_ = require 'underscore'
_.each({1,2,3}, print)

1 1 {
1 : 1
2 : 2
3 : 3
}
2 2 {
1 : 1
2 : 2
3 : 3
}
3 3 {
1 : 1
2 : 2
3 : 3
}

很明显,这个模块为迭代器、数组等提供了一些函数。但是为什么要使用 _ ,因为你可以使用任何其他比下划线更明显的字符?特别是当许多编辑没有突出显示它并且阅读起来非常麻烦时。我不太明白有什么大惊小怪的。

最佳答案

But why use _ when you can just use any other character that is more visible than an underscore?

*咳咳*:

heavily inspired by Underscore.js

因为那是Underscore.js用过。

我怀疑下划线字符的选择恰恰是因为它不是很明显。这些实用程序的要点是您可以在任何地方使用它们,它们是您对数据结构进行基本操作所需的日常用品。

因此,它们应该看起来是语言中可用的标准函数,访问它们所需的语法最少。但是你不能只是将它们转储到全局表/ namespace /等中;这可能与现有代码冲突。因此,您为它们提供了访问它们所需的绝对最少的语法。

关于lua - Lua 中 Underscore.lua 模块的用途是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41428503/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com