Use builtin filter, elem, concatLists functions if available

This commit is contained in:
Eelco Dolstra 2012-08-13 11:55:45 -04:00
parent 3bf4437622
commit 431c55cbf1
2 changed files with 13 additions and 9 deletions

View file

@ -5,7 +5,7 @@ with {
inherit (import ./trivial.nix) or;
inherit (import ./default.nix) fold;
inherit (import ./strings.nix) concatStringsSep;
inherit (import ./lists.nix) concatMap;
inherit (import ./lists.nix) concatMap concatLists;
inherit (import ./misc.nix) eqStrict;
};
@ -66,7 +66,7 @@ rec {
catAttrs "a" [{a = 1;} {b = 0;} {a = 2;}]
=> [1 2]
*/
catAttrs = attr: l: fold (s: l: if hasAttr attr s then [(getAttr attr s)] ++ l else l) [] l;
catAttrs = attr: l: concatLists (map (s: if hasAttr attr s then [(getAttr attr s)] else []) l);
/* Filter an attribute set by removing all attributes for which the

View file

@ -35,7 +35,7 @@ rec {
# Concatenate a list of lists.
concatLists = fold (x: y: x ++ y) [];
concatLists = builtins.concatLists or (fold (x: y: x ++ y) []);
# Map and concatenate the result.
@ -53,20 +53,24 @@ rec {
# Filter a list using a predicate; that is, return a list containing
# every element from `list' for which `pred' returns true.
filter = pred: list:
fold (x: y: if pred x then [x] ++ y else y) [] list;
filter =
builtins.filter or
(pred: list:
fold (x: y: if pred x then [x] ++ y else y) [] list);
# Remove elements 'e' from a list. Useful for buildInputs
# Remove elements equal to 'e' from a list. Useful for buildInputs.
remove = e: filter (x: x != e);
# Given two lists, removes all elements of the first list from the second list
removeList = l: filter (x: elem x l);
# Return true if `list' has an element `x':
elem = x: list: fold (a: bs: x == a || bs) false list;
# Return true if `list' has an element `x'.
elem =
builtins.elem or
(x: list: fold (a: bs: x == a || bs) false list);
# Find the sole element in the list matching the specified