Merge "tests/functional/lang: Clean up lib.nix" into main

This commit is contained in:
piegames
2025-05-24 10:57:02 +00:00
committed by Lix Systems Gerrit
10 changed files with 13 additions and 61 deletions
@@ -1 +1 @@
"newxfoonewxy"
[ "newx" "foo" "newx" "y" ]
@@ -1,5 +1,3 @@
with import ./lib.nix;
let
attrs = {y = "y"; x = "x"; foo = "foo";} // rec {x = "newx"; bar = x;};
@@ -8,4 +6,6 @@ let
values = map (name: builtins.getAttr name attrs) names;
in assert values == builtins.attrValues attrs; concat values
in
assert values == builtins.attrValues attrs;
values
+1 -1
View File
@@ -11,5 +11,5 @@ in
}:
{
result = lib.concat [xyzzy xyzzy2 fb];
result = lib.id (builtins.concatStringsSep "" [xyzzy xyzzy2 fb]);
}
+1 -1
View File
@@ -8,6 +8,6 @@ let
else [{key = builtins.sub key 9;} {key = builtins.sub key 13; foo = true;}];
};
sort = (import ./lib.nix).sortBy (a: b: builtins.lessThan a.key b.key);
sort = builtins.sort (a: b: builtins.lessThan a.key b.key);
in sort closure
@@ -1 +0,0 @@
"1234567"
@@ -1,8 +0,0 @@
with import ./lib.nix;
let
l = ["1" "2" ["3" ["4"] ["5" "6"]] "7"];
in
concat (flatten l)
+1 -3
View File
@@ -1,3 +1 @@
with import ./lib.nix;
concat ["foo" "bar" "bla" "test"]
builtins.concatStringsSep "" ["foo" "bar" "bla" "test"]
@@ -1,11 +1,10 @@
# this test shows how to use listToAttrs and that evaluation is still lazy (throw isn't called)
with import ./lib.nix;
let
let
asi = name: value : { inherit name value; };
list = [ ( asi "a" "A" ) ( asi "b" "B" ) ];
a = builtins.listToAttrs list;
b = builtins.listToAttrs ( list ++ list );
r = builtins.listToAttrs [ (asi "result" [ a b ]) ( asi "throw" (throw "this should not be thrown")) ];
x = builtins.listToAttrs [ (asi "foo" "bar") (asi "foo" "bla") ];
in concat (map (x: x.a) r.result) + x.foo
in
builtins.concatStringsSep "" (map (x: x.a) r.result) + x.foo
+1 -3
View File
@@ -1,3 +1 @@
with import ./lib.nix;
concat (map (x: x + "bar") [ "foo" "bla" "xyzzy" ])
builtins.concatStringsSep "" (map (x: x + "bar") [ "foo" "bla" "xyzzy" ])
+2 -36
View File
@@ -7,15 +7,7 @@ rec {
then nul
else op (head list) (fold op nul (tail list));
concat =
fold (x: y: x + y) "";
and = fold (x: y: x && y) true;
flatten = x:
if isList x
then fold (x: y: (flatten x) ++ y) [] x
else [x];
and = all id;
sum = foldl' (x: y: add x y) 0;
@@ -23,33 +15,7 @@ rec {
let lenFileName = stringLength fileName;
lenExt = stringLength ext;
in !(lessThan lenFileName lenExt) &&
substring (sub lenFileName lenExt) lenFileName fileName == ext;
# Split a list at the given position.
splitAt = pos: list:
if pos == 0 then {first = []; second = list;} else
if list == [] then {first = []; second = [];} else
let res = splitAt (sub pos 1) (tail list);
in {first = [(head list)] ++ res.first; second = res.second;};
# Stable merge sort.
sortBy = comp: list:
if lessThan 1 (length list)
then
let
split = splitAt (div (length list) 2) list;
first = sortBy comp split.first;
second = sortBy comp split.second;
in mergeLists comp first second
else list;
mergeLists = comp: list1: list2:
if list1 == [] then list2 else
if list2 == [] then list1 else
if comp (head list2) (head list1) then [(head list2)] ++ mergeLists comp list1 (tail list2) else
[(head list1)] ++ mergeLists comp (tail list1) list2;
id = x: x;
substring (sub lenFileName lenExt) lenFileName fileName == ext; id = x: x;
const = x: y: x;