diff options
author | V1K1NGbg <victor@ilchev.com> | 2024-08-22 17:49:06 -0300 |
---|---|---|
committer | Hisham Muhammad <hisham@gobolinux.org> | 2024-10-21 13:30:51 -0300 |
commit | db62bad5a02e3dd8b44b81aae0384e632672eee1 (patch) | |
tree | 822fb722f6a51278ea64e5c5ba7c59817c893591 | |
parent | e6622d574c3e7ec86bc066c9f072cc92e4904f7d (diff) | |
download | luarocks-db62bad5a02e3dd8b44b81aae0384e632672eee1.tar.gz luarocks-db62bad5a02e3dd8b44b81aae0384e632672eee1.tar.bz2 luarocks-db62bad5a02e3dd8b44b81aae0384e632672eee1.zip |
Teal: convert luarocks.fun
-rw-r--r-- | src/luarocks/fun.tl | 91 |
1 files changed, 43 insertions, 48 deletions
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 @@ | |||
1 | 1 | ||
2 | --- A set of basic functional utilities | 2 | --- A set of basic functional utilities |
3 | local fun = {} | 3 | local record fun |
4 | 4 | end | |
5 | local unpack = table.unpack or unpack | ||
6 | 5 | ||
7 | function fun.concat(xs, ys) | 6 | function fun.concat<K>(xs: {K}, ys: {K}): {K} |
8 | local rs = {} | 7 | local rs = {} |
9 | local n = #xs | 8 | local n = #xs |
10 | for i = 1, n do | 9 | for i = 1, n do |
@@ -13,10 +12,11 @@ function fun.concat(xs, ys) | |||
13 | for i = 1, #ys do | 12 | for i = 1, #ys do |
14 | rs[i + n] = ys[i] | 13 | rs[i + n] = ys[i] |
15 | end | 14 | end |
15 | |||
16 | return rs | 16 | return rs |
17 | end | 17 | end |
18 | 18 | ||
19 | function fun.contains(xs, v) | 19 | function fun.contains<K>(xs: {K}, v: K): boolean |
20 | for _, x in ipairs(xs) do | 20 | for _, x in ipairs(xs) do |
21 | if v == x then | 21 | if v == x then |
22 | return true | 22 | return true |
@@ -25,7 +25,7 @@ function fun.contains(xs, v) | |||
25 | return false | 25 | return false |
26 | end | 26 | end |
27 | 27 | ||
28 | function fun.map(xs, f) | 28 | function fun.map<K, V>(xs: {K}, f: function(K): V): {V} |
29 | local rs = {} | 29 | local rs = {} |
30 | for i = 1, #xs do | 30 | for i = 1, #xs do |
31 | rs[i] = f(xs[i]) | 31 | rs[i] = f(xs[i]) |
@@ -33,7 +33,7 @@ function fun.map(xs, f) | |||
33 | return rs | 33 | return rs |
34 | end | 34 | end |
35 | 35 | ||
36 | function fun.filter(xs, f) | 36 | function fun.filter<K>(xs: {K}, f: function): {K} |
37 | local rs = {} | 37 | local rs = {} |
38 | for i = 1, #xs do | 38 | for i = 1, #xs do |
39 | local v = xs[i] | 39 | local v = xs[i] |
@@ -44,13 +44,7 @@ function fun.filter(xs, f) | |||
44 | return rs | 44 | return rs |
45 | end | 45 | end |
46 | 46 | ||
47 | function fun.traverse(t, f) | 47 | function fun.reverse_in<K>(t: {K}): {K} |
48 | return fun.map(t, function(x) | ||
49 | return type(x) == "table" and fun.traverse(x, f) or f(x) | ||
50 | end) | ||
51 | end | ||
52 | |||
53 | function fun.reverse_in(t) | ||
54 | for i = 1, math.floor(#t/2) do | 48 | for i = 1, math.floor(#t/2) do |
55 | local m, n = i, #t - i + 1 | 49 | local m, n = i, #t - i + 1 |
56 | local a, b = t[m], t[n] | 50 | local a, b = t[m], t[n] |
@@ -60,50 +54,51 @@ function fun.reverse_in(t) | |||
60 | return t | 54 | return t |
61 | end | 55 | end |
62 | 56 | ||
63 | function fun.sort_in(t, f) | 57 | function fun.sort_in<K>(t: {K}, f: table.SortFunction<K>): {K} |
64 | table.sort(t, f) | 58 | table.sort(t, f) |
65 | return t | 59 | return t |
66 | end | 60 | end |
67 | 61 | ||
68 | function fun.flip(f) | 62 | function fun.flip<K, V, S>(f: function(K, V): S): function(V, K): S |
69 | return function(a, b) | 63 | return function(a: V, b: K): S |
70 | return f(b, a) | 64 | return f(b, a) |
71 | end | 65 | end |
72 | end | 66 | end |
73 | 67 | ||
74 | function fun.find(xs, f) | 68 | function fun.find<X, R>(xs: function():(X) | {X} , f: function(X):(R) ): R |
75 | if type(xs) == "function" then | 69 | if xs is {X} then |
76 | for v in xs do | 70 | for _, x in ipairs(xs) do |
77 | local x = f(v) | 71 | local r = f(x) |
78 | if x then | 72 | if r then |
79 | return x | 73 | return r |
80 | end | 74 | end |
81 | end | 75 | end |
82 | elseif type(xs) == "table" then | 76 | else |
83 | for _, v in ipairs(xs) do | 77 | for x in xs do |
84 | local x = f(v) | 78 | local r = f(x) |
85 | if x then | 79 | if r then |
86 | return x | 80 | return r |
87 | end | 81 | end |
88 | end | 82 | end |
89 | end | 83 | end |
90 | end | 84 | end |
91 | 85 | ||
92 | function fun.partial(f, ...) | 86 | |
87 | function fun.partial<K>(f: (function(...: any): K), ...: any): (function(...: any): K) | ||
93 | local n = select("#", ...) | 88 | local n = select("#", ...) |
94 | if n == 1 then | 89 | if n == 1 then |
95 | local a = ... | 90 | local a = ... |
96 | return function(...) | 91 | return function(...): K |
97 | return f(a, ...) | 92 | return f(a, ...) |
98 | end | 93 | end |
99 | elseif n == 2 then | 94 | elseif n == 2 then |
100 | local a, b = ... | 95 | local a, b = ... |
101 | return function(...) | 96 | return function(...): K |
102 | return f(a, b, ...) | 97 | return f(a, b, ...) |
103 | end | 98 | end |
104 | else | 99 | else |
105 | local pargs = { n = n, ... } | 100 | local pargs = { n = n, ... } |
106 | return function(...) | 101 | return function(...): K |
107 | local m = select("#", ...) | 102 | local m = select("#", ...) |
108 | local fargs = { ... } | 103 | local fargs = { ... } |
109 | local args = {} | 104 | local args = {} |
@@ -113,31 +108,31 @@ function fun.partial(f, ...) | |||
113 | for i = 1, m do | 108 | for i = 1, m do |
114 | args[i+n] = fargs[i] | 109 | args[i+n] = fargs[i] |
115 | end | 110 | end |
116 | return f(unpack(args, 1, n+m)) | 111 | return f(table.unpack(args, 1, n+m)) |
117 | end | 112 | end |
118 | end | 113 | end |
119 | end | 114 | end |
120 | 115 | ||
121 | function fun.memoize(fn) | 116 | function fun.memoize<A, B, C>(fn: (function(A):B, C)): (function(A):B, C) |
122 | local memory = setmetatable({}, { __mode = "k" }) | 117 | local memory: {A: B} = setmetatable({}, { __mode = "k" }) |
123 | local errors = setmetatable({}, { __mode = "k" }) | 118 | local errors: {A: C} = setmetatable({}, { __mode = "k" }) |
124 | local NIL = {} | 119 | local NIL: B = {} as B |
125 | return function(arg) | 120 | return function(a: A): B, C |
126 | if memory[arg] then | 121 | if memory[a] then |
127 | if memory[arg] == NIL then | 122 | if memory[a] == NIL then |
128 | return nil, errors[arg] | 123 | return nil, errors[a] |
129 | end | 124 | end |
130 | return memory[arg] | 125 | return memory[a] |
131 | end | 126 | end |
132 | local ret1, ret2 = fn(arg) | 127 | local ret1, ret2 = fn(a) |
133 | if ret1 ~= nil then | 128 | if ret1 then |
134 | memory[arg] = ret1 | 129 | memory[a] = ret1 |
135 | else | 130 | else |
136 | memory[arg] = NIL | 131 | memory[a] = NIL |
137 | errors[arg] = ret2 | 132 | errors[a] = ret2 |
138 | end | 133 | end |
139 | return ret1, ret2 | 134 | return ret1, ret2 |
140 | end | 135 | end |
141 | end | 136 | end |
142 | 137 | ||
143 | return fun | 138 | return fun \ No newline at end of file |