Skip to content

Commit 2e6d2cc

Browse files
committed
add more Bicep/ARM function samples
1 parent 612a180 commit 2e6d2cc

File tree

2 files changed

+22
-8
lines changed

2 files changed

+22
-8
lines changed

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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -183,11 +183,12 @@ var dogs = [
183183
184184
output dogNames array = map(dogs, dog => dog.name)
185185
output sayHi array = map(dogs, dog => 'Hello ${dog.name}!')
186-
output mapObject array = map(range(0, length(dogs)), i => {
186+
output mapArray array = map(range(0, length(dogs)), i => {
187187
i: i
188188
dog: dogs[i].name
189189
greeting: 'Ahoy, ${dogs[i].name}!'
190190
})
191+
output mapArrayIndex array = map(dogs, (x, i) => { index: i, val: x.name})
191192
```
192193

193194
The output from the preceding example is:
@@ -196,9 +197,10 @@ The output from the preceding example is:
196197
| ---- | ---- | ----- |
197198
| dogNames | Array | ["Evie","Casper","Indy","Kira"] |
198199
| 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!"}] |
200+
| 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!"}] |
201+
| mapArrayIndex | Array | [{"index":0,"val":"Evie"},{"index":1,"val":"Casper"},{"index":2,"val":"Indy"},{"index":3,"val":"Kira"}] |
200202

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.
203+
**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.
202204

203205
## mapValues
204206

@@ -287,6 +289,7 @@ var dogs = [
287289
var ages = map(dogs, dog => dog.age)
288290
output totalAge int = reduce(ages, 0, (cur, next) => cur + next)
289291
output totalAgeAdd1 int = reduce(ages, 1, (cur, next) => cur + next)
292+
output oddAge int = reduce(ages, 0, (cur, next, i) => (i % 2 == 0) ? cur + next : cur)
290293
```
291294

292295
The output from the preceding example is:
@@ -295,8 +298,9 @@ The output from the preceding example is:
295298
| ---- | ---- | ----- |
296299
| totalAge | int | 18 |
297300
| totalAgeAdd1 | int | 19 |
301+
| oddAge | int | 7 |
298302

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.
303+
**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).
300304

301305
```bicep
302306
output reduceObjectUnion object = reduce([

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

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -260,9 +260,13 @@ The following example shows how to use the `map` function.
260260
"type": "array",
261261
"value": "[map(variables('dogs'), lambda('dog', format('Hello {0}!', lambdaVariables('dog').name)))]"
262262
},
263-
"mapObject": {
263+
"mapArray": {
264264
"type": "array",
265265
"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))))]"
266+
},
267+
"mapArrayIndex": {
268+
"type": "array",
269+
"value": "[map(variables('dogs'), lambda('x', 'i', createObject('index', lambdaVariables('i'), 'val', lambdaVariables('x').name)))]"
266270
}
267271
}
268272
}
@@ -274,9 +278,10 @@ The output from the preceding example is:
274278
| ---- | ---- | ----- |
275279
| dogNames | Array | ["Evie","Casper","Indy","Kira"] |
276280
| 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!"}] |
281+
| 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!"}] |
282+
| mapArrayIndex | Array | [{"index":0,"val":"Evie"},{"index":1,"val":"Casper"},{"index":2,"val":"Indy"},{"index":3,"val":"Kira"}] |
278283

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.
284+
**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.
280285

281286
## mapValues
282287

@@ -400,6 +405,10 @@ The following examples show how to use the `reduce` function.
400405
"totalAgeAdd1": {
401406
"type": "int",
402407
"value": "[reduce(variables('ages'), 1, lambda('cur', 'next', add(lambdaVariables('cur'), lambdaVariables('next'))))]"
408+
},
409+
"oddAge": {
410+
"type": "int",
411+
"value": "[reduce(variables('ages'), 0, lambda('cur', 'next', 'i', if(equals(mod(lambdaVariables('i'), 2), 0), add(lambdaVariables('cur'), lambdaVariables('next')), lambdaVariables('cur'))))]"
403412
}
404413
}
405414
}
@@ -411,8 +420,9 @@ The output from the preceding example is:
411420
| ---- | ---- | ----- |
412421
| totalAge | int | 18 |
413422
| totalAgeAdd1 | int | 19 |
423+
| oddAge | int | 7 |
414424

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.
425+
**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).
416426

417427
```json
418428
{

0 commit comments

Comments
 (0)