-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdefault.nix
More file actions
89 lines (75 loc) · 1.8 KB
/
default.nix
File metadata and controls
89 lines (75 loc) · 1.8 KB
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
{
pkgs ? import ../locked.nix,
}:
let
lib = pkgs.lib;
abs = x: if x < 0 then x * -1 else x;
modulo = a: b: a - b * builtins.floor (a / b);
parseLines =
lineFn: text:
let
lines = lib.strings.splitString "\n" (lib.strings.trim text);
in
builtins.map lineFn lines;
splitEmptyLine =
text:
let
lines = (lib.strings.splitString "\n" (lib.strings.trim text));
addIfNonEmpty = { acc, lst }: if builtins.length lst > 0 then acc ++ [ lst ] else acc;
reducer =
{ currLst, acc }:
line:
if line == "" then
{
currLst = [ ];
acc = addIfNonEmpty {
inherit acc;
lst = currLst;
};
}
else
{
inherit acc;
currLst = currLst ++ [ line ];
};
reduced = builtins.foldl' reducer {
currLst = [ ];
acc = [ ];
} lines;
in
addIfNonEmpty {
acc = reduced.acc;
lst = reduced.currLst;
};
getOrDefault =
{
key,
default,
attrs,
}:
if builtins.hasAttr key attrs then builtins.getAttr key attrs else default;
sumList = lst: lib.lists.foldl' builtins.add 0 lst;
# ggenerates all permutations of length n from a list of values
permutations =
values: n:
if n == 0 then
[ [ ] ] # Base case: a list containing an empty list
else
let
# Recursive step: prepend each value from `values` to all permutations of length n-1
smallerPermutations = permutations values (n - 1);
in
builtins.concatMap (p: map (v: ([ v ] ++ p)) values) smallerPermutations;
in
{
inherit
abs
splitEmptyLine
parseLines
getOrDefault
sumList
modulo
permutations
;
grid = import ./grid.nix { inherit pkgs; };
}