Debian Codegraph @

doc/_includes/examples/comprehensions.jsonnet

raw
1 local arr = std.range(5, 8);
2 {
3 array_comprehensions: {
4 higher: [x + 3 for x in arr],
5 lower: [x - 3 for x in arr],
6 evens: [x for x in arr if x % 2 == 0],
7 odds: [x for x in arr if x % 2 == 1],
8 evens_and_odds: [
9 '%d-%d' % [x, y]
10 for x in arr
11 if x % 2 == 0
12 for y in arr
13 if y % 2 == 1
14 ],
15 },
16 object_comprehensions: {
17 evens: {
18 ['f' + x]: true
19 for x in arr
20 if x % 2 == 0
21 },
22 // Use object composition (+) to add in
23 // static fields:
24 mixture: {
25 f: 1,
26 g: 2,
27 } + {
28 [x]: 0
29 for x in ['a', 'b', 'c']
30 },
31 },
32 }