-
-
Notifications
You must be signed in to change notification settings - Fork 14.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
lib/attrsets: add concatMapAttrs #201527
lib/attrsets: add concatMapAttrs #201527
Conversation
lib/attrsets.nix
Outdated
=> { x = "a"; xa = "a"; y = "b"; yb = "b"; } | ||
*/ | ||
mapAttrsMulti = f: flip pipe [ (mapAttrsToList f) concatLists listToAttrs ]; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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 b
This 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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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 = true
set 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.sh
to update generated release notes