From 338340f993563551d8cb45941da987408abef65f Mon Sep 17 00:00:00 2001 From: Nikolay Amiantov Date: Wed, 13 Apr 2016 15:33:11 +0300 Subject: [PATCH] tryAttrs: init function --- lib/attrsets.nix | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/lib/attrsets.nix b/lib/attrsets.nix index 4161fa546c8f..70986195ae05 100644 --- a/lib/attrsets.nix +++ b/lib/attrsets.nix @@ -438,6 +438,24 @@ rec { overrideExisting = old: new: old // listToAttrs (map (attr: nameValuePair attr (attrByPath [attr] old.${attr} new)) (attrNames old)); + /* Try given attributes in order. If no attributes are found, return + attribute list itself. + + Example: + tryAttrs ["a" "b"] { a = 1; b = 2; } + => 1 + tryAttrs ["a" "b"] { c = 3; } + => { c = 3; } + */ + tryAttrs = allAttrs: set: + let tryAttrs_ = attrs: + if attrs == [] then set + else + (let h = head attrs; in + if hasAttr h set then getAttr h set + else tryAttrs_ (tail attrs)); + in tryAttrs_ allAttrs; + /*** deprecated stuff ***/