From 431c55cbf1f99748cb28566a33d15fb22bf76fe6 Mon Sep 17 00:00:00 2001 From: Eelco Dolstra Date: Mon, 13 Aug 2012 11:55:45 -0400 Subject: [PATCH] Use builtin filter, elem, concatLists functions if available --- pkgs/lib/attrsets.nix | 4 ++-- pkgs/lib/lists.nix | 18 +++++++++++------- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/pkgs/lib/attrsets.nix b/pkgs/lib/attrsets.nix index 8d1521812f9..982f23230c9 100644 --- a/pkgs/lib/attrsets.nix +++ b/pkgs/lib/attrsets.nix @@ -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 diff --git a/pkgs/lib/lists.nix b/pkgs/lib/lists.nix index 67b0add50dc..dcb89aeec14 100644 --- a/pkgs/lib/lists.nix +++ b/pkgs/lib/lists.nix @@ -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