From 3b6169f87be45c77ec4b56d118a5e2c718ff3f2b Mon Sep 17 00:00:00 2001 From: Silvan Mosberger Date: Fri, 22 Sep 2023 12:22:04 +0200 Subject: lib.lists.foldl': Make strict in the initial accumulator To maintain backwards compatibility, this can't be changed in the Nix language. We can however ensure that the version Nixpkgs has the more intuitive behavior. --- nixos/doc/manual/release-notes/rl-2311.section.md | 3 +++ 1 file changed, 3 insertions(+) (limited to 'nixos') diff --git a/nixos/doc/manual/release-notes/rl-2311.section.md b/nixos/doc/manual/release-notes/rl-2311.section.md index cdb73fb49fa81..011fa84c96af3 100644 --- a/nixos/doc/manual/release-notes/rl-2311.section.md +++ b/nixos/doc/manual/release-notes/rl-2311.section.md @@ -226,6 +226,9 @@ - `networking.networkmanager.firewallBackend` was removed as NixOS is now using iptables-nftables-compat even when using iptables, therefore Networkmanager now uses the nftables backend unconditionally. +- [`lib.lists.foldl'`](https://nixos.org/manual/nixpkgs/stable#function-library-lib.lists.foldl-prime) now always evaluates the initial accumulator argument first. + If you depend on the lazier behavior, consider using [`lib.lists.foldl`](https://nixos.org/manual/nixpkgs/stable#function-library-lib.lists.foldl) or [`builtins.foldl'`](https://nixos.org/manual/nix/stable/language/builtins.html#builtins-foldl') instead. + - `rome` was removed because it is no longer maintained and is succeeded by `biome`. - The `services.mtr-exporter.target` has been removed in favor of `services.mtr-exporter.jobs` which allows specifying multiple targets. -- cgit 1.4.1 From dd72ff27f783ff62c93d78f625633a09c4658344 Mon Sep 17 00:00:00 2001 From: Silvan Mosberger Date: Fri, 22 Sep 2023 12:24:27 +0200 Subject: lib.attrsets.foldlAttrs: Make stricter See the parent commit for the same change to lib.lists.foldl' --- lib/attrsets.nix | 10 +++++----- lib/tests/misc.nix | 4 ++-- nixos/doc/manual/release-notes/rl-2311.section.md | 2 ++ 3 files changed, 9 insertions(+), 7 deletions(-) (limited to 'nixos') diff --git a/lib/attrsets.nix b/lib/attrsets.nix index 11932c05dd29b..b8960cf73f208 100644 --- a/lib/attrsets.nix +++ b/lib/attrsets.nix @@ -338,7 +338,7 @@ rec { ); /* - Like builtins.foldl' but for attribute sets. + Like [`lib.lists.foldl'`](#function-library-lib.lists.foldl-prime) but for attribute sets. Iterates over every name-value pair in the given attribute set. The result of the callback function is often called `acc` for accumulator. It is passed between callbacks from left to right and the final `acc` is the return value of `foldlAttrs`. @@ -372,9 +372,9 @@ rec { 123 foldlAttrs - (_: _: v: v) - (throw "initial accumulator not needed") - { z = 3; a = 2; }; + (acc: _: _: acc) + 3 + { z = throw "value not needed"; a = throw "value not needed"; }; -> 3 @@ -392,7 +392,7 @@ rec { foldlAttrs :: ( a -> String -> b -> a ) -> a -> { ... :: b } -> a */ foldlAttrs = f: init: set: - builtins.foldl' + foldl' (acc: name: f acc name set.${name}) init (attrNames set); diff --git a/lib/tests/misc.nix b/lib/tests/misc.nix index d40d92049880d..ec306acbb765f 100644 --- a/lib/tests/misc.nix +++ b/lib/tests/misc.nix @@ -740,7 +740,7 @@ runTests { # should just return the initial value emptySet = foldlAttrs (throw "function not needed") 123 { }; # should just evaluate to the last value - accNotNeeded = foldlAttrs (_acc: _name: v: v) (throw "accumulator not needed") { z = 3; a = 2; }; + valuesNotNeeded = foldlAttrs (acc: _name: _v: acc) 3 { z = throw "value z not needed"; a = throw "value a not needed"; }; # the accumulator doesnt have to be an attrset it can be as trivial as being just a number or string trivialAcc = foldlAttrs (acc: _name: v: acc * 10 + v) 1 { z = 1; a = 2; }; }; @@ -750,7 +750,7 @@ runTests { names = [ "bar" "foo" ]; }; emptySet = 123; - accNotNeeded = 3; + valuesNotNeeded = 3; trivialAcc = 121; }; }; diff --git a/nixos/doc/manual/release-notes/rl-2311.section.md b/nixos/doc/manual/release-notes/rl-2311.section.md index 011fa84c96af3..b5a1986b7a3e8 100644 --- a/nixos/doc/manual/release-notes/rl-2311.section.md +++ b/nixos/doc/manual/release-notes/rl-2311.section.md @@ -229,6 +229,8 @@ - [`lib.lists.foldl'`](https://nixos.org/manual/nixpkgs/stable#function-library-lib.lists.foldl-prime) now always evaluates the initial accumulator argument first. If you depend on the lazier behavior, consider using [`lib.lists.foldl`](https://nixos.org/manual/nixpkgs/stable#function-library-lib.lists.foldl) or [`builtins.foldl'`](https://nixos.org/manual/nix/stable/language/builtins.html#builtins-foldl') instead. +- [`lib.attrsets.foldlAttrs`](https://nixos.org/manual/nixpkgs/stable#function-library-lib.attrsets.foldlAttrs) now always evaluates the initial accumulator argument first. + - `rome` was removed because it is no longer maintained and is succeeded by `biome`. - The `services.mtr-exporter.target` has been removed in favor of `services.mtr-exporter.jobs` which allows specifying multiple targets. -- cgit 1.4.1