Skip to content

Commit a16eedb

Browse files
committed
refactor: move std.len to std.operator.len.
In addition to being useful for more than just tables, the len operation is great for functional programming. * specs/std_spec.yaml (len): Move from here... * specs/operator_spec.yaml (len): ...to here. * lib/std.lua.in (len): Move from here... * lib/std/operator (len): ...to here. Adjust all callers. * NEWS.md (Deprecations): Update. Signed-off-by: Gary V. Vaughan <gary@gnu.org>
1 parent c2e6dc2 commit a16eedb

16 files changed

Lines changed: 73 additions & 67 deletions

NEWS.md

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@
6666
- New `operator.eqv` is similar to `operator.eq`, except that it succeeds
6767
when recursive table contents are equivalent.
6868

69-
- New `std.len` replaces deprecated `std.table.len`.
69+
- New `std.operator.len` replaces deprecated `std.table.len`.
7070

7171
- `std.npairs` and `std.rnpairs` now respect `__len` metamethod, if any.
7272

@@ -78,9 +78,8 @@
7878
- `std.object.prototype` has been deprecated in favor of
7979
`std.object.type` for orthogonality with `io.type` and `math.type`.
8080

81-
- `std.table.len` has been deprecated in favour of `std.len`, because it
82-
is a portable stand-in for the core `#` operator, and because it also
83-
works on strings, Objects and other traversable types.
81+
- `std.table.len` has been deprecated in favour of `std.operator.len`,
82+
because it is not just for tables!
8483

8584
### Bug fixes
8685

lib/std.lua.in

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -185,13 +185,6 @@ M = {
185185
-- for e in rielems (l) do process (e) end
186186
ireverse = X ("ireverse (table)", std.ireverse),
187187

188-
--- Equivalent to `#` operation, but respecting `__len` even on Lua 5.1.
189-
-- @function len
190-
-- @tparam object|string|table x operand
191-
-- @treturn int length of list part of *t*
192-
-- @usage for i = 1, len (t) do process (t[i]) end
193-
len = X ("len (object|string|table)", std.len),
194-
195188
--- Overwrite core methods and metamethods with `std` enhanced versions.
196189
--
197190
-- Write all functions from this module, except `std.barrel` and

lib/std/base.lua

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -495,7 +495,6 @@ return {
495495
ielems = ielems,
496496
ipairs = ipairs,
497497
ireverse = ireverse,
498-
len = len,
499498
npairs = npairs,
500499
pairs = pairs,
501500
require = require,
@@ -539,6 +538,10 @@ return {
539538
mapfields = mapfields,
540539
},
541540

541+
operator = {
542+
len = len,
543+
},
544+
542545
package = {
543546
dirsep = dirsep,
544547
},

lib/std/debug.lua

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,11 @@ local std = require "std.base"
3434

3535
local _DEBUG = debug_init._DEBUG
3636

37-
local ipairs, len, pairs, stdtype, tostring =
38-
std.ipairs, std.len, std.pairs, std.type, std.tostring
37+
local ipairs, pairs, stdtype, tostring =
38+
std.ipairs, std.pairs, std.type, std.tostring
3939
local copy, last, raise = std.base.copy, std.base.last, std.base.raise
4040
local argerror = std.debug.argerror
41+
local len = std.operator.len
4142
local split = std.string.split
4243
local insert, maxn, unpack =
4344
std.table.insert, std.table.maxn, std.table.unpack

lib/std/functional.lua

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,11 @@
1010

1111
local std = require "std.base"
1212

13-
local ielems, ipairs, ireverse, len, npairs, pairs =
14-
std.ielems, std.ipairs, std.ireverse, std.len, std.npairs, std.pairs
13+
local ielems, ipairs, ireverse, npairs, pairs =
14+
std.ielems, std.ipairs, std.ireverse, std.npairs, std.pairs
1515
local copy = std.base.copy
1616
local callable, reduce = std.functional.callable, std.functional.reduce
17+
local len = std.operator.len
1718
local unpack = std.table.unpack
1819
local loadstring = loadstring or load
1920

lib/std/io.lua

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,9 @@ local std = require "std.base"
1515
local debug = require "std.debug"
1616

1717
local argerror, setmetatable = debug.argerror, debug.setmetatable
18-
local ipairs, len, pairs = std.ipairs, std.len, std.pairs
18+
local ipairs, pairs = std.ipairs, std.pairs
1919
local catfile = std.io.catfile
20+
local len = std.operator.len
2021
local dirsep = std.package.dirsep
2122
local split = std.string.split
2223
local insert = std.table.insert

lib/std/list.lua

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,9 @@ local std = require "std.base"
1616

1717
local Object = require "std.object".prototype
1818

19-
local ipairs, len, pairs = std.ipairs, std.len, std.pairs
19+
local ipairs, pairs = std.ipairs, std.pairs
2020
local compare = std.list.compare
21+
local len = std.operator.len
2122
local unpack = std.table.unpack
2223

2324
local M, List

lib/std/operator.lua

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,13 @@ local M = {
6262
-- functional.foldl (concat, "=> ", {10000, 100, 10})
6363
concat = function (a, b) return tostring (a) .. tostring (b) end,
6464

65+
--- Equivalent to `#` operation, but respecting `__len` even on Lua 5.1.
66+
-- @function len
67+
-- @tparam object|string|table x operand
68+
-- @treturn int length of list part of *t*
69+
-- @usage for i = 1, len (t) do process (t[i]) end
70+
len = std.operator.len,
71+
6572
--- Dereference a table.
6673
-- @tparam table t a table
6774
-- @param k a key to lookup in *t*

lib/std/optparse.lua

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,9 @@ local std = require "std.base"
1616

1717
local Object = require "std.object".prototype
1818

19-
local ipairs, len, pairs = std.ipairs, std.len, std.pairs
19+
local ipairs, pairs = std.ipairs, std.pairs
2020
local last = std.base.last
21+
local len = std.operator.len
2122
local insert = std.table.insert
2223

2324

lib/std/string.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ local debug = require "std.debug"
1515

1616
local StrBuf = require "std.strbuf".prototype
1717

18-
local getmetamethod, len, pairs =
19-
std.getmetamethod, std.len, std.pairs
18+
local getmetamethod, pairs = std.getmetamethod, std.pairs
2019
local copy = std.base.copy
20+
local len = std.operator.len
2121
local render = std.string.render
2222
local insert = std.table.insert
2323

0 commit comments

Comments
 (0)