my splitStringByLen function was extremely slow, and lists/iterating lists is slow in general so cahnge to splitByLen make old version 60% faster, and a version without the intermediate loop it is 4s

This commit is contained in:
RingOfStorms (Joshua Bell) 2025-12-10 09:46:39 -06:00
parent e9f2b69924
commit 802a8dde26
3 changed files with 67 additions and 23 deletions

View file

@ -5,7 +5,6 @@ rec {
min = a: b: if a < b then a else b;
max = a: b: if a > b then a else b;
split = delim: input: filter (s: isString s && s != "") (builtins.split delim input);
reduce = foldl'; # foldl' (acc: elem: acc + elem) 0 [1 2 3]
flatten = input: concatMap (x: if isList x then x else [ x ]) input;
rangeInclusive =
min: max:
@ -16,30 +15,14 @@ rec {
len = max - min + 1;
in
genList (i: min + i) len;
sum = input: reduce (sum: v: sum + v) 0 input;
sum = input: foldl' (sum: v: sum + v) 0 input;
reverseList =
list:
if builtins.length list == 0 then
[ ]
else
# Recursively reverse the tail and append the head to the end
(reverseList(builtins.tail list)) ++ [ (builtins.head list) ];
(reverseList (builtins.tail list)) ++ [ (builtins.head list) ];
splitStringByLength =
# @param s: The input string.
# @param len: The desired length of each part.
s: len:
let
# Recursive helper function
splitRec =
currentString: acc:
if stringLength currentString <= 0 then
reverseList acc
else
let
chunk = substring 0 len currentString;
remaining = substring len (stringLength currentString - len) currentString;
in
splitRec remaining ([ chunk ] ++ acc);
in
splitRec s [ ];
str: len: map (i: substring (i * len) (len) str) (genList (i: i) (stringLength str / len));
}