forked from lua-stdlib/lua-stdlib
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstd.lua.in
More file actions
355 lines (304 loc) · 11.2 KB
/
std.lua.in
File metadata and controls
355 lines (304 loc) · 11.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
--[[--
Enhanced Lua core functions, and others.
After requiring this module, simply referencing symbols in the
submodule hierarchy will load the necessary modules on demand.
By default there are no changes to any global symbols, or monkey
patching of core module tables and metatables. However, sometimes it's
still convenient to do that: For example, when using stdlib from the
REPL, or in a prototype where you want to throw caution to the wind and
compatibility with other modules be damned. In that case, you can give
stdlib permission to scribble all over your namespaces by using the
various `monkey_patch` calls in the library.
@todo Write a style guide (indenting/wrapping, capitalisation,
function and variable names); library functions should call
error, not die; OO vs non-OO (a thorny problem).
@todo pre-compile.
@corefunction std
]]
local std = require "std.base"
local M, monkeys
local function monkey_patch (namespace)
std.base.copy (namespace or _G, monkeys)
return M
end
local function barrel (namespace)
namespace = namespace or _G
-- Older releases installed the following into _G by default.
for _, name in pairs {
"functional.bind", "functional.collect", "functional.compose",
"functional.curry", "functional.filter", "functional.id",
"functional.map",
"io.die", "io.warn",
"string.pickle", "string.prettytostring", "string.render",
"table.pack",
"tree.ileaves", "tree.inodes", "tree.leaves", "tree.nodes",
} do
local module, method = name:match "^(.*)%.(.-)$"
namespace[method] = M[module][method]
end
-- Support old api names, for backwards compatibility.
namespace.fold = M.functional.fold
namespace.metamethod = M.getmetamethod
namespace.op = M.operator
namespace.require_version = M.require
require "std.io".monkey_patch (namespace)
require "std.math".monkey_patch (namespace)
require "std.string".monkey_patch (namespace)
require "std.table".monkey_patch (namespace)
return monkey_patch (namespace)
end
--[[ ================= ]]--
--[[ Public Interface. ]]--
--[[ ================= ]]--
local function X (decl, fn)
return require "std.debug".argscheck ("std." .. decl, fn)
end
M = {
--- Release version string.
-- @field version
version = "General Lua libraries / @VERSION@",
--- Core Functions
-- @section corefuncs
--- Enhance core `assert` to also allow formatted arguments.
-- @function assert
-- @param expect expression, expected to be *truthy*
-- @string[opt=""] f format string
-- @param[opt] ... arguments to format
-- @return value of *expect*, if *truthy*
-- @usage
-- std.assert (expect == nil, "100% unexpected!")
-- std.assert (expect == "expect", "%s the unexpected!", expect)
assert = X ("assert (?any, ?string, [any...])", std.assert),
--- Evaluate a string as Lua code.
-- @function eval
-- @string s string of Lua code
-- @return result of evaluating `s`
-- @usage
-- --> 2
-- std.eval "math.min (2, 10)"
eval = X ("eval (string)", std.eval),
--- Return named metamethod, if any, otherwise `nil`.
-- The value found at the given key in the metatable of *x* must be a
-- function or have its own `__call` metamethod to qualify as a
-- callable. Any other value found at key *n* will cause this function
-- to return `nil`.
-- @function getmetamethod
-- @param x item to act on
-- @string n name of metamethod to lookup
-- @treturn callable|nil callable metamethod, or `nil` if no metamethod
-- @usage
-- clone = std.getmetamethod (std.object.prototype, "__call")
getmetamethod = X ("getmetamethod (?any, string)", std.getmetamethod),
--- Enhance core `tostring` to render table contents as a string.
-- @function tostring
-- @param x object to convert to string
-- @treturn string compact string rendering of *x*
-- @usage
-- -- {1=baz,foo=bar}
-- print (std.tostring {foo="bar","baz"})
tostring = X ("tostring (?any)", std.tostring),
--- Type of an object, or primitive.
-- @function type
-- @param x anything
-- @treturn string type of *x*
-- @see std.object.type
type = X ("type (?any)", std.type),
--- Module Functions
-- @section modulefuncs
--- A [barrel of monkey_patches](http://dictionary.reference.com/browse/barrel+of+monkeys).
--
-- Apply **all** of stdlib's `monkey_patch` functions to *namespace*.
--
-- Additionally, for backwards compatibility only, write an historical
-- selection of stdlib submodule functions into the given namespace too
-- (at least until the next major release).
-- @function barrel
-- @tparam[opt=_G] table namespace where to install global functions
-- @treturn table module table
-- @usage local std = require "std".barrel ()
barrel = X ("barrel (?table)", barrel),
--- Overwrite core methods and metamethods with `std` enhanced versions.
--
-- Write all functions from this module, except `std.barrel` and
-- `std.monkey_patch`, into *namespace*.
-- @function monkey_patch
-- @tparam[opt=_G] table namespace where to install global functions
-- @treturn table the module table
-- @usage local std = require "std".monkey_patch ()
monkey_patch = X ("monkey_patch (?table)", monkey_patch),
--- Enhance core `require` to assert version number compatibility.
-- By default match against the last substring of (dot-delimited)
-- digits in the module version string.
-- @function require
-- @string module module to require
-- @string[opt] min lowest acceptable version
-- @string[opt] too_big lowest version that is too big
-- @string[opt] pattern to match version in `module.version` or
-- `module._VERSION` (default: `"([%.%d]+)%D*$"`)
-- @usage
-- -- posix.version == "posix library for Lua 5.2 / 32"
-- posix = require ("posix", "29")
require = X ("require (string, ?string, ?string, ?string)", std.require),
--- Iterator Functions
-- @section iteratorfuncs
--- An iterator over all values of a table.
-- If *t* has a `__pairs` metamethod, use that to iterate.
-- @function elems
-- @tparam table t a table
-- @treturn function iterator function
-- @treturn table *t*, the table being iterated over
-- @return *key*, the previous iteration key
-- @see ielems
-- @see pairs
-- @usage
-- --> foo
-- --> bar
-- --> baz
-- --> 5
-- std.functional.map (print, std.ielems, {"foo", "bar", [4]="baz", d=5})
elems = X ("elems (table)", std.elems),
--- An iterator over the integer keyed elements of a table.
--
-- If *t* has a `__len` metamethod, iterate up to the index it
-- returns, otherwise up to the first `nil`.
--
-- This function does **not** support the Lua 5.2 `__ipairs` metamethod.
-- @function ielems
-- @tparam table t a table
-- @treturn function iterator function
-- @treturn table *t*, the table being iterated over
-- @treturn int *index*, the previous iteration index
-- @see elems
-- @see ipairs
-- @usage
-- --> foo
-- --> bar
-- std.functional.map (print, std.ielems, {"foo", "bar", [4]="baz", d=5})
ielems = X ("ielems (table)", std.ielems),
--- An iterator over integer keyed pairs of a sequence.
--
-- Like Lua 5.1 and 5.3, this iterator returns successive key-value
-- pairs with integer keys starting at 1, up to the first `nil` valued
-- pair.
--
-- If there is a `_len` metamethod, keep iterating up to and including
-- that element, regardless of any intervening `nil` values.
--
-- This function does **not** support the Lua 5.2 `__ipairs` metamethod.
-- @function ipairs
-- @tparam table t a table
-- @treturn function iterator function
-- @treturn table *t*, the table being iterated over
-- @treturn int *index*, the previous iteration index
-- @see ielems
-- @see npairs
-- @see pairs
-- @usage
-- --> 1 foo
-- --> 2 bar
-- std.functional.map (print, std.ipairs, {"foo", "bar", [4]="baz", d=5})
ipairs = X ("ipairs (table)", std.ipairs),
--- Return a new sequence with element order reversed.
--
-- Apart from the order of the elements returned, this function follows
-- the same rules as @{ipairs} for determining first and last elements.
-- @function ireverse
-- @tparam table t a table
-- @treturn table a new table with integer keyed elements in reverse
-- order with respect to *t*
-- @see ielems
-- @see ipairs
-- @usage
-- local rielems = std.functional.compose (std.ireverse, std.ielems)
-- --> bar
-- --> foo
-- std.functional.map (print, rielems, {"foo", "bar", [4]="baz", d=5})
ireverse = X ("ireverse (table)", std.ireverse),
--- Ordered iterator for integer keyed values.
-- Like ipairs, but does not stop until the __len or maxn of *t*.
-- @function npairs
-- @tparam table t a table
-- @treturn function iterator function
-- @treturn table t
-- @see ipairs
-- @see rnpairs
-- @usage
-- --> 1 foo
-- --> 2 bar
-- --> 3 nil
-- --> 4 baz
-- std.functional.map (print, std.npairs, {"foo", "bar", [4]="baz", d=5})
npairs = X ("npairs (table)", std.npairs),
--- Enhance core `pairs` to respect `__pairs` even in Lua 5.1.
-- @function pairs
-- @tparam table t a table
-- @treturn function iterator function
-- @treturn table *t*, the table being iterated over
-- @return *key*, the previous iteration key
-- @see elems
-- @see ipairs
-- @usage
-- --> 1 foo
-- --> 2 bar
-- --> 4 baz
-- --> d 5
-- std.functional.map (print, std.pairs, {"foo", "bar", [4]="baz", d=5})
pairs = X ("pairs (table)", std.pairs),
--- An iterator like ipairs, but in reverse.
-- Apart from the order of the elements returned, this function follows
-- the same rules as @{ipairs} for determining first and last elements.
-- @function ripairs
-- @tparam table t any table
-- @treturn function iterator function
-- @treturn table *t*
-- @treturn number `#t + 1`
-- @see ipairs
-- @see rnpairs
-- @usage
-- --> 2 bar
-- --> 1 foo
-- std.functional.map (print, std.ripairs, {"foo", "bar", [4]="baz", d=5})
ripairs = X ("ripairs (table)", std.ripairs),
--- An iterator like npairs, but in reverse.
-- Apart from the order of the elements returned, this function follows
-- the same rules as @{npairs} for determining first and last elements.
-- @function rnpairs
-- @tparam table t a table
-- @treturn function iterator function
-- @treturn table t
-- @see npairs
-- @see ripairs
-- @usage
-- --> 4 baz
-- --> 3 nil
-- --> 2 bar
-- --> 1 foo
-- std.functional.map (print, std.rnpairs, {"foo", "bar", [4]="baz", d=5})
rnpairs = X ("rnpairs (table)", std.rnpairs),
}
monkeys = std.base.copy ({}, M)
-- Don't monkey_patch these apis into _G!
for _, api in ipairs {"barrel", "monkey_patch", "version"} do
monkeys[api] = nil
end
--- Metamethods
-- @section Metamethods
return setmetatable (M, {
--- Lazy loading of stdlib modules.
-- Don't load everything on initial startup, wait until first attempt
-- to access a submodule, and then load it on demand.
-- @function __index
-- @string name submodule name
-- @treturn table|nil the submodule that was loaded to satisfy the missing
-- `name`, otherwise `nil` if nothing was found
-- @usage
-- local std = require "std"
-- local Object = std.object.prototype
__index = function (self, name)
local ok, t = pcall (require, "std." .. name)
if ok then
rawset (self, name, t)
return t
end
end,
})