query
On this page

attrValues

builtins.attrValues

Primop
Docs pulled from | This Revision | 21 minutes ago


Nixpkgs manual

Returns the values of all attributes in the given set, sorted by attribute name.

Type

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

Examples

lib.attrsets.attrValues usage example

attrValues {c = 3; a = 1; b = 2;}
=> [1 2 3]
(lib.attrsets.attrValues)

Nix manual

Takes 1 arguments

set

Return the values of the attributes in the set set in the order corresponding to the sorted attribute names.

Time Complexity

  • O(n log n), where:

n = number of attributes in the set

Noogle detected

Aliases

Implementation

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

src/libexpr/primops.cc:3103

static void prim_attrValues(EvalState & state, const PosIdx pos, Value ** args, Value & v)
{
    state.forceAttrs(*args[0], pos, "while evaluating the argument passed to builtins.attrValues");

    auto list = state.buildList(args[0]->attrs()->size());

    for (const auto & [n, i] : enumerate(*args[0]->attrs()))
        list[n] = (Value *) &i;

    std::sort(list.begin(), list.end(), [&](Value * v1, Value * v2) {
        std::string_view s1 = state.symbols[((Attr *) v1)->name], s2 = state.symbols[((Attr *) v2)->name];
        return s1 < s2;
    });

    for (auto & v : list)
        v = ((Attr *) v)->value;

    v.mkList(list);
}