Skip to content

Commit 9a16bbe

Browse files
Merge pull request #211069 from mumian/0913-lambda
add lambda functions
2 parents e5b9173 + 0188869 commit 9a16bbe

File tree

4 files changed

+354
-1
lines changed

4 files changed

+354
-1
lines changed

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

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ ms.date: 04/12/2022
99
---
1010
# Array functions for Bicep
1111

12-
This article describes the Bicep functions for working with arrays.
12+
This article describes the Bicep functions for working with arrays. The lambda functions for working with arrays can be found [here](./bicep-functions-lambda.md).
1313

1414
## array
1515

@@ -235,6 +235,43 @@ The output from the preceding example with the default values is:
235235
| arrayOutput | String | one |
236236
| stringOutput | String | O |
237237

238+
## flatten
239+
240+
`flatten(arrayToFlatten)`
241+
242+
Takes an array of arrays, and returns an array of sub-array elements, in the original order. Sub-arrays are only flattened once, not recursively.
243+
244+
Namespace: [sys](bicep-functions.md#namespaces-for-functions).
245+
246+
### Parameters
247+
248+
| Parameter | Required | Type | Description |
249+
|:--- |:--- |:--- |:--- |
250+
| arrayToFlattern |Yes |array |The array of sub-arrays to flatten.|
251+
252+
### Return value
253+
254+
Array
255+
256+
### Example
257+
258+
The following example shows how to use the flatten function.
259+
260+
```bicep
261+
param arrayToTest array = [
262+
['one', 'two']
263+
['three']
264+
['four', 'five']
265+
]
266+
output arrayOutput array = flatten(arrayToTest)
267+
```
268+
269+
The output from the preceding example with the default values is:
270+
271+
| Name | Type | Value |
272+
| ---- | ---- | ----- |
273+
| arrayOutput | array | ['one', 'two', 'three', 'four', 'five'] |
274+
238275
## indexOf
239276

240277
`indexOf(arrayToSearch, itemToFind)`
Lines changed: 303 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,303 @@
1+
---
2+
title: Bicep functions - lambda
3+
description: Describes the lambda functions to use in a Bicep file.
4+
author: mumian
5+
ms.topic: conceptual
6+
ms.author: jgao
7+
ms.date: 09/20/2022
8+
9+
---
10+
# Lambda functions for Bicep
11+
12+
This article describes the lambda functions to use in Bicep. Lambda expressions (or lambda functions) are essentially blocks of code that can be passed as an argument. In Bicep, lambda expression is in this format:
13+
14+
```bicep
15+
<lambda variable> => <expression>
16+
```
17+
18+
> [!NOTE]
19+
> The lambda functions are only supported in Bicep CLI version 0.10.61 or newer.
20+
21+
## Limitations
22+
23+
Bicep lambda function has these limitations:
24+
25+
- Lambda expression can only be specified directly as function arguments in these functions: [`filter()`](#filter), [`map()`](#map), [`reduce()`](#reduce), and [`sort()`](#sort).
26+
- Using lambda variables (the temporary variables used in the lambda expressions) inside resource or module array access isn't currently supported.
27+
- Using lambda variables inside the [`listKeys`](./bicep-functions-resource.md#list) function isn't currently supported.
28+
- Using lambda variables inside the [reference](./bicep-functions-resource.md#reference) function isn't currently supported.
29+
30+
## filter
31+
32+
`filter(inputArray, lambda expression)`
33+
34+
Filters an array with a custom filtering function.
35+
36+
Namespace: [sys](bicep-functions.md#namespaces-for-functions).
37+
38+
### Parameters
39+
40+
| Parameter | Required | Type | Description |
41+
|:--- |:--- |:--- |:--- |
42+
| inputArray |Yes |array |The array to filter.|
43+
| lambda expression |Yes |expression |The lambda expression applied to each input array element. If false, the item will be filtered out of the output array.|
44+
45+
### Return value
46+
47+
An array.
48+
49+
### Examples
50+
51+
The following examples show how to use the filter function.
52+
53+
```bicep
54+
var dogs = [
55+
{
56+
name: 'Evie'
57+
age: 5
58+
interests: ['Ball', 'Frisbee']
59+
}
60+
{
61+
name: 'Casper'
62+
age: 3
63+
interests: ['Other dogs']
64+
}
65+
{
66+
name: 'Indy'
67+
age: 2
68+
interests: ['Butter']
69+
}
70+
{
71+
name: 'Kira'
72+
age: 8
73+
interests: ['Rubs']
74+
}
75+
]
76+
77+
output oldDogs array = filter(dogs, dog => dog.age >=5)
78+
```
79+
80+
The output from the preceding example shows the dogs that are five or older:
81+
82+
| Name | Type | Value |
83+
| ---- | ---- | ----- |
84+
| oldDogs | Array | [{"name":"Evie","age":5,"interests":["Ball","Frisbee"]},{"name":"Kira","age":8,"interests":["Rubs"]}] |
85+
86+
```bicep
87+
var itemForLoop = [for item in range(0, 10): item]
88+
89+
output filteredLoop array = filter(itemForLoop, i => i > 5)
90+
output isEven array = filter(range(0, 10), i => 0 == i % 2)
91+
```
92+
93+
The output from the preceding example:
94+
95+
| Name | Type | Value |
96+
| ---- | ---- | ----- |
97+
| filteredLoop | Array | [6, 7, 8, 9] |
98+
| isEven | Array | [0, 2, 4, 6, 8] |
99+
100+
**filterdLoop** shows the numbers in an array that are greater than 5; and **isEven** shows the even numbers in the array.
101+
102+
## map
103+
104+
`map(inputArray, lambda expression)`
105+
106+
Applies a custom mapping function to each element of an array.
107+
108+
Namespace: [sys](bicep-functions.md#namespaces-for-functions).
109+
110+
### Parameters
111+
112+
| Parameter | Required | Type | Description |
113+
|:--- |:--- |:--- |:--- |
114+
| inputArray |Yes |array |The array to map.|
115+
| lambda expression |Yes |expression |The lambda expression applied to each input array element, in order to generate the output array.|
116+
117+
### Return value
118+
119+
An array.
120+
121+
### Example
122+
123+
The following example shows how to use the map function.
124+
125+
```bicep
126+
var dogs = [
127+
{
128+
name: 'Evie'
129+
age: 5
130+
interests: ['Ball', 'Frisbee']
131+
}
132+
{
133+
name: 'Casper'
134+
age: 3
135+
interests: ['Other dogs']
136+
}
137+
{
138+
name: 'Indy'
139+
age: 2
140+
interests: ['Butter']
141+
}
142+
{
143+
name: 'Kira'
144+
age: 8
145+
interests: ['Rubs']
146+
}
147+
]
148+
149+
output dogNames array = map(dogs, dog => dog.name)
150+
output sayHi array = map(dogs, dog => 'Hello ${dog.name}!')
151+
output mapObject array = map(range(0, length(dogs)), i => {
152+
i: i
153+
dog: dogs[i].name
154+
greeting: 'Ahoy, ${dogs[i].name}!'
155+
})
156+
```
157+
158+
The output from the preceding example is:
159+
160+
| Name | Type | Value |
161+
| ---- | ---- | ----- |
162+
| dogNames | Array | ["Evie","Casper","Indy","Kira"] |
163+
| sayHi | Array | ["Hello Evie!","Hello Casper!","Hello Indy!","Hello Kira!"] |
164+
| 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!"}] |
165+
166+
**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.
167+
168+
## reduce
169+
170+
`reduce(inputArray, initialValue, lambda expression)`
171+
172+
Reduces an array with a custom reduce function.
173+
174+
Namespace: [sys](bicep-functions.md#namespaces-for-functions).
175+
176+
### Parameters
177+
178+
| Parameter | Required | Type | Description |
179+
|:--- |:--- |:--- |:--- |
180+
| inputArray |Yes |array |The array to reduce.|
181+
| initialValue |No |any |Initial value.|
182+
| lambda expression |Yes |expression |The lambda expression used to aggregate the current value and the next value.|
183+
184+
### Return value
185+
186+
Any.
187+
188+
### Example
189+
190+
The following examples show how to use the reduce function.
191+
192+
```bicep
193+
var dogs = [
194+
{
195+
name: 'Evie'
196+
age: 5
197+
interests: ['Ball', 'Frisbee']
198+
}
199+
{
200+
name: 'Casper'
201+
age: 3
202+
interests: ['Other dogs']
203+
}
204+
{
205+
name: 'Indy'
206+
age: 2
207+
interests: ['Butter']
208+
}
209+
{
210+
name: 'Kira'
211+
age: 8
212+
interests: ['Rubs']
213+
}
214+
]
215+
var ages = map(dogs, dog => dog.age)
216+
output totalAge int = reduce(ages, 0, (cur, prev) => cur + prev)
217+
output totalAgeAdd1 int = reduce(ages, 1, (cur, prev) => cur + prev)
218+
```
219+
220+
The output from the preceding example is:
221+
222+
| Name | Type | Value |
223+
| ---- | ---- | ----- |
224+
| totalAge | int | 18 |
225+
| totalAgeAdd1 | int | 19 |
226+
227+
**totalAge** sums the ages of the dogs; **totalAgeAdd1** has an initial value of 1, and adds all the dog ages to the initial values.
228+
229+
```bicep
230+
output reduceObjectUnion object = reduce([
231+
{ foo: 123 }
232+
{ bar: 456 }
233+
{ baz: 789 }
234+
], {}, (cur, next) => union(cur, next))
235+
```
236+
237+
The output from the preceding example is:
238+
239+
| Name | Type | Value |
240+
| ---- | ---- | ----- |
241+
| reduceObjectUnion | object | {"foo":123,"bar":456,"baz":789} |
242+
243+
The [union](./bicep-functions-object.md#union) function returns a single object with all elements from the parameters. The function call unionizes the key value pairs of the objects into a new object.
244+
245+
## sort
246+
247+
`sort(inputArray, lambda expression)`
248+
249+
Sorts an array with a custom sort function.
250+
251+
Namespace: [sys](bicep-functions.md#namespaces-for-functions).
252+
253+
### Parameters
254+
255+
| Parameter | Required | Type | Description |
256+
|:--- |:--- |:--- |:--- |
257+
| inputArray |Yes |array |The array to sort.|
258+
| lambda expression |Yes |expression |The lambda expression used to compare two array elements for ordering. If true, the second element will be ordered after the first in the output array.|
259+
260+
### Return value
261+
262+
An array.
263+
264+
### Example
265+
266+
The following example shows how to use the sort function.
267+
268+
```bicep
269+
var dogs = [
270+
{
271+
name: 'Evie'
272+
age: 5
273+
interests: ['Ball', 'Frisbee']
274+
}
275+
{
276+
name: 'Casper'
277+
age: 3
278+
interests: ['Other dogs']
279+
}
280+
{
281+
name: 'Indy'
282+
age: 2
283+
interests: ['Butter']
284+
}
285+
{
286+
name: 'Kira'
287+
age: 8
288+
interests: ['Rubs']
289+
}
290+
]
291+
292+
output dogsByAge array = sort(dogs, (a, b) => a.age < b.age)
293+
```
294+
295+
The output from the preceding example sorts the dog objects from the youngest to the oldest:
296+
297+
| Name | Type | Value |
298+
| ---- | ---- | ----- |
299+
| dogsByAge | Array | [{"name":"Indy","age":2,"interests":["Butter"]},{"name":"Casper","age":3,"interests":["Other dogs"]},{"name":"Evie","age":5,"interests":["Ball","Frisbee"]},{"name":"Kira","age":8,"interests":["Rubs"]}] |
300+
301+
## Next steps
302+
303+
- See [Bicep functions - arrays](./bicep-functions-array.md) for additional array related Bicep functions.

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ The following functions are available for working with arrays. All of these func
4040
* [empty](./bicep-functions-array.md#empty)
4141
* [indexOf](./bicep-functions-array.md#indexof)
4242
* [first](./bicep-functions-array.md#first)
43+
* [flatten](./bicep-functions-array.md#flatten)
4344
* [intersection](./bicep-functions-array.md#intersection)
4445
* [last](./bicep-functions-array.md#last)
4546
* [lastIndexOf](./bicep-functions-array.md#lastindexof)
@@ -75,6 +76,16 @@ The following functions are available for loading the content from external file
7576
* [loadJsonContent](bicep-functions-files.md#loadjsoncontent)
7677
* [loadTextContent](bicep-functions-files.md#loadtextcontent)
7778

79+
## Lambda functions
80+
81+
The following functions are available for working with lambda expressions. All of these functions are in the `sys` namespace.
82+
83+
* [filter](bicep-functions-lambda.md#filter)
84+
* [map](bicep-functions-lambda.md#map)
85+
* [reduce](bicep-functions-lambda.md#reduce)
86+
* [sort](bicep-functions-lambda.md#sort)
87+
88+
7889
## Logical functions
7990

8091
The following function is available for working with logical conditions. This function is in the `sys` namespace.

articles/azure-resource-manager/bicep/toc.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,8 @@
312312
href: bicep-functions-deployment.md
313313
- name: File functions
314314
href: bicep-functions-files.md
315+
- name: Lambda functions
316+
href: bicep-functions-lambda.md
315317
- name: Logical functions
316318
href: bicep-functions-logical.md
317319
- name: Numeric functions

0 commit comments

Comments
 (0)