about summary refs log tree commit diff
path: root/lib/attrs.nix
diff options
context:
space:
mode:
authorsefidel <contact@sefidel.net>2023-03-29 20:54:19 +0900
committersefidel <contact@sefidel.net>2023-04-03 18:32:29 +0900
commitce06f43476863da90dc60dcee606d2b6c5a89a8e (patch)
tree5d14946330cb09ff0ebd97bee59407fccee4d860 /lib/attrs.nix
downloadinfra-ce06f43476863da90dc60dcee606d2b6c5a89a8e.tar.gz
infra-ce06f43476863da90dc60dcee606d2b6c5a89a8e.zip
project: initial commit
Diffstat (limited to 'lib/attrs.nix')
-rw-r--r--lib/attrs.nix26
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);
+}