persiskv deployment
[myNix.git] / lib / simple.nix
blob76e85d68c7232099725f5868cb38a1b8c4087b0d
1 { lib
2 , mylib
3 , ...
4 }:
6 let
7   inherit (builtins)
8     foldl'
9     isAttrs
10     isInt
11     isString
12     isBool
13     isList
14     isFloat
15     isFunction
16     isPath
17     ;
18   inherit (lib)
19     optionalString
20     optionals
21     optionalAttrs
22     ;
23   inherit (mylib)
24     I
25     K
26     ;
28 rec {
30   isFunction = builtins.isFunction;
31   isFunctor = f: f ? __functor;
32   isCallable = lib.isFunction;
34   # functor: f -> functor
35   functor = f:
36     if isFunctor f then f
37     else { __functor = _: f; };
39   toFunction = x:
40     if isFunction x then x
41     else if isFunctor x then toFunction (x.__functor x)
42     else K x;
44   # foldr': (a -> b -> c) -> list -> iv
45   # foldr' f [a, b, c] d == f a (f b (f c d))
46   foldr' = op: ls: iv:
47     foldl' (f: b: c: f (op b c)) I ls iv;
49   ifElse = cond: then': else':
50     if cond then then' else else';
52   optionalRef = cond: sth: ifElse cond sth null;
54   maybe = cond: sth:
55     if isAttrs sth then optionalAttrs cond sth
56     else if isInt sth then ifElse cond sth 0
57     else if isString sth then optionalString cond sth
58     else if isBool sth then ifElse cond sth false
59     else if isList sth then optionals cond sth
60     else if isFloat sth then ifElse cond sth 0.0
61     else if isFunction sth then optionalRef cond sth
62     else if isPath sth then optionalRef cond sth
63     else throw "unsupported result type";