Differences From
Artifact [5997708826]:
- File
lib/tbl.lua
— part of check-in
[72eebac4bc]
at
2020-09-26 18:49:51
on branch trunk
— add writing stand for editing codexes; add scissors, ink, erasure fluid, pens; touch up codex UI; add many recipe notes; add craft divination type for crafttools; defuckulate fucktarded crafttool impl; enhance table library with missing features like lua's table.unpack; many bug fixes and enhancements; blood for the blood god
(user:
lexi,
size: 1805)
[annotate]
[blame]
[check-ins using]
55 55 return ks
56 56 end
57 57
58 58 fn.pick = function(lst)
59 59 local keys = fn.keys(lst)
60 60 return keys[math.random(#keys)]
61 61 end
62 +
63 +fn.unpack = function(tbl,i)
64 + if i and #tbl == i then return tbl[i] end
65 + i = i or 1
66 + return tbl[i], fn.unpack(tbl, i+1)
67 +end
68 +
69 +fn.each = function(tbl,fn)
70 + local r = {}
71 + for k,v in pairs(tbl) do
72 + r[#r+1] = fn(v,k)
73 + end
74 + return r
75 +end
76 +
77 +fn.each_o = function(tbl,fn)
78 + local keys = fn.keys(tbl)
79 + table.sort(keys)
80 + return fn.each(keys, function(k,i)
81 + return fn(tbl[k],k,i)
82 + end)
83 +end
84 +
85 +fn.iter = function(tbl,fn)
86 + for i=1,#tbl do
87 + fn(tbl[i], i)
88 + end
89 +end
90 +
91 +fn.map = function(tbl,fn)
92 + local new = {}
93 + for k,v in pairs(tbl) do
94 + local nv, nk = fn(v, k)
95 + new[nk or k] = nv
96 + end
97 + return new
98 +end
99 +
100 +fn.fold = function(tbl,fn,acc)
101 + if #tbl == 0 then
102 + fn.each_o(tbl, function(v)
103 + acc = fn(acc, v, k)
104 + end)
105 + else
106 + for i=0,#tbl do
107 + acc = fn(acc,tbl[i],i)
108 + end
109 + end
110 + return acc
111 +end
62 112
63 113 return fn