query
On this page

genericClosure

builtins.genericClosure

Primop
Docs pulled from | This Revision | 6 days ago

Takes 1 arguments

attrset


builtins.genericClosure iteratively computes the transitive closure over an arbitrary relation defined by a function.

It takes attrset with two attributes named startSet and operator, and returns a list of attribute sets:

  • startSet: The initial list of attribute sets.

  • operator: A function that takes an attribute set and returns a list of attribute sets. It defines how each item in the current set is processed and expanded into more items.

Each attribute set in the list startSet and the list returned by operator must have an attribute key, which must support equality comparison. The value of key can be one of the following types:

The result is produced by calling the operator on each item that has not been called yet, including newly added items, until no new items are added. Items are compared by their key attribute.

Common usages are:

  • Generating unique collections of items, such as dependency graphs.
  • Traversing through structures that may contain cycles or loops.
  • Processing data structures with complex internal relationships.

Example

builtins.genericClosure {
  startSet = [ {key = 5;} ];
  operator = item: [{
    key = if (item.key / 2 ) * 2 == item.key
         then item.key / 2
         else 3 * item.key + 1;
  }];
}

evaluates to

[ { key = 5; } { key = 16; } { key = 8; } { key = 4; } { key = 2; } { key = 1; } ]
This function is not defined in a .nix file. It is likely a builtins function or an alias of a builtins function. builtins functions are predefined functions provided by Nix.

Noogle detected

Aliases

Detected Type
genericClosure :: AttrSet -> [AttrSet]

Implementation

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