Repository: gcanti/fp-ts (ts)

Issue:
StateReaderTaskEither.traverseArray drops state updates between iterations
## 🐛 Bug report

### Current Behavior

In fp-ts 2.10.2, SRTE.traverseArray does not pass the output state from each step to the subsequent step. Each step receives the initial state. As a result, all state transformations are lost except for the final one.

### Expected behavior

In fp-ts 2.9.5, SRTE.traverseArray passes the output state from each step to the subsequent step. This is the expected behavior.

### Reproducible example

```typescript
import * as E from "fp-ts/Either";
import * as SRTE from "fp-ts/StateReaderTaskEither";
import { pipe } from "fp-ts/function";

async function main(): Promise<void> {
  const state = pipe(
    await pipe([1, 2, 3], SRTE.traverseArray(fn))([])({})(),
    E.map(([_, state]) => state),
    E.getOrElseW(() => null)
  );

  console.log(state);
}

function fn(
  v: number
): SRTE.StateReaderTaskEither<ReadonlyArray<number>, {}, Error, void> {
  return SRTE.modify((a) => [...a, v]);
}

main();
```

2.9.5 prints:

```[ 1, 2, 3 ]```

2.10.2 prints:

```[ 3 ]```


| Software   | Version(s) |
| ---------- | ---------- |
| fp-ts          | 2.10.2           |
| TypeScript |   4.2.4         |


Pull request description:
StateReaderTaskEither: `traverseArrayWithIndex` does not pass the out…
…put state from each step to the subsequent step, fix #1486


Patch:
diff --git a/CHANGELOG.md b/CHANGELOG.md
index baeb4745..d7ee9b49 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -15,6 +15,12 @@
 **Note**: A feature tagged as Experimental is in a
 high state of flux, you're at risk of it changing without notice.
 
+# 2.10.3
+
+- **Bug Fix**
+  - `StateReaderTaskEither`
+    - `traverseArrayWithIndex` does not pass the output state from each step to the subsequent step, #1486
+
 # 2.10.2
 
 - **Bug Fix**
diff --git a/package.json b/package.json
index 4941f78c..42ae436c 100644
--- a/package.json
+++ b/package.json
@@ -1,6 +1,6 @@
 {
   "name": "fp-ts",
-  "version": "2.10.2",
+  "version": "2.10.3",
   "description": "Functional programming in TypeScript",
   "main": "lib/index.js",
   "module": "es6/index.js",
diff --git a/src/StateReaderTaskEither.ts b/src/StateReaderTaskEither.ts
index 311ad277..73b3b7cf 100644
--- a/src/StateReaderTaskEither.ts
+++ b/src/StateReaderTaskEither.ts
@@ -965,7 +965,7 @@ export const traverseArrayWithIndex = <S, R, E, A, B>(
           : f(
               i,
               a
-            )(s)(r)().then((eb) => {
+            )(ebs.right[1])(r)().then((eb) => {
               if (E.isLeft(eb)) {
                 return eb
               }


Test patch:
diff --git a/test/StateReaderTaskEither.ts b/test/StateReaderTaskEither.ts
index 10ee5938..03797904 100644
--- a/test/StateReaderTaskEither.ts
+++ b/test/StateReaderTaskEither.ts
@@ -1,7 +1,7 @@
 import * as assert from 'assert'
 import * as A from '../src/Array'
 import * as E from '../src/Either'
-import { pipe } from '../src/function'
+import { pipe, tuple } from '../src/function'
 import * as I from '../src/IO'
 import * as IE from '../src/IOEither'
 import * as O from '../src/Option'
@@ -355,4 +355,17 @@ describe('StateReaderTaskEither', () => {
     const e = await pipe(_.fromState(s), _.evaluate(state))({})()
     U.deepStrictEqual(e, E.right(1))
   })
+
+  it('#1486', async () => {
+    const append = (n: number): _.StateReaderTaskEither<ReadonlyArray<number>, {}, Error, void> =>
+      _.modify((a) => [...a, n])
+    U.deepStrictEqual(
+      await pipe(
+        [1, 2, 3],
+        _.traverseArray(append),
+        _.map(() => undefined)
+      )([])({})(),
+      E.right(tuple(undefined, [1, 2, 3]))
+    )
+  })
 })