Skip to content

Commit

Permalink
doc: improve lib.compareLists documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
hsjobeki committed Dec 27, 2024
1 parent 08de518 commit ea5e99f
Showing 1 changed file with 20 additions and 8 deletions.
28 changes: 20 additions & 8 deletions lib/lists.nix
Original file line number Diff line number Diff line change
Expand Up @@ -1330,35 +1330,47 @@ rec {
pairs);

/**
Compare two lists element-by-element.
Compare two lists element-by-element with a comparison function `cmp`.
List elements are compared in order, the first non-equal pair of elements determines the result.
:::{.note}
The `<` operator can also be used to compare lists in a bi-state manner. (e.g. `[1 2] < [1 3]` is `true`).
See also [language operators](https://nix.dev/manual/nix/stable/language/operators#comparison) for more information.
:::
# Inputs
`cmp`
: 1\. Function argument
: The comparison function `a: b: ...` must return:
- `0` if `a` and `b` are equal
- `1` if `a` is greater than `b`
- `-1` if `a` is less than `b`
See [lib.compare](#function-library-lib.trivial.compare) for a an example implementation.
`a`
: 2\. Function argument
: The first list
`b`
: 3\. Function argument
: The second list
# Examples
:::{.example}
## `lib.lists.compareLists` usage example
```nix
compareLists compare [] []
compareLists lib.compare [] []
=> 0
compareLists compare [] [ "a" ]
compareLists lib.compare [] [ "a" ]
=> -1
compareLists compare [ "a" ] []
compareLists lib.compare [ "a" ] []
=> 1
compareLists compare [ "a" "b" ] [ "a" "c" ]
compareLists lib.compare [ "a" "b" ] [ "a" "c" ]
=> -1
```
Expand Down

0 comments on commit ea5e99f

Please sign in to comment.