This repository has been archived by the owner on Apr 9, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
std.nix
84 lines (84 loc) · 2.22 KB
/
std.nix
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
{inputs, ...}: let
std = let
baseStd = inputs.std.lib;
inherit (baseStd) set function list bool types optional tuple;
mergeWith = let
append = {
path,
values,
canMerge,
mapToSet,
}: let
mergeWith' = values:
mergeWith {
inherit canMerge mapToSet path;
sets = list.map (v: (mapToSet path v).value) values;
};
mergeUntil = list.findIndex (function.not (canMerge path)) values;
len = list.length values;
in
if len == 0
then {}
else if len == 1
then list.unsafeHead values
else if list.all (canMerge path) values
then mergeWith' values
else
optional.match mergeUntil {
just = i: let
split = list.splitAt i values;
in
if i > 0
then mergeWith' split._0
else list.unsafeHead values;
nothing = list.unsafeHead values;
};
in
{
canMerge ? path: v: optional.isJust (mapToSet path v),
mapToSet ? _: v: bool.toOptional (types.attrs.check v) v,
path ? [],
sets,
}:
set.mapZip (name: values:
append {
path = path ++ list.One name;
inherit canMerge mapToSet values;
})
sets;
merge = sets:
mergeWith {
inherit sets;
};
remap = f: s: set.fromList (list.map f (set.toList s));
renames = names:
remap ({
_0,
_1,
}:
tuple.tuple2 (names.${_0} or _0) _1);
rename = oldName: newName: renames {${oldName} = newName;};
in
merge [
baseStd
{
function = {
pipe = list.foldl' (function.flip function.compose) function.id;
};
set = {
inherit merge mergeWith remap renames rename;
recursiveMap = f: s: let
recurse = str: s: let
g = str1: str2:
if types.attrs.check str2
then f (str ++ [str1]) (recurse (str ++ [str1]) str2)
else f (str ++ [str1]) str2;
in
set.map g s;
in
f [] (recurse [] s);
};
}
];
in
std