diff options
author | figsoda <figsoda@pm.me> | 2022-11-16 12:20:38 -0500 |
---|---|---|
committer | figsoda <figsoda@pm.me> | 2022-11-17 10:41:53 -0500 |
commit | f993f8a18659bb15bc5697b6875caf0cba8b1825 (patch) | |
tree | d671b3289bac8aaad0c867096808d99b338dd208 /lib | |
parent | 4536ebad69010f41df3c67c18bff3d85513faf86 (diff) | |
download | nixlib-f993f8a18659bb15bc5697b6875caf0cba8b1825.tar nixlib-f993f8a18659bb15bc5697b6875caf0cba8b1825.tar.gz nixlib-f993f8a18659bb15bc5697b6875caf0cba8b1825.tar.bz2 nixlib-f993f8a18659bb15bc5697b6875caf0cba8b1825.tar.lz nixlib-f993f8a18659bb15bc5697b6875caf0cba8b1825.tar.xz nixlib-f993f8a18659bb15bc5697b6875caf0cba8b1825.tar.zst nixlib-f993f8a18659bb15bc5697b6875caf0cba8b1825.zip |
lib/attrsets: add concatMapAttrs
Diffstat (limited to 'lib')
-rw-r--r-- | lib/attrsets.nix | 21 | ||||
-rw-r--r-- | lib/default.nix | 2 | ||||
-rw-r--r-- | lib/tests/misc.nix | 17 |
3 files changed, 38 insertions, 2 deletions
diff --git a/lib/attrsets.nix b/lib/attrsets.nix index 0a4c3c8ebcf4..8b5c0ef4cea6 100644 --- a/lib/attrsets.nix +++ b/lib/attrsets.nix @@ -3,7 +3,7 @@ let inherit (builtins) head tail length; - inherit (lib.trivial) id; + inherit (lib.trivial) flip id mergeAttrs pipe; inherit (lib.strings) concatStringsSep concatMapStringsSep escapeNixIdentifier sanitizeDerivationName; inherit (lib.lists) foldr foldl' concatMap concatLists elemAt all partition groupBy take foldl; in @@ -77,6 +77,25 @@ rec { let errorMsg = "cannot find attribute `" + concatStringsSep "." attrPath + "'"; in attrByPath attrPath (abort errorMsg); + /* Map each attribute in the given set and merge them into a new attribute set. + + Type: + concatMapAttrs :: + (String -> a -> AttrSet) + -> AttrSet + -> AttrSet + + Example: + concatMapAttrs + (name: value: { + ${name} = value; + ${name + value} = value; + }) + { x = "a"; y = "b"; } + => { x = "a"; xa = "a"; y = "b"; yb = "b"; } + */ + concatMapAttrs = f: flip pipe [ (mapAttrs f) attrValues (foldl' mergeAttrs { }) ]; + /* Update or set specific paths of an attribute set. diff --git a/lib/default.nix b/lib/default.nix index 8bb06954518b..cc4bedc5869b 100644 --- a/lib/default.nix +++ b/lib/default.nix @@ -78,7 +78,7 @@ let inherit (self.attrsets) attrByPath hasAttrByPath setAttrByPath getAttrFromPath attrVals attrValues getAttrs catAttrs filterAttrs filterAttrsRecursive foldAttrs collect nameValuePair mapAttrs - mapAttrs' mapAttrsToList mapAttrsRecursive mapAttrsRecursiveCond + mapAttrs' mapAttrsToList concatMapAttrs mapAttrsRecursive mapAttrsRecursiveCond genAttrs isDerivation toDerivation optionalAttrs zipAttrsWithNames zipAttrsWith zipAttrs recursiveUpdateUntil recursiveUpdate matchAttrs overrideExisting showAttrPath getOutput getBin diff --git a/lib/tests/misc.nix b/lib/tests/misc.nix index 31c938a8ffda..b73da4f1010d 100644 --- a/lib/tests/misc.nix +++ b/lib/tests/misc.nix @@ -478,6 +478,23 @@ runTests { # ATTRSETS + testConcatMapAttrs = { + expr = concatMapAttrs + (name: value: { + ${name} = value; + ${name + value} = value; + }) + { + foo = "bar"; + foobar = "baz"; + }; + expected = { + foo = "bar"; + foobar = "baz"; + foobarbaz = "baz"; + }; + }; + # code from the example testRecursiveUpdateUntil = { expr = recursiveUpdateUntil (path: l: r: path == ["foo"]) { |