about summary refs log tree commit diff stats
path: root/util.lua
blob: 98536a167baee2ec200ec79a065c7bea687fe042 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
--- lam.util

local util = {}

function util.table (x)
	if type(x) == "table" then
		return x
	else
		return { x }
	end
end

function util.pop (tbl)
	return table.remove(tbl, 1)
end

function util.car (tbl)
	return tbl[1]
end

function util.cdr (tbl)
	local t = {}
	for i = 2, #tbl do t[i-1] = tbl[i] end
	return t
end

function util.reduce (tbl, seed, fn)
	if #tbl == 0 then return seed end
	return util.reduce(tbl, fn(seed, util.pop(tbl)), fn)
end

function util.map (fn, tbl)
	local out = {}
	for k, v in pairs(tbl) do
		out[k] = fn(v)
	end
	return out
end

---
return util