bahaghari: reformat codebase

This commit is contained in:
Gabriel Arazas 2024-02-29 16:12:21 +08:00
parent fb634531e4
commit e5083302b2
No known key found for this signature in database
GPG Key ID: ADE0C41DAB221FCC
3 changed files with 84 additions and 53 deletions

View File

@ -5,8 +5,24 @@
let let
glyphList = glyphList =
[ "0" "1" "2" "3" "4" "5" "6" "7" [
"8" "9" "A" "B" "C" "D" "E" "F" ]; "0"
"1"
"2"
"3"
"4"
"5"
"6"
"7"
"8"
"9"
"A"
"B"
"C"
"D"
"E"
"F"
];
baseSet = lib.generateBaseDigitType glyphList; baseSet = lib.generateBaseDigitType glyphList;
in in

View File

@ -99,58 +99,57 @@ rec {
in in
pkgs.lib.listToAttrs glyphsList'; pkgs.lib.listToAttrs glyphsList';
/* A factory function for generating an attribute set containing a glyph /* A factory function for generating an attribute set containing a glyph
set, a conversion table, and a conversion function to and from decimal. set, a conversion table, and a conversion function to and from decimal.
Accepts the same list as `generateGlyphSet` and Accepts the same list as `generateGlyphSet` and
`generateConversionTable` where it assumes it is sorted and `generateConversionTable` where it assumes it is sorted and
zero-indexed. zero-indexed.
Type: generateBaseDigitType :: [ String ] -> Attrs Type: generateBaseDigitType :: [ String ] -> Attrs
Example: Example:
generateBaseDigitType [ "0" "1" ] generateBaseDigitType [ "0" "1" ]
=> { => {
base = 2; base = 2;
glyphSet = { "0" = "0"; "1" = "1"; }; glyphSet = { "0" = "0"; "1" = "1"; };
conversionTable = { "0" = 0; "1" = 1; }; conversionTable = { "0" = 0; "1" = 1; };
isValidDigit = <function>; fromDec = <function>;
fromDec = <function>; toDec = <function>;
toDec = <function>; }
} */
*/ generateBaseDigitType = glyphsList: rec {
generateBaseDigitType = glyphsList: rec { base = pkgs.lib.length glyphsList;
base = pkgs.lib.length glyphsList; glyphSet = generateGlyphSet glyphsList;
glyphSet = generateGlyphSet glyphsList; conversionTable = generateConversionTable glyphsList;
conversionTable = generateConversionTable glyphsList;
# Unfortunately, these functions cannot handle negative numbers unless we # Unfortunately, these functions cannot handle negative numbers unless we
# implement something like Two's complement. For now, we're not worrying # implement something like Two's complement. For now, we're not worrying
# about that since most of the use cases here will be mostly for color # about that since most of the use cases here will be mostly for color
# generation that typically uses hexadecimal (RGB). Plus I don't want to # generation that typically uses hexadecimal (RGB). Plus I don't want to
# open a can of worms about implementing this with stringy types. # open a can of worms about implementing this with stringy types.
fromDec = decimal: fromDec = decimal:
let let
iter = product: value: iter = product: value:
let let
quotient = value / base; quotient = value / base;
remainder = value - (base * quotient); remainder = value - (base * quotient);
baseDigit = glyphSet.${builtins.toString remainder} + product; baseDigit = glyphSet.${builtins.toString remainder} + product;
in in
if quotient <= 0 if quotient <= 0
then baseDigit then baseDigit
else iter baseDigit quotient; else iter baseDigit quotient;
in in
iter "" decimal; iter "" decimal;
toDec = digit: toDec = digit:
let let
chars = pkgs.lib.stringToCharacters digit; chars = pkgs.lib.stringToCharacters digit;
maxDigits = (pkgs.lib.length chars) - 1; maxDigits = (pkgs.lib.length chars) - 1;
convertDigitToDec = convertDigitToDec =
pkgs.lib.lists.imap0 (i: v: conversionTable.${v} * (pow base (maxDigits - i))) chars; pkgs.lib.lists.imap0 (i: v: conversionTable.${v} * (pow base (maxDigits - i))) chars;
in in
pkgs.lib.foldl (sum: v: sum + v) 0 convertDigitToDec; pkgs.lib.foldl (sum: v: sum + v) 0 convertDigitToDec;
}; };
/* Exponentiates the given base with the exponent. /* Exponentiates the given base with the exponent.
@ -169,7 +168,7 @@ rec {
iter = product: counter: max-count: iter = product: counter: max-count:
if counter > max-count if counter > max-count
then product then product
else (iter (product * base) (counter + 1) max-count); else iter (product * base) (counter + 1) max-count;
in in
iter 1 1 exponent; iter 1 1 exponent;
} }

View File

@ -95,8 +95,24 @@ pkgs.lib.runTests {
testGenerateConversionTable2 = { testGenerateConversionTable2 = {
expr = lib.trivial.generateConversionTable expr = lib.trivial.generateConversionTable
[ "0" "1" "2" "3" "4" "5" "6" "7" [
"8" "9" "A" "B" "C" "D" "E" "F" ]; "0"
"1"
"2"
"3"
"4"
"5"
"6"
"7"
"8"
"9"
"A"
"B"
"C"
"D"
"E"
"F"
];
expected = { expected = {
"0" = 0; "0" = 0;
"1" = 1; "1" = 1;