From c782c20a4a965fb491b7d9491d27ab34d2babe92 Mon Sep 17 00:00:00 2001 From: Gabriel Arazas Date: Thu, 12 Sep 2024 15:46:45 +0800 Subject: [PATCH] lib/math: init subset Just copied over from Bahaghari lul --- lib/default.nix | 3 ++- lib/math.nix | 37 +++++++++++++++++++++++++++++++++++++ tests/lib/default.nix | 1 + tests/lib/math.nix | 34 ++++++++++++++++++++++++++++++++++ 4 files changed, 74 insertions(+), 1 deletion(-) create mode 100644 lib/math.nix create mode 100644 tests/lib/math.nix diff --git a/lib/default.nix b/lib/default.nix index 70ce7029..ff2b993c 100644 --- a/lib/default.nix +++ b/lib/default.nix @@ -17,11 +17,12 @@ pkgs.lib.makeExtensible builders = callLib ./builders; trivial = callLib ./trivial.nix; data = callLib ./data.nix; + math = callLib ./math.nix; fetchers = callLib ./fetchers; inherit (self.builders) makeXDGMimeAssociationList makeXDGPortalConfiguration makeXDGDesktopEntry; - inherit (self.trivial) countAttrs; + inherit (self.trivial) countAttrs filterAttrs'; inherit (self.data) importYAML renderTeraTemplate renderMustacheTemplate; inherit (self.fetchers) fetchInternetArchive; } // lib.optionalAttrs (builtins ? fetchTree) { diff --git a/lib/math.nix b/lib/math.nix new file mode 100644 index 00000000..ef868753 --- /dev/null +++ b/lib/math.nix @@ -0,0 +1,37 @@ +# Math subset. +{ pkgs, lib, self }: + +rec { + /* Returns the absolute value of the given number. + + Example: + abs -4 + => 4 + + abs (1 / 5) + => 0.2 + */ + abs = number: + if number < 0 then -(number) else number; + /* Exponentiates the given base with the exponent. + + Example: + pow 2 3 + => 8 + + pow 6 4 + => 1296 + */ + pow = base: exponent: + # Just to be a contrarian, I'll just make this as a tail recursive function + # instead lol. + let + absValue = abs exponent; + iter = product: counter: maxCount: + if counter > maxCount + then product + else iter (product * base) (counter + 1) maxCount; + value = iter 1 1 absValue; + in + if exponent < 0 then (1 / value) else value; +} diff --git a/tests/lib/default.nix b/tests/lib/default.nix index 25c138a7..4ce462b6 100644 --- a/tests/lib/default.nix +++ b/tests/lib/default.nix @@ -20,6 +20,7 @@ in builders = callLib ./builders.nix; trivial = callLib ./trivial.nix; data = callLib ./data; + math = callLib ./math.nix; # Environment-specific subset. home-manager = callLib ./home-manager.nix; diff --git a/tests/lib/math.nix b/tests/lib/math.nix new file mode 100644 index 00000000..32ec4d14 --- /dev/null +++ b/tests/lib/math.nix @@ -0,0 +1,34 @@ +{ pkgs, lib, self }: + + +lib.runTests { + testMathAbsoluteValue = { + expr = self.math.abs 5493; + expected = 5493; + }; + + testMathAbsoluteValue2 = { + expr = self.math.abs (-435354); + expected = 435354; + }; + + testMathPowPositive = { + expr = self.math.pow 2 8; + expected = 256; + }; + + testMathPowNegative = { + expr = self.math.pow 2.0 (-1); + expected = 0.5; + }; + + testMathPowZero = { + expr = self.math.pow 31 0; + expected = 1; + }; + + testsMathPowWithFloat = { + expr = self.math.pow 2 7.0; + expected = 128.0; + }; +}