diff --git a/functions/flatten.nix b/functions/flatten.nix new file mode 100644 index 0000000..0582102 --- /dev/null +++ b/functions/flatten.nix @@ -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 diff --git a/functions/join-string.nix b/functions/join-string.nix new file mode 100644 index 0000000..963cb24 --- /dev/null +++ b/functions/join-string.nix @@ -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 diff --git a/functions/match-all-flat.nix b/functions/match-all-flat.nix new file mode 100644 index 0000000..8acb11d --- /dev/null +++ b/functions/match-all-flat.nix @@ -0,0 +1,3 @@ +with import (toString ./.); + +with builtins; pattern: s: flatten (match-all pattern s) diff --git a/functions/match-all.nix b/functions/match-all.nix new file mode 100644 index 0000000..94db927 --- /dev/null +++ b/functions/match-all.nix @@ -0,0 +1 @@ +with builtins; pattern: s: filter isList (split pattern s) diff --git a/functions/name-to-mac.nix b/functions/name-to-mac.nix new file mode 100644 index 0000000..dc85899 --- /dev/null +++ b/functions/name-to-mac.nix @@ -0,0 +1,4 @@ +with import (toString ./.); +with builtins; + +elts: name: join-string ":" (take elts (match-all-flat "(..)" (hashString "sha256" name))) diff --git a/functions/take.nix b/functions/take.nix new file mode 100644 index 0000000..aafb14d --- /dev/null +++ b/functions/take.nix @@ -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