-
-
Notifications
You must be signed in to change notification settings - Fork 14.5k
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -651,6 +651,20 @@ rec { | |
f: | ||
listToAttrs (map (n: nameValuePair n (f n)) names); | ||
|
||
/* Generate an attribute set by mapping over a list of items | ||
Taking two functions, one to apply for the name and one to | ||
apply for the value of the attribute set. | ||
|
||
Example: | ||
genAttrs' [ {a = 2; b.c = "a" } {a = 1; b.c = "b"} ] | ||
(i: i.b.c) (i: i.a) | ||
=> { 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 commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yet another alternative is
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 commentThe reason will be displayed to describe this comment to others. Learn more. Whatever
There's probably a more related function I'm not thinking of. |
||
*/ | ||
genAttrs' = items: f: g: | ||
listToAttrs (map (i: nameValuePair (f i) (g i)) items); | ||
|
||
/* Check whether the argument is a derivation. Any set with | ||
`{ type = "derivation"; }` counts as a derivation. | ||
|
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
andb
, e.g.: