diff options
Diffstat (limited to 'users/sterni/nix/int/default.nix')
-rw-r--r-- | users/sterni/nix/int/default.nix | 38 |
1 files changed, 13 insertions, 25 deletions
diff --git a/users/sterni/nix/int/default.nix b/users/sterni/nix/int/default.nix index 54b5596472..8707445223 100644 --- a/users/sterni/nix/int/default.nix +++ b/users/sterni/nix/int/default.nix @@ -2,37 +2,28 @@ let - # TODO(sterni): implement nix.float and figure out which of these - # functions can be split out into a common nix.num - # library. - inherit (depot.users.sterni.nix) string + num ; inherit (builtins) bitOr bitAnd bitXor - mul - div - add - sub ; - abs = i: if i < 0 then -i else i; - exp = base: pow: if pow > 0 then base * (exp base (pow - 1)) else if pow < 0 - then 1.0 / exp base (abs pow) + then 1.0 / exp base (num.abs pow) else 1; bitShiftR = bit: count: if count == 0 then bit - else div (bitShiftR bit (count - 1)) 2; + else (bitShiftR bit (count - 1)) / 2; bitShiftL = bit: count: if count == 0 @@ -52,7 +43,7 @@ let in if int == 0 then "0" - else "${sign}${go (abs int)}"; + else "${sign}${go (num.abs int)}"; fromHexMap = builtins.listToAttrs (lib.imap0 (i: c: { name = c; value = i; }) @@ -94,26 +85,24 @@ let odd = x: bitAnd x 1 == 1; even = x: bitAnd x 1 == 0; - # div and mod behave like quot and rem in Haskell, - # i. e. they truncate towards 0 - mod = a: b: let res = a / b; in a - (res * b); - - inRange = a: b: x: x >= a && x <= b; + quot' = builtins.div; # no typecheck + rem = a: b: + assert builtins.isInt a && builtins.isInt b; + let res = quot' a b; in a - (res * b); + quot = a: b: + assert builtins.isInt a && builtins.isInt b; + quot' a b; in { inherit maxBound minBound - abs exp odd even - add - sub - mul - div - mod + quot + rem bitShiftR bitShiftL bitOr @@ -121,6 +110,5 @@ in bitXor toHex fromHex - inRange ; } |