diff --git a/crates/nu-std/std/iter/mod.nu b/crates/nu-std/std/iter/mod.nu index 8c25269690..daa62d2dba 100644 --- a/crates/nu-std/std/iter/mod.nu +++ b/crates/nu-std/std/iter/mod.nu @@ -101,8 +101,9 @@ export def intersperse [ # -> list # Returns a list of intermediate steps performed by `reduce` # (`fold`). It takes two arguments, an initial value to seed the # initial state and a closure that takes two arguments, the first -# being the internal state and the second the list element in the -# current iteration. +# being the list element in the current iteration and the second +# the internal state. +# The internal state is also provided as pipeline input. # # # Example # ``` @@ -123,7 +124,8 @@ export def scan [ # -> list --noinit(-n) # remove the initial value from the result ] { reduce --fold [$init] {|it, acc| - $acc ++ [(do $fn ($acc | last) $it)] + let acc_last = $acc | last + $acc ++ [($acc_last | do $fn $it $acc_last)] } | if $noinit { $in | skip diff --git a/crates/nu-std/tests/test_iter.nu b/crates/nu-std/tests/test_iter.nu index 2b768c3d9d..3ace342788 100644 --- a/crates/nu-std/tests/test_iter.nu +++ b/crates/nu-std/tests/test_iter.nu @@ -49,7 +49,10 @@ def iter_scan [] { let scanned = ([1 2 3] | iter scan 0 {|x, y| $x + $y}) assert equal $scanned [0, 1, 3, 6] - let scanned = ([a b c d] | iter scan "" {|x, y| [$x, $y] | str join} -n) + let scanned = ([a b c d] | iter scan "" {|it, acc| [$acc, $it] | str join} -n) + assert equal $scanned ["a" "ab" "abc" "abcd"] + + let scanned = ([a b c d] | iter scan "" {|it, acc| append $it | str join} -n) assert equal $scanned ["a" "ab" "abc" "abcd"] }