31 lines
1.1 KiB
Nix
31 lines
1.1 KiB
Nix
|
|
let
|
|
lib = import <nixpkgs/lib>;
|
|
utils = with lib; rec {
|
|
# Passthrough trace for debugging
|
|
pTrace = v: traceSeq v v;
|
|
# find the total sum of a int list
|
|
sum = foldr (x: y: x + y) 0;
|
|
# splits a list of length two into two params then they're passed to a func
|
|
split = f: pair: f (head pair) (last pair);
|
|
# Finds the max value in a list
|
|
maxList = foldr max 0;
|
|
# Sorts a int list. Greatest value first
|
|
sortList = sort (x: y: x > y);
|
|
# Cuts a list in half and returns the two parts in a list
|
|
cutInHalf = l: [(take (length l / 2) l) (drop (length l / 2) l)];
|
|
# Splits a list into a list of lists with length cnt
|
|
chunksOf = cnt: l:
|
|
if length l >= cnt then
|
|
[(take cnt l)] ++ chunksOf cnt (drop cnt l)
|
|
else [];
|
|
# same as intersectLists but takes an array of lists to intersect instead of just two
|
|
intersectManyLists = l:
|
|
if length l == 0 then []
|
|
else if length l == 1 then head l
|
|
else intersectLists (head l) (intersectManyLists (tail l));
|
|
# converts a boolean to a int (c style)
|
|
boolToInt = b: if b then 1 else 0;
|
|
};
|
|
in
|
|
lib // lib.strings // builtins // utils |