lib: Use arithmetic operators rather than builtins.add etc.

+20 -28
lib/lists.nix
···
with import ./trivial.nix;
-
let
-
-
inc = builtins.add 1;
-
-
dec = n: builtins.sub n 1;
-
-
in rec {
+
rec {
inherit (builtins) head tail length isList elemAt concatLists filter elem;
···
fold' = n:
if n == len
then nul
-
else op (elemAt list n) (fold' (inc n));
+
else op (elemAt list n) (fold' (n + 1));
in fold' 0;
# Left fold: `fold op nul [x_1 x_2 ... x_n] == op (... (op (op nul
···
let
len = length list;
foldl' = n:
-
if n == minus1
+
if n == -1
then nul
-
else op (foldl' (dec n)) (elemAt list n);
-
in foldl' (dec (length list));
-
-
minus1 = dec 0;
+
else op (foldl' (n - 1)) (elemAt list n);
+
in foldl' (length list - 1);
# map with index: `imap (i: v: "${v}-${toString i}") ["a" "b"] ==
···
imap' = n:
if n == len
then []
-
else [ (f (inc n) (elemAt list n)) ] ++ imap' (inc n);
+
else [ (f (n + 1) (elemAt list n)) ] ++ imap' (n + 1);
in imap' 0;
···
# Count how many times function `pred' returns true for the elements
# of `list'.
-
count = pred: fold (x: c: if pred x then inc c else c) 0;
+
count = pred: fold (x: c: if pred x then c + 1 else c) 0;
# Return a singleton list or an empty list, depending on a boolean
···
# Return a list of integers from `first' up to and including `last'.
range = first: last:
-
if lessThan last first
+
if last < first
then []
-
else [first] ++ range (add first 1) last;
+
else [first] ++ range (first + 1) last;
# Partition the elements of a list in two lists, `right' and
···
let
len1 = length fst;
len2 = length snd;
-
len = if lessThan len1 len2 then len1 else len2;
+
len = if len1 < len2 then len1 else len2;
zipListsWith' = n:
if n != len then
[ (f (elemAt fst n) (elemAt snd n)) ]
-
++ zipListsWith' (inc n)
+
++ zipListsWith' (n + 1)
else [];
in zipListsWith' 0;
···
let
len = length list;
first = head list;
-
pivot' = n: acc@{ left, right }: let el = elemAt list n; next = pivot' (inc n); in
+
pivot' = n: acc@{ left, right }: let el = elemAt list n; next = pivot' (n + 1); in
if n == len
then acc
else if strictLess first el
···
next { left = [ el ] ++ left; inherit right; };
pivot = pivot' 1 { left = []; right = []; };
in
-
if lessThan len 2 then list
+
if len < 2 then list
else (sort strictLess pivot.left) ++ [ first ] ++ (sort strictLess pivot.right);
···
if n == len || n == count
then []
else
-
[ (elemAt list n) ] ++ take' (inc n);
+
[ (elemAt list n) ] ++ take' (n + 1);
in take' 0;
···
let
len = length list;
drop' = n:
-
if n == minus1 || lessThan n count
+
if n == -1 || n < count
then []
else
-
drop' (dec n) ++ [ (elemAt list n) ];
-
in drop' (dec len);
+
drop' (n - 1) ++ [ (elemAt list n) ];
+
in drop' (len - 1);
# Return the last element of a list.
last = list:
-
assert list != []; elemAt list (dec (length list));
+
assert list != []; elemAt list (length list - 1);
# Return all elements but the last
···
let
len1 = length xs;
len2 = length ys;
-
len = if lessThan len1 len2 then len1 else len2;
+
len = if len1 < len2 then len1 else len2;
zipTwoLists' = n:
if n != len then
[ { first = elemAt xs n; second = elemAt ys n; } ]
-
++ zipTwoLists' (inc n)
+
++ zipTwoLists' (n + 1)
else [];
in zipTwoLists' 0;
+1 -1
lib/modules.nix
···
let
defaultPrio = 100;
getPrio = def: if def.value._type or "" == "override" then def.value.priority else defaultPrio;
-
min = x: y: if builtins.lessThan x y then x else y;
+
min = x: y: if x < y then x else y;
highestPrio = fold (def: prio: min (getPrio def) prio) 9999 defs;
strip = def: if def.value._type or "" == "override" then def // { value = def.value.content; } else def;
in concatMap (def: if getPrio def == highestPrio then [(strip def)] else []) defs;
+8 -8
lib/strings.nix
···
let lib = import ./default.nix;
-
inherit (builtins) add sub lessThan length;
+
inherit (builtins) length;
in
···
stringToCharacters = s: let l = stringLength s; in
if l == 0
then []
-
else map (p: substring p 1 s) (lib.range 0 (sub l 1));
+
else map (p: substring p 1 s) (lib.range 0 (l - 1));
# Manipulate a string charcater by character and replace them by strings
···
toUpper = replaceChars lowerChars upperChars;
# Appends string context from another string
-
addContextFrom = a: b: (substring 0 0 a)+b;
+
addContextFrom = a: b: substring 0 0 a + b;
# Compares strings not requiring context equality
# Obviously, a workaround but works on all Nix versions
···
s = addContextFrom _sep _s;
sepLen = stringLength sep;
sLen = stringLength s;
-
lastSearch = sub sLen sepLen;
+
lastSearch = sLen - sepLen;
startWithSep = startAt:
substring startAt sepLen s == sep;
recurse = index: startAt:
-
let cutUntil = i: [(substring startAt (sub i startAt) s)]; in
-
if lessThan index lastSearch then
+
let cutUntil = i: [(substring startAt (i - startAt) s)]; in
+
if index < lastSearch then
if startWithSep index then
-
let restartAt = add index sepLen; in
+
let restartAt = index + sepLen; in
cutUntil index ++ recurse restartAt restartAt
else
-
recurse (add index 1) startAt
+
recurse (index + 1) startAt
else
cutUntil sLen;
in
+1 -1
lib/systems.nix
···
isCpuType = x: isType "cpu-type" x
&& elem x.bits [8 16 32 64 128]
-
&& (builtins.lessThan 8 x.bits -> isSignificantByte x.significantByte);
+
&& (8 < x.bits -> isSignificantByte x.significantByte);
cpuTypes = with significantBytes;
setTypes "cpu-type" {