Destructuring splits a lambda parameter into its individual components. For instance, a pair has two components, while a generic iterable can have many.
The following types support destructuring:
Iterable, including PairDictionary, as an iterable of pairsA value is destructured into N components when all of the following conditions are met:
.foreachN > 1 lambda parametersWhen Quarkdown destructures the lambda argument, it operates on the individual components rather than the element itself.
.foreachIn this example, we define a Dictionary and iterate over its destructured key-value components.
.var {mydictionary}
.dictionary
- a: 1
- b: 2
- c: 3
.foreach {.mydictionary}
key value:
**.key** has value **.value**a has value 1
b has value 2
c has value 3
.sortedIn this example, we define a Dictionary and iterate over its destructured key-value components using .foreach, but only after sorting its entries by value using .sorted, which takes a lambda that defines the ordering criteria.
Remember that
@lambdais required when declaring an inline lambda.
.var {mydictionary}
.dictionary
- a: 3
- b: 1
- c: 2
.foreach {.mydictionary::sorted by:{@lambda name value: .value}}
name value:
.nameb
c
a