From e726057b320132c13c5f9c08fe7b864fde550533 Mon Sep 17 00:00:00 2001 From: Eelco Dolstra Date: Thu, 21 Dec 2006 00:09:40 +0000 Subject: [PATCH] * Function `findSingle' for looking up values in dictionary-like lists. svn path=/nixpkgs/trunk/; revision=7446 --- pkgs/lib/default.nix | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/pkgs/lib/default.nix b/pkgs/lib/default.nix index 829f8e52a20..da279302306 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