bahaghari/lib: refactor math subset

This commit is contained in:
Gabriel Arazas 2024-06-15 19:54:57 +08:00
parent f7a9452ee5
commit e1848def6d
No known key found for this signature in database
GPG Key ID: 62104B43D00AA360

View File

@ -44,13 +44,14 @@ rec {
=> 1296 => 1296
*/ */
pow = base: exponent: pow = base: exponent:
# I'll just make this as a tail recursive function instead. # Just to be a contrarian, I'll just make this as a tail recursive function
# instead lol.
let let
absValue = abs exponent; absValue = abs exponent;
iter = product: counter: max-count: iter = product: counter: maxCount:
if counter > max-count if counter > maxCount
then product then product
else iter (product * base) (counter + 1) max-count; else iter (product * base) (counter + 1) maxCount;
value = iter 1 1 absValue; value = iter 1 1 absValue;
in in
if exponent < 0 then (1 / value) else value; if exponent < 0 then (1 / value) else value;
@ -179,10 +180,7 @@ rec {
=> 1 => 1
*/ */
grow' = min: max: value: number: grow' = min: max: value: number:
let self.trivial.clamp min max (grow number value);
res = grow number value;
in
lib.min max (lib.max res min);
/* Given a number, return its value by the given percentage. /* Given a number, return its value by the given percentage.
@ -220,8 +218,7 @@ rec {
round 2.7 round 2.7
=> 3 => 3
*/ */
round = number: round = round' 0;
round' 0 number;
/* Given a tens place (10 ^ n) and a number, round the nearest integer to its /* Given a tens place (10 ^ n) and a number, round the nearest integer to its
given place. given place.