-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcollection.rego
More file actions
110 lines (84 loc) · 1.65 KB
/
collection.rego
File metadata and controls
110 lines (84 loc) · 1.65 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
package collection
my_add(x, y) = x + y
map_add[x] = val {
col := input.col
delta := input.delta
val := my_add(col[x], delta)
}
test_map_add {
3 == my_add(1, 2)
some i
mapped := map_add with input as {
"col": [1, 2, 3, 4],
"delta": 2,
}
[3, 4, 5, 6][i] == mapped[i]
[8, 4, 7, 6][i] != mapped[i]
}
filter[x] = val {
col := input.col
filter_with := input.filter_with
col[x] != filter_with
val := col[x]
}
test_filter {
some i
filtered := filter with input as {
"col": [1, null, 2, 3, null, 4],
"filter_with": null,
}
[1, 2, 3, 4][i] == filtered[i]
[5, 2, 5, 4][i] != filtered[i]
}
test_both {
some i
filtered := filter with input as {
"col": [1, null, 2, 3, null, 4],
"filter_with": null,
}
[1, 2, 3, 4][i] == filtered[i]
[5, 2, 5, 4][i] != filtered[i]
mapped := map_add with input as {
"col": filtered,
"delta": 2,
}
[3, 4, 5, 6][i] == mapped[i]
[8, 4, 7, 6][i] != mapped[i]
}
map_each_mul[x] = val {
col := input.col
delta := input.delta
val := col[x] * delta[x]
}
test_map_each_mul {
2 == 1 * 2
some i
mapped := map_each_mul with input as {
"col": [1, 2, 3, 4],
"delta": [1, 10, 100, 1000],
}
[1, 20, 300, 4000][i] == mapped[i]
[8, 4, 7, 6][i] != mapped[i]
}
object_to_array(obj) = [l[_] | l := obj]
test_object_to_array {
[0, 1] == object_to_array({
"0": 0,
"1": 1,
})
[0, 1] == object_to_array({
"2": 0,
"4": 1,
})
}
array_reverse(arr) = [reversed |
some i
# this line is crucial to make the whole expression safe
arr[i]
revered_i := (count(arr) - i) - 1
reversed := arr[revered_i]
]
test_array_reverse {
array_reverse([1, 2]) == [2, 1]
array_reverse([5, 4, 3, 2, 1]) == [1, 2, 3, 4, 5]
}