summaryrefslogtreecommitdiffstats
path: root/pkgs/lib
diff options
context:
space:
mode:
authorEelco Dolstra <eelco.dolstra@logicblox.com>2012-04-05 15:37:52 +0000
committerEelco Dolstra <eelco.dolstra@logicblox.com>2012-04-05 15:37:52 +0000
commite78a048265df66a95499b301cee0d7331e534ff1 (patch)
treefd002bc658fa4c27d5b16033ec4c1cd78583ab33 /pkgs/lib
parent68b6d23a350b691a164aae420484756bf781783a (diff)
* Add a function "filterAttrs" and clean up some comments.
svn path=/nixpkgs/trunk/; revision=33626
Diffstat (limited to 'pkgs/lib')
-rw-r--r--pkgs/lib/attrsets.nix11
-rw-r--r--pkgs/lib/lists.nix25
2 files changed, 29 insertions, 7 deletions
diff --git a/pkgs/lib/attrsets.nix b/pkgs/lib/attrsets.nix
index fcaf4f5c9b5c..db4b9fb210c6 100644
--- a/pkgs/lib/attrsets.nix
+++ b/pkgs/lib/attrsets.nix
@@ -78,6 +78,17 @@ rec {
catAttrs = attr: l: fold (s: l: if hasAttr attr s then [(getAttr attr s)] ++ l else l) [] l;
+ /* Filter an attribute set by removing all attributes for which the
+ given predicate return false.
+
+ Example:
+ filterAttrs (n: v: n == "foo") { foo = 1; bar = 2; }
+ => { foo = 1; }
+ */
+ filterAttrs = pred: set:
+ listToAttrs (fold (n: ys: let v = getAttr n set; in if pred n v then [(nameValuePair n v)] ++ ys else ys) [] (attrNames set));
+
+
/* Recursively collect sets that verify a given predicate named `pred'
from the set `attrs'. The recursion is stopped when the predicate is
verified.
diff --git a/pkgs/lib/lists.nix b/pkgs/lib/lists.nix
index e5b47f0d9ce7..67b0add50dcb 100644
--- a/pkgs/lib/lists.nix
+++ b/pkgs/lib/lists.nix
@@ -27,11 +27,13 @@ rec {
then nul
else foldl op (op nul (head list)) (tail list);
+
# map with index: `imap (i: v: "${v}-${toString i}") ["a" "b"] ==
# ["a-1" "b-2"]'
imap = f: list:
zipListsWith f (range 1 (length list)) list;
+
# Concatenate a list of lists.
concatLists = fold (x: y: x ++ y) [];
@@ -54,12 +56,15 @@ rec {
filter = pred: list:
fold (x: y: if pred x then [x] ++ y else y) [] list;
+
# Remove elements '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;
@@ -145,17 +150,19 @@ rec {
zipLists = zipListsWith (fst: snd: { inherit fst snd; });
- # invert the order of the elements of a list.
+
+ # Reverse the order of the elements of a list.
reverseList = l:
let reverse_ = accu: l:
if l == [] then accu
else reverse_ ([(head l)] ++ accu) (tail l);
in reverse_ [] l;
- # Sort a list based on the `strictLess' function which compare the two
- # elements and return true if the first argument is strictly below the
- # second argument. The returned list is sorted in an increasing order.
- # The implementation does a quick-sort.
+
+ # Sort a list based on a comparator function which compares two
+ # elements and returns true if the first argument is strictly below
+ # the second argument. The returned list is sorted in an increasing
+ # order. The implementation does a quick-sort.
sort = strictLess: list:
let
# This implementation only have one element lists on the left hand
@@ -171,25 +178,29 @@ rec {
qs list [];
- # haskell's take: take 2 [1 2 3 4] yields [1 2]
+ # Return the first (at most) N elements of a list.
take = count: list:
if list == [] || count == 0 then []
else [ (head list) ] ++ take (builtins.sub count 1) (tail list);
- # haskell's drop. drop count elements from head of list
+
+ # Remove the first (at most) N elements of a list.
drop = count: list:
if count == 0 then list
else drop (builtins.sub count 1) (tail list);
+
last = list:
assert list != [];
let loop = l: if tail l == [] then head l else loop (tail l); in
loop list;
+
# Zip two lists together.
zipTwoLists = xs: ys:
if xs != [] && ys != [] then
[ {first = head xs; second = head ys;} ]
++ zipTwoLists (tail xs) (tail ys)
else [];
+
}