.docname {Destructuring}
.include {docs}

Destructuring splits a [lambda](lambda.qd) parameter into its individual components. For instance, a [pair](iterable.qd#pair) has two components, while a generic [iterable](iterable.qd) can have many.

The following types support destructuring:
- [`Iterable`](iterable.qd), including [`Pair`](iterable.qd#pair)
- [`Dictionary`](dictionary.qd), as an iterable of pairs

A value is destructured into `N` components when all of the following conditions are met:
- The type supports destructuring
- The lambda expects a single argument, such as with [`.foreach`](loops.qd)
- You supply `N > 1` lambda parameters

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](dictionary.qd) and iterate over its destructured key-value components.

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

    .foreach {.mydictionary}
        key value:
        **.key** has value **.value**

## Example: `.sorted`

In this example, we define a [Dictionary](dictionary.qd) and iterate over its destructured key-value components using `.foreach`, but only after sorting its entries by value using [`.sorted`](iterable.qd#operations), which takes a lambda that defines the ordering criteria.

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

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