From 600cac3ce351d25acc0a1ae619e23ea0d9322331 Mon Sep 17 00:00:00 2001 From: Emile Clark-Boman Date: Sun, 14 Dec 2025 12:59:31 +1000 Subject: [PATCH] use `with builtins nib.types` --- nib/parse/default.nix | 13 +++++++------ nib/parse/struct.nix | 14 +++++++------- nib/types/attrs.nix | 17 +++++++++-------- nib/types/fault.nix | 5 +++-- nib/types/lists.nix | 13 +++++++------ nib/types/result.nix | 5 +++-- 6 files changed, 36 insertions(+), 31 deletions(-) diff --git a/nib/parse/default.nix b/nib/parse/default.nix index 5b24085..f636706 100644 --- a/nib/parse/default.nix +++ b/nib/parse/default.nix @@ -1,10 +1,11 @@ {nib, ...} @ args: let struct = import ./struct.nix args; in - nib.types.mergeAttrsList [ - # submodule is included directly to this module (ie self.myFunc) - struct + with nib.types; + mergeAttrsList [ + # submodule is included directly to this module (ie self.myFunc) + struct - # submodule content is accessible first by submodule name - # then by the name of the content (ie self.submodule.myFunc) - ] + # submodule content is accessible first by submodule name + # then by the name of the content (ie self.submodule.myFunc) + ] diff --git a/nib/parse/struct.nix b/nib/parse/struct.nix index 54ace2f..93feae7 100644 --- a/nib/parse/struct.nix +++ b/nib/parse/struct.nix @@ -1,23 +1,23 @@ {nib, ...}: with builtins nib.types; rec { cmpStructErr' = errBadKeys: errBadValues: path: S: T: - if builtins.isAttrs S && builtins.isAttrs T + if isAttrs S && isAttrs T then let - keysS = builtins.attrNames S; - keysT = builtins.attrNames T; + keysS = attrNames S; + keysT = attrNames T; in # ensure all key names match, then recurse if !(keysS == keysT) then errBadKeys path keysS keysT else (firstErr - (builtins.map + (map (k: cmpStructErr' errBadKeys errBadValues (path ++ [k]) (keysS.${k}) (keysT.${k})) keysS)) else # terminating leaf in recursion tree reached # ensure values' types match - (builtins.typeOf S == builtins.typeOf T) + (typeOf S == typeOf T) || errBadValues path S T; cmpStructErr = errBadKeys: errBadValues: cmpStructErr' errBadKeys errBadValues []; @@ -60,9 +60,9 @@ with builtins nib.types; rec { in errOr ({...}: Ok ( - attrs.mapAttrsRecursive ( + mapAttrsRecursive ( path: value: let - valueS = attrs.attrValueAt S path; + valueS = attrValueAt S path; in if valueS != null then valueS diff --git a/nib/types/attrs.nix b/nib/types/attrs.nix index f815bf8..9ae9000 100644 --- a/nib/types/attrs.nix +++ b/nib/types/attrs.nix @@ -1,9 +1,10 @@ -{nib, ...}: rec { +{nib, ...}: +with builtins; rec { nameValuePair = name: value: {inherit name value;}; identityAttrs = value: {${value} = value;}; - identityAttrsList = values: builtins.map (v: identityAttrs v) values; + identityAttrsList = values: map (v: identityAttrs v) values; /** Generate an attribute set by mapping a function over a list of @@ -73,13 +74,13 @@ ::: */ - genAttrs' = xs: f: builtins.listToAttrs (map f xs); + genAttrs' = xs: f: listToAttrs (map f xs); mapAttrsRecursiveCond = cond: f: set: let recurse = path: - builtins.mapAttrs ( + mapAttrs ( name: value: - if builtins.isAttrs value && cond value + if isAttrs value && cond value then recurse (path ++ [name]) value else f (path ++ [name]) value ); @@ -92,7 +93,7 @@ # given path as a list of strings, return that value of an # attribute set at that path attrValueAt = nib.types.foldl (l: r: - if l != null && builtins.hasAttr r l + if l != null && hasAttr r l then l.${r} else null); @@ -108,11 +109,11 @@ binaryMerge start (start + (end - start) / 2) // binaryMerge (start + (end - start) / 2) end else # Otherwise there will be exactly 1 element due to the invariant, in which case we just return it directly - builtins.elemAt list start; + elemAt list start; in if list == [] then # Calling binaryMerge as below would not satisfy its invariant {} - else binaryMerge 0 (builtins.length list); + else binaryMerge 0 (length list); } diff --git a/nib/types/fault.nix b/nib/types/fault.nix index b437ded..9a34889 100644 --- a/nib/types/fault.nix +++ b/nib/types/fault.nix @@ -1,4 +1,5 @@ -{...}: rec { +{...}: +with builtins; rec { # Fault Monad # Wrapper around an error (ie builtins.abort) Fault = error: { @@ -6,7 +7,7 @@ }; # Pattern Matching - isFault = F: builtins.attrNames F == ["error"]; + isFault = F: attrNames F == ["error"]; # Unwrap (Monadic Return Operation) unwrapFault = F: F.error; diff --git a/nib/types/lists.nix b/nib/types/lists.nix index a8e9ff3..8c0fd67 100644 --- a/nib/types/lists.nix +++ b/nib/types/lists.nix @@ -1,13 +1,14 @@ -{...}: rec { +{...}: +with builtins; rec { foldl = op: nul: list: let foldl' = n: if n == -1 then nul - else op (foldl' (n - 1)) (builtins.elemAt list n); + else op (foldl' (n - 1)) (elemAt list n); in - foldl' (builtins.length list - 1); + foldl' (length list - 1); - crossLists = f: foldl (fs: args: builtins.concatMap (f: map f args) fs) [f]; + crossLists = f: foldl (fs: args: concatMap (f: map f args) fs) [f]; findFirstIndex = pred: default: list: let # A naive recursive implementation would be much simpler, but @@ -23,7 +24,7 @@ # # We start with index -1 and the 0'th element of the list, which satisfies the invariant resultIndex = - builtins.foldl' ( + foldl' ( index: el: if index < 0 then @@ -50,5 +51,5 @@ in if index == null then default - else builtins.elemAt list index; + else elemAt list index; } diff --git a/nib/types/result.nix b/nib/types/result.nix index f57d1b5..fede393 100644 --- a/nib/types/result.nix +++ b/nib/types/result.nix @@ -1,4 +1,5 @@ -{nib, ...}: rec { +{nib, ...}: +with builtins; rec { # Res (Result) Monad Res = success: value: {inherit success value;}; Ok = value: Res true value; @@ -7,7 +8,7 @@ Err' = Err "err"; # Pattern Matching - isRes = R: builtins.attrNames R == ["success" "value"]; + isRes = R: attrNames R == ["success" "value"]; isOk = R: isRes R && R.success; isErr = R: isRes R && !R.success;