add lib.math

main
Felix Stupp 1 month ago
parent c8f786cd40
commit 5b0b20b3a9
Signed by: zocker
GPG Key ID: 93E1BD26F6B02FB7

@ -8,6 +8,8 @@ libO
# groups # groups
math = (libO.math or { }) // lib.importFlakeMod ./math.nix;
modules = libO.modules // lib.importFlakeMod ./modules.nix; modules = libO.modules // lib.importFlakeMod ./modules.nix;
types = libO.types // lib.importFlakeMod ./types.nix; types = libO.types // lib.importFlakeMod ./types.nix;

@ -0,0 +1,46 @@
# inefficient, but sufficient math functions missing in Nix & nixpkgs
{ lib, ... }@flakeArg:
let
inherit (builtins) foldl' genList isInt;
inherit (lib.lists) imap0 reverseList;
inherit (lib.strings) stringToCharacters;
inherit (lib.trivial) flip pipe;
in
rec {
bitAsString = bool: if bool then "1" else "0";
binToInt = flip pipe [
stringToCharacters
reverseList
(imap0 (i: x: if x == "1" then pow 2 i else 0))
(foldl' (a: v: a + v) 0)
];
intToBin =
len: num:
assert isInt len && isInt num;
let
maxVal = pow 2 len;
twoPowers = genList (i: pow 2 (len - 1 - i)) len;
init = {
number = num;
output = "";
};
folder =
{ number, output }:
power:
let
bit = number >= power;
in
{
number = if bit then number - power else number;
output = "${output}${bitAsString bit}";
};
in
assert num < maxVal;
(foldl' folder init twoPowers).output;
pow = x: exp: foldl' (a: b: a * b) 1 (genList (_: x) exp); # TODO
}
Loading…
Cancel
Save