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 genAttrs' #270127

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open

Conversation

dali99
Copy link
Member

@dali99 dali99 commented Nov 26, 2023

Description of changes

As wished by @infinisil in #197004 I'm splitting each function into other PRs to merge incrementally.

Main changes from #197004 is that I'm no longer including them in lib/default.nix ref: #266103
And I've added types and tests.

I've also added a test for genAttrs, which seemed missing.

Things done

  • Built on platform(s)
    • x86_64-linux
    • aarch64-linux
    • x86_64-darwin
    • aarch64-darwin
  • For non-Linux: Is sandboxing enabled in nix.conf? (See Nix manual)
    • sandbox = relaxed
    • sandbox = true
  • 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/)
  • 23.11 Release Notes (or backporting 23.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
  • Fits CONTRIBUTING.md.

Priorities

Add a 👍 reaction to pull requests you find important.

@github-actions github-actions bot added the 6.topic: lib The Nixpkgs function library label Nov 26, 2023
@dali99 dali99 requested a review from h7x4 November 26, 2023 12:25
@dali99 dali99 changed the title lib/attrsets: add genAttrs' lib/attrsets: add genAttrs' Nov 26, 2023
@dali99
Copy link
Member Author

dali99 commented Nov 27, 2023

@ofborg eval

@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 27, 2023
Comment on lines +659 to +660
genAttrs' [ {a = 2; b.c = "a" } {a = 1; b.c = "b"} ]
(i: i.b.c) (i: i.a)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't overload a and b, e.g.:

Suggested change
genAttrs' [ {a = 2; b.c = "a" } {a = 1; b.c = "b"} ]
(i: i.b.c) (i: i.a)
genAttrs' [ {p = 2; q.r = "a" } {p = 1; q.r = "b"} ]
(i: i.q.r) (i: i.p)

=> { a = 2; b = 1; }

Type:
genAttrs' :: [ a ] -> (a -> String) -> (a -> Any) -> AttrSet
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A denotationally equivalent function would be

-genAttrs' :: [ a ] -> (a -> String) -> (a -> Any) -> AttrSet
+genAttrs' :: [ a ] -> (a -> { name :: String; value :: a}) -> AttrSet

However, operationally, the latter can use a let binding to share computation between the two attributes.
The prior can not share a variable that depends on the a.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yet another alternative is

genAttrs' :: [ a ] -> (a -> Attrs) -> Attrs

This more compact, more powerful, and you can still read that a name and value are computed.

genAttrs' [ {a = 2; b.c = "a" } {a = 1; b.c = "b"} ]
  (i: { ${i.b.c} = i.a; })

while also allowing filtering behavior:

genAttrs' [ {a = 2; b.c = "a" } {a = 1; b.c = "b"} ]
  (i: optionalAttrs (i.a > 1) { ${i.b.c} = i.a; })

or multiple keys per item

genAttrs' [ {a = 2; b.c = "a" } {a = 1; b.c = "b"} ]
  (i: {
    ${i.b.c} = i.a;
    "${i.b.c}-squared" = i.a * i.a;
  })

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Whatever genAttrs' ends up being, its documentation should refer to the other functions that are similar, and the other functions should refer back.
Some functions I'd think of

  • genAttrs
  • concatMapAttrs
  • listToAttrs

There's probably a more related function I'm not thinking of.

@wegank wegank added the 2.status: stale https://github.com/NixOS/nixpkgs/blob/master/.github/STALE-BOT.md label Jul 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
2.status: stale https://github.com/NixOS/nixpkgs/blob/master/.github/STALE-BOT.md 6.topic: lib The Nixpkgs function library 10.rebuild-darwin: 0 This PR does not cause any packages to rebuild on Darwin 10.rebuild-linux: 1-10 awaiting_changes
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants