Compare commits
2 commits
b541b78145
...
53dc94ca00
Author | SHA1 | Date | |
---|---|---|---|
53dc94ca00 | |||
8fda6facde |
1 changed files with 14 additions and 14 deletions
|
@ -10,8 +10,8 @@ rec {
|
||||||
|
|
||||||
inRange = i: range: i >= range.start && i < range.end;
|
inRange = i: range: i >= range.start && i < range.end;
|
||||||
|
|
||||||
# Build a selector function that will filters point-by-point any index in xs.
|
# Build a selector function that will filters point-by-point any index in xs.
|
||||||
# e.g. if you want to select specific indexes you can just use that.
|
# e.g. if you want to select specific indexes you can just use that.
|
||||||
# If you want to select contiguous interval of indexes, you are better served by
|
# If you want to select contiguous interval of indexes, you are better served by
|
||||||
# `mkIntervalFilter`.
|
# `mkIntervalFilter`.
|
||||||
mkPointwiseFilter = xs: index: any (allowedIndex: index == allowedIndex) xs;
|
mkPointwiseFilter = xs: index: any (allowedIndex: index == allowedIndex) xs;
|
||||||
|
@ -21,26 +21,26 @@ rec {
|
||||||
# to `inRange`.
|
# to `inRange`.
|
||||||
mkIntervalFilter = intervals: index: any (allowedRange: inRange index allowedRange) intervals;
|
mkIntervalFilter = intervals: index: any (allowedRange: inRange index allowedRange) intervals;
|
||||||
|
|
||||||
# Build an attribute set map from values to indexes.
|
# Build an attribute set map from values to indexes.
|
||||||
# e.g. reversedEnumerate [ "a" "b" ] == { "a" = 0; "b" = 1; }.
|
# e.g. reversedEnumerate [ "a" "b" ] == { "a" = 0; "b" = 1; }.
|
||||||
reversedEnumerate = list: listToAttrs
|
reversedEnumerate = list: listToAttrs
|
||||||
(zipListsWith
|
(zipListsWith
|
||||||
(index: value: nameValuePair value index)
|
(index: value: nameValuePair value index)
|
||||||
(range 0 (length list - 1))
|
(range 0 (length list - 1))
|
||||||
list);
|
list);
|
||||||
|
|
||||||
# Collect a list of attribute sets into an attribute set.
|
# Collect a list of attribute sets into an attribute set.
|
||||||
# Merge order depends on attrValues iteration order and foldl.
|
# Merge order depends on attrValues iteration order and foldl.
|
||||||
chainAttrs = attrs: foldl (a: b: a // b) (builtins.attrValues attrs);
|
chainAttrs = attrs: foldl (a: b: a // b) { } (builtins.attrValues attrs);
|
||||||
|
|
||||||
# Given an attribute set of an attribute set of items, does it describe a valid partition of some global set?
|
# Given an attribute set of an attribute set of items, does it describe a valid partition of some global set?
|
||||||
# This does not check for completeness.
|
# This does not check for completeness.
|
||||||
# idFunction :: Attrs K V → List Identifier
|
# idFunction :: Attrs K V → List Identifier
|
||||||
isValidPartition = attrs:
|
isValidPartition = attrs:
|
||||||
let
|
let
|
||||||
values = builtins.attrValues attrs;
|
values = builtins.attrValues attrs;
|
||||||
in
|
in
|
||||||
# TODO(performance?): this is the simple dumb idea.
|
# TODO(performance?): this is the simple dumb idea.
|
||||||
# A better idea would use n(n - 1)/2 iterations over values to exploit symmetry of item equality.
|
# A better idea would use n(n - 1)/2 iterations over values to exploit symmetry of item equality.
|
||||||
# To do so, a strategy could be to consider all shifted toplevel identifiers lists and zip them.
|
# To do so, a strategy could be to consider all shifted toplevel identifiers lists and zip them.
|
||||||
# There's sum_k(n - k) such lists, and therefore: n(n - 1)/2 lists.
|
# There's sum_k(n - k) such lists, and therefore: n(n - 1)/2 lists.
|
||||||
|
@ -48,16 +48,16 @@ rec {
|
||||||
# So, if we have N subsets in the partition and each subset has at most K items, we end up doing something like (K log K) * N(N - 1)/2
|
# So, if we have N subsets in the partition and each subset has at most K items, we end up doing something like (K log K) * N(N - 1)/2
|
||||||
# In practice, K should be the biggest and N is quite small.
|
# In practice, K should be the biggest and N is quite small.
|
||||||
lib.all (subset:
|
lib.all (subset:
|
||||||
lib.all (anotherSubset:
|
lib.all (anotherSubset:
|
||||||
subset != anotherSubset -> lib.intersectAttrs subset anotherSubset == {}
|
subset != anotherSubset -> lib.intersectAttrs subset anotherSubset == {}
|
||||||
) values
|
) values
|
||||||
) values;
|
) values;
|
||||||
|
|
||||||
# Renumber an attribute set of items.
|
# Renumber an attribute set of items.
|
||||||
# For each item in the attribute set, we replace its value by a call to the renumbering function
|
# For each item in the attribute set, we replace its value by a call to the renumbering function
|
||||||
# where we pass renumberedIndex and value.
|
# where we pass renumberedIndex and value.
|
||||||
# It's a form of imap for attribute sets.
|
# It's a form of imap for attribute sets.
|
||||||
renumber = indexFn: renumberingFn: attrs:
|
renumber = indexFn: renumberingFn: attrs:
|
||||||
let
|
let
|
||||||
indexes = reversedEnumerate (map (n: toString (indexFn n)) (builtins.attrValues attrs));
|
indexes = reversedEnumerate (map (n: toString (indexFn n)) (builtins.attrValues attrs));
|
||||||
in
|
in
|
||||||
|
|
Loading…
Reference in a new issue