1
0
Fork 1
mirror of https://github.com/NixOS/nixpkgs.git synced 2025-06-09 17:46:29 +09:00

lib: refactor lib.attrsets.{mapAttrs', mapAttrsToList} and mapAttrs'' in the strongswan-swanctl module

The new implementation of `mapAttrsToList` is simpler than the previous one, avoiding an extra string conversion. Benchmarking shows a slight performance improvement. See the discussion here: https://discourse.nixos.org/t/another-implementation-of-mapattrstolist

Additionally, I searched nixpkgs for expressions equivalent to the old `mapAttrsToList` and replaced them with direct calls to the new implementation.
This commit is contained in:
ccicnce113424 2025-05-08 13:58:54 +08:00
parent a5d4ca206b
commit 4551258940
2 changed files with 3 additions and 3 deletions

View file

@ -1042,7 +1042,7 @@ rec {
::: :::
*/ */
mapAttrs' = f: set: listToAttrs (map (attr: f attr set.${attr}) (attrNames set)); mapAttrs' = f: set: listToAttrs (mapAttrsToList f set);
/** /**
Call a function for each attribute in the given set and return Call a function for each attribute in the given set and return
@ -1076,7 +1076,7 @@ rec {
::: :::
*/ */
mapAttrsToList = f: attrs: map (name: f name attrs.${name}) (attrNames attrs); mapAttrsToList = f: attrs: attrValues (mapAttrs f attrs);
/** /**
Deconstruct an attrset to a list of name-value pairs as expected by [`builtins.listToAttrs`](https://nixos.org/manual/nix/stable/language/builtins.html#builtins-listToAttrs). Deconstruct an attrset to a list of name-value pairs as expected by [`builtins.listToAttrs`](https://nixos.org/manual/nix/stable/language/builtins.html#builtins-listToAttrs).

View file

@ -89,7 +89,7 @@ rec {
in in
recurse [ ] set; recurse [ ] set;
mapAttrs'' = f: set: foldl' (a: b: a // b) { } (map (attr: f attr set.${attr}) (attrNames set)); mapAttrs'' = f: set: foldl' (a: b: a // b) { } (mapAttrsToList f set);
# Extract the options from the given set of parameters. # Extract the options from the given set of parameters.
paramsToOptions = ps: mapParamsRecursive (_path: name: param: { ${name} = param.option; }) ps; paramsToOptions = ps: mapParamsRecursive (_path: name: param: { ${name} = param.option; }) ps;