aboutsummaryrefslogtreecommitdiffstats
path: root/test/testdata/lib.nix
diff options
context:
space:
mode:
Diffstat (limited to 'test/testdata/lib.nix')
-rw-r--r--test/testdata/lib.nix61
1 files changed, 61 insertions, 0 deletions
diff --git a/test/testdata/lib.nix b/test/testdata/lib.nix
new file mode 100644
index 0000000..028a538
--- /dev/null
+++ b/test/testdata/lib.nix
@@ -0,0 +1,61 @@
1with builtins;
2
3rec {
4
5 fold = op: nul: list:
6 if list == []
7 then nul
8 else op (head list) (fold op nul (tail list));
9
10 concat =
11 fold (x: y: x + y) "";
12
13 and = fold (x: y: x && y) true;
14
15 flatten = x:
16 if isList x
17 then fold (x: y: (flatten x) ++ y) [] x
18 else [x];
19
20 sum = foldl' (x: y: add x y) 0;
21
22 hasSuffix = ext: fileName:
23 let lenFileName = stringLength fileName;
24 lenExt = stringLength ext;
25 in !(lessThan lenFileName lenExt) &&
26 substring (sub lenFileName lenExt) lenFileName fileName == ext;
27
28 # Split a list at the given position.
29 splitAt = pos: list:
30 if pos == 0 then {first = []; second = list;} else
31 if list == [] then {first = []; second = [];} else
32 let res = splitAt (sub pos 1) (tail list);
33 in {first = [(head list)] ++ res.first; second = res.second;};
34
35 # Stable merge sort.
36 sortBy = comp: list:
37 if lessThan 1 (length list)
38 then
39 let
40 split = splitAt (div (length list) 2) list;
41 first = sortBy comp split.first;
42 second = sortBy comp split.second;
43 in mergeLists comp first second
44 else list;
45
46 mergeLists = comp: list1: list2:
47 if list1 == [] then list2 else
48 if list2 == [] then list1 else
49 if comp (head list2) (head list1) then [(head list2)] ++ mergeLists comp list1 (tail list2) else
50 [(head list1)] ++ mergeLists comp (tail list1) list2;
51
52 id = x: x;
53
54 const = x: y: x;
55
56 range = first: last:
57 if first > last
58 then []
59 else genList (n: first + n) (last - first + 1);
60
61}