add some support functions

This commit is contained in:
James Andariese 2022-09-27 11:07:58 -05:00
parent 429f846f9b
commit a57f4b0094
6 changed files with 42 additions and 0 deletions

11
functions/flatten.nix Normal file
View File

@ -0,0 +1,11 @@
with builtins;
let flatten-once = foldl' (x: y: x ++ (if isList y then y else [y])) []; # curried. takes a list as final arg.
flatten-many' = flatten-many': l:
let flattened = flatten-once l;
in
if flattened == l
then l
else flatten-many' flatten-many' flattened;
flatten-many = flatten-many' flatten-many';
in
flatten-many

12
functions/join-string.nix Normal file
View File

@ -0,0 +1,12 @@
with builtins;
let join' = join': sep: sl:
if sl == []
then ""
else if length sl == 1
then head sl
else "${head sl}${sep}${join' join' sep (tail sl)}";
join = join' join';
in
join

View File

@ -0,0 +1,3 @@
with import (toString ./.);
with builtins; pattern: s: flatten (match-all pattern s)

1
functions/match-all.nix Normal file
View File

@ -0,0 +1 @@
with builtins; pattern: s: filter isList (split pattern s)

View File

@ -0,0 +1,4 @@
with import (toString ./.);
with builtins;
elts: name: join-string ":" (take elts (match-all-flat "(..)" (hashString "sha256" name)))

11
functions/take.nix Normal file
View File

@ -0,0 +1,11 @@
with builtins;
let take' = take': n: l:
if n < 0 then throw "take requires a positive whole number or 0." else
if n == 0 || l == []
then []
else [ (head l) ] ++ (take' take' (n - 1) (tail l));
take = take' take';
in
take