lib: init lists.uniqueStrings
This commit is contained in:
parent
86603255c3
commit
78ac637056
@ -287,6 +287,7 @@ let
|
|||||||
init
|
init
|
||||||
crossLists
|
crossLists
|
||||||
unique
|
unique
|
||||||
|
uniqueStrings
|
||||||
allUnique
|
allUnique
|
||||||
intersectLists
|
intersectLists
|
||||||
subtractLists
|
subtractLists
|
||||||
|
|||||||
@ -11,7 +11,7 @@ let
|
|||||||
warn
|
warn
|
||||||
pipe
|
pipe
|
||||||
;
|
;
|
||||||
inherit (lib.attrsets) mapAttrs;
|
inherit (lib.attrsets) mapAttrs attrNames;
|
||||||
inherit (lib) max;
|
inherit (lib) max;
|
||||||
in
|
in
|
||||||
rec {
|
rec {
|
||||||
@ -1839,6 +1839,10 @@ rec {
|
|||||||
/**
|
/**
|
||||||
Remove duplicate elements from the `list`. O(n^2) complexity.
|
Remove duplicate elements from the `list`. O(n^2) complexity.
|
||||||
|
|
||||||
|
:::{.note}
|
||||||
|
If the list only contains strings and order is not important, the complexity can be reduced to O(n log n) by using [`lib.lists.uniqueStrings`](#function-library-lib.lists.uniqueStrings) instead.
|
||||||
|
:::
|
||||||
|
|
||||||
# Inputs
|
# Inputs
|
||||||
|
|
||||||
`list`
|
`list`
|
||||||
@ -1864,6 +1868,43 @@ rec {
|
|||||||
*/
|
*/
|
||||||
unique = foldl' (acc: e: if elem e acc then acc else acc ++ [ e ]) [ ];
|
unique = foldl' (acc: e: if elem e acc then acc else acc ++ [ e ]) [ ];
|
||||||
|
|
||||||
|
/**
|
||||||
|
Removes duplicate strings from the `list`. O(n log n) complexity.
|
||||||
|
|
||||||
|
:::{.note}
|
||||||
|
Order is not preserved.
|
||||||
|
|
||||||
|
All elements of the list must be strings without context.
|
||||||
|
|
||||||
|
This function fails when the list contains a non-string element or a [string with context](https://nix.dev/manual/nix/latest/language/string-context.html).
|
||||||
|
In that case use [`lib.lists.unique`](#function-library-lib.lists.unique) instead.
|
||||||
|
:::
|
||||||
|
|
||||||
|
# Inputs
|
||||||
|
|
||||||
|
`list`
|
||||||
|
|
||||||
|
: List of strings
|
||||||
|
|
||||||
|
# Type
|
||||||
|
|
||||||
|
```
|
||||||
|
uniqueStrings :: [ String ] -> [ String ]
|
||||||
|
```
|
||||||
|
|
||||||
|
# Examples
|
||||||
|
:::{.example}
|
||||||
|
## `lib.lists.uniqueStrings` usage example
|
||||||
|
|
||||||
|
```nix
|
||||||
|
uniqueStrings [ "foo" "bar" "foo" ]
|
||||||
|
=> [ "bar" "foo" ] # order is not preserved
|
||||||
|
```
|
||||||
|
|
||||||
|
:::
|
||||||
|
*/
|
||||||
|
uniqueStrings = list: attrNames (groupBy id list);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
Check if list contains only unique elements. O(n^2) complexity.
|
Check if list contains only unique elements. O(n^2) complexity.
|
||||||
|
|
||||||
|
|||||||
@ -113,6 +113,7 @@ let
|
|||||||
toIntBase10
|
toIntBase10
|
||||||
toShellVars
|
toShellVars
|
||||||
types
|
types
|
||||||
|
uniqueStrings
|
||||||
updateManyAttrsByPath
|
updateManyAttrsByPath
|
||||||
versions
|
versions
|
||||||
xor
|
xor
|
||||||
@ -1934,6 +1935,69 @@ runTests {
|
|||||||
expected = false;
|
expected = false;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
testUniqueStrings_empty = {
|
||||||
|
expr = uniqueStrings [ ];
|
||||||
|
expected = [ ];
|
||||||
|
};
|
||||||
|
testUniqueStrings_singles = {
|
||||||
|
expr = uniqueStrings [
|
||||||
|
"all"
|
||||||
|
"unique"
|
||||||
|
"already"
|
||||||
|
];
|
||||||
|
expected = [
|
||||||
|
"all"
|
||||||
|
"already"
|
||||||
|
"unique"
|
||||||
|
];
|
||||||
|
};
|
||||||
|
testUniqueStrings_allDuplicates = {
|
||||||
|
expr = uniqueStrings [
|
||||||
|
"dup"
|
||||||
|
"dup"
|
||||||
|
"dup"
|
||||||
|
];
|
||||||
|
expected = [ "dup" ];
|
||||||
|
};
|
||||||
|
testUniqueStrings_some_duplicates = {
|
||||||
|
expr = uniqueStrings [
|
||||||
|
"foo"
|
||||||
|
"foo"
|
||||||
|
"bar"
|
||||||
|
"bar"
|
||||||
|
"baz"
|
||||||
|
];
|
||||||
|
expected = [
|
||||||
|
"bar"
|
||||||
|
"baz"
|
||||||
|
"foo"
|
||||||
|
];
|
||||||
|
};
|
||||||
|
testUniqueStrings_unicode = {
|
||||||
|
expr = uniqueStrings [
|
||||||
|
"café"
|
||||||
|
"@"
|
||||||
|
"#"
|
||||||
|
"@"
|
||||||
|
"#"
|
||||||
|
"$"
|
||||||
|
"😎"
|
||||||
|
"😎"
|
||||||
|
"🙃"
|
||||||
|
""
|
||||||
|
""
|
||||||
|
];
|
||||||
|
expected = [
|
||||||
|
""
|
||||||
|
"#"
|
||||||
|
"$"
|
||||||
|
"@"
|
||||||
|
"café"
|
||||||
|
"😎"
|
||||||
|
"🙃"
|
||||||
|
];
|
||||||
|
};
|
||||||
|
|
||||||
# ATTRSETS
|
# ATTRSETS
|
||||||
|
|
||||||
testConcatMapAttrs = {
|
testConcatMapAttrs = {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user