55
56
57
58
59
60
61
62
63
|
return ks
end
fn.pick = function(lst)
local keys = fn.keys(lst)
return keys[math.random(#keys)]
end
return fn
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
|
return ks
end
fn.pick = function(lst)
local keys = fn.keys(lst)
return keys[math.random(#keys)]
end
fn.unpack = function(tbl,i)
if i and #tbl == i then return tbl[i] end
i = i or 1
return tbl[i], fn.unpack(tbl, i+1)
end
fn.each = function(tbl,fn)
local r = {}
for k,v in pairs(tbl) do
r[#r+1] = fn(v,k)
end
return r
end
fn.each_o = function(tbl,fn)
local keys = fn.keys(tbl)
table.sort(keys)
return fn.each(keys, function(k,i)
return fn(tbl[k],k,i)
end)
end
fn.iter = function(tbl,fn)
for i=1,#tbl do
fn(tbl[i], i)
end
end
fn.map = function(tbl,fn)
local new = {}
for k,v in pairs(tbl) do
local nv, nk = fn(v, k)
new[nk or k] = nv
end
return new
end
fn.fold = function(tbl,fn,acc)
if #tbl == 0 then
fn.each_o(tbl, function(v)
acc = fn(acc, v, k)
end)
else
for i=0,#tbl do
acc = fn(acc,tbl[i],i)
end
end
return acc
end
return fn
|