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 more ways to map over/to attrsets #197004

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 33 additions & 1 deletion lib/attrsets.nix
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ let
inherit (builtins) head tail length;
inherit (lib.trivial) flip id mergeAttrs pipe;
inherit (lib.strings) concatStringsSep concatMapStringsSep escapeNixIdentifier sanitizeDerivationName;
inherit (lib.lists) foldr foldl' concatMap concatLists elemAt all partition groupBy take foldl;
inherit (lib.lists) foldr foldl' concatMap concatLists elemAt all partition groupBy take foldl imap0;
in

rec {
Expand Down Expand Up @@ -518,6 +518,27 @@ rec {
set:
listToAttrs (map (attr: f attr set.${attr}) (attrNames set));

/* Like mapAttrs, but additionally taking a variable that
will be automatically incremented, starting from 0.
Example:
imapAttrs (i: name: value: value + i)
{ a = 1; b = 1; c = 1; }
=> { a = 1; b = 2; c = 3; }
*/
imapAttrs = f: set:
listToAttrs (imap0 (i: attr: nameValuePair attr (f i attr set.${attr})) (attrNames set));

/* Like mapAttrs', but additionally taking a variable that
will be automatically incremented, starting from 0.
Example:
imapAttrs' (i: name: value: nameValuePair "${name}${toString i}" value + i)
Copy link
Contributor

Choose a reason for hiding this comment

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

Both functions have the same signature? If so, what is the difference between them?

Copy link
Member Author

Choose a reason for hiding this comment

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

they apply f in different places, see the examples:

imapAttrs has a built in nameValuePair, calling f on just the value, you can't change the names from the input attrset.
imapAttrs' wants a function that provides a nameValuePair, so you can modify both the name and the value of the input attrset.

{ a = 2; b = 3; }
=> { a0 = 2; b1 = 4 }
*/
imapAttrs' = f: set:
listToAttrs (imap0 (i: attr: f i attr set.${attr}) (attrNames set));

/* Call a function for each attribute in the given set and return
the result in a list.
Expand Down Expand Up @@ -617,6 +638,17 @@ 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; }
*/
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.
Expand Down
5 changes: 3 additions & 2 deletions lib/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,9 @@ let
inherit (self.attrsets) attrByPath hasAttrByPath setAttrByPath
getAttrFromPath attrVals attrValues getAttrs catAttrs filterAttrs
filterAttrsRecursive foldlAttrs foldAttrs collect nameValuePair mapAttrs
mapAttrs' mapAttrsToList concatMapAttrs mapAttrsRecursive mapAttrsRecursiveCond
genAttrs isDerivation toDerivation optionalAttrs
mapAttrs' imapAttrs imapAttrs' mapAttrsToList
concatMapAttrs mapAttrsRecursive mapAttrsRecursiveCond
genAttrs genAttrs' isDerivation toDerivation optionalAttrs
zipAttrsWithNames zipAttrsWith zipAttrs recursiveUpdateUntil
recursiveUpdate matchAttrs overrideExisting showAttrPath getOutput getBin
getLib getDev getMan chooseDevOutputs zipWithNames zip
Expand Down