Skip to content

Commit fcea7ad

Browse files
committed
maint: remove monkey_patch functions.
* lib/std/init.lua (barrel, monkey_patch): Remove. * lib/std/io.lua, lib/std/math.lua, lib/std/string.lua, lib/std/table.lua (monkey_patch): Remove. * specs/io_spec.yaml, specs/math_spec.yaml, specs/std_spec.yaml, specs/string_spec.yaml, specs/table_spec.yaml: Remove examples accordingly. * NEWS.md: Update. Signed-off-by: Gary V. Vaughan <gary@gnu.org>
1 parent 4b6968e commit fcea7ad

File tree

11 files changed

+20
-420
lines changed

11 files changed

+20
-420
lines changed

NEWS.md

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,11 @@
121121
`std.strict` and `std.tuple` have been moved to their own packages,
122122
and are no longer shipped as part of stdlib.
123123
124+
- Monkey patching calls `std.barrel`, `std.monkey_patch`,
125+
`std.io.monkey_patch`, `std.math.monkey_patch`,
126+
`std.string.monkey_patch` and `std.table.monkey_patch` have all
127+
been removed.
128+
124129
- `std.debug.argerror`, `std.debug.argcheck`, `std.debug.argscheck`,
125130
`std.debug.extramsg_mismatch`, `std.debug.extramsg_toomany`,
126131
`std.debug.parsetypes`, `std.debug.resulterror` and `std.debug.typesplit`
@@ -131,19 +136,13 @@
131136
removed. At some point these will resurface in a new standalone
132137
package.
133138
134-
- Deprecated multi-argument `functional.bind` has been removed.
135-
136139
- Deprecated methods `list:depair`, `list:elems`, `list:enpair`,
137140
`list:filter`, `list:flatten`, `list:foldl`, `list:foldr`,
138141
`list:index_key`, `list:index_value`, `list:map`, `list:map_with`,
139142
`list:project`, `list:relems`, `list:reverse`, `list:shape`,
140143
`list:transpose` and `list:zip_with` have been removed.
141144
142-
- Deprecated functions `functional.eval`, `functional.fold`,
143-
`functional.op["[]"]`, `functional.op["+"]`, `functional.op["-"]`,
144-
`functional.op["*"]`, `functional.op["/"], `functional.op["and"]',
145-
`functional.op["or"]`, `functional.op["not"]`, `functional.op["=="]`,
146-
`functional.op["~="]`, `list.depair`, `list.elems`, `list.enpair`,
145+
- Deprecated functions `list.depair`, `list.elems`, `list.enpair`,
147146
`list.filter`, `list.flatten`, `list.foldl`, `list.foldr`,
148147
`list.index_key`, `list.index_value`, `list.map`, `list.map_with`,
149148
`list.project`, `list.relems`, `list.reverse`, `list.shape`,

lib/std/init.lua

