From dabc6be64008fe5cedba11d01305b5692722adef Mon Sep 17 00:00:00 2001 From: Rodrigo Arias Mallo Date: Fri, 13 Nov 2020 19:06:31 +0100 Subject: [PATCH] Add more helper functions --- garlic/tools.nix | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/garlic/tools.nix b/garlic/tools.nix index 342a877..deb6762 100644 --- a/garlic/tools.nix +++ b/garlic/tools.nix @@ -41,6 +41,30 @@ let /* Given a trebuchet, returns the experiment */ getExperimentStage = drv: drv.nextStage.nextStage.nextStage; + + # Computes the exponentiation operation + pow = x: n: fold (a: b: a*b) 1 (map (a: x) (range 1 n)); + + # Generates a list of exponents from a to b inclusive, and raises base to + # each element of the list. + expRange = base: a: b: (map (ex: pow base ex) (range a b)); + + # Generates a list of integers by halving number N until it reaches 1. Is + # sorted from the smallest to largest. + divList = N: + let + _divList = n: if (n == 0) then [] else (_divList (n / 2)) ++ [ n ]; + in + _divList N; + + # Generates a set given a list of keys, where all values are null. + genNullAttr = l: genAttrs l (name: null); + + # From the keys in the lis l, generates a set with the values in the set a, + # if they don't exist, they are not taken. Values set to null are removed. + optionalInherit = l: a: filterAttrs (n: v: v!=null) + (overrideExisting (genNullAttr l) a); + }; in gen