From ee82681346814fc20ccca9f1e397206ea7538465 Mon Sep 17 00:00:00 2001 From: V1K1NGbg Date: Thu, 1 Aug 2024 17:31:14 +0300 Subject: fun completed --- src/luarocks/fetch/cvs.tl | 2 +- src/luarocks/fun-original.lua | 143 ++++++++++++++++++++++++++++++++++++++++++ src/luarocks/fun.lua | 37 ++++++----- src/luarocks/fun.tl | 26 ++++---- src/luarocks/type_check.tl | 2 +- 5 files changed, 179 insertions(+), 31 deletions(-) create mode 100644 src/luarocks/fun-original.lua (limited to 'src') diff --git a/src/luarocks/fetch/cvs.tl b/src/luarocks/fetch/cvs.tl index 1d874daf..60f55957 100644 --- a/src/luarocks/fetch/cvs.tl +++ b/src/luarocks/fetch/cvs.tl @@ -17,7 +17,7 @@ local type Rockspec = cfg.Rockspec -- @return (string, string) or (nil, string): The absolute pathname of -- the fetched source tarball and the temporary directory created to -- store it; or nil and an error message. -function cvs.get_sources(rockspec: Rockspec, extract, dest_dir?: string): string, string --? extract not needed +function cvs.get_sources(rockspec: Rockspec, extract: boolean, dest_dir?: string): string, string --? extract not needed local cvs_cmd = rockspec.variables.CVS local ok, err_msg = fs.is_tool_available(cvs_cmd, "CVS") if not ok then diff --git a/src/luarocks/fun-original.lua b/src/luarocks/fun-original.lua new file mode 100644 index 00000000..80bf7c20 --- /dev/null +++ b/src/luarocks/fun-original.lua @@ -0,0 +1,143 @@ + +--- A set of basic functional utilities +local fun = {} + +local unpack = table.unpack or unpack + +function fun.concat(xs, ys) + local rs = {} + local n = #xs + for i = 1, n do + rs[i] = xs[i] + end + for i = 1, #ys do + rs[i + n] = ys[i] + end + return rs +end + +function fun.contains(xs, v) + for _, x in ipairs(xs) do + if v == x then + return true + end + end + return false +end + +function fun.map(xs, f) + local rs = {} + for i = 1, #xs do + rs[i] = f(xs[i]) + end + return rs +end + +function fun.filter(xs, f) + local rs = {} + for i = 1, #xs do + local v = xs[i] + if f(v) then + rs[#rs+1] = v + end + end + return rs +end + +function fun.traverse(t, f) + return fun.map(t, function(x) + return type(x) == "table" and fun.traverse(x, f) or f(x) + end) +end + +function fun.reverse_in(t) + for i = 1, math.floor(#t/2) do + local m, n = i, #t - i + 1 + local a, b = t[m], t[n] + t[m] = b + t[n] = a + end + return t +end + +function fun.sort_in(t, f) + table.sort(t, f) + return t +end + +function fun.flip(f) + return function(a, b) + return f(b, a) + end +end + +function fun.find(xs, f) + if type(xs) == "function" then + for v in xs do + local x = f(v) + if x then + return x + end + end + elseif type(xs) == "table" then + for _, v in ipairs(xs) do + local x = f(v) + if x then + return x + end + end + end +end + +function fun.partial(f, ...) + local n = select("#", ...) + if n == 1 then + local a = ... + return function(...) + return f(a, ...) + end + elseif n == 2 then + local a, b = ... + return function(...) + return f(a, b, ...) + end + else + local pargs = { n = n, ... } + return function(...) + local m = select("#", ...) + local fargs = { ... } + local args = {} + for i = 1, n do + args[i] = pargs[i] + end + for i = 1, m do + args[i+n] = fargs[i] + end + return f(unpack(args, 1, n+m)) + end + end +end + +function fun.memoize(fn) + local memory = setmetatable({}, { __mode = "k" }) + local errors = setmetatable({}, { __mode = "k" }) + local NIL = {} + return function(arg) + if memory[arg] then + if memory[arg] == NIL then + return nil, errors[arg] + end + return memory[arg] + end + local ret1, ret2 = fn(arg) + if ret1 ~= nil then + memory[arg] = ret1 + else + memory[arg] = NIL + errors[arg] = ret2 + end + return ret1, ret2 + end +end + +return fun diff --git a/src/luarocks/fun.lua b/src/luarocks/fun.lua index 80bf7c20..b315711d 100644 --- a/src/luarocks/fun.lua +++ b/src/luarocks/fun.lua @@ -1,8 +1,7 @@ +local _tl_compat; if (tonumber((_VERSION or ''):match('[%d.]*$')) or 0) < 5.3 then local p, m = pcall(require, 'compat53.module'); if p then _tl_compat = m end end; local ipairs = _tl_compat and _tl_compat.ipairs or ipairs; local math = _tl_compat and _tl_compat.math or math; local table = _tl_compat and _tl_compat.table or table; local _tl_table_unpack = unpack or table.unpack ---- A set of basic functional utilities local fun = {} -local unpack = table.unpack or unpack function fun.concat(xs, ys) local rs = {} @@ -13,6 +12,7 @@ function fun.concat(xs, ys) for i = 1, #ys do rs[i + n] = ys[i] end + return rs end @@ -38,7 +38,7 @@ function fun.filter(xs, f) for i = 1, #xs do local v = xs[i] if f(v) then - rs[#rs+1] = v + rs[#rs + 1] = v end end return rs @@ -46,12 +46,17 @@ end function fun.traverse(t, f) return fun.map(t, function(x) - return type(x) == "table" and fun.traverse(x, f) or f(x) + + if type(x) == "table" then + fun.traverse(x, f) + else + f(x) + end end) end function fun.reverse_in(t) - for i = 1, math.floor(#t/2) do + for i = 1, math.floor(#t / 2) do local m, n = i, #t - i + 1 local a, b = t[m], t[n] t[m] = b @@ -111,9 +116,9 @@ function fun.partial(f, ...) args[i] = pargs[i] end for i = 1, m do - args[i+n] = fargs[i] + args[i + n] = fargs[i] end - return f(unpack(args, 1, n+m)) + return f(_tl_table_unpack(args, 1, n + m)) end end end @@ -122,19 +127,19 @@ function fun.memoize(fn) local memory = setmetatable({}, { __mode = "k" }) local errors = setmetatable({}, { __mode = "k" }) local NIL = {} - return function(arg) - if memory[arg] then - if memory[arg] == NIL then - return nil, errors[arg] + return function(a) + if memory[a] then + if memory[a] == NIL then + return nil, errors[a] end - return memory[arg] + return memory[a] end - local ret1, ret2 = fn(arg) + local ret1, ret2 = fn(a) if ret1 ~= nil then - memory[arg] = ret1 + memory[a] = ret1 else - memory[arg] = NIL - errors[arg] = ret2 + memory[a] = NIL + errors[a] = ret2 end return ret1, ret2 end diff --git a/src/luarocks/fun.tl b/src/luarocks/fun.tl index b9960e3b..a63f0404 100644 --- a/src/luarocks/fun.tl +++ b/src/luarocks/fun.tl @@ -123,23 +123,23 @@ function fun.partial(f: (function(...: any): K), ...: any): (function(...: an end end -function fun.memoize(fn) - local memory = setmetatable({}, { __mode = "k" }) - local errors = setmetatable({}, { __mode = "k" }) - local NIL = {} - return function(arg) - if memory[arg] then - if memory[arg] == NIL then - return nil, errors[arg] +function fun.memoize(fn: (function(A):B, C)): (function(A):B, C) + local memory: {A: B} = setmetatable({}, { __mode = "k" }) + local errors: {A: C} = setmetatable({}, { __mode = "k" }) + local NIL: B = {} as B + return function(a: A): B, C + if memory[a] then + if memory[a] == NIL then + return nil, errors[a] end - return memory[arg] + return memory[a] end - local ret1, ret2 = fn(arg) + local ret1, ret2 = fn(a) if ret1 ~= nil then - memory[arg] = ret1 + memory[a] = ret1 else - memory[arg] = NIL - errors[arg] = ret2 + memory[a] = NIL + errors[a] = ret2 end return ret1, ret2 end diff --git a/src/luarocks/type_check.tl b/src/luarocks/type_check.tl index e546492c..09dfa96d 100644 --- a/src/luarocks/type_check.tl +++ b/src/luarocks/type_check.tl @@ -43,7 +43,7 @@ do -- and the value is a schema specification. Schema versions are considered -- incremental: version "2.0" only needs to specify what's new/changed from -- version "1.0". - function type_check.declare_schemas(inputs: {string: any}): {any: any} {any} --? + function type_check.declare_schemas(inputs: {string: any}): {any: any}, {any} --? local schemas = {} local parent_version -- cgit v1.2.3-55-g6feb