-
-
Notifications
You must be signed in to change notification settings - Fork 14.4k
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
base: master
Are you sure you want to change the base?
lib/attrsets: add genAttrs'
#270127
Conversation
@ofborg eval |
genAttrs' [ {a = 2; b.c = "a" } {a = 1; b.c = "b"} ] | ||
(i: i.b.c) (i: i.a) |
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.
Don't overload a
and b
, e.g.:
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 |
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.
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
.
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.
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;
})
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.
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.
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: #266103And I've added types and tests.
I've also added a test for genAttrs, which seemed missing.
Things done
nix.conf
? (See Nix manual)sandbox = relaxed
sandbox = true
nix-shell -p nixpkgs-review --run "nixpkgs-review rev HEAD"
. Note: all changes have to be committed, also see nixpkgs-review usage./result/bin/
)Priorities
Add a 👍 reaction to pull requests you find important.