git.iko.soy/typeable/nixpkgs
on master help sign in
lib: introduce imap0, imap1 (#25543)

* lib: introduce imap0, imap1 For historical reasons, imap starts counting at 1 and it's not consistent with the rest of the lib. So for now we split imap into imap0 that starts counting at zero and imap1 that starts counting at 1. And imap is marked as deprecated. See https://github.com/NixOS/nixpkgs/commit/c71e2d42359f9900ea2c290d141c0d606471da16#commitcomment-21873221 * replace uses of lib.imap * lib: move imap to deprecated.nix

commit4d545297d85c8f32f7ab496d0759f40d881bd61d authorzimbatm <zimbatm@zimbatm.com> dateTue 4 Jul 2017, 23:29 (9 years ago) parenta0fa617
11 files changed+31−17
lib/deprecated.nix+8 lib/lists.nix+10−4 lib/modules.nix+1−1 lib/strings.nix+2−2 lib/types.nix+4−4 nixos/modules/misc/meta.nix+1−1 nixos/modules/services/cluster/kubernetes.nix+1−1 nixos/modules/services/networking/libreswan.nix+1−1 nixos/modules/services/networking/networkmanager.nix+1−1 nixos/modules/services/x11/xserver.nix+1−1 pkgs/games/uqm/default.nix+1−1
lib/deprecated.nix+8
@@ -423,4 +423,12 @@ rec {
423423 else if isInt x then "int"
424424 else "string";
425425
426+ /* deprecated:
427+
428+ For historical reasons, imap has an index starting at 1.
429+
430+ But for consistency with the rest of the library we want an index
431+ starting at zero.
432+ */
433+ imap = imap1;
426434}
lib/lists.nix+10−4
@@ -77,15 +77,21 @@ rec {
7777 */
7878 foldl' = builtins.foldl' or foldl;
7979
80− /* Map with index
80+ /* Map with index starting from 0
8181
82− FIXME(zimbatm): why does this start to count at 1?
82+ Example:
83+ imap0 (i: v: "${v}-${toString i}") ["a" "b"]
84+ => [ "a-0" "b-1" ]
85+ */
86+ imap0 = f: list: genList (n: f n (elemAt list n)) (length list);
87+
88+ /* Map with index starting from 1
8389
8490 Example:
85− imap (i: v: "${v}-${toString i}") ["a" "b"]
91+ imap1 (i: v: "${v}-${toString i}") ["a" "b"]
8692 => [ "a-1" "b-2" ]
8793 */
88− imap = f: list: genList (n: f (n + 1) (elemAt list n)) (length list);
94+ imap1 = f: list: genList (n: f (n + 1) (elemAt list n)) (length list);
8995
9096 /* Map and concatenate the result.
9197
lib/modules.nix+1−1
@@ -98,7 +98,7 @@ rec {
9898 /* Close a set of modules under the ‘imports’ relation. */
9999 closeModules = modules: args:
100100 let
101− toClosureList = file: parentKey: imap (n: x:
101+ toClosureList = file: parentKey: imap1 (n: x:
102102 if isAttrs x || isFunction x then
103103 let key = "${parentKey}:anon-${toString n}"; in
104104 unifyModuleSyntax file key (unpackSubmodule (applyIfFunction key) x args)
lib/strings.nix+2−2
@@ -33,7 +33,7 @@ rec {
3333 concatImapStrings (pos: x: "${toString pos}-${x}") ["foo" "bar"]
3434 => "1-foo2-bar"
3535 */
36− concatImapStrings = f: list: concatStrings (lib.imap f list);
36+ concatImapStrings = f: list: concatStrings (lib.imap1 f list);
3737
3838 /* Place an element between each element of a list
3939
@@ -70,7 +70,7 @@ rec {
7070 concatImapStringsSep "-" (pos: x: toString (x / pos)) [ 6 6 6 ]
7171 => "6-3-2"
7272 */
73− concatImapStringsSep = sep: f: list: concatStringsSep sep (lib.imap f list);
73+ concatImapStringsSep = sep: f: list: concatStringsSep sep (lib.imap1 f list);
7474
7575 /* Construct a Unix-style search path consisting of each `subDir"
7676 directory of the given list of packages.
lib/types.nix+4−4
@@ -179,9 +179,9 @@ rec {
179179 description = "list of ${elemType.description}s";
180180 check = isList;
181181 merge = loc: defs:
182− map (x: x.value) (filter (x: x ? value) (concatLists (imap (n: def:
182+ map (x: x.value) (filter (x: x ? value) (concatLists (imap1 (n: def:
183183 if isList def.value then
184− imap (m: def':
184+ imap1 (m: def':
185185 (mergeDefinitions
186186 (loc ++ ["[definition ${toString n}-entry ${toString m}]"])
187187 elemType
@@ -220,7 +220,7 @@ rec {
220220 if isList def.value then
221221 { inherit (def) file;
222222 value = listToAttrs (
223− imap (elemIdx: elem:
223+ imap1 (elemIdx: elem:
224224 { name = elem.name or "unnamed-${toString defIdx}.${toString elemIdx}";
225225 value = elem;
226226 }) def.value);
@@ -233,7 +233,7 @@ rec {
233233 name = "loaOf";
234234 description = "list or attribute set of ${elemType.description}s";
235235 check = x: isList x || isAttrs x;
236− merge = loc: defs: attrOnly.merge loc (imap convertIfList defs);
236+ merge = loc: defs: attrOnly.merge loc (imap1 convertIfList defs);
237237 getSubOptions = prefix: elemType.getSubOptions (prefix ++ ["<name?>"]);
238238 getSubModules = elemType.getSubModules;
239239 substSubModules = m: loaOf (elemType.substSubModules m);
nixos/modules/misc/meta.nix+1−1
@@ -17,7 +17,7 @@ let
1717 # }
1818 merge = loc: defs:
1919 zipAttrs
20− (flatten (imap (n: def: imap (m: def':
20+ (flatten (imap1 (n: def: imap1 (m: def':
2121 maintainer.merge (loc ++ ["[${toString n}-${toString m}]"])
2222 [{ inherit (def) file; value = def'; }]) def.value) defs));
2323 };
nixos/modules/services/cluster/kubernetes.nix+1−1
@@ -44,7 +44,7 @@ let
4444
4545 cniConfig = pkgs.buildEnv {
4646 name = "kubernetes-cni-config";
47− paths = imap (i: entry:
47+ paths = imap1 (i: entry:
4848 pkgs.writeTextDir "${toString (10+i)}-${entry.type}.conf" (builtins.toJSON entry)
4949 ) cfg.kubelet.cni.config;
5050 };
nixos/modules/services/networking/libreswan.nix+1−1
@@ -11,7 +11,7 @@ let
1111
1212 trim = chars: str: let
1313 nonchars = filter (x : !(elem x.value chars))
14− (imap (i: v: {ind = (sub i 1); value = v;}) (stringToCharacters str));
14+ (imap0 (i: v: {ind = i; value = v;}) (stringToCharacters str));
1515 in
1616 if length nonchars == 0 then ""
1717 else substring (head nonchars).ind (add 1 (sub (last nonchars).ind (head nonchars).ind)) str;
nixos/modules/services/networking/networkmanager.nix+1−1
@@ -253,7 +253,7 @@ in {
253253 { source = overrideNameserversScript;
254254 target = "NetworkManager/dispatcher.d/02overridedns";
255255 }
256− ++ lib.imap (i: s: {
256+ ++ lib.imap1 (i: s: {
257257 inherit (s) source;
258258 target = "NetworkManager/dispatcher.d/${dispatcherTypesSubdirMap.${s.type}}03userscript${lib.fixedWidthNumber 4 i}";
259259 }) cfg.dispatcherScripts;
nixos/modules/services/x11/xserver.nix+1−1
@@ -71,7 +71,7 @@ let
7171 name = "multihead${toString num}";
7272 inherit config;
7373 };
74− in imap mkHead cfg.xrandrHeads;
74+ in imap1 mkHead cfg.xrandrHeads;
7575
7676 xrandrDeviceSection = let
7777 monitors = flip map xrandrHeads (h: ''
pkgs/games/uqm/default.nix+1−1
@@ -18,7 +18,7 @@ let
1818 inherit stdenv requireFile writeText fetchurl haskellPackages;
1919 };
2020
21− remixPacks = imap (num: sha256: fetchurl rec {
21+ remixPacks = imap1 (num: sha256: fetchurl rec {
2222 name = "uqm-remix-disc${toString num}.uqm";
2323 url = "mirror://sourceforge/sc2/${name}";
2424 inherit sha256;