aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorV1K1NGbg <victor@ilchev.com>2024-07-27 01:05:34 +0300
committerV1K1NGbg <victor@ilchev.com>2024-08-05 20:49:17 +0300
commit7101268df03250c371bec9cc25de791ae790d22f (patch)
tree16eb93b7e51c53fe5a1e3ee8c09bb3cb22a8dd52
parent2fa95314cc273c2cfc941e97bf8a183e048bccf6 (diff)
downloadluarocks-7101268df03250c371bec9cc25de791ae790d22f.tar.gz
luarocks-7101268df03250c371bec9cc25de791ae790d22f.tar.bz2
luarocks-7101268df03250c371bec9cc25de791ae790d22f.zip
util and fun
-rw-r--r--src/luarocks/build.lua2
-rw-r--r--src/luarocks/cmd/install.lua2
-rw-r--r--src/luarocks/core/util.lua3
-rw-r--r--src/luarocks/fun.tl148
-rw-r--r--src/luarocks/util-original.lua (renamed from src/luarocks/util.lua)0
-rw-r--r--src/luarocks/util.tl32
6 files changed, 151 insertions, 36 deletions
diff --git a/src/luarocks/build.lua b/src/luarocks/build.lua
index 56e5ebd4..c294b98e 100644
--- a/src/luarocks/build.lua
+++ b/src/luarocks/build.lua
@@ -14,7 +14,7 @@ local repos = require("luarocks.repos")
14local writer = require("luarocks.manif.writer") 14local writer = require("luarocks.manif.writer")
15local deplocks = require("luarocks.deplocks") 15local deplocks = require("luarocks.deplocks")
16 16
17build.opts = util.opts_table("build.opts", { 17build.opts = util.opts_table("build.opts", { --WORK
18 need_to_fetch = "boolean", 18 need_to_fetch = "boolean",
19 minimal_mode = "boolean", 19 minimal_mode = "boolean",
20 deps_mode = "string", 20 deps_mode = "string",
diff --git a/src/luarocks/cmd/install.lua b/src/luarocks/cmd/install.lua
index c3b5f811..4f27e471 100644
--- a/src/luarocks/cmd/install.lua
+++ b/src/luarocks/cmd/install.lua
@@ -54,7 +54,7 @@ function install.add_to_parser(parser)
54 parser:flag("--sign"):hidden(true) 54 parser:flag("--sign"):hidden(true)
55end 55end
56 56
57install.opts = util.opts_table("install.opts", { 57install.opts = util.opts_table("install.opts", { --WORK
58 namespace = "string?", 58 namespace = "string?",
59 keep = "boolean", 59 keep = "boolean",
60 force = "boolean", 60 force = "boolean",
diff --git a/src/luarocks/core/util.lua b/src/luarocks/core/util.lua
index f72f1ec6..8cd2d25e 100644
--- a/src/luarocks/core/util.lua
+++ b/src/luarocks/core/util.lua
@@ -9,10 +9,9 @@ local util = {Ordering = {}, }
9 9
10 10
11 11
12local dir_sep = package.config:sub(1, 1)
13
14 12
15 13
14local dir_sep = package.config:sub(1, 1)
16 15
17 16
18 17
diff --git a/src/luarocks/fun.tl b/src/luarocks/fun.tl
new file mode 100644
index 00000000..f52e059d
--- /dev/null
+++ b/src/luarocks/fun.tl
@@ -0,0 +1,148 @@
1
2--- A set of basic functional utilities
3local record fun
4end
5
6function fun.concat(xs: {any}, ys: {any}): {any} --? not generic becuse lua suports differnt types in one array
7 local rs = {}
8 local n = #xs
9 for i = 1, n do
10 rs[i] = xs[i]
11 end
12 for i = 1, #ys do
13 rs[i + n] = ys[i]
14 end
15 return rs
16end
17
18function fun.contains(xs: {any}, v: any): boolean --? same logic as above fine?
19 for _, x in ipairs(xs) do
20 if v == x then
21 return true
22 end
23 end
24 return false
25end
26
27function fun.map<K, V>(xs: {K}, f: function(K): V): {V} --? right
28 local rs = {}
29 for i = 1, #xs do
30 rs[i] = f(xs[i])
31 end
32 return rs
33end
34
35function fun.filter<K>(xs: {K}, f: function): {K}
36 local rs = {}
37 for i = 1, #xs do
38 local v = xs[i]
39 if f(v) then
40 rs[#rs+1] = v
41 end
42 end
43 return rs
44end
45
46function fun.traverse<K, V>(t: {K}, f: function(K): V): {V} --? right or {any} | any
47 return fun.map(t, function(x: K): V
48 -- return x is table and fun.traverse(x, f) or f(x) --?
49 if x is {any} then
50 fun.traverse(x, f)
51 else
52 f(x)
53 end
54 end)
55end
56
57function fun.reverse_in(t: {any}): {any}
58 for i = 1, math.floor(#t/2) do
59 local m, n = i, #t - i + 1
60 local a, b = t[m], t[n]
61 t[m] = b
62 t[n] = a
63 end
64 return t
65end
66
67function fun.sort_in<K>(t: {K}, f: table.SortFunction<K>): {K}
68 table.sort(t, f)
69 return t
70end
71
72function fun.flip<K, V, S>(f: function(K, V): S): function(V, K): S
73 return function(a: V, b: K): S
74 return f(b, a)
75 end
76end
77
78function fun.find(xs: function | {any}, f: function): any
79 if xs is function then
80 for v in xs do
81 local x = f(v)
82 if x then
83 return x
84 end
85 end
86 elseif xs is {any} then
87 for _, v in ipairs(xs) do
88 local x = f(v)
89 if x then
90 return x
91 end
92 end
93 end
94end
95
96function fun.partial<K>(f: (function(...: any): K), ...: any): (function(...: any): K)
97 local n = select("#", ...)
98 if n == 1 then
99 local a = ...
100 return function(...): K
101 return f(a, ...)
102 end
103 elseif n == 2 then
104 local a, b = ...
105 return function(...): K
106 return f(a, b, ...)
107 end
108 else
109 local pargs = { n = n, ... }
110 return function(...): K
111 local m = select("#", ...)
112 local fargs = { ... }
113 local args = {}
114 for i = 1, n do
115 args[i] = pargs[i]
116 end
117 for i = 1, m do
118 args[i+n] = fargs[i]
119 end
120 return f(table.unpack(args, 1, n+m))
121 end
122 end
123end
124
125function fun.memoize(fn)
126 local memory = setmetatable({}, { __mode = "k" })
127 local errors = setmetatable({}, { __mode = "k" })
128 local NIL = {}
129 return function(arg)
130 if memory[arg] then
131 if memory[arg] == NIL then
132 return nil, errors[arg]
133 end
134 return memory[arg]
135 end
136 local ret1, ret2 = fn(arg)
137 if ret1 ~= nil then
138 memory[arg] = ret1
139 else
140 memory[arg] = NIL
141 errors[arg] = ret2
142 end
143 return ret1, ret2
144 end
145end
146
147return fun
148--? look for wrong/missing generics usage \ No newline at end of file
diff --git a/src/luarocks/util.lua b/src/luarocks/util-original.lua
index de9157fc..de9157fc 100644
--- a/src/luarocks/util.lua
+++ b/src/luarocks/util-original.lua
diff --git a/src/luarocks/util.tl b/src/luarocks/util.tl
index 505584a0..aa04a3f3 100644
--- a/src/luarocks/util.tl
+++ b/src/luarocks/util.tl
@@ -548,38 +548,6 @@ do
548 } 548 }
549 end 549 end
550end 550end
551
552function util.opts_table(type_name: string, valid_opts: {string: string}): function({string : string}): {string : string}
553 local opts_mt: metatable<{string : string}> = {
554 -- type: function(): string --!
555 }
556
557 opts_mt.__index = opts_mt
558
559 function opts_mt.type(): string --!
560 return type_name
561 end
562
563 return function(opts: {string: string}): {string: string}
564 for k, v in pairs(opts) do
565 local tv = type(v)
566 if not valid_opts[k] then
567 error("invalid option: "..k)
568 end
569 local vo, optional = valid_opts[k]:match("^(.-)(%??)$")
570 if not (tv == vo or (optional == "?" and tv == nil)) then
571 error("invalid type option: "..k.." - got "..tv..", expected "..vo)
572 end
573 end
574 for k, v in pairs(valid_opts) do
575 if (not v:find("?", 1, true)) and opts[k] == nil then
576 error("missing option: "..k)
577 end
578 end
579 return setmetatable(opts, opts_mt)
580 end
581end
582
583--- Return a table of modules that are already provided by the VM, which 551--- Return a table of modules that are already provided by the VM, which
584-- can be specified as dependencies without having to install an actual rock. 552-- can be specified as dependencies without having to install an actual rock.
585-- @param rockspec (optional) a rockspec table, so that rockspec format 553-- @param rockspec (optional) a rockspec table, so that rockspec format