about summary refs log tree commit diff
path: root/pkgs/lib/default.nix
diff options
context:
space:
mode:
Diffstat (limited to 'pkgs/lib/default.nix')
-rw-r--r--pkgs/lib/default.nix10
1 files changed, 10 insertions, 0 deletions
diff --git a/pkgs/lib/default.nix b/pkgs/lib/default.nix
index 829f8e52a208..da279302306f 100644
--- a/pkgs/lib/default.nix
+++ b/pkgs/lib/default.nix
@@ -61,6 +61,16 @@ rec {
     fold (x: y: if pred x then [x] ++ y else y) [] list;
 
 
+  # Find the sole element in the list matching the specified
+  # predicate, or returns the default value.
+  findSingle = pred: default: list:
+    let found = filter pred list;
+    in if found == [] then default
+       else if tail found != [] then
+         abort "Multiple elements match predicate in findSingle."
+       else head found;
+
+
   # Return true if each element of a list is equal, false otherwise.
   eqLists = xs: ys:
     if xs == [] && ys == [] then true