diff options
author | sefidel <contact@sefidel.net> | 2024-01-23 00:20:54 +0900 |
---|---|---|
committer | sefidel <contact@sefidel.net> | 2024-01-23 01:05:04 +0900 |
commit | b311d7f8459fe1e90f7b0cbf5862b22a2cbedff5 (patch) | |
tree | d7b8af8e685699a1e66afcb10b668363d604106e /lib/attrs.nix | |
parent | b670811db1c645c9c4effff1829e90b380318427 (diff) | |
download | nixrc-b311d7f8459fe1e90f7b0cbf5862b22a2cbedff5.tar.gz nixrc-b311d7f8459fe1e90f7b0cbf5862b22a2cbedff5.zip |
feat(lib)!: refactor
Diffstat (limited to 'lib/attrs.nix')
-rw-r--r-- | lib/attrs.nix | 26 |
1 files changed, 26 insertions, 0 deletions
diff --git a/lib/attrs.nix b/lib/attrs.nix new file mode 100644 index 0000000..0f8ebd1 --- /dev/null +++ b/lib/attrs.nix @@ -0,0 +1,26 @@ +{ lib, ... }: + +with builtins; +with lib; +rec { + # attrsToList + attrsToList = attrs: + mapAttrsToList (name: value: { inherit name value; }) attrs; + + # mapFilterAttrs :: + # (name -> value -> bool) + # (name -> value -> { name = any; value = any; }) + # attrs + mapFilterAttrs = pred: f: attrs: filterAttrs pred (mapAttrs' f attrs); + + # Generate an attribute set by mapping a function over a list of values. + genAttrs' = values: f: listToAttrs (map f values); + + # anyAttrs :: (name -> value -> bool) attrs + anyAttrs = pred: attrs: + any (attr: pred attr.name attr.value) (attrsToList attrs); + + # countAttrs :: (name -> value -> bool) attrs + countAttrs = pred: attrs: + count (attr: pred attr.name attr.value) (attrsToList attrs); +} |