blob: 276e12d12dadcc6986e00a25b74b4d92baff0092 (
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
42
43
44
45
46
47
48
49
50
51
|
;; table funs
(fn contains [t x]
"does table t contain element x?"
(accumulate [found false
_ v (ipairs t)
&until found] ; escape early
(or found (= x v))))
(fn head [t]
"return the first item in a table"
(if (> (length t) 0)
(?. t 1)
[]))
(fn tail [t]
"return the table minus the head"
(icollect [i v (ipairs t)]
(if (> i 1)
v)))
(fn keys [t]
"takes a table returns a sequential list of its keys"
(local out [])
(each [k v (pairs t)] (table.insert out k))
out)
(fn flip [t]
"takes a table of {key value} and returns a table of {value key}"
(collect [k v (pairs t)] (values v k)))
(fn print [tbl]
"print a table"
(each [k v (pairs tbl)]
(let [table? (= (type v) :table)]
(print k v))))
(fn slice [t start stop]
"return a slice of a table"
(fcollect [i start (or stop (length t))]
(. t i)))
{
: contains
: flip
: head
: keys
: print
: slice
: tail
}
|