-
Notifications
You must be signed in to change notification settings - Fork 4
/
list.sh
342 lines (273 loc) · 10.1 KB
/
list.sh
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
#!/bin/bash
#
# https://github.com/EngineeringSoftware/gobash/blob/main/LICENSE
#
# List collection.
if [ -n "${LIST_MOD:-}" ]; then return 0; fi
readonly LIST_MOD=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
. ${LIST_MOD}/../lang/p.sh
. ${LIST_MOD}/math.sh
# ----------
# Functions.
function List() {
# List collection.
local ctx; is_ctx "${1}" && ctx="${1}" && shift
[ $# -lt 0 ] && { ctx_wn $ctx; return $EC; }
shift 0 || { ctx_wn $ctx; return $EC; }
local lst=$(unsafe_list_make $ctx)
local el
for el in "$@"; do
List_add $ctx "${lst}" "${el}" || \
{ ctx_w $ctx "cannot add an el"; return $EC; }
done
echo "${lst}"
}
function List_len() {
# Return (length of the list, 0). If there is an error, return
# (_, $EC).
local ctx; is_ctx "${1}" && ctx="${1}" && shift
[ $# -ne 1 ] && { ctx_wn $ctx; return $EC; }
local -r lst="${1}"
shift 1 || { ctx_wn $ctx; return $EC; }
unsafe_len $ctx "${lst}"
}
function List_add() {
# Add an element to the list.
local ctx; is_ctx "${1}" && ctx="${1}" && shift
[ $# -ne 2 ] && { ctx_wn $ctx; return $EC; }
local -r lst="${1}"
local val="${2}"
shift 2 || { ctx_wn $ctx; return $EC; }
unsafe_list_add $ctx "${lst}" "${val}"
}
function List_get() {
# Get an element at the index.
local ctx; is_ctx "${1}" && ctx="${1}" && shift
[ $# -ne 2 ] && { ctx_wn $ctx; return $EC; }
local -r lst="${1}"
local -r ix="${2}"
shift 2 || { ctx_wn $ctx; return $EC; }
unsafe_list_get $ctx "${lst}" "${ix}"
}
function List_delete() {
# Delete an element at the index.
local ctx; is_ctx "${1}" && ctx="${1}" && shift
[ $# -ne 2 ] && { ctx_wn $ctx; return $EC; }
local -r lst="${1}"
local -r -i ix="${2}"
shift 2 || { ctx_wn $ctx; return $EC; }
unsafe_list_delete "${lst}" "${ix}"
}
function List_clear() {
# Empty the list.
local ctx; is_ctx "${1}" && ctx="${1}" && shift
[ $# -ne 1 ] && { ctx_wn $ctx; return $EC; }
local -r lst="${1}"
shift 1 || { ctx_wn $ctx; return $EC; }
[ -z "${lst}" ] && { ctx_w $ctx "no lst"; return $EC; }
unsafe_list_clear $ctx "${lst}"
}
function List_sum() {
# Sum all the elements (ints only).
local ctx; is_ctx "${1}" && ctx="${1}" && shift
[ $# -ne 1 ] && { ctx_wn $ctx; return $EC; }
local -r lst="${1}"
shift 1 || { ctx_wn $ctx; return $EC; }
local len
len=$(List_len $ctx "${lst}") || return $EC
local sum=0
local -i i
for (( i=0; i<${len}; i++ )); do
sum=$(( ${sum} + $(List_get $ctx "${lst}" "${i}") ))
done
echo "${sum}"
}
function List_min() {
# Find min value (ints only).
local ctx; is_ctx "${1}" && ctx="${1}" && shift
[ $# -ne 1 ] && { ctx_wn $ctx; return $EC; }
local -r lst="${1}"
shift 1 || { ctx_wn $ctx; return $EC; }
local len
len=$(List_len $ctx "${lst}") || \
{ ctx_w $ctx "cannot get len"; return $EC; }
[ ${len} -eq 0 ] && { ctx_w $ctx "incorrect len"; return $EC; }
local min=$(List_get $ctx "${lst}" 0)
local -i i
for (( i=1; i<${len}; i++ )); do
local el
el=$(List_get $ctx "${lst}" "${i}") || \
{ ctx_w $ctx "cannot get an el"; return $EC; }
if [ ${min} -gt ${el} ]; then
min=${el}
fi
done
echo "${min}"
}
function List_is_empty() {
# Return true if the list is empty.
local ctx; is_ctx "${1}" && ctx="${1}" && shift
[ $# -ne 1 ] && { ctx_wn $ctx; return $EC; }
local -r lst="${1}"
shift 1 || { ctx_wn $ctx; return $EC; }
local -i len
len=$(List_len $ctx "${lst}") || \
{ ctx_w $ctx "cannog get len"; return $EC; }
[ ${len} -eq 0 ]
}
function List_contains() {
# Return true if the list contains the given value.
local ctx; is_ctx "${1}" && ctx="${1}" && shift
[ $# -ne 2 ] && { ctx_wn $ctx; return $EC; }
local -r lst="${1}"
local -r val="${2}"
shift 2 || { ctx_wn $ctx; return $EC; }
[ -z "${lst}" ] && { ctx_w $ctx "no lst"; return $EC; }
local -i len
len=$(List_len $ctx "${lst}") || \
{ ctx_w $ctx "cannot get len"; return $EC; }
local -i i
for (( i=0; i<${len}; i++ )); do
local el
el=$(List_get $ctx "${lst}" ${i}) || \
{ ctx_w $ctx "cannot get an el"; return $EC; }
if [ "${el}" = "${val}" ]; then
return $TRUE
fi
done
return $FALSE
}
function List_eq() {
# Return true if two lists are the same (using references).
local ctx; is_ctx "${1}" && ctx="${1}" && shift
[ $# -ne 2 ] && { ctx_wn $ctx; return $EC; }
local -r lst="${1}"
local -r other="${2}"
shift 2 || { ctx_wn $ctx; return $EC; }
unsafe_list_eq $ctx "${lst}" "${other}"
}
function List_filter() {
# Create a list by filtering this list using the predicate.
local ctx; is_ctx "${1}" && ctx="${1}" && shift
[ $# -ne 2 ] && { ctx_wn $ctx; return $EC; }
local -r lst="${1}"
local -r predicate="${2}"
shift 2 || { ctx_wn $ctx; return $EC; }
local -i len
len=$(List_len $ctx "${lst}") || return $EC
local nlst=$(List $ctx)
local -i i
for (( i=0; i<${len}; i++ )); do
local el
el=$(List_get $ctx "${lst}" "${i}") || return $EC
local ec=0
${predicate} $ctx "${el}" || \
{ ec=$?; [ ${ec} -gt 1 ] && { ctx_w $ctx "predicate error"; return $EC; }; }
if [ ${ec} -eq 0 ]; then
$nlst $ctx add "${el}"
fi
done
echo "${nlst}"
}
function List_map() {
# Apply mapper to each element in the list.
local ctx; is_ctx "${1}" && ctx="${1}" && shift
[ $# -ne 2 ] && { ctx_wn $ctx; return $EC; }
local -r lst="${1}"
local -r mapper="${2}"
shift 2 || { ctx_wn $ctx; return $EC; }
local -i len
len=$(List_len $ctx "${lst}") || \
{ ctx_w $ctx "cannot get len"; return $EC; }
local nlst=$(List $ctx)
local -i i
for (( i=0; i<${len}; i++ )); do
local el
el=$(List_get $ctx "${lst}" "${i}") || \
{ ctx_w $ctx "cannot get an el"; return $EC; }
local nel=$(${mapper} $ctx "${el}")
$nlst $ctx add "${nel}"
done
echo "${nlst}"
}
function List_reduce() {
# Reduce elements in the list with the given binary operator.
local ctx; is_ctx "${1}" && ctx="${1}" && shift
[ $# -ne 3 ] && { ctx_wn $ctx; return $EC; }
local -r lst="${1}"
local -r identity="${2}"
local -r bi_op="${3}"
shift 3 || { ctx_wn $ctx; return $EC; }
local -i len
len=$(List_len $ctx "${lst}") || \
{ ctx_w $ctx "cannot get len"; return $EC; }
local res="${identity}"
local -i i
for (( i=0; i<${len}; i++ )); do
local el
el=$(List_get $ctx "${lst}" "${i}") || \
{ ctx_w $ctx "cannot get an el"; return $EC; }
res=$(${bi_op} $ctx "${res}" "${el}")
done
echo "${res}"
}
function List_any_match() {
# Return true if any element satisfies the predicate.
local ctx; is_ctx "${1}" && ctx="${1}" && shift
[ $# -ne 2 ] && { ctx_wn $ctx; return $EC; }
local -r lst="${1}"
local -r predicate="${2}"
shift 2 || { ctx_wn $ctx; return $EC; }
local -i len
len=$(List_len $ctx "${lst}") || \
{ ctx_w $ctx "cannot get len"; return $EC; }
local -i i
for (( i=0; i<${len}; i++ )); do
local el
el=$(List_get $ctx "${lst}" "${i}") || \
{ ctx_w $ctx "cannot get an el"; return $EC; }
local ec=0
${predicate} $ctx "${el}" ||
{ ec=$?; [ ${ec} -gt 1 ] && { ctx_w $ctx "predicate error"; return $EC; }; }
if [ ${ec} -eq 0 ]; then
return $TRUE
fi
done
return $FALSE
}
function List_all_match() {
# Return true if all elements satisfy the predicate.
local ctx; is_ctx "${1}" && ctx="${1}" && shift
[ $# -ne 2 ] && { ctx_wn $ctx; return $EC; }
local -r lst="${1}"
local -r predicate="${2}"
shift 2 || { ctx_wn $ctx; return $EC; }
local nlst
nlst=$(List_filter $ctx "${lst}" "${predicate}") || \
{ ctx_w $ctx "could not filter"; return $EC; }
[ $(List_len $ctx "${nlst}") -eq $(List_len $ctx "${lst}") ]
}
function List_first() {
# Return (the first element, 0); (_, $EC) if none exists.
local ctx; is_ctx "${1}" && ctx="${1}" && shift
[ $# -ne 1 ] && { ctx_wn $ctx; return $EC; }
local -r lst="${1}"
shift 1 || { ctx_wn $ctx; return $EC; }
List_get $ctx "${lst}" 0
}
function List_second() {
# Return (the second element, 0); (_, $EC) if none exists.
local ctx; is_ctx "${1}" && ctx="${1}" && shift
[ $# -ne 1 ] && { ctx_wn $ctx; return $EC; }
local -r lst="${1}"
shift 1 || { ctx_wn $ctx; return $EC; }
List_get $ctx "${lst}" 1
}
function List_last() {
# Return (the last element, 0); (_, $EC) if none exists.
local ctx; is_ctx "${1}" && ctx="${1}" && shift
[ $# -ne 1 ] && { ctx_wn $ctx; return $EC; }
local -r lst="${1}"
shift 1 || { ctx_wn $ctx; return $EC; }
List_get $ctx "${lst}" $(( $(List_len $ctx "${lst}") - 1 ))
}