-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathREADME
129 lines (90 loc) · 2.53 KB
/
README
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
52
53
54
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
lua-enumerable is a quick and dirty port of the ruby Enumerable library plus some extras.
It's a native lua library. Installation is as simple as dropping lua-enumerable.lua in
your project and requiring it:
require("lua-enumerable")
Synopsis:
local test = {1,2,3}
table.each(test, function (x) print(x) end)
-- 1
-- 2
-- 3
local holes = {1,2,7}
table.each(holes, function(x) print(x) end)
-- 1 2 7
table.every(holes, function(x) print(x) end)
-- 1 2 7
holes["1"] = "one"
holes["2"] = "two"
holes["7"] = "seven"
table.every(holes, function(x) print(x) end)
-- 1 2 7 one seven two
--BUT!
table.each(holes, function(x) print(x) end)
--1 2 7
table.collect(test, function (x) return x + 1 end)
-- {2, 3, 4}
table.select(test, function (x) return x < 3 end)
-- {1, 2}
table.reject(test, function (x) return x < 3 end)
-- {3}
table.without(test, 3)
-- {1, 2}
table.partition(test, function (x) return x < 3 end)
-- {1, 2}, {3}
table.includes(test, 3)
-- true
table.detect(test, function (x) return x == 3 end)
-- 3
table.detect(test, function (x) return x == 4 end)
-- nil
table.merge({a=1, b=2}, {c=3})
-- {a=1, b=2, c=3}
-- Note: modifies the first table
table.times(4, function (x) print(x) end)
-- 1
-- 2
-- 3
-- 4
table.keys({a=1,b=2,c=3})
-- {"a", "c", "b"}
Provides some array manipulation functions:
table.pop(test)
-- 3
-- Note: modifies the input array {1,2}
table.shift(test)
-- 1
-- Note: modifies the input array {2,3}
table.unshift(test, 4)
-- {4, 1, 2, 3}
-- Note: modifies the input array
table.push(test, 4)
-- {1, 2, 3, 4}
-- Note: modifies the input array
table.reverse({1,2,3})
-- {3,2,1}
Random elements:
table.random(test)
-- {3}
table.shuffle(test)
-- {3,2,1}
-- Note: modifies the input table
Boolean tests for sexy if statements:
table.empty({})
-- true
table.empty(nil)
-- true
table.empty({1,2,3})
-- false
table.present({})
-- false
table.present(nil)
-- false
table.present({1,2,3})
-- true
Misc:
table.dup({1,2,3})
-- {1,2,3}
If you've got any useful functions or suggestions, send it to me via a pull request,
email ([email protected]) or paste into an issue here in github. (The transmission
method doesn't really matter. What matters is that you're being awesome.)
Thanks for checking out my humble little library.