git.iko.soy/typeable/nixpkgs
on master help sign in
lib/attrsets: document all the functions
commit523e3283189710a9a58bd2828f45a7413f3ede3e authorzimbatm <zimbatm@zimbatm.com> dateSun 28 Feb 2016, 21:49 (11 years ago) parenta086858
1 file changed+109−28
lib/attrsets.nix+109−28
lib/attrsets.nix+109−28
@@ -12,9 +12,15 @@ rec {
1212 inherit (builtins) attrNames listToAttrs hasAttr isAttrs getAttr;
1313
1414
15− /* Return an attribute from nested attribute sets. For instance
16− ["x" "y"] applied to some set e returns e.x.y, if it exists. The
17− default value is returned otherwise. */
15+ /* Return an attribute from nested attribute sets.
16+
17+ Example:
18+ x = { a = { b = 3; }; }
19+ attrByPath ["a" "b"] 6 x
20+ => 3
21+ attrByPath ["z" "z"] 6 x
22+ => 6
23+ */
1824 attrByPath = attrPath: default: e:
1925 let attr = head attrPath;
2026 in
@@ -24,8 +30,15 @@ rec {
2430 else default;
2531
2632 /* Return if an attribute from nested attribute set exists.
27− For instance ["x" "y"] applied to some set e returns true, if e.x.y exists. False
28− is returned otherwise. */
33+
34+ Example:
35+ x = { a = { b = 3; }; }
36+ hasAttrByPath ["a" "b"] x
37+ => true
38+ hasAttrByPath ["z" "z"] x
39+ => false
40+
41+ */
2942 hasAttrByPath = attrPath: e:
3043 let attr = head attrPath;
3144 in
@@ -35,14 +48,28 @@ rec {
3548 else false;
3649
3750
38− /* Return nested attribute set in which an attribute is set. For instance
39− ["x" "y"] applied with some value v returns `x.y = v;' */
51+ /* Return nested attribute set in which an attribute is set.
52+
53+ Example:
54+ setAttrByPath ["a" "b"] 3
55+ => { a = { b = 3; }; }
56+ */
4057 setAttrByPath = attrPath: value:
4158 if attrPath == [] then value
4259 else listToAttrs
4360 [ { name = head attrPath; value = setAttrByPath (tail attrPath) value; } ];
4461
4562
63+ /* Like `getAttrPath' without a default value. If it doesn't find the
64+ path it will throw.
65+
66+ Example:
67+ x = { a = { b = 3; }; }
68+ getAttrFromPath ["a" "b"] x
69+ => 3
70+ getAttrFromPath ["z" "z"] x
71+ => error: cannot find attribute `z.z'
72+ */
4673 getAttrFromPath = attrPath: set:
4774 let errorMsg = "cannot find attribute `" + concatStringsSep "." attrPath + "'";
4875 in attrByPath attrPath (abort errorMsg) set;
@@ -109,9 +136,11 @@ rec {
109136 ) (attrNames set)
110137 );
111138
112− /* foldAttrs: apply fold functions to values grouped by key. Eg accumulate values as list:
113− foldAttrs (n: a: [n] ++ a) [] [{ a = 2; } { a = 3; }]
114− => { a = [ 2 3 ]; }
139+ /* Apply fold functions to values grouped by key.
140+
141+ Example:
142+ foldAttrs (n: a: [n] ++ a) [] [{ a = 2; } { a = 3; }]
143+ => { a = [ 2 3 ]; }
115144 */
116145 foldAttrs = op: nul: list_of_attrs:
117146 fold (n: a:
@@ -147,7 +176,12 @@ rec {
147176
148177
149178 /* Utility function that creates a {name, value} pair as expected by
150− builtins.listToAttrs. */
179+ builtins.listToAttrs.
180+
181+ Example:
182+ nameValuePair "some" 6
183+ => { name = "some"; value = 6; }
184+ */
151185 nameValuePair = name: value: { inherit name value; };
152186
153187
@@ -248,11 +282,19 @@ rec {
248282 listToAttrs (map (n: nameValuePair n (f n)) names);
249283
250284
251− /* Check whether the argument is a derivation. */
252− isDerivation = x: isAttrs x && x ? type && x.type == "derivation";
285+ /* Check whether the argument is a derivation. Any set with
286+ { type = "derivation"; } counts as a derivation.
253287
288+ Example:
289+ nixpkgs = import <nixpkgs> {}
290+ isDerivation nixpkgs.ruby
291+ => true
292+ isDerivation "foobar"
293+ => false
294+ */
295+ isDerivation = x: isAttrs x && x ? type && x.type == "derivation";
254296
255− /* Convert a store path to a fake derivation. */
297+ /* Converts a store path to a fake derivation. */
256298 toDerivation = path:
257299 let path' = builtins.storePath path; in
258300 { type = "derivation";
@@ -262,32 +304,49 @@ rec {
262304 };
263305
264306
265− /* If the Boolean `cond' is true, return the attribute set `as',
266− otherwise an empty attribute set. */
307+ /* If `cond' is true, return the attribute set `as',
308+ otherwise an empty attribute set.
309+
310+ Example:
311+ optionalAttrs (true) { my = "set"; }
312+ => { my = "set"; }
313+ optionalAttrs (false) { my = "set"; }
314+ => { }
315+ */
267316 optionalAttrs = cond: as: if cond then as else {};
268317
269318
270319 /* Merge sets of attributes and use the function f to merge attributes
271− values. */
320+ values.
321+
322+ Example:
323+ zipAttrsWithNames ["a"] (name: vs: vs) [{a = "x";} {a = "y"; b = "z";}]
324+ => { a = ["x" "y"]; }
325+ */
272326 zipAttrsWithNames = names: f: sets:
273327 listToAttrs (map (name: {
274328 inherit name;
275329 value = f name (catAttrs name sets);
276330 }) names);
277331
278− # implentation note: Common names appear multiple times in the list of
279− # names, hopefully this does not affect the system because the maximal
280− # laziness avoid computing twice the same expression and listToAttrs does
281− # not care about duplicated attribute names.
332+ /* Implentation note: Common names appear multiple times in the list of
333+ names, hopefully this does not affect the system because the maximal
334+ laziness avoid computing twice the same expression and listToAttrs does
335+ not care about duplicated attribute names.
336+
337+ Example:
338+ zipAttrsWith (name: values: values) [{a = "x";} {a = "y"; b = "z";}]
339+ => { a = ["x" "y"]; b = ["z"] }
340+ */
282341 zipAttrsWith = f: sets: zipAttrsWithNames (concatMap attrNames sets) f sets;
342+ /* Like `zipAttrsWith' with `(name: values: value)' as the function.
283343
344+ Example:
345+ zipAttrs [{a = "x";} {a = "y"; b = "z";}]
346+ => { a = ["x" "y"]; b = ["z"] }
347+ */
284348 zipAttrs = zipAttrsWith (name: values: values);
285349
286− /* backward compatibility */
287− zipWithNames = zipAttrsWithNames;
288− zip = builtins.trace "lib.zip is deprecated, use lib.zipAttrsWith instead" zipAttrsWith;
289−
290−
291350 /* Does the same as the update operator '//' except that attributes are
292351 merged until the given pedicate is verified. The predicate should
293352 accept 3 arguments which are the path to reach the attribute, a part of
@@ -351,6 +410,15 @@ rec {
351410 !(isAttrs lhs && isAttrs rhs)
352411 ) lhs rhs;
353412
413+ /* Returns true if the pattern is contained in the set. False otherwise.
414+
415+ FIXME(zimbatm): this example doesn't work !!!
416+
417+ Example:
418+ sys = mkSystem { }
419+ matchAttrs { cpu = { bits = 64; }; } sys
420+ => true
421+ */
354422 matchAttrs = pattern: attrs:
355423 fold or false (attrValues (zipAttrsWithNames (attrNames pattern) (n: values:
356424 let pat = head values; val = head (tail values); in
@@ -359,10 +427,23 @@ rec {
359427 else pat == val
360428 ) [pattern attrs]));
361429
362− # override only the attributes that are already present in the old set
363− # useful for deep-overriding
430+ /* Override only the attributes that are already present in the old set
431+ useful for deep-overriding.
432+
433+ Example:
434+ x = { a = { b = 4; c = 3; }; }
435+ overrideExisting x { a = { b = 6; d = 2; }; }
436+ => { a = { b = 6; d = 2; }; }
437+ */
364438 overrideExisting = old: new:
365439 old // listToAttrs (map (attr: nameValuePair attr (attrByPath [attr] old.${attr} new)) (attrNames old));
366440
441+
442+ /*** deprecated stuff ***/
443+
367444 deepSeqAttrs = throw "removed 2016-02-29 because unused and broken";
445+ zipWithNames = zipAttrsWithNames;
446+ zip = builtins.trace
447+ "lib.zip is deprecated, use lib.zipAttrsWith instead" zipAttrsWith;
448+
368449}