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

This commit is contained in:
Eelco Dolstra 2014-10-04 17:02:29 +02:00
parent 65242d4a74
commit 6c2bf141cf
4 changed files with 30 additions and 38 deletions

View File

@ -2,13 +2,7 @@
with import ./trivial.nix; with import ./trivial.nix;
let rec {
inc = builtins.add 1;
dec = n: builtins.sub n 1;
in rec {
inherit (builtins) head tail length isList elemAt concatLists filter elem; inherit (builtins) head tail length isList elemAt concatLists filter elem;
@ -29,7 +23,7 @@ in rec {
fold' = n: fold' = n:
if n == len if n == len
then nul then nul
else op (elemAt list n) (fold' (inc n)); else op (elemAt list n) (fold' (n + 1));
in fold' 0; in fold' 0;
# Left fold: `fold op nul [x_1 x_2 ... x_n] == op (... (op (op nul # Left fold: `fold op nul [x_1 x_2 ... x_n] == op (... (op (op nul
@ -38,12 +32,10 @@ in rec {
let let
len = length list; len = length list;
foldl' = n: foldl' = n:
if n == minus1 if n == -1
then nul then nul
else op (foldl' (dec n)) (elemAt list n); else op (foldl' (n - 1)) (elemAt list n);
in foldl' (dec (length list)); in foldl' (length list - 1);
minus1 = dec 0;
# map with index: `imap (i: v: "${v}-${toString i}") ["a" "b"] == # map with index: `imap (i: v: "${v}-${toString i}") ["a" "b"] ==
@ -54,7 +46,7 @@ in rec {
imap' = n: imap' = n:
if n == len if n == len
then [] then []
else [ (f (inc n) (elemAt list n)) ] ++ imap' (inc n); else [ (f (n + 1) (elemAt list n)) ] ++ imap' (n + 1);
in imap' 0; in imap' 0;
@ -104,7 +96,7 @@ in rec {
# Count how many times function `pred' returns true for the elements # Count how many times function `pred' returns true for the elements
# of `list'. # 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 singleton list or an empty list, depending on a boolean
@ -125,9 +117,9 @@ in rec {
# Return a list of integers from `first' up to and including `last'. # Return a list of integers from `first' up to and including `last'.
range = first: last: range = first: last:
if lessThan last first if last < first
then [] 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 # Partition the elements of a list in two lists, `right' and
@ -144,11 +136,11 @@ in rec {
let let
len1 = length fst; len1 = length fst;
len2 = length snd; len2 = length snd;
len = if lessThan len1 len2 then len1 else len2; len = if len1 < len2 then len1 else len2;
zipListsWith' = n: zipListsWith' = n:
if n != len then if n != len then
[ (f (elemAt fst n) (elemAt snd n)) ] [ (f (elemAt fst n) (elemAt snd n)) ]
++ zipListsWith' (inc n) ++ zipListsWith' (n + 1)
else []; else [];
in zipListsWith' 0; in zipListsWith' 0;
@ -167,7 +159,7 @@ in rec {
let let
len = length list; len = length list;
first = head 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 if n == len
then acc then acc
else if strictLess first el else if strictLess first el
@ -176,7 +168,7 @@ in rec {
next { left = [ el ] ++ left; inherit right; }; next { left = [ el ] ++ left; inherit right; };
pivot = pivot' 1 { left = []; right = []; }; pivot = pivot' 1 { left = []; right = []; };
in in
if lessThan len 2 then list if len < 2 then list
else (sort strictLess pivot.left) ++ [ first ] ++ (sort strictLess pivot.right); else (sort strictLess pivot.left) ++ [ first ] ++ (sort strictLess pivot.right);
@ -188,7 +180,7 @@ in rec {
if n == len || n == count if n == len || n == count
then [] then []
else else
[ (elemAt list n) ] ++ take' (inc n); [ (elemAt list n) ] ++ take' (n + 1);
in take' 0; in take' 0;
@ -197,16 +189,16 @@ in rec {
let let
len = length list; len = length list;
drop' = n: drop' = n:
if n == minus1 || lessThan n count if n == -1 || n < count
then [] then []
else else
drop' (dec n) ++ [ (elemAt list n) ]; drop' (n - 1) ++ [ (elemAt list n) ];
in drop' (dec len); in drop' (len - 1);
# Return the last element of a list. # Return the last element of a list.
last = list: last = list:
assert list != []; elemAt list (dec (length list)); assert list != []; elemAt list (length list - 1);
# Return all elements but the last # Return all elements but the last
@ -218,11 +210,11 @@ in rec {
let let
len1 = length xs; len1 = length xs;
len2 = length ys; len2 = length ys;
len = if lessThan len1 len2 then len1 else len2; len = if len1 < len2 then len1 else len2;
zipTwoLists' = n: zipTwoLists' = n:
if n != len then if n != len then
[ { first = elemAt xs n; second = elemAt ys n; } ] [ { first = elemAt xs n; second = elemAt ys n; } ]
++ zipTwoLists' (inc n) ++ zipTwoLists' (n + 1)
else []; else [];
in zipTwoLists' 0; in zipTwoLists' 0;

View File

@ -278,7 +278,7 @@ rec {
let let
defaultPrio = 100; defaultPrio = 100;
getPrio = def: if def.value._type or "" == "override" then def.value.priority else defaultPrio; 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; 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; 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; in concatMap (def: if getPrio def == highestPrio then [(strip def)] else []) defs;

View File

@ -2,7 +2,7 @@
let lib = import ./default.nix; let lib = import ./default.nix;
inherit (builtins) add sub lessThan length; inherit (builtins) length;
in in
@ -79,7 +79,7 @@ rec {
stringToCharacters = s: let l = stringLength s; in stringToCharacters = s: let l = stringLength s; in
if l == 0 if l == 0
then [] 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 # Manipulate a string charcater by character and replace them by strings
@ -123,7 +123,7 @@ rec {
toUpper = replaceChars lowerChars upperChars; toUpper = replaceChars lowerChars upperChars;
# Appends string context from another string # 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 # Compares strings not requiring context equality
# Obviously, a workaround but works on all Nix versions # Obviously, a workaround but works on all Nix versions
@ -139,18 +139,18 @@ rec {
s = addContextFrom _sep _s; s = addContextFrom _sep _s;
sepLen = stringLength sep; sepLen = stringLength sep;
sLen = stringLength s; sLen = stringLength s;
lastSearch = sub sLen sepLen; lastSearch = sLen - sepLen;
startWithSep = startAt: startWithSep = startAt:
substring startAt sepLen s == sep; substring startAt sepLen s == sep;
recurse = index: startAt: recurse = index: startAt:
let cutUntil = i: [(substring startAt (sub i startAt) s)]; in let cutUntil = i: [(substring startAt (i - startAt) s)]; in
if lessThan index lastSearch then if index < lastSearch then
if startWithSep index then if startWithSep index then
let restartAt = add index sepLen; in let restartAt = index + sepLen; in
cutUntil index ++ recurse restartAt restartAt cutUntil index ++ recurse restartAt restartAt
else else
recurse (add index 1) startAt recurse (index + 1) startAt
else else
cutUntil sLen; cutUntil sLen;
in in

View File

@ -24,7 +24,7 @@ rec {
isCpuType = x: isType "cpu-type" x isCpuType = x: isType "cpu-type" x
&& elem x.bits [8 16 32 64 128] && 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; cpuTypes = with significantBytes;
setTypes "cpu-type" { setTypes "cpu-type" {