From 273fa99d716aa7df20118b5ec6de750c560fe55e Mon Sep 17 00:00:00 2001 From: Philipp Schuster Date: Sun, 29 Oct 2023 17:29:37 +0100 Subject: [PATCH 1/2] doc: primops: add more info for foldl From the existing doc it is not obvious whether the first or the second argument is the accumulator. This is however relevant to know, as for certain scenarios, this might change the behavior. --- src/libexpr/primops.cc | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/libexpr/primops.cc b/src/libexpr/primops.cc index 704e7007b08..d602517aa1a 100644 --- a/src/libexpr/primops.cc +++ b/src/libexpr/primops.cc @@ -3180,9 +3180,14 @@ static RegisterPrimOp primop_foldlStrict({ .doc = R"( Reduce a list by applying a binary operator, from left to right, e.g. `foldl' op nul [x0 x1 x2 ...] = op (op (op nul x0) x1) x2) - ...`. For example, `foldl' (x: y: x + y) 0 [1 2 3]` evaluates to 6. - The return value of each application of `op` is evaluated immediately, - even for intermediate values. + ...`. + For example, `foldl' (acc: elem: acc + elem) 0 [1 2 3]` evaluates + to `6` and `foldl' (acc: elem: { "${elem}" = elem; } // acc) {} + ["a" "b"]` evaluates to `{ a = "a"; b = "b"; }`. + The first argument of `op` is the accumulator wheres the second + argument is the current element being processed. The return value + of each application of `op` is evaluated immediately, even for + intermediate values. )", .fun = prim_foldlStrict, }); From 6a0f3d54e3eef4f762131bd9bc56b53a7cbd6c16 Mon Sep 17 00:00:00 2001 From: Valentin Gagarin Date: Thu, 23 Nov 2023 21:27:49 +0100 Subject: [PATCH 2/2] Update primops.cc --- src/libexpr/primops.cc | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/libexpr/primops.cc b/src/libexpr/primops.cc index d602517aa1a..0721384b8d1 100644 --- a/src/libexpr/primops.cc +++ b/src/libexpr/primops.cc @@ -3181,9 +3181,11 @@ static RegisterPrimOp primop_foldlStrict({ Reduce a list by applying a binary operator, from left to right, e.g. `foldl' op nul [x0 x1 x2 ...] = op (op (op nul x0) x1) x2) ...`. + For example, `foldl' (acc: elem: acc + elem) 0 [1 2 3]` evaluates to `6` and `foldl' (acc: elem: { "${elem}" = elem; } // acc) {} ["a" "b"]` evaluates to `{ a = "a"; b = "b"; }`. + The first argument of `op` is the accumulator wheres the second argument is the current element being processed. The return value of each application of `op` is evaluated immediately, even for