query
On this page

catAttrs

lib.catAttrs

Primop
Docs pulled from | This Revision | 18 minutes ago


Nixpkgs manual

Collect each attribute named attr from a list of attribute sets. Sets that don't contain the named attribute are ignored.

Inputs

attr

The attribute name to get out of the sets.

list

The list of attribute sets to go through

Type

catAttrs :: String -> [{ [String] :: a }] -> [a]

Examples

lib.attrsets.catAttrs usage example

catAttrs "a" [{a = 1;} {b = 0;} {a = 2;}]
=> [1 2]
(lib.attrsets.catAttrs)

Nix manual

Takes 2 arguments

attr, list

Collect each attribute named attr from a list of attribute sets. Attrsets that don't contain the named attribute are ignored. For example,

builtins.catAttrs "a" [{a = 1;} {b = 0;} {a = 2;}]

evaluates to [1 2].

Time Complexity

O(n * log m) where:

n = list length m = number of attributes per set

Noogle detected

Aliases

Implementation

This function is implemented in c++ and is part of the native nix runtime.

src/libexpr/primops.cc:3479

static void prim_catAttrs(EvalState & state, const PosIdx pos, Value ** args, Value & v)
{
    auto attrName = state.symbols.create(
        state.forceStringNoCtx(*args[0], pos, "while evaluating the first argument passed to builtins.catAttrs"));
    state.forceList(*args[1], pos, "while evaluating the second argument passed to builtins.catAttrs");

    SmallValueVector<nonRecursiveStackReservation> res(args[1]->listSize());
    size_t found = 0;

    for (auto v2 : args[1]->listView()) {
        state.forceAttrs(
            *v2, pos, "while evaluating an element in the list passed as second argument to builtins.catAttrs");
        if (auto i = v2->attrs()->get(attrName))
            res[found++] = i->value;
    }

    auto list = state.buildList(found);
    for (size_t n = 0; n < found; ++n)
        list[n] = res[n];
    v.mkList(list);
}