From db62bad5a02e3dd8b44b81aae0384e632672eee1 Mon Sep 17 00:00:00 2001 From: V1K1NGbg Date: Thu, 22 Aug 2024 17:49:06 -0300 Subject: Teal: convert luarocks.fun --- src/luarocks/fun.tl | 91 +++++++++++++++++++++++++---------------------------- 1 file changed, 43 insertions(+), 48 deletions(-) (limited to 'src') diff --git a/src/luarocks/fun.tl b/src/luarocks/fun.tl index 80bf7c20..5b413187 100644 --- a/src/luarocks/fun.tl +++ b/src/luarocks/fun.tl @@ -1,10 +1,9 @@ --- A set of basic functional utilities -local fun = {} - -local unpack = table.unpack or unpack +local record fun +end -function fun.concat(xs, ys) +function fun.concat(xs: {K}, ys: {K}): {K} local rs = {} local n = #xs for i = 1, n do @@ -13,10 +12,11 @@ function fun.concat(xs, ys) for i = 1, #ys do rs[i + n] = ys[i] end + return rs end -function fun.contains(xs, v) +function fun.contains(xs: {K}, v: K): boolean for _, x in ipairs(xs) do if v == x then return true @@ -25,7 +25,7 @@ function fun.contains(xs, v) return false end -function fun.map(xs, f) +function fun.map(xs: {K}, f: function(K): V): {V} local rs = {} for i = 1, #xs do rs[i] = f(xs[i]) @@ -33,7 +33,7 @@ function fun.map(xs, f) return rs end -function fun.filter(xs, f) +function fun.filter(xs: {K}, f: function): {K} local rs = {} for i = 1, #xs do local v = xs[i] @@ -44,13 +44,7 @@ function fun.filter(xs, f) 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) +function fun.reverse_in(t: {K}): {K} for i = 1, math.floor(#t/2) do local m, n = i, #t - i + 1 local a, b = t[m], t[n] @@ -60,50 +54,51 @@ function fun.reverse_in(t) return t end -function fun.sort_in(t, f) +function fun.sort_in(t: {K}, f: table.SortFunction): {K} table.sort(t, f) return t end -function fun.flip(f) - return function(a, b) +function fun.flip(f: function(K, V): S): function(V, K): S + return function(a: V, b: K): S 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 +function fun.find(xs: function():(X) | {X} , f: function(X):(R) ): R + if xs is {X} then + for _, x in ipairs(xs) do + local r = f(x) + if r then + return r end end - elseif type(xs) == "table" then - for _, v in ipairs(xs) do - local x = f(v) - if x then - return x + else + for x in xs do + local r = f(x) + if r then + return r end end end end -function fun.partial(f, ...) + +function fun.partial(f: (function(...: any): K), ...: any): (function(...: any): K) local n = select("#", ...) if n == 1 then local a = ... - return function(...) + return function(...): K return f(a, ...) end elseif n == 2 then local a, b = ... - return function(...) + return function(...): K return f(a, b, ...) end else local pargs = { n = n, ... } - return function(...) + return function(...): K local m = select("#", ...) local fargs = { ... } local args = {} @@ -113,31 +108,31 @@ function fun.partial(f, ...) for i = 1, m do args[i+n] = fargs[i] end - return f(unpack(args, 1, n+m)) + return f(table.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] +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) - if ret1 ~= nil then - memory[arg] = ret1 + local ret1, ret2 = fn(a) + if ret1 then + memory[a] = ret1 else - memory[arg] = NIL - errors[arg] = ret2 + memory[a] = NIL + errors[a] = ret2 end return ret1, ret2 end end -return fun +return fun \ No newline at end of file -- cgit v1.2.3-55-g6feb