From efbcfce7a78d12a5a6adebba5f1ec3bb9f602286 Mon Sep 17 00:00:00 2001
From: tv <tv@krebsco.de>
Date: Tue, 20 Dec 2022 19:08:42 +0100
Subject: [PATCH] lib: add uniq and uniqBy

---
 lib/default.nix | 24 ++++++++++++++++++++++++
 1 file changed, 24 insertions(+)

diff --git a/lib/default.nix b/lib/default.nix
index 1a57df266..280f04299 100644
--- a/lib/default.nix
+++ b/lib/default.nix
@@ -187,6 +187,30 @@ let
     in
       filter (x: x != []) ([acc.chunk] ++ acc.chunks);
 
+    # Filter adjacent duplicate elements.
+    uniq = uniqBy eq;
+
+    # Filter adjacent duplicate elements determined via the given function.
+    uniqBy = cmp: let
+      f = a: s:
+        if length s == 0 then
+          []
+        else let
+          b = head s;
+        in
+          if cmp a b then
+            f b (tail s)
+          else
+            [b] ++ f b (tail s);
+    in
+      s:
+        if length s == 0 then
+          []
+        else let
+          b = head s;
+        in
+          [b] ++ f b (tail s);
+
     warnOldVersion = oldName: newName:
       if compareVersions oldName newName != -1 then
         trace "Upstream `${oldName}' gets overridden by `${newName}'." newName