* Add a function "filterAttrs" and clean up some comments.
svn path=/nixpkgs/trunk/; revision=33626
This commit is contained in:
parent
68b6d23a35
commit
e78a048265
|
@ -78,6 +78,17 @@ rec {
|
||||||
catAttrs = attr: l: fold (s: l: if hasAttr attr s then [(getAttr attr s)] ++ l else l) [] l;
|
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'
|
/* Recursively collect sets that verify a given predicate named `pred'
|
||||||
from the set `attrs'. The recursion is stopped when the predicate is
|
from the set `attrs'. The recursion is stopped when the predicate is
|
||||||
verified.
|
verified.
|
||||||
|
|
|
@ -27,11 +27,13 @@ rec {
|
||||||
then nul
|
then nul
|
||||||
else foldl op (op nul (head list)) (tail list);
|
else foldl op (op nul (head list)) (tail list);
|
||||||
|
|
||||||
|
|
||||||
# map with index: `imap (i: v: "${v}-${toString i}") ["a" "b"] ==
|
# map with index: `imap (i: v: "${v}-${toString i}") ["a" "b"] ==
|
||||||
# ["a-1" "b-2"]'
|
# ["a-1" "b-2"]'
|
||||||
imap = f: list:
|
imap = f: list:
|
||||||
zipListsWith f (range 1 (length list)) list;
|
zipListsWith f (range 1 (length list)) list;
|
||||||
|
|
||||||
|
|
||||||
# Concatenate a list of lists.
|
# Concatenate a list of lists.
|
||||||
concatLists = fold (x: y: x ++ y) [];
|
concatLists = fold (x: y: x ++ y) [];
|
||||||
|
|
||||||
|
@ -54,12 +56,15 @@ rec {
|
||||||
filter = pred: list:
|
filter = pred: list:
|
||||||
fold (x: y: if pred x then [x] ++ y else y) [] list;
|
fold (x: y: if pred x then [x] ++ y else y) [] list;
|
||||||
|
|
||||||
|
|
||||||
# Remove elements 'e' from a list. Useful for buildInputs
|
# Remove elements 'e' from a list. Useful for buildInputs
|
||||||
remove = e: filter (x: x != e);
|
remove = e: filter (x: x != e);
|
||||||
|
|
||||||
|
|
||||||
# Given two lists, removes all elements of the first list from the second list
|
# Given two lists, removes all elements of the first list from the second list
|
||||||
removeList = l: filter (x: elem x l);
|
removeList = l: filter (x: elem x l);
|
||||||
|
|
||||||
|
|
||||||
# Return true if `list' has an element `x':
|
# Return true if `list' has an element `x':
|
||||||
elem = x: list: fold (a: bs: x == a || bs) false list;
|
elem = x: list: fold (a: bs: x == a || bs) false list;
|
||||||
|
|
||||||
|
@ -145,17 +150,19 @@ rec {
|
||||||
|
|
||||||
zipLists = zipListsWith (fst: snd: { inherit fst snd; });
|
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:
|
reverseList = l:
|
||||||
let reverse_ = accu: l:
|
let reverse_ = accu: l:
|
||||||
if l == [] then accu
|
if l == [] then accu
|
||||||
else reverse_ ([(head l)] ++ accu) (tail l);
|
else reverse_ ([(head l)] ++ accu) (tail l);
|
||||||
in reverse_ [] 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
|
# Sort a list based on a comparator function which compares two
|
||||||
# second argument. The returned list is sorted in an increasing order.
|
# elements and returns true if the first argument is strictly below
|
||||||
# The implementation does a quick-sort.
|
# the second argument. The returned list is sorted in an increasing
|
||||||
|
# order. The implementation does a quick-sort.
|
||||||
sort = strictLess: list:
|
sort = strictLess: list:
|
||||||
let
|
let
|
||||||
# This implementation only have one element lists on the left hand
|
# This implementation only have one element lists on the left hand
|
||||||
|
@ -171,25 +178,29 @@ rec {
|
||||||
qs list [];
|
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:
|
take = count: list:
|
||||||
if list == [] || count == 0 then []
|
if list == [] || count == 0 then []
|
||||||
else [ (head list) ] ++ take (builtins.sub count 1) (tail list);
|
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:
|
drop = count: list:
|
||||||
if count == 0 then list
|
if count == 0 then list
|
||||||
else drop (builtins.sub count 1) (tail list);
|
else drop (builtins.sub count 1) (tail list);
|
||||||
|
|
||||||
|
|
||||||
last = list:
|
last = list:
|
||||||
assert list != [];
|
assert list != [];
|
||||||
let loop = l: if tail l == [] then head l else loop (tail l); in
|
let loop = l: if tail l == [] then head l else loop (tail l); in
|
||||||
loop list;
|
loop list;
|
||||||
|
|
||||||
|
|
||||||
# Zip two lists together.
|
# Zip two lists together.
|
||||||
zipTwoLists = xs: ys:
|
zipTwoLists = xs: ys:
|
||||||
if xs != [] && ys != [] then
|
if xs != [] && ys != [] then
|
||||||
[ {first = head xs; second = head ys;} ]
|
[ {first = head xs; second = head ys;} ]
|
||||||
++ zipTwoLists (tail xs) (tail ys)
|
++ zipTwoLists (tail xs) (tail ys)
|
||||||
else [];
|
else [];
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue