diff options
author | Gabriel Volpe <volpegabriel@gmail.com> | 2024-04-15 19:16:15 +0200 |
---|---|---|
committer | Gabriel Volpe <volpegabriel@gmail.com> | 2024-04-15 19:16:15 +0200 |
commit | fe2bead78b1034052eca1d695118997f31826924 (patch) | |
tree | f217b407bcca7e0e4610ded01c9c44ebaaf0b76b /lib/attrsets.nix | |
parent | 10517cf9abe8d54c66b1dd5fb18fc4b03750037f (diff) |
lib/attrsets: introduce mapCartesianProduct
Diffstat (limited to 'lib/attrsets.nix')
-rw-r--r-- | lib/attrsets.nix | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/lib/attrsets.nix b/lib/attrsets.nix index 5c4f735b63da2..83f8d0f34186e 100644 --- a/lib/attrsets.nix +++ b/lib/attrsets.nix @@ -914,6 +914,40 @@ rec { /** + Return the result of function f applied to the cartesian product of attribute set value combinations. + Equivalent to using cartesianProduct followed by map. + + # Inputs + + `f` + + : A function, given an attribute set, it returns a new value. + + `attrsOfLists` + + : Attribute set with attributes that are lists of values + + # Type + + ``` + mapCartesianProduct :: (AttrSet -> a) -> AttrSet -> [a] + ``` + + # Examples + :::{.example} + ## `lib.attrsets.mapCartesianProduct` usage example + + ```nix + mapCartesianProduct ({a, b}: "${a}-${b}") { a = [ "1" "2" ]; b = [ "3" "4" ]; } + => [ "1-3" "1-4" "2-3" "2-4" ] + ``` + + ::: + + */ + mapCartesianProduct = f: attrsOfLists: map f (cartesianProduct attrsOfLists); + + /** Utility function that creates a `{name, value}` pair as expected by `builtins.listToAttrs`. |