-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpointfree_array.lua
More file actions
222 lines (183 loc) · 4.32 KB
/
pointfree_array.lua
File metadata and controls
222 lines (183 loc) · 4.32 KB
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
local pointfree_array = {}
pointfree_array._VERSION = 0.1
--[=====[
A collection of functional style (haskell in particular) high order functions operate on table array
--]=====]
function pointfree_array.map(arr, f)
local newarr = pointfree_array.Pointfree({})
for i, v in ipairs(arr) do
table.insert(newarr, f(v))
end
return newarr
end
function pointfree_array.apply(arr, f)
for i, v in ipairs(arr) do
f(v)
end
end
function pointfree_array.flatten(arr)
local newarr = pointfree_array.Pointfree({})
for _, v in ipairs(arr) do
for _, inner_v in ipairs(v) do
table.insert(newarr, inner_v)
end
end
return newarr
end
function pointfree_array.filter(arr, f)
local newarr = pointfree_array.Pointfree({})
for _, v in ipairs(arr) do
if p(v) then
table.insert(newarr, v)
end
end
return newarr
end
function pointfree_array.shuffle(arr)
local size = #arr
for i = size, 1, -1 do
local rand = math.random(size)
arr[i], arr[rand] = arr[rand], arr[i]
end
return pointfree_array.Pointfree(arr)
end
function pointfree_array.tail(arr)
local newarr = pointfree_array.Pointfree({})
for k, v in ipairs(arr) do
if k ~= 1 then
table.insert(newarr, v)
end
end
return newarr
end
function pointfree_array.init(arr)
local newarr = pointfree_array.Pointfree({})
local arr_len = #arr
for k, v in ipairs(arr) do
if k ~= arr_len then
table.insert(newarr, v)
end
end
return newarr
end
function pointfree_array.take(arr, num)
local count = 0
local newarr = pointfree_array.Pointfree({})
for _, v in ipairs(arr) do
table.insert(newarr, v)
count = count + 1
if (count == num) then
break
end
end
return newarr
end
function pointfree_array.takeWhile(arr, p)
-- Takes longest prefix of elements of 'list' that satisfy 'predicate'.
local newarr = pointfree_array.Pointfree({})
for _, v in ipairs(arr) do
if p(v) then
table.insert(newarr, v)
else
return newarr
end
end
return newarr
end
function pointfree_array.drop(arr, num)
local count = 0
local newarr = pointfree_array.Pointfree({})
for _, v in ipairs(arr) do
count = count + 1
if (count > num) then
table.insert(newarr, v)
end
end
return newarr
end
function pointfree_array.dropWhile(arr, p)
local newarr = pointfree_array.Pointfree({})
local b_finish = false
for _, v in ipairs(arr) do
if not b_finish then
if not p(v) then
b_finish = true
end
end
if b_finish then
table.insert(newarr, v)
end
end
return newarr
end
-- for array of bool values
local function _and(arr)
for _, v in ipairs(arr) do
if not v then
return false
end
end
return true
end
local function _or(arr)
for _, v in ipairs(arr) do
if v then
return true
end
end
return false
end
function pointfree_array.reverse(arr)
local newarr = pointfree_array.Pointfree({})
local length = table.maxn(arr)
for i = length, 1, -1 do
table.insert(newarr, arr[i])
end
return newarr
end
-- hereafter, not return pointfree style array
function pointfree_array.elem(arr, val)
for _, value in ipairs(arr) do
if value == val then
return true
end
end
return false
end
function pointfree_array.any(arr, p)
return _or (pointfree_array.map(arr, p))
end
function pointfree_array.all(arr, p)
return _and(pointfree_array.map(arr, p))
end
function pointfree_array.un_pointfree(arr)
setmetatable(arr, nil)
return arr
end
local Pointfree = {
-- return functional table
map = pointfree_array.map,
filter = pointfree_array.filter,
flatten = pointfree_array.flatten,
shuffle = pointfree_array.shuffle,
tail = pointfree_array.tail,
init = pointfree_array.init,
take = pointfree_array.take,
drop = pointfree_array.drop,
takeWhile = pointfree_array.takeWhile,
dropWhile = pointfree_array.dropWhile,
reverse = pointfree_array.reverse,
-- return other types or non-pointfree style of array.
-- if use pointfree style composition, those should be at the last
elem = pointfree_array.elem,
any = pointfree_array.any,
all = pointfree_array.all,
apply = pointfree_array.apply,
un_pointfree = pointfree_array.un_pointfree
}
function pointfree_array.Pointfree(arr)
setmetatable(arr, {__index = Pointfree})
return arr
end
----
return pointfree_array