Initial commit

This commit is contained in:
2024-09-27 21:15:41 +02:00
committed by h7x4
commit 21ab209d6e
8 changed files with 146 additions and 0 deletions

10
src/genAttrs.nix Normal file
View File

@@ -0,0 +1,10 @@
lib:
let
genAttrsOld = names: f: lib.listToAttrs (map (n: lib.nameValuePair n (f n)) names);
genAttrsNew = names: f: lib.listToAttrs (map (n: { name = n; value = f n; }) names);
bigdata = lib.imap0 (i: _: toString i) (lib.replicate 999999 null);
in {
old = genAttrsOld bigdata (_: null);
new = genAttrsNew bigdata (_: null);
}

10
src/keepAttrs.nix Normal file
View File

@@ -0,0 +1,10 @@
lib:
let
keepAttrsOld = attrs: names: lib.filterAttrs (n: _: builtins.elem n names) attrs;
keepAttrsNew = attrs: names: builtins.intersectAttrs (lib.genAttrs names (_: null)) attrs;
bigdata = lib.genAttrs (lib.imap0 (i: _: toString i) (lib.replicate 999999 null)) (_: null);
in {
old = keepAttrsOld bigdata [ "123" "456" ];
new = keepAttrsNew bigdata [ "123" "456" ];
}

17
src/subtractLists.nix Normal file
View File

@@ -0,0 +1,17 @@
lib:
let
# O(nm)
subtractListsOld = e: builtins.filter (x: !(builtins.elem x e));
# O(n + m) (hopefully)
subtractListsNew = e: let
# Assuming genAttrs is O(n)
e' = lib.genAttrs e (_: null);
# Assuming hasAttr is O(1)
in builtins.filter (x: !(builtins.hasAttr x e'));
bigdata = lib.imap0 (i: _: toString i) (lib.replicate 999999 null);
in {
old = subtractListsOld [ "123" "456" "789" ] bigdata;
new = subtractListsNew [ "123" "456" "789" ] bigdata;
}

12
src/unique.nix Normal file
View File

@@ -0,0 +1,12 @@
lib:
let
uniqueOld = builtins.foldl' (acc: e: if builtins.elem e acc then acc else acc ++ [ e ]) [];
# uniqueNew = xs: let
# entries = lib.genAttrs xs (_: null);
# in builtins.concatMap (x: if );
bigdataMostlyDuplicates = (lib.replicate 999999 null) ++ ["unique"];
bigdataMostlyUnique = lib.imap0 (i: _: toString i) (lib.replicate 999999 null) ++ ["123" "456"];
in {
old = uniqueOld bigdataMostlyDuplicates;
}