Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
rec {
# id :: a -> a
id = value: value;
# not :: Bool -> Bool
# not :: (a -> Bool) -> a -> Bool
not = value: with builtins;
if isBool value then
! value
else if isFunction value then
x: ! (value x)
else
throw ("value is a " + typeOf value + " while a Boolean or a Function was expected");
# filterAnd :: [ (a -> Bool) ] -> a -> Bool
filterAnd = lambdas: value: with builtins;
all (lambda: lambda value) lambdas;
# filterOr :: [ (a -> Bool) ] -> a -> Bool
filterOr = lambdas: value: with builtins;
any (lambda: lambda value) lambdas;
# readFilterDir :: ({name:String, path:Path, type:String, ...} -> Bool) -> Path -> [ String ]
readFilterDir = lambda: path: with builtins;
let
dirContents = readDir path;
filterFunc = name: lambda rec {
inherit name;
path = path + "/${name}";
type = dirContents.${name};
};
in filter filterFunc (attrNames dirContents);
# filterDirHidden :: {name:String, ...} -> Bool
filterDirHidden = { name, ... }:
(builtins.substring 0 1 name) == ".";
# filterDirDirs :: {type:String, ...} -> Bool
filterDirDirs = { type, ... }:
type == "directory";
# filterDirFiles :: {type:String, ...} -> Bool
filterDirFiles = { type, ... }:
type == "regular";
# filterDirSymlinks :: {type:String, ...} -> Bool
filterDirSymlinks = { type, ... }:
type == "symlink";
# keysToAttrs :: ( String -> a ) -> [ String ] -> { *: a }
keysToAttrs = lambda: strings:
builtins.listToAttrs (map (k: {
name = k;
value = lambda k;
}) strings);
# isMaybe :: a -> Bool
isMaybe = m: with builtins;
if not isAttrs m then false else
if not (hasAttr "isMaybe") m then false else
if not isBool m.isMaybe then false else
m.isMaybe;
# Just :: a -> Maybe a
Just = a: {isMaybe = true; hasValue = true; value = a;};
# Nothing :: Maybe a
Nothing = {isMaybe = true; hasValue = false;};
# maybe :: b -> (a -> b) -> Maybe a -> b
maybe = default: transform: m:
if not isMaybe m then
throw "maybe: ${m} is not a Maybe."
else
if m.hasValue then
transform m.value
else
default
;
# maybeToList :: Maybe a -> [ a ]
maybeToList = m:
if not isMaybe m then
throw "maybeToList: ${m} is not a Maybe."
else if m.hasValue then
[ m.value ]
else
[];
# toExistingPath :: Path -> Maybe Path
toExistingPath = path: with builtins;
if pathExists path then Just path else Nothing;
}