Author: eelco
Date: Thu Apr 5 15:37:52 2012
New Revision: 33626
URL: https://nixos.org/websvn/nix/?rev=33626&sc=1
Log:
* Add a function "filterAttrs" and clean up some comments.
Modified:
nixpkgs/trunk/pkgs/lib/attrsets.nix
nixpkgs/trunk/pkgs/lib/lists.nix
Modified: nixpkgs/trunk/pkgs/lib/attrsets.nix
==============================================================================
--- nixpkgs/trunk/pkgs/lib/attrsets.nix Thu Apr 5 15:31:10 2012 (r33625)
+++ nixpkgs/trunk/pkgs/lib/attrsets.nix Thu Apr 5 15:37:52 2012 (r33626)
@@ -78,6 +78,17 @@
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.
Modified: nixpkgs/trunk/pkgs/lib/lists.nix
==============================================================================
--- nixpkgs/trunk/pkgs/lib/lists.nix Thu Apr 5 15:31:10 2012 (r33625)
+++ nixpkgs/trunk/pkgs/lib/lists.nix Thu Apr 5 15:37:52 2012 (r33626)
@@ -27,11 +27,13 @@
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 @@
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 @@
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 @@
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 [];
+
}
_______________________________________________
nix-commits mailing list
[email protected]
http://lists.science.uu.nl/mailman/listinfo/nix-commits