-
-
Save sternenseemann/00ab9c381e28cb2acb4e20a781731eed to your computer and use it in GitHub Desktop.
constant time append in nix
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ lib, ... }: | |
let | |
goodAttrByPath = p: v: | |
builtins.foldl' (set: attr: let res = set."${attr}"; in res) v p; | |
seqAttrs = attrs: | |
let | |
evalAttrs = builtins.foldl' | |
(x: a: builtins.seq x attrs."${a}") | |
null | |
(builtins.attrNames attrs); | |
in builtins.seq evalAttrs attrs; | |
empty = {}; | |
size = a: a.size or 0; | |
append = a: v: | |
let | |
new = { | |
size = 1 + size a; | |
previous = a; | |
value = v; | |
}; | |
in seqAttrs new; | |
get = i: a: | |
let | |
depth = size a - (i + 1); | |
path = builtins.genList | |
(j: | |
if j == depth | |
then "value" | |
else "previous") (depth + 1); | |
in | |
builtins.trace (toString i) (goodAttrByPath path a); | |
toList = a: | |
builtins.genList (lib.flip get a) (size a); | |
in { | |
inherit | |
empty | |
append | |
toList | |
; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment