query
On this page

cartesianProduct

lib.attrsets.cartesianProduct

Docs pulled from | This Revision | 13 minutes ago


Return the cartesian product of attribute set value combinations.

Inputs

attrsOfLists

Attribute set with attributes that are lists of values

Type

cartesianProduct :: AttrSet -> [AttrSet]

Examples

lib.attrsets.cartesianProduct usage example

cartesianProduct { a = [ 1 2 ]; b = [ 10 20 ]; }
=> [
     { a = 1; b = 10; }
     { a = 1; b = 20; }
     { a = 2; b = 10; }
     { a = 2; b = 20; }
   ]

Noogle detected

Aliases

Implementation

The following is the current implementation of this function.

cartesianProduct =
    attrsOfLists:
    foldl' (listOfAttrs: attrName:
      concatMap (attrs:
        map (listValue: attrs // { ${attrName} = listValue; }) attrsOfLists.${attrName}
      ) listOfAttrs
    ) [{}] (attrNames attrsOfLists);