Skip to content
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

Merged
merged 4 commits into from
Nov 21, 2022
Merged

lib/attrsets: add concatMapAttrs #201527

merged 4 commits into from
Nov 21, 2022

Conversation

figsoda
Copy link
Member

@figsoda figsoda commented Nov 16, 2022

Description of changes

concatMapAttrs: Map each attribute in the given set and merge them into a new attribute set.

concatMapAttrs ::
  (String -> a -> AttrSet)
  -> AttrSet
  -> AttrSet
Things done
  • Built on platform(s)
    • x86_64-linux
    • aarch64-linux
    • x86_64-darwin
    • aarch64-darwin
  • For non-Linux: Is sandbox = true set in nix.conf? (See Nix manual)
  • Tested, as applicable:
  • Tested compilation of all packages that depend on this change using nix-shell -p nixpkgs-review --run "nixpkgs-review rev HEAD". Note: all changes have to be committed, also see nixpkgs-review usage
  • Tested basic functionality of all binary files (usually in ./result/bin/)
  • 22.11 Release Notes (or backporting 22.05 Release notes)
    • (Package updates) Added a release notes entry if the change is major or breaking
    • (Module updates) Added a release notes entry if the change is significant
    • (Module addition) Added a release notes entry if adding a new NixOS module
    • (Release notes changes) Ran nixos/doc/manual/md-to-db.sh to update generated release notes
  • Fits CONTRIBUTING.md.

@github-actions github-actions bot added 6.topic: nixos Issues or PRs affecting NixOS modules, or package usability issues specific to NixOS 6.topic: vim 8.has: module (update) This PR changes an existing module in `nixos/` labels Nov 16, 2022
@ofborg ofborg bot added 10.rebuild-darwin: 0 This PR does not cause any packages to rebuild on Darwin 10.rebuild-linux: 1-10 labels Nov 16, 2022
lib/attrsets.nix Outdated
=> { x = "a"; xa = "a"; y = "b"; yb = "b"; }
*/
mapAttrsMulti = f: flip pipe [ (mapAttrsToList f) concatLists listToAttrs ];

Copy link
Member

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.

Copy link
Member Author

@figsoda figsoda Nov 17, 2022

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

Copy link
Member

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.

@roberth
Copy link
Member

roberth commented Nov 17, 2022

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.

@figsoda figsoda changed the title lib/attrsets: add mapAttrsMulti and filterMapAttrs lib/attrsets: add concatMapAttrs and filterMapAttrs Nov 17, 2022
@figsoda
Copy link
Member Author

figsoda commented Nov 17, 2022

I changed mapAttrsMulti to concatMapAttrs and changed filterMapAttrs to a similar definition, also added a test for each

lib/attrsets.nix Outdated Show resolved Hide resolved
@figsoda figsoda changed the title lib/attrsets: add concatMapAttrs and filterMapAttrs lib/attrsets: add concatMapAttrs Nov 17, 2022
@figsoda figsoda merged commit 91969d8 into NixOS:master Nov 21, 2022
@figsoda figsoda deleted the lib branch November 21, 2022 16:01
@roberth
Copy link
Member

roberth commented Jun 14, 2023

EDIT: Comment turned into issue

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
6.topic: nixos Issues or PRs affecting NixOS modules, or package usability issues specific to NixOS 6.topic: vim 8.has: module (update) This PR changes an existing module in `nixos/` 10.rebuild-darwin: 0 This PR does not cause any packages to rebuild on Darwin 10.rebuild-linux: 1-10
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants