From 9beef9f1ba3c97c939c38af5c0ea6752fff11bb5 Mon Sep 17 00:00:00 2001 From: Johannes Kirschbauer Date: Fri, 15 Mar 2024 23:42:48 +0100 Subject: [PATCH 1/2] lib.foldl': avoid unnecessary function call --- lib/lists.nix | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/lib/lists.nix b/lib/lists.nix index b12b78754a38..d8cf8732d239 100644 --- a/lib/lists.nix +++ b/lib/lists.nix @@ -254,13 +254,11 @@ rec { foldl' = op: acc: - list: - # The builtin `foldl'` is a bit lazier than one might expect. # See https://github.com/NixOS/nix/pull/7158. # In particular, the initial accumulator value is not forced before the first iteration starts. builtins.seq acc - (builtins.foldl' op acc list); + (builtins.foldl' op acc); /** Map with index starting from 0 From c8885b86b269742bb6618b8d841cffb4e7725504 Mon Sep 17 00:00:00 2001 From: Johannes Kirschbauer Date: Sat, 16 Mar 2024 22:58:14 +0100 Subject: [PATCH 2/2] lib.foldl': document eta expansion --- lib/lists.nix | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/lib/lists.nix b/lib/lists.nix index d8cf8732d239..c162f921280d 100644 --- a/lib/lists.nix +++ b/lib/lists.nix @@ -228,7 +228,15 @@ rec { `acc` - : The initial accumulator value + : The initial accumulator value. + + The accumulator value is evaluated in any case before the first iteration starts. + + To avoid evaluation even before the `list` argument is given an eta expansion can be used: + + ```nix + list: lib.foldl' op acc list + ``` `list`