/

Destructuring

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:

A value is destructured into N components when all of the following conditions are met:

When Quarkdown destructures the lambda argument, it operates on the individual components rather than the element itself.

Example: .foreach

In this example, we define a Dictionary and iterate over its destructured key-value components.

Example 1

.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

Example: .sorted

In 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 @lambda is required when declaring an inline lambda.

Example 2

.var {mydictionary}
    .dictionary
        - a: 3
        - b: 1
        - c: 2

.foreach {.mydictionary::sorted by:{@lambda name value: .value}}
    name value:
    .name

b

c

a