Skip to content

Commit

Permalink
lib: add concatMapAttrsToList
Browse files Browse the repository at this point in the history
This is the concatMap counterpart to mapAttrsToList.

Co-authored-by: Dusk Banks <[email protected]>
  • Loading branch information
9ary and bb010g committed Dec 29, 2024
1 parent d05ee73 commit b9bcf7c
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
38 changes: 38 additions & 0 deletions lib/attrsets.nix
Original file line number Diff line number Diff line change
Expand Up @@ -1094,6 +1094,44 @@ rec {
attrs:
map (name: f name attrs.${name}) (attrNames attrs);

/**
Call a function for each attribute in the given set and return
the concatenated results.
# Inputs
`f`
: A function, given an attribute's name and value, returns a list of new values.
`attrs`
: Attribute set to map over.
# Type
```
# FIXME
concatMapAttrsToList :: (String -> a -> b) -> AttrSet -> [b]
```
# Examples
:::{.example}
## `lib.attrsets.concatMapAttrsToList` usage example
```nix
concatMapAttrsToList (name: value: [ name (name + value) ])
{ x = "a"; y = "b"; }
=> [ "x" "xa" "y" "yb" ]
```
:::
*/
concatMapAttrsToList =
f:
attrs:
concatMap (name: f name attrs.${name}) (attrNames attrs);

/**
Deconstruct an attrset to a list of name-value pairs as expected by [`builtins.listToAttrs`](https://nixos.org/manual/nix/stable/language/builtins.html#builtins-listToAttrs).
Each element of the resulting list is an attribute set with these attributes:
Expand Down
2 changes: 1 addition & 1 deletion lib/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ let
inherit (self.attrsets) attrByPath hasAttrByPath setAttrByPath
getAttrFromPath attrVals attrNames attrValues getAttrs catAttrs filterAttrs
filterAttrsRecursive foldlAttrs foldAttrs collect nameValuePair mapAttrs
mapAttrs' mapAttrsToList attrsToList concatMapAttrs mapAttrsRecursive
mapAttrs' mapAttrsToList concatMapAttrsToList attrsToList concatMapAttrs mapAttrsRecursive
mapAttrsRecursiveCond genAttrs isDerivation toDerivation optionalAttrs
zipAttrsWithNames zipAttrsWith zipAttrs recursiveUpdateUntil
recursiveUpdate matchAttrs mergeAttrsList overrideExisting showAttrPath getOutput getFirstOutput
Expand Down

0 comments on commit b9bcf7c

Please sign in to comment.