add mergeAttrsList + fix bad listToAttrs usage
This commit is contained in:
parent
3d2f657e9d
commit
a210d5d037
4 changed files with 23 additions and 3 deletions
|
|
@ -5,7 +5,7 @@
|
||||||
result = std.result;
|
result = std.result;
|
||||||
};
|
};
|
||||||
in
|
in
|
||||||
builtins.listToAttrs [
|
std.attrs.mergeAttrsList [
|
||||||
# submodule content is accessible first by submodule name
|
# submodule content is accessible first by submodule name
|
||||||
# then by the name of the content (ie self.submodule.myFunc)
|
# then by the name of the content (ie self.submodule.myFunc)
|
||||||
{inherit parse;}
|
{inherit parse;}
|
||||||
|
|
|
||||||
|
|
@ -4,7 +4,7 @@
|
||||||
}: let
|
}: let
|
||||||
struct = import ./struct.nix {inherit attrs result;};
|
struct = import ./struct.nix {inherit attrs result;};
|
||||||
in
|
in
|
||||||
builtins.listToAttrs [
|
attrs.mergeAttrsList [
|
||||||
# submodule is included directly to this module (ie self.myFunc)
|
# submodule is included directly to this module (ie self.myFunc)
|
||||||
struct
|
struct
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -95,4 +95,24 @@
|
||||||
if l != null && builtins.hasAttr r l
|
if l != null && builtins.hasAttr r l
|
||||||
then l.${r}
|
then l.${r}
|
||||||
else null);
|
else null);
|
||||||
|
|
||||||
|
mergeAttrsList = list: let
|
||||||
|
# `binaryMerge start end` merges the elements at indices `index` of `list` such that `start <= index < end`
|
||||||
|
# Type: Int -> Int -> Attrs
|
||||||
|
binaryMerge = start: end:
|
||||||
|
# assert start < end; # Invariant
|
||||||
|
if end - start >= 2
|
||||||
|
then
|
||||||
|
# If there's at least 2 elements, split the range in two, recurse on each part and merge the result
|
||||||
|
# The invariant is satisfied because each half will have at least 1 element
|
||||||
|
binaryMerge start (start + (end - start) / 2) // binaryMerge (start + (end - start) / 2) end
|
||||||
|
else
|
||||||
|
# Otherwise there will be exactly 1 element due to the invariant, in which case we just return it directly
|
||||||
|
builtins.elemAt list start;
|
||||||
|
in
|
||||||
|
if list == []
|
||||||
|
then
|
||||||
|
# Calling binaryMerge as below would not satisfy its invariant
|
||||||
|
{}
|
||||||
|
else binaryMerge 0 (builtins.length list);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -3,7 +3,7 @@
|
||||||
lists = import ./lists.nix {};
|
lists = import ./lists.nix {};
|
||||||
result = import ./lists.nix {inherit lists;};
|
result = import ./lists.nix {inherit lists;};
|
||||||
in
|
in
|
||||||
builtins.listToAttrs [
|
attrs.mergeAttrsList [
|
||||||
# submodule is included directly to this module (ie self.myFunc)
|
# submodule is included directly to this module (ie self.myFunc)
|
||||||
|
|
||||||
# submodule content is accessible first by submodule name
|
# submodule content is accessible first by submodule name
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue