Funzioni lambda per i modelli di Resource Manager
Questo articolo descrive le funzioni lambda da usare nei modelli di Resource Manager. Le funzioni lambda sono essenzialmente blocchi di codice che possono essere passati come argomento. Possono accettare più parametri, ma sono limitati a una singola riga di codice. In Bicep l'espressione lambda è in questo formato:
lambda(<lambda variable>, [<lambda variable>, ...], <expression>)
Suggerimento
È consigliabile usare Bicep perché offre le stesse funzionalità dei modelli arm e la sintassi è più facile da usare. Per altre informazioni, vedere Funzioni di distribuzione .
Limiti
La funzione lambda del modello di Resource Manager presenta queste limitazioni:
- La funzione lambda può essere specificata direttamente come argomenti di funzione nelle funzioni seguenti:
filter()
,map()
,reduce()
,sort()
etoObject()
. - L'uso di variabili lambda (le variabili temporanee usate nelle funzioni lambda) all'interno dell'accesso alle matrici di risorse o moduli non è attualmente supportato.
- L'uso di variabili lambda all'interno della
listKeys
funzione non è attualmente supportato. - L'uso di variabili lambda all'interno della funzione di riferimento non è attualmente supportato.
filter
filter(inputArray, lambda function)
Filtra una matrice con una funzione di filtro personalizzata.
In Bicep usare la funzione di filtro .
Parametri
Parametro | Obbligatorio | Type | Descrizione |
---|---|---|---|
inputArray | Sì | array | Matrice da filtrare. |
Funzione lambda | Sì | expression | Funzione lambda applicata a ogni elemento della matrice di input. Se false, l'elemento viene filtrato fuori dalla matrice di output. |
Valore restituito
Matrice .
Esempi
Negli esempi seguenti viene illustrato come usare la filter
funzione .
{
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"variables": {
"dogs": [
{
"name": "Evie",
"age": 5,
"interests": [
"Ball",
"Frisbee"
]
},
{
"name": "Casper",
"age": 3,
"interests": [
"Other dogs"
]
},
{
"name": "Indy",
"age": 2,
"interests": [
"Butter"
]
},
{
"name": "Kira",
"age": 8,
"interests": [
"Rubs"
]
}
]
},
"resources": [],
"outputs": {
"oldDogs": {
"type": "array",
"value": "[filter(variables('dogs'), lambda('dog', greaterOrEquals(lambdaVariables('dog').age, 5)))]"
}
}
}
L'output dell'esempio precedente mostra i cani che hanno cinque o versioni precedenti:
Nome | Type | Valore |
---|---|---|
oldDogs | Matrice | [{"name":"Evie","age":5,"interests":["Ball","Frisbee"]},{"name":"Kira","age":8,"interests":["Rubs"]}] |
{
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"variables": {
"copy": [
{
"name": "itemForLoop",
"count": "[length(range(0, 10))]",
"input": "[range(0, 10)[copyIndex('itemForLoop')]]"
}
]
},
"resources": [],
"outputs": {
"filteredLoop": {
"type": "array",
"value": "[filter(variables('itemForLoop'), lambda('i', greater(lambdaVariables('i'), 5)))]"
},
"isEven": {
"type": "array",
"value": "[filter(range(0, 10), lambda('i', equals(0, mod(lambdaVariables('i'), 2))))]"
}
}
}
Output dell'esempio precedente:
Nome | Type | Valore |
---|---|---|
filteredLoop | Matrice | [6, 7, 8, 9] |
isEven | Matrice | [0, 2, 4, 6, 8] |
filterdLoop mostra i numeri in una matrice maggiore di 5 e isEven mostra i numeri pari nella matrice.
mappa
map(inputArray, lambda function)
Applica una funzione di mapping personalizzata a ogni elemento di una matrice.
In Bicep usare la funzione map .
Parametri
Parametro | Obbligatorio | Type | Descrizione |
---|---|---|---|
inputArray | Sì | array | Matrice da mappare. |
Funzione lambda | Sì | expression | La funzione lambda applicata a ogni elemento della matrice di input, per generare la matrice di output. |
Valore restituito
Matrice .
Esempio
L'esempio seguente illustra come usare la funzione map
.
{
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"variables": {
"dogs": [
{
"name": "Evie",
"age": 5,
"interests": [
"Ball",
"Frisbee"
]
},
{
"name": "Casper",
"age": 3,
"interests": [
"Other dogs"
]
},
{
"name": "Indy",
"age": 2,
"interests": [
"Butter"
]
},
{
"name": "Kira",
"age": 8,
"interests": [
"Rubs"
]
}
]
},
"resources": [],
"outputs": {
"dogNames": {
"type": "array",
"value": "[map(variables('dogs'), lambda('dog', lambdaVariables('dog').name))]"
},
"sayHi": {
"type": "array",
"value": "[map(variables('dogs'), lambda('dog', format('Hello {0}!', lambdaVariables('dog').name)))]"
},
"mapObject": {
"type": "array",
"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))))]"
}
}
}
L'output dell'esempio precedente è:
Nome | Type | Valore |
---|---|---|
dogNames | Matrice | ["Evie","Casper","Indy","Kira"] |
sayHi | Matrice | ["Hello Evie!","Hello Casper!","Hello Indy!","Hello Kira!"] |
mapObject | Matrice | [{"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!"}] |
dogNames mostra i nomi dei cani dalla matrice di oggetti; sayHi concatena "Hello" e ognuno dei nomi dei cani e mapObject crea un'altra matrice di oggetti.
reduce
reduce(inputArray, initialValue, lambda function)
Riduce una matrice con una funzione di riduzione personalizzata.
In Bicep usare la funzione reduce .
Parametri
Parametro | Obbligatorio | Type | Descrizione |
---|---|---|---|
inputArray | Sì | array | Matrice da ridurre. |
initialValue | Sì | qualsiasi | Valore iniziale. |
Funzione lambda | Sì | expression | Funzione lambda usata per aggregare il valore corrente e il valore successivo. |
Valore restituito
Qualsiasi.
Esempio
Negli esempi seguenti viene illustrato come usare la reduce
funzione .
{
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"variables": {
"dogs": [
{
"name": "Evie",
"age": 5,
"interests": [
"Ball",
"Frisbee"
]
},
{
"name": "Casper",
"age": 3,
"interests": [
"Other dogs"
]
},
{
"name": "Indy",
"age": 2,
"interests": [
"Butter"
]
},
{
"name": "Kira",
"age": 8,
"interests": [
"Rubs"
]
}
],
"ages": "[map(variables('dogs'), lambda('dog', lambdaVariables('dog').age))]"
},
"resources": [],
"outputs": {
"totalAge": {
"type": "int",
"value": "[reduce(variables('ages'), 0, lambda('cur', 'next', add(lambdaVariables('cur'), lambdaVariables('next'))))]"
},
"totalAgeAdd1": {
"type": "int",
"value": "[reduce(variables('ages'), 1, lambda('cur', 'next', add(lambdaVariables('cur'), lambdaVariables('next'))))]"
}
}
}
L'output dell'esempio precedente è:
Nome | Type | Valore |
---|---|---|
totalAge | int | 18 |
totalAgeAdd1 | int | 19 |
totalAge somma le età dei cani; totalAgeAdd1 ha un valore iniziale pari a 1 e aggiunge tutte le età del cane ai valori iniziali.
{
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"resources": [],
"outputs": {
"reduceObjectUnion": {
"type": "object",
"value": "[reduce(createArray(createObject('foo', 123), createObject('bar', 456), createObject('baz', 789)), createObject(), lambda('cur', 'next', union(lambdaVariables('cur'), lambdaVariables('next'))))]"
}
}
}
L'output dell'esempio precedente è:
Nome | Type | Valore |
---|---|---|
reduceObjectUnion | oggetto | {"foo":123,"bar":456,"baz":789} |
La funzione union restituisce un singolo oggetto con tutti gli elementi dei parametri. La chiamata di funzione unisce le coppie chiave-valore degli oggetti in un nuovo oggetto .
ordinamento
sort(inputArray, lambda function)
Ordina una matrice con una funzione di ordinamento personalizzata.
In Bicep usare la funzione di ordinamento .
Parametri
Parametro | Obbligatorio | Type | Descrizione |
---|---|---|---|
inputArray | Sì | array | Matrice da ordinare. |
Funzione lambda | Sì | expression | Funzione lambda usata per confrontare due elementi della matrice per l'ordinamento. Se true, il secondo elemento verrà ordinato dopo il primo nella matrice di output. |
Valore restituito
Matrice .
Esempio
L'esempio seguente illustra come usare la funzione sort
.
{
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"variables": {
"dogs": [
{
"name": "Evie",
"age": 5,
"interests": [
"Ball",
"Frisbee"
]
},
{
"name": "Casper",
"age": 3,
"interests": [
"Other dogs"
]
},
{
"name": "Indy",
"age": 2,
"interests": [
"Butter"
]
},
{
"name": "Kira",
"age": 8,
"interests": [
"Rubs"
]
}
]
},
"resources": [],
"outputs": {
"dogsByAge": {
"type": "array",
"value": "[sort(variables('dogs'), lambda('a', 'b', less(lambdaVariables('a').age, lambdaVariables('b').age)))]"
}
}
}
L'output dell'esempio precedente ordina gli oggetti dog dal più giovane al più vecchio:
Nome | Type | Valore |
---|---|---|
dogsByAge | Matrice | [{"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"]}] |
Toobject
toObject(inputArray, lambda function, [lambda function])
Converte una matrice in un oggetto con una funzione chiave personalizzata e una funzione valore personalizzato facoltativa. Vedere gli elementi sulla conversione di un oggetto in una matrice.
In Bicep usare la funzione toObject .
Parametri
Parametro | Obbligatorio | Type | Descrizione |
---|---|---|---|
inputArray | Sì | array | Matrice utilizzata per la creazione di un oggetto . |
Funzione lambda | Sì | expression | Funzione lambda usata per fornire il predicato della chiave. |
Funzione lambda | No | expression | Funzione lambda usata per fornire il predicato di valore. |
Valore restituito
Oggetto .
Esempio
L'esempio seguente illustra come usare la toObject
funzione con i due parametri obbligatori:
{
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"variables": {
"dogs": [
{
"name": "Evie",
"age": 5,
"interests": [
"Ball",
"Frisbee"
]
},
{
"name": "Casper",
"age": 3,
"interests": [
"Other dogs"
]
},
{
"name": "Indy",
"age": 2,
"interests": [
"Butter"
]
},
{
"name": "Kira",
"age": 8,
"interests": [
"Rubs"
]
}
]
},
"resources": [],
"outputs": {
"dogsObject": {
"type": "object",
"value": "[toObject(variables('dogs'), lambda('entry', lambdaVariables('entry').name))]"
}
}
}
L'esempio precedente genera un oggetto basato su una matrice.
Nome | Type | Valore |
---|---|---|
dogsObject | Object | {"Evie":{"name":"Evie","age":5,"interests":["Ball","Frisbee"]},"Casper":{"name":"Casper","age":3,"interests":["Other dogs""]},"Indy":{"name":"Indy","age":2,"interests":["Butter"]},"Kira":{"name":"Kira","age":8,"interests":["Rubs"]}} |
La funzione seguente toObject
con il terzo parametro fornisce lo stesso output.
"outputs": {
"dogsObject": {
"type": "object",
"value": "[toObject(variables('dogs'), lambda('entry', lambdaVariables('entry').name), lambda('entry', lambdaVariables('entry')))]"
}
}
L'esempio seguente illustra come usare la toObject
funzione con tre parametri.
{
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"variables": {
"dogs": [
{
"name": "Evie",
"properties": {
"age": 5,
"interests": [
"Ball",
"Frisbee"
]
}
},
{
"name": "Casper",
"properties": {
"age": 3,
"interests": [
"Other dogs"
]
}
},
{
"name": "Indy",
"properties": {
"age": 2,
"interests": [
"Butter"
]
}
},
{
"name": "Kira",
"properties": {
"age": 8,
"interests": [
"Rubs"
]
}
}
]
},
"resources": [],
"outputs": {
"dogsObject": {
"type": "object",
"value": "[toObject(variables('dogs'), lambda('entry', lambdaVariables('entry').name), lambda('entry', lambdaVariables('entry').properties))]"
}
}
}
L'esempio precedente genera un oggetto basato su una matrice.
Nome | Type | Valore |
---|---|---|
dogsObject | Object | {"Evie":{"age":5,"interests":["Ball","Frisbee"]},"Casper":{"age":3,"interests":["Other dogs"]},"Indy":{"age":2,"interests":["Butter"]},"Kira":{"age":8,"interests":["Rubs"]}} |
Passaggi successivi
- Per altre funzioni di modello correlate alle matrici, vedere Funzioni modello - Matrici .