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