forked from purescript/purescript-arrays
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArray.lua
More file actions
214 lines (206 loc) · 5.3 KB
/
Copy pathArray.lua
File metadata and controls
214 lines (206 loc) · 5.3 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
return {
rangeImpl = (function(start, end_)
local step = start > end_ and -1 or 1
local result = {}
local i, n = start, 1
while i ~= end_ do
result[n] = i
n = n + 1
i = i + step
end
result[n] = i
return result
end),
replicateImpl = (function(count, value)
if count < 1 then return {} end
local result = {}
for i = 1, count do result[i] = value end
return result
end),
fromFoldableImpl = ((function()
local function Cons(head, tail) return {head = head, tail = tail} end
local emptyList = {}
local function curryCons(head) return function(tail) return Cons(head, tail) end end
local function listToArray(list)
local result = {}
local count = 1
local xs = list
while xs ~= emptyList do
result[count] = xs.head
count = count + 1
xs = xs.tail
end
return result
end
return function(foldr, xs) return listToArray(foldr(curryCons)(emptyList)(xs)) end
end)()),
length = (function(xs) return #xs end),
unconsImpl = (function(empty, next, xs)
if #xs == 0 then return empty({}) end
return next(xs[1])({unpack(xs, 2)})
end),
indexImpl = (function(just, nothing, xs, i)
if i < 0 or i >= #xs then
return nothing
else
return just(xs[i + 1])
end
end),
findMapImpl = (function(nothing, isJust, f, xs)
for i = 1, #xs do
local result = f(xs[i])
if isJust(result) then return result end
end
return nothing
end),
findIndexImpl = (function(just, nothing, f, xs)
for i = 1, #xs do if f(xs[i]) then return just(i - 1) end end
return nothing
end),
findLastIndexImpl = (function(just, nothing, f, xs)
for i = #xs, 1, -1 do if f(xs[i]) then return just(i - 1) end end
return nothing
end),
_insertAt = (function(just, nothing, i, a, l)
if i < 0 or i > #l then return nothing end
local l1 = {unpack(l)}
table.insert(l1, i + 1, a)
return just(l1)
end),
_deleteAt = (function(just, nothing, i, l)
if i < 0 or i >= #l then return nothing end
local l1 = {unpack(l)}
table.remove(l1, i + 1)
return just(l1)
end),
_updateAt = (function(just, nothing, i, f, l)
if i < 0 or i >= #l then return nothing end
local l1 = {unpack(l)}
l1[i + 1] = f(l1[i + 1])
return just(l1)
end),
reverse = (function(xs)
local result, l = {}, #xs
for i = l, 1, -1 do result[l - i + 1] = xs[i] end
return result
end),
concat = (function(xss)
local result = {}
local l = 1
for i = 1, #xss do
local xs = xss[i]
for j = 1, #xs do
result[l] = xs[j]
l = l + 1
end
end
return result
end),
filterImpl = (function(f, xs)
local result = {}
local l = 1
for i = 1, #xs do
local x = xs[i]
if f(x) then
result[l] = x
l = l + 1
end
end
return result
end),
partitionImpl = (function(f, xs)
local yes, no = {}, {}
local l1, l2 = 1, 1
for i = 1, #xs do
local x = xs[i]
if f(x) then
yes[l1] = x
l1 = l1 + 1
else
no[l2] = x
l2 = l2 + 1
end
end
return {yes = yes, no = no}
end),
scanlImpl = (function(f, b, xs)
local result = {}
local acc = b
for i = 1, #xs do
acc = f(acc)(xs[i])
result[i] = acc
end
return result
end),
scanrImpl = (function(f, b, xs)
local result = {}
local acc = b
for i = #xs, 1, -1 do
acc = f(xs[i])(acc)
result[i] = acc
end
return result
end),
sortByImpl = ((function()
local function rshift(x, by) return math.floor(x / 2 ^ by) end
local function mergeFromTo(compare, fromOrdering, xs1, xs2, from, to)
local mid, i, j, k, x, y, c
mid = from + rshift(to - from, 1)
if mid - from > 1 then mergeFromTo(compare, fromOrdering, xs2, xs1, from, mid) end
if to - mid > 1 then mergeFromTo(compare, fromOrdering, xs2, xs1, mid, to) end
i = from
j = mid
k = from
while i < mid and j < to do
x = xs2[i + 1]
y = xs2[j + 1]
c = fromOrdering(compare(x)(y))
if c > 0 then
xs1[k + 1] = y
j = j + 1
else
xs1[k + 1] = x
i = i + 1
end
k = k + 1
end
while i < mid do
xs1[k + 1] = xs2[i + 1]
i = i + 1
k = k + 1
end
while j < to do
xs1[k + 1] = xs2[j + 1]
j = j + 1
k = k + 1
end
end
return function(compare, fromOrdering, xs)
if #xs < 2 then return xs end
local out = {unpack(xs)}
local slice = {unpack(xs)}
mergeFromTo(compare, fromOrdering, out, slice, 0, #xs)
return out
end
end)()),
sliceImpl = (function(s, e, t)
local spliced = {}
for i, el in ipairs(t) do if i > s and i <= e then table.insert(spliced, el) end end
return spliced
end),
zipWithImpl = (function(f, xs, ys)
local l = #xs < #ys and #xs or #ys
local result = {}
for i = 1, l do result[i] = f(xs[i])(ys[i]) end
return result
end),
anyImpl = (function(p, xs)
for i = 1, #xs do if p(xs[i]) then return true end end
return false
end),
allImpl = (function(p, xs)
for i = 1, #xs do if not p(xs[i]) then return false end end
return true
end),
unsafeIndexImpl = (function(xs, n) return xs[n + 1] end)
}