Conversation
Best way to return a large concatenated list from a `foldl'`, and probably more use cases. It's a means of last resort, but it's good to have somewhat of a soft landing at least... Engineering around the call stack was fun for me, so I figured I'd just do it.
a16a8a3 to
8ce7b25
Compare
|
Any thoughts on something like creating a balanced binary tree (rope) and a fold-tree implementation (like https://hal.science/hal-04580379/document)? Alternatively, thoughts on using the Scott encoding of lists if you need O(1) eliminators? IIRC attribute sets are fairly expensive and there’s no special case for them like with small lists. |
I would prefer to make attrsets about equally inexpensive, which I feel more positive is achievable considering recent Nix contributor activity. Similarly, I would like the lists in Nix to ultimately be more clever, because these aren't really things that should be the concern of Nix expressions.
That's worth a try, but also harder for contributors to understand as opposed to a cons that's expressed as a straightforward data type. |
|
As an aside, have you considered adding a tree-fold to foldt =
op: nul: list:
let
# Fold over range [lo, lo+count)
foldRange =
lo: count:
if count == 0 then
nul
else if count == 1 then
elemAt list lo
else
# If the count is even, count/2 and (count+1)/2 sum back to count (due to integer division truncating the latter).
# If the count is odd, count/2 and (count+1)/2 sum back to count (due to integer division truncating the former).
# Computing them here avoids creating environment variables for them.
op
(foldRange lo (count / 2))
(foldRange (lo + count / 2) ((count + 1) / 2));
in
foldRange 0 (length list);With that, I was able to compute length (foldt (a: b: a ++ b) [ ] (genList (n: genList (_: 0) n) 16000))where |
Best way to return a large concatenated list from a
foldl', and probably more use cases.It's a means of last resort, but it's very helpful when needed.
Things done
passthru.tests.nixpkgs-reviewon this PR. See nixpkgs-review usage../result/bin/.Add a 👍 reaction to pull requests you find important.