Lines changed: 5 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,10 @@
11
--[[--
22
Enhanced Lua core functions, and others.
33
4-
After requiring this module, simply referencing symbols in the
5-
submodule hierarchy will load the necessary modules on demand.
6-
7-
By default there are no changes to any global symbols, or monkey
8-
patching of core module tables and metatables. However, sometimes it's
9-
still convenient to do that: For example, when using stdlib from the
10-
REPL, or in a prototype where you want to throw caution to the wind and
11-
compatibility with other modules be damned. In that case, you can give
12-
stdlib permission to scribble all over your namespaces by using the
13-
various `monkey_patch` calls in the library.
4+
After requiring this module, simply referencing symbols in the submodule
5+
hierarchy will load the necessary modules on demand. There are no
6+
changes to any global symbols, or monkey patching of core module tables
7+
and metatables.
148
159
@todo Write a style guide (indenting/wrapping, capitalisation,
1610
function and variable names); library functions should call
@@ -21,7 +15,6 @@
2115

2216

2317
local _ENV = _ENV
24-
local _G = _G
2518
local error = error
2619
local ipairs = ipairs
2720
local pairs = pairs
@@ -87,13 +80,7 @@ _ = nil
8780
--[[ =============== ]]--
8881

8982

90-
local M, monkeys
91-
92-
93-
local function monkey_patch (namespace)
94-
copy (namespace or _G, monkeys)
95-
return M
96-
end
83+
local M
9784

9885

9986
local function _assert (expect, fmt, arg1, ...)
@@ -102,37 +89,6 @@ local function _assert (expect, fmt, arg1, ...)
10289
end
10390

10491

105-
local function barrel (namespace)
106-
namespace = namespace or _G
107-
108-
-- Older releases installed the following into _G by default.
109-
for _, name in pairs {
110-
"io.die", "io.warn",
111-
112-
"string.pickle", "string.prettytostring", "string.render",
113-
114-
"table.pack",
115-
116-
"tree.ileaves", "tree.inodes", "tree.leaves", "tree.nodes",
117-
} do
118-
local module, method = name:match "^(.*)%.(.-)$"
119-
namespace[method] = M[module][method]
120-
end
121-
122-
-- Support old api names, for backwards compatibility.
123-
namespace.metamethod = M.getmetamethod
124-
namespace.op = M.operator
125-
namespace.require_version = M.require
126-
127-
require "std.io".monkey_patch (namespace)
128-
require "std.math".monkey_patch (namespace)
129-
require "std.string".monkey_patch (namespace)
130-
require "std.table".monkey_patch (namespace)
131-
132-
return monkey_patch (namespace)
133-
end
134-
135-
13692
local function elems (t)
13793
-- capture _pairs iterator initial state
13894
local fn, istate, ctrl = _pairs (t)
@@ -255,29 +211,6 @@ M = {
255211
--- Module Functions
256212
-- @section modulefuncs
257213

258-
--- A [barrel of monkey_patches](http://dictionary.reference.com/browse/barrel+of+monkeys).
259-
--
260-
-- Apply **all** of stdlib's `monkey_patch` functions to *namespace*.
261-
--
262-
-- Additionally, for backwards compatibility only, write an historical
263-
-- selection of stdlib submodule functions into the given namespace too
264-
-- (at least until the next major release).
265-
-- @function barrel
266-
-- @tparam[opt=_G] table namespace where to install global functions
267-
-- @treturn table module table
268-
-- @usage local std = require "std".barrel ()
269-
barrel = X ("barrel (?table)", barrel),
270-
271-
--- Overwrite core methods and metamethods with `std` enhanced versions.
272-
--
273-
-- Write all functions from this module, except `std.barrel` and
274-
-- `std.monkey_patch`, into *namespace*.
275-
-- @function monkey_patch
276-
-- @tparam[opt=_G] table namespace where to install global functions
277-
-- @treturn table the module table
278-
-- @usage local std = require "std".monkey_patch ()
279-
monkey_patch = X ("monkey_patch (?table)", monkey_patch),
280-
281214
--- Enhance core `require` to assert version number compatibility.
282215
-- By default match against the last substring of (dot-delimited)
283216
-- digits in the module version string.
@@ -422,14 +355,6 @@ M = {
422355
}
423356

424357

425-
monkeys = copy ({}, M)
426-
427-
-- Don't monkey_patch these apis into _G!
428-
for _, api in ipairs {"barrel", "monkey_patch", "version"} do
429-
monkeys[api] = nil
430-
end
431-
432-
433358
--- Metamethods
434359
-- @section Metamethods
435360

lib/std/io.lua

Lines changed: 1 addition & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ local _DEBUG = _.debug_init._DEBUG
4343
local _ipairs = _.std.ipairs
4444
local _tostring = _.std.tostring
4545
local catfile = _.std.io.catfile
46-
local copy = _.std.base.copy
4746
local dirsep = _.std.package.dirsep
4847
local leaves = _.std.tree.leaves
4948
local len = _.std.operator.len
@@ -85,7 +84,7 @@ _ = nil
8584
--[[ =============== ]]--
8685

8786

88-
local M, monkeys
87+
local M
8988

9089

9190
local function input_handle (h)
@@ -134,21 +133,6 @@ local function writelines (h, ...)
134133
end
135134

136135

137-
local function monkey_patch (namespace)
138-
namespace = namespace or _G
139-
namespace.io = copy (namespace.io or {}, monkeys)
140-
141-
if namespace.io.stdin then
142-
local mt = getmetatable (namespace.io.stdin) or {}
143-
mt.readlines = M.readlines
144-
mt.writelines = M.writelines
145-
setmetatable (namespace.io.stdin, mt)
146-
end
147-
148-
return M
149-
end
150-
151-
152136
local function process_files (fn)
153137
-- N.B. "arg" below refers to the global array of command-line args
154138
if len (arg) == 0 then
@@ -284,19 +268,6 @@ M = {
284268
function (path) return split (path, dirsep) end),
285269

286270

287-
--- Module Functions
288-
-- @section modulefuncs
289-
290-
--- Overwrite core `io` methods with `std` enhanced versions.
291-
--
292-
-- Also adds @{readlines} and @{writelines} metamethods to core file objects.
293-
-- @function monkey_patch
294-
-- @tparam[opt=_G] table namespace where to install global functions
295-
-- @treturn table the `std.io` module table
296-
-- @usage local io = require "std.io".monkey_patch ()
297-
monkey_patch = X ("monkey_patch (?table)", monkey_patch),
298-
299-
300271
--- IO Functions
301272
-- @section iofuncs
302273

@@ -351,9 +322,6 @@ M = {
351322
}
352323

353324

354-
monkeys = copy ({}, M) -- before deprecations and core merge
355-
356-
357325
return merge (M, io)
358326

359327

lib/std/math.lua

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ local _ = {
2222
}
2323

2424
local _DEBUG = _.debug_init._DEBUG
25-
local copy = _.std.base.copy
2625
local merge = _.std.base.merge
2726

2827

@@ -69,13 +68,6 @@ local function floor (n, p)
6968
end
7069

7170

72-
local function monkey_patch (namespace)
73-
namespace = namespace or _G
74-
namespace.math = copy (namespace.math or {}, M)
75-
return M
76-
end
77-
78-
7971
local function round (n, p)
8072
local e = 10 ^ (p or 0)
8173
return math_floor (n * e + 0.5) / e
@@ -112,17 +104,6 @@ M = {
112104
-- @treturn number `n` rounded to `p` decimal places
113105
-- @usage roughly = round (exactly, 2)
114106
round = X ("round (number, ?int)", round),
115-
116-
117-
--- Module Functions
118-
-- @section modulefuncs
119-
120-
--- Overwrite core `math` methods with `std` enhanced versions.
121-
-- @function monkey_patch
122-
-- @tparam[opt=_G] table namespace where to install global functions
123-
-- @treturn table the module table
124-
-- @usage require "std.math".monkey_patch ()
125-
monkey_patch = X ("monkey_patch (?table)", monkey_patch),
126107
}
127108

128109

lib/std/string.lua

Lines changed: 1 addition & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -124,18 +124,6 @@ local function finds (s, p, i, ...)
124124
end
125125

126126

127-
local function monkey_patch (namespace)
128-
namespace = namespace or _G
129-
namespace.string = copy (namespace.string or {}, M)
130-
131-
local string_metatable = getmetatable ""
132-
string_metatable.__concat = M.__concat
133-
string_metatable.__index = M.__index
134-
135-
return M
136-
end
137-
138-
139127
local function caps (s)
140128
return (s:gsub ("(%w)([%w]*)", function (l, ls) return l:upper () .. ls end))
141129
end
@@ -302,7 +290,7 @@ M = {
302290
-- @param o object to stringify and concatenate
303291
-- @return s .. tostring (o)
304292
-- @usage
305-
-- local string = require "std.string".monkey_patch ()
293+
-- local string = setmetatable ("", require "std.string")
306294
-- concatenated = "foo" .. {"bar"}
307295
__concat = __concat,
308296

@@ -492,20 +480,6 @@ M = {
492480
-- @usage
493481
-- print (wrap (copyright, 72, 4))
494482
wrap = X ("wrap (string, ?int, ?int, ?int)", wrap),
495-
496-
497-
--- Module Functions
498-
-- @section modulefuncs
499-
500-
--- Overwrite core `string` methods with `std` enhanced versions.
501-
--
502-
-- Also adds auto-stringification to `..` operator on core strings, and
503-
-- integer indexing of strings with `[]` dereferencing.
504-
-- @function monkey_patch
505-
-- @tparam[opt=_G] table namespace where to install global functions
506-
-- @treturn table the module table
507-
-- @usage local string = require "std.string".monkey_patch ()
508-
monkey_patch = X ("monkey_patch (?table)", monkey_patch),
509483
}
510484

511485

lib/std/table.lua

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ _ = nil
7575
--[[ =============== ]]--
7676

7777

78-
local M, monkeys
78+
local M
7979

8080

8181
local function merge_allfields (t, u, map, nometa)
@@ -180,13 +180,6 @@ local function sort (t, c)
180180
end
181181

182182

183-
local function monkey_patch (namespace)
184-
namespace = namespace or _G
185-
namespace.table = copy (namespace.table or {}, monkeys)
186-
return M
187-
end
188-
189-
190183
local _remove = table.remove
191184

192185
local function remove (t, pos)
@@ -444,23 +437,9 @@ M = {
444437
-- @usage merge_select (_G, require "std.debug", {"say"}, false)
445438
merge_select = X ("merge_select (table, table, [table], ?boolean|:nometa)",
446439
merge_namedfields),
447-
448-
449-
--- Module Functions
450-
-- @section modulefuncs
451-
452-
--- Overwrite core `table` methods with `std` enhanced versions.
453-
-- @function monkey_patch
454-
-- @tparam[opt=_G] table namespace where to install global functions
455-
-- @treturn table the module table
456-
-- @usage local table = require "std.table".monkey_patch ()
457-
monkey_patch = X ("monkey_patch (?table)", monkey_patch),
458440
}
459441

460442

461-
monkeys = copy ({}, M) -- before deprecations and core merge
462-
463-
464443
return merge (M, table)
465444

466445

0 commit comments

Comments
 (0)