tree-wide: use mapCartesianProduct

This commit is contained in:
Gabriel Volpe 2024-04-15 19:17:53 +02:00
parent fe2bead78b
commit d864c36d57
No known key found for this signature in database
GPG Key ID: 121D4302A64B2261
6 changed files with 33 additions and 21 deletions

View File

@ -1688,16 +1688,32 @@ rec {
## `lib.lists.crossLists` usage example ## `lib.lists.crossLists` usage example
```nix ```nix
crossLists (x:y: "${toString x}${toString y}") [[1 2] [3 4]] crossLists (x: y: "${toString x}${toString y}") [[1 2] [3 4]]
=> [ "13" "14" "23" "24" ] => [ "13" "14" "23" "24" ]
``` ```
The following function call is equivalent to the one deprecated above:
```nix
mapCartesianProduct (x: "${toString x.a}${toString x.b}") { a = [1 2]; b = [3 4]; }
=> [ "13" "14" "23" "24" ]
```
::: :::
*/ */
crossLists = warn crossLists = warn
"lib.crossLists is deprecated, use lib.cartesianProductOfSets instead." ''lib.crossLists is deprecated, use lib.mapCartesianProduct instead.
(f: foldl (fs: args: concatMap (f: map f args) fs) [f]);
For example, the following function call:
nix-repl> lib.crossLists (x: y: x+y) [[1 2] [3 4]]
[ 4 5 5 6 ]
Can now be replaced by the following one:
nix-repl> lib.mapCartesianProduct ({x,y}: x+y) { x = [1 2]; y = [3 4]; }
[ 4 5 5 6 ]
''
(f: foldl (fs: args: concatMap (f: map f args) fs) [f]);
/** /**
Remove duplicate elements from the `list`. O(n^2) complexity. Remove duplicate elements from the `list`. O(n^2) complexity.

View File

@ -284,7 +284,7 @@ in
in in
# We will generate every possible pair of WM and DM. # We will generate every possible pair of WM and DM.
concatLists ( concatLists (
builtins.map lib.mapCartesianProduct
({dm, wm}: let ({dm, wm}: let
sessionName = "${dm.name}${optionalString (wm.name != "none") ("+" + wm.name)}"; sessionName = "${dm.name}${optionalString (wm.name != "none") ("+" + wm.name)}";
script = xsession dm wm; script = xsession dm wm;
@ -312,7 +312,7 @@ in
providedSessions = [ sessionName ]; providedSessions = [ sessionName ];
}) })
) )
(cartesianProductOfSets { dm = dms; wm = wms; }) { dm = dms; wm = wms; }
); );
}; };

View File

@ -63,16 +63,15 @@ in stdenv.mkDerivation {
runHook postCheck runHook postCheck
''; '';
meta = { meta = with lib; {
description = "Sandboxed execution environment"; description = "Sandboxed execution environment";
homepage = "https://github.com/solo5/solo5"; homepage = "https://github.com/solo5/solo5";
license = lib.licenses.isc; license = licenses.isc;
maintainers = with lib.maintainers; [ ehmry ]; maintainers = [ maintainers.ehmry ];
platforms = builtins.map ({arch, os}: "${arch}-${os}") platforms = mapCartesianProduct ({ arch, os }: "${arch}-${os}") {
(lib.cartesianProductOfSets { arch = [ "aarch64" "x86_64" ];
arch = [ "aarch64" "x86_64" ]; os = [ "freebsd" "genode" "linux" "openbsd" ];
os = [ "freebsd" "genode" "linux" "openbsd" ]; };
});
}; };
} }

View File

@ -15,14 +15,13 @@ let
''); '');
in in
builtins.listToAttrs ( builtins.listToAttrs (
map lib.mapCartesianProduct texTest
texTest {
(lib.attrsets.cartesianProductOfSets {
tex = [ "xelatex" "lualatex" ]; tex = [ "xelatex" "lualatex" ];
fonttype = [ "ttf" "otf" ]; fonttype = [ "ttf" "otf" ];
package = [ "junicode" ]; package = [ "junicode" ];
file = [ ./test.tex ]; file = [ ./test.tex ];
}) }
++ ++
[ [
(texTest { (texTest {

View File

@ -9,9 +9,8 @@ let
palette = [ "Frappe" "Latte" "Macchiato" "Mocha" ]; palette = [ "Frappe" "Latte" "Macchiato" "Mocha" ];
color = [ "Blue" "Dark" "Flamingo" "Green" "Lavender" "Light" "Maroon" "Mauve" "Peach" "Pink" "Red" "Rosewater" "Sapphire" "Sky" "Teal" "Yellow" ]; color = [ "Blue" "Dark" "Flamingo" "Green" "Lavender" "Light" "Maroon" "Mauve" "Peach" "Pink" "Red" "Rosewater" "Sapphire" "Sky" "Teal" "Yellow" ];
}; };
product = lib.attrsets.cartesianProductOfSets dimensions;
variantName = { palette, color }: (lib.strings.toLower palette) + color; variantName = { palette, color }: (lib.strings.toLower palette) + color;
variants = map variantName product; variants = lib.mapCartesianProduct variantName dimensions;
in in
stdenvNoCC.mkDerivation rec { stdenvNoCC.mkDerivation rec {
pname = "catppuccin-cursors"; pname = "catppuccin-cursors";

View File

@ -7,14 +7,13 @@ let
thickness = [ "" "Slim_" ]; # Thick or slim edges. thickness = [ "" "Slim_" ]; # Thick or slim edges.
handedness = [ "" "LH_" ]; # Right- or left-handed. handedness = [ "" "LH_" ]; # Right- or left-handed.
}; };
product = lib.cartesianProductOfSets dimensions;
variantName = variantName =
{ color, opacity, thickness, handedness }: { color, opacity, thickness, handedness }:
"${handedness}${opacity}${thickness}${color}"; "${handedness}${opacity}${thickness}${color}";
variants = variants =
# (The order of this list is already good looking enough to show in the # (The order of this list is already good looking enough to show in the
# meta.longDescription.) # meta.longDescription.)
map variantName product; lib.mapCartesianProduct variantName dimensions;
in in
stdenvNoCC.mkDerivation rec { stdenvNoCC.mkDerivation rec {
pname = "comixcursors"; pname = "comixcursors";