aboutsummaryrefslogtreecommitdiff
path: root/lib/trivial.nix
diff options
context:
space:
mode:
authorJan Malakhovski <oxij@oxij.org>2017-12-07 21:26:30 +0000
committerJan Malakhovski <oxij@oxij.org>2017-12-07 21:26:30 +0000
commitbccab965b9226e7d515e1ee1cdf47bb32d1df63d (patch)
treecd46278495ed5ec59a22bbc111b9d9b2065a320b /lib/trivial.nix
parentaeb32cf187edd854cb5541f5f4fe40713c0e96b5 (diff)
lib: implement `compare`, `splitByAndCompare`, and `compareLists`
Diffstat (limited to 'lib/trivial.nix')
-rw-r--r--lib/trivial.nix25
1 files changed, 25 insertions, 0 deletions
diff --git a/lib/trivial.nix b/lib/trivial.nix
index c452c7b65bc1..5f18c0b61cc0 100644
--- a/lib/trivial.nix
+++ b/lib/trivial.nix
@@ -81,6 +81,31 @@ rec {
*/
mod = base: int: base - (int * (builtins.div base int));
+ /* C-style comparisons
+
+ a < b => -1
+ a == b => 0
+ a > b => 1
+ */
+ compare = a: b:
+ if a < b
+ then -1
+ else if a > b
+ then 1
+ else 0;
+
+ /* Split type into two subtypes by predicate `p`, assume
+
+ forall x y . x < y if p x == true && p y == false
+
+ compare elements of the same subtype with `yes` and `no`
+ comparisons respectively.
+ */
+ splitByAndCompare = p: yes: no: a: b:
+ if p a
+ then if p b then yes a b else -1
+ else if p b then 1 else no a b;
+
/* Reads a JSON file. */
importJSON = path:
builtins.fromJSON (builtins.readFile path);