Skip to content

Commit 7e26c63

Browse files
authored
Merge pull request #276476 from mumian/0523-bicep-function-samples
0523 bicep function samples
2 parents ec5a310 + a86e71a commit 7e26c63

File tree

2 files changed

+44
-19
lines changed

2 files changed

+44
-19
lines changed

articles/azure-resource-manager/bicep/bicep-functions-lambda.md

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ This article describes the lambda functions to use in Bicep. [Lambda expressions
2020

2121
Bicep lambda function has these limitations:
2222

23-
- Lambda expression can only be specified directly as function arguments in these functions: [`filter()`](#filter), [`map()`](#map), [`reduce()`](#reduce), [`sort()`](#sort), and [`toObject()`](#toobject).
23+
- Lambda expression can only be specified directly as function arguments in these functions: [`filter()`](#filter), [`groupBy()`](#groupby), [`map()`](#map), [`mapValues()`](#mapvalues), [`reduce()`](#reduce), [`sort()`](#sort), and [`toObject()`](#toobject).
2424
- Using lambda variables (the temporary variables used in the lambda expressions) inside resource or module array access isn't currently supported.
2525
- Using lambda variables inside the [`listKeys`](./bicep-functions-resource.md#list) function isn't currently supported.
2626
- Using lambda variables inside the [reference](./bicep-functions-resource.md#reference) function isn't currently supported.
@@ -66,20 +66,24 @@ var dogs = [
6666
interests: ['Butter']
6767
}
6868
{
69-
name: 'Kira'
69+
name: 'Cira'
7070
age: 8
7171
interests: ['Rubs']
7272
}
7373
]
7474
7575
output oldDogs array = filter(dogs, dog => dog.age >=5)
76+
output dogNameIndex array = filter(dogs, (val, i) => i < 2 && substring(val.name, 0, 1) == 'C')
7677
```
7778

78-
The output from the preceding example shows the dogs that are five or older:
79+
The outputs from the preceding example:
7980

8081
| Name | Type | Value |
8182
| ---- | ---- | ----- |
8283
| oldDogs | Array | [{"name":"Evie","age":5,"interests":["Ball","Frisbee"]},{"name":"Kira","age":8,"interests":["Rubs"]}] |
84+
| dogNameIndex | Array | [{"name":"Casper","age":3,"interests":["Other dogs"]}] |
85+
86+
**oldDogs** lists the dogs that are five or older; **dogNameIndex** identifies the dogs whose index number is less than two and whose name starts with the letter "C".
8387

8488
```bicep
8589
var itemForLoop = [for item in range(0, 10): item]
@@ -88,7 +92,7 @@ output filteredLoop array = filter(itemForLoop, i => i > 5)
8892
output isEven array = filter(range(0, 10), i => 0 == i % 2)
8993
```
9094

91-
The output from the preceding example:
95+
The outputs from the preceding example:
9296

9397
| Name | Type | Value |
9498
| ---- | ---- | ----- |
@@ -118,15 +122,15 @@ An object.
118122

119123
### Examples
120124

121-
The following examples show how to use the `groupBy` function.
125+
The following example shows how to use the `groupBy` function.
122126

123127
```bicep
124128
var inputArray = ['foo', 'bar', 'baz']
125129
126130
output outObject object = groupBy(inputArray, x => substring(x, 0, 1))
127131
```
128132

129-
The output from the preceding example shows the dogs that are five or older:
133+
The output from the preceding example:
130134

131135
| Name | Type | Value |
132136
| ---- | ---- | ----- |
@@ -183,22 +187,24 @@ var dogs = [
183187
184188
output dogNames array = map(dogs, dog => dog.name)
185189
output sayHi array = map(dogs, dog => 'Hello ${dog.name}!')
186-
output mapObject array = map(range(0, length(dogs)), i => {
190+
output mapArray array = map(range(0, length(dogs)), i => {
187191
i: i
188192
dog: dogs[i].name
189193
greeting: 'Ahoy, ${dogs[i].name}!'
190194
})
195+
output mapArrayIndex array = map(dogs, (x, i) => { index: i, val: x.name})
191196
```
192197

193-
The output from the preceding example is:
198+
The outputs from the preceding example are:
194199

195200
| Name | Type | Value |
196201
| ---- | ---- | ----- |
197202
| dogNames | Array | ["Evie","Casper","Indy","Kira"] |
198203
| sayHi | Array | ["Hello Evie!","Hello Casper!","Hello Indy!","Hello Kira!"] |
199-
| mapObject | Array | [{"i":0,"dog":"Evie","greeting":"Ahoy, Evie!"},{"i":1,"dog":"Casper","greeting":"Ahoy, Casper!"},{"i":2,"dog":"Indy","greeting":"Ahoy, Indy!"},{"i":3,"dog":"Kira","greeting":"Ahoy, Kira!"}] |
204+
| mapArray | Array | [{"i":0,"dog":"Evie","greeting":"Ahoy, Evie!"},{"i":1,"dog":"Casper","greeting":"Ahoy, Casper!"},{"i":2,"dog":"Indy","greeting":"Ahoy, Indy!"},{"i":3,"dog":"Kira","greeting":"Ahoy, Kira!"}] |
205+
| mapArrayIndex | Array | [{"index":0,"val":"Evie"},{"index":1,"val":"Casper"},{"index":2,"val":"Indy"},{"index":3,"val":"Kira"}] |
200206

201-
**dogNames** shows the dog names from the array of objects; **sayHi** concatenates "Hello" and each of the dog names; and **mapObject** creates another array of objects.
207+
**dogNames** shows the dog names from the array of objects; **sayHi** concatenates "Hello" and each of the dog names; **mapArray** and **mapArrayIndex** create another two arrays of objects.
202208

203209
## mapValues
204210

@@ -287,16 +293,18 @@ var dogs = [
287293
var ages = map(dogs, dog => dog.age)
288294
output totalAge int = reduce(ages, 0, (cur, next) => cur + next)
289295
output totalAgeAdd1 int = reduce(ages, 1, (cur, next) => cur + next)
296+
output oddAge int = reduce(ages, 0, (cur, next, i) => (i % 2 == 0) ? cur + next : cur)
290297
```
291298

292-
The output from the preceding example is:
299+
The outputs from the preceding example are:
293300

294301
| Name | Type | Value |
295302
| ---- | ---- | ----- |
296303
| totalAge | int | 18 |
297304
| totalAgeAdd1 | int | 19 |
305+
| oddAge | int | 7 |
298306

299-
**totalAge** sums the ages of the dogs; **totalAgeAdd1** has an initial value of 1, and adds all the dog ages to the initial values.
307+
**totalAge** sums the ages of the dogs; **totalAgeAdd1** has an initial value of 1, and adds all the dog ages to the initial values. **oddAge** sums the ages of dogs that are located at even indices, specifically **5** (Evie) and **2** (Indy).
300308

301309
```bicep
302310
output reduceObjectUnion object = reduce([
@@ -477,4 +485,4 @@ The preceding example generates an object based on an array.
477485

478486
## Next steps
479487

480-
- See [Bicep functions - arrays](./bicep-functions-array.md) for additional array related Bicep functions.
488+
- See [Bicep functions - arrays](./bicep-functions-array.md) for more array-related Bicep functions.

articles/azure-resource-manager/templates/template-functions-lambda.md

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ lambda(<lambda variable>, [<lambda variable>, ...], <expression>)
2121

2222
ARM template lambda function has these limitations:
2323

24-
- Lambda function can only be specified directly as function arguments in these functions: [`filter()`](#filter), [`map()`](#map), [`reduce()`](#reduce), [`sort()`](#sort), and [`toObject()`](#toobject).
24+
- Lambda expression can only be specified directly as function arguments in these functions: [`filter()`](#filter), [`groupBy()`](#groupby), [`map()`](#map), [`mapValues()`](#mapvalues), [`reduce()`](#reduce), [`sort()`](#sort), and [`toObject()`](#toobject).
2525
- Using lambda variables (the temporary variables used in the lambda functions) inside resource or module array access isn't currently supported.
2626
- Using lambda variables inside the [`listKeys`](./template-functions-resource.md#list) function isn't currently supported.
2727
- Using lambda variables inside the [reference](./template-functions-resource.md#reference) function isn't currently supported.
@@ -91,16 +91,23 @@ The following examples show how to use the `filter` function.
9191
"oldDogs": {
9292
"type": "array",
9393
"value": "[filter(variables('dogs'), lambda('dog', greaterOrEquals(lambdaVariables('dog').age, 5)))]"
94+
},
95+
"dogNameIndex": {
96+
"type": "array",
97+
"value": "[filter(variables('dogs'), lambda('val', 'i', and(less(lambdaVariables('i'), 2), equals(substring(lambdaVariables('val').name, 0, 1), 'C'))))]"
9498
}
9599
}
96100
}
97101
```
98102

99-
The output from the preceding example shows the dogs that are five or older:
103+
The outputs from the preceding example:
100104

101105
| Name | Type | Value |
102106
| ---- | ---- | ----- |
103107
| oldDogs | Array | [{"name":"Evie","age":5,"interests":["Ball","Frisbee"]},{"name":"Kira","age":8,"interests":["Rubs"]}] |
108+
| dogNameIndex | Array | [{"name":"Casper","age":3,"interests":["Other dogs"]}] |
109+
110+
**oldDogs** lists the dogs that are five or older; **dogNameIndex** identifies the dogs whose index number is less than two and whose name starts with the letter "C".
104111

105112
```json
106113
{
@@ -260,9 +267,13 @@ The following example shows how to use the `map` function.
260267
"type": "array",
261268
"value": "[map(variables('dogs'), lambda('dog', format('Hello {0}!', lambdaVariables('dog').name)))]"
262269
},
263-
"mapObject": {
270+
"mapArray": {
264271
"type": "array",
265272
"value": "[map(range(0, length(variables('dogs'))), lambda('i', createObject('i', lambdaVariables('i'), 'dog', variables('dogs')[lambdaVariables('i')].name, 'greeting', format('Ahoy, {0}!', variables('dogs')[lambdaVariables('i')].name))))]"
273+
},
274+
"mapArrayIndex": {
275+
"type": "array",
276+
"value": "[map(variables('dogs'), lambda('x', 'i', createObject('index', lambdaVariables('i'), 'val', lambdaVariables('x').name)))]"
266277
}
267278
}
268279
}
@@ -274,9 +285,10 @@ The output from the preceding example is:
274285
| ---- | ---- | ----- |
275286
| dogNames | Array | ["Evie","Casper","Indy","Kira"] |
276287
| sayHi | Array | ["Hello Evie!","Hello Casper!","Hello Indy!","Hello Kira!"] |
277-
| mapObject | Array | [{"i":0,"dog":"Evie","greeting":"Ahoy, Evie!"},{"i":1,"dog":"Casper","greeting":"Ahoy, Casper!"},{"i":2,"dog":"Indy","greeting":"Ahoy, Indy!"},{"i":3,"dog":"Kira","greeting":"Ahoy, Kira!"}] |
288+
| mapArray | Array | [{"i":0,"dog":"Evie","greeting":"Ahoy, Evie!"},{"i":1,"dog":"Casper","greeting":"Ahoy, Casper!"},{"i":2,"dog":"Indy","greeting":"Ahoy, Indy!"},{"i":3,"dog":"Kira","greeting":"Ahoy, Kira!"}] |
289+
| mapArrayIndex | Array | [{"index":0,"val":"Evie"},{"index":1,"val":"Casper"},{"index":2,"val":"Indy"},{"index":3,"val":"Kira"}] |
278290

279-
**dogNames** shows the dog names from the array of objects; **sayHi** concatenates "Hello" and each of the dog names; and **mapObject** creates another array of objects.
291+
**dogNames** shows the dog names from the array of objects; **sayHi** concatenates "Hello" and each of the dog names; **mapArray** and **mapArrayIndex** create another two arrays of objects.
280292

281293
## mapValues
282294

@@ -400,6 +412,10 @@ The following examples show how to use the `reduce` function.
400412
"totalAgeAdd1": {
401413
"type": "int",
402414
"value": "[reduce(variables('ages'), 1, lambda('cur', 'next', add(lambdaVariables('cur'), lambdaVariables('next'))))]"
415+
},
416+
"oddAge": {
417+
"type": "int",
418+
"value": "[reduce(variables('ages'), 0, lambda('cur', 'next', 'i', if(equals(mod(lambdaVariables('i'), 2), 0), add(lambdaVariables('cur'), lambdaVariables('next')), lambdaVariables('cur'))))]"
403419
}
404420
}
405421
}
@@ -411,8 +427,9 @@ The output from the preceding example is:
411427
| ---- | ---- | ----- |
412428
| totalAge | int | 18 |
413429
| totalAgeAdd1 | int | 19 |
430+
| oddAge | int | 7 |
414431

415-
**totalAge** sums the ages of the dogs; **totalAgeAdd1** has an initial value of 1, and adds all the dog ages to the initial values.
432+
**totalAge** sums the ages of the dogs; **totalAgeAdd1** has an initial value of 1, and adds all the dog ages to the initial values. **oddAge** sums the ages of dogs that are located at even indices, specifically **5** (Evie) and **2** (Indy).
416433

417434
```json
418435
{

0 commit comments

Comments
 (0)