summary refs log tree commit diff
path: root/lib
diff options
context:
space:
mode:
authorpolykernel <81340136+polykernel@users.noreply.github.com>2021-12-27 17:53:15 -0500
committerpolykernel <81340136+polykernel@users.noreply.github.com>2021-12-27 22:24:28 -0500
commit63ce7d3184bf841da8239d8a78e7e9c931022a5c (patch)
treeae2d54ceebbe31e6ef689f1488f400df5f8fc32d /lib
parenta54f2231c9c4ea9e456511e855df11fe7df5ba71 (diff)
downloadnixpkgs-63ce7d3184bf841da8239d8a78e7e9c931022a5c.tar
nixpkgs-63ce7d3184bf841da8239d8a78e7e9c931022a5c.tar.gz
nixpkgs-63ce7d3184bf841da8239d8a78e7e9c931022a5c.tar.bz2
nixpkgs-63ce7d3184bf841da8239d8a78e7e9c931022a5c.tar.lz
nixpkgs-63ce7d3184bf841da8239d8a78e7e9c931022a5c.tar.xz
nixpkgs-63ce7d3184bf841da8239d8a78e7e9c931022a5c.tar.zst
nixpkgs-63ce7d3184bf841da8239d8a78e7e9c931022a5c.zip
lib/attrset: miscellaneous optimizations
- Eta reduce `mapAttrsRecursiveCond`, `foldAttrs`, `getAttrFromPath`.
- Modify `matchAttrs` to use `elemAt` instead of `head (tail xs)` to access
  elements.
- Modify `matchAttrs` to use `any id` instead of `foldr and true`.
Diffstat (limited to 'lib')
-rw-r--r--lib/attrsets.nix20
1 files changed, 10 insertions, 10 deletions
diff --git a/lib/attrsets.nix b/lib/attrsets.nix
index 4e88601dbd3..9e47304959a 100644
--- a/lib/attrsets.nix
+++ b/lib/attrsets.nix
@@ -3,9 +3,9 @@
 
 let
   inherit (builtins) head tail length;
-  inherit (lib.trivial) and;
+  inherit (lib.trivial) id;
   inherit (lib.strings) concatStringsSep sanitizeDerivationName;
-  inherit (lib.lists) foldr foldl' concatMap concatLists elemAt;
+  inherit (lib.lists) foldr foldl' concatMap concatLists elemAt all;
 in
 
 rec {
@@ -73,9 +73,9 @@ rec {
        getAttrFromPath ["z" "z"] x
        => error: cannot find attribute `z.z'
   */
-  getAttrFromPath = attrPath: set:
+  getAttrFromPath = attrPath:
     let errorMsg = "cannot find attribute `" + concatStringsSep "." attrPath + "'";
-    in attrByPath attrPath (abort errorMsg) set;
+    in attrByPath attrPath (abort errorMsg);
 
 
   /* Return the specified attributes from a set.
@@ -154,12 +154,12 @@ rec {
        foldAttrs (n: a: [n] ++ a) [] [{ a = 2; } { a = 3; }]
        => { a = [ 2 3 ]; }
   */
-  foldAttrs = op: nul: list_of_attrs:
+  foldAttrs = op: nul:
     foldr (n: a:
         foldr (name: o:
           o // { ${name} = op n.${name} (a.${name} or nul); }
         ) a (attrNames n)
-    ) {} list_of_attrs;
+    ) {};
 
 
   /* Recursively collect sets that verify a given predicate named `pred'
@@ -295,14 +295,14 @@ rec {
   */
   mapAttrsRecursiveCond = cond: f: set:
     let
-      recurse = path: set:
+      recurse = path:
         let
           g =
             name: value:
             if isAttrs value && cond value
               then recurse (path ++ [name]) value
               else f (path ++ [name]) value;
-        in mapAttrs g set;
+        in mapAttrs g;
     in recurse [] set;
 
 
@@ -455,8 +455,8 @@ rec {
        => true
    */
   matchAttrs = pattern: attrs: assert isAttrs pattern;
-    foldr and true (attrValues (zipAttrsWithNames (attrNames pattern) (n: values:
-      let pat = head values; val = head (tail values); in
+    all id (attrValues (zipAttrsWithNames (attrNames pattern) (n: values:
+      let pat = head values; val = elemAt values 1; in
       if length values == 1 then false
       else if isAttrs pat then isAttrs val && matchAttrs pat val
       else pat == val