lib/attrsets: add concatMapAttrs#201527
Conversation
lib/attrsets.nix
Outdated
There was a problem hiding this comment.
This function is related to concatMap.
Let's first generalize concatMap. I'll use List a instead of [a].
concatMap :: (a -> List b) -> List a -> List b
-- generalize List -->
bind :: (a -> f b) -> f a -> f b
-- specialize f to Attrset -->
concatMapAttrs :: (a -> Attrset b) -> Attrset a -> Attrset bThis is a slightly different, but perhaps nicer interface.
# Example:
concatMapAttrs
(name: value: {
${name} = value;
"${name + value}" = value;
})
{ x = "a"; y = "b"; }
=> { x = "a"; xa = "a"; y = "b"; yb = "b"; }It'd be something like this, I think.
flip pipe [ (mapAttrs f) attrValues (foldl' merge {}) ]
I would think that the last function was already available somewhere.
I also think joinAttrs :: (String -> String -> String) -> Attrset (Attrset a) -> Attrset a would be a good function to have.
It could be generalized to joinAttrsRecursiveUntil as well, with (List String -> String).
Well, the join ones are just a thought, but concatMapAttrs will make this PR a bit cleaner.
There was a problem hiding this comment.
I'm not sure if bind is possible with nix's type system, maybe we just need something like foldMap
There was a problem hiding this comment.
bind was just to illustrate the transformation. Might have been a bit too elaborate.
|
Please also add one or two test cases to make sure we won't accidentally invert the priority of attributes in the case of colliding names. |
|
I changed |
5645d17 to
f3af2dc
Compare
|
EDIT: Comment turned into issue |
Description of changes
concatMapAttrs: Map each attribute in the given set and merge them into a new attribute set.
Things done
sandbox = trueset innix.conf? (See Nix manual)nix-shell -p nixpkgs-review --run "nixpkgs-review rev HEAD". Note: all changes have to be committed, also see nixpkgs-review usage./result/bin/)nixos/doc/manual/md-to-db.shto update generated release notes