|
| 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. |
0 commit comments