diff options
author | Nikolay Amiantov <ab@fmap.me> | 2016-04-13 15:33:11 +0300 |
---|---|---|
committer | Nikolay Amiantov <ab@fmap.me> | 2016-04-13 22:05:45 +0300 |
commit | 338340f993563551d8cb45941da987408abef65f (patch) | |
tree | fb0c34c5b2944a8bd4b039e68c6aacab996aa451 | |
parent | 5ef56700b9e142978673db5889579a0838956d49 (diff) |
tryAttrs: init function
-rw-r--r-- | lib/attrsets.nix | 18 |
1 files changed, 18 insertions, 0 deletions
diff --git a/lib/attrsets.nix b/lib/attrsets.nix index 4161fa546c8fa..70986195ae054 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 ***/ |