Repository: gcanti/fp-ts
Pull request #1478 (2021-04-13 12:06:33)

Add ReaderTask.chainFirstW and Reader.chainFirstW


Patch:
diff --git a/docs/modules/Reader.ts.md b/docs/modules/Reader.ts.md
index 743f059f..b670a2b4 100644
--- a/docs/modules/Reader.ts.md
+++ b/docs/modules/Reader.ts.md
@@ -40,6 +40,7 @@ Added in v2.0.0
   - [asksE](#askse)
   - [asksEW](#asksew)
   - [chainFirst](#chainfirst)
+  - [chainFirstW](#chainfirstw)
   - [flap](#flap)
   - [flatten](#flatten)
   - [local](#local)
@@ -305,6 +306,22 @@ export declare const chainFirst: <A, E, B>(f: (a: A) => Reader<E, B>) => (first:
 
 Added in v2.0.0
 
+## chainFirstW
+
+Less strict version of [`chainFirst`](#chainFirst).
+
+Derivable from `Chain`.
+
+**Signature**
+
+```ts
+export declare const chainFirstW: <R2, A, B>(
+  f: (a: A) => Reader<R2, B>
+) => <R1>(ma: Reader<R1, A>) => Reader<R1 & R2, A>
+```
+
+Added in v2.11.0
+
 ## flap
 
 Derivable from `Functor`.
diff --git a/docs/modules/ReaderTask.ts.md b/docs/modules/ReaderTask.ts.md
index 0178f841..8cbc3a2b 100644
--- a/docs/modules/ReaderTask.ts.md
+++ b/docs/modules/ReaderTask.ts.md
@@ -30,6 +30,7 @@ Added in v2.3.0
   - [chainFirst](#chainfirst)
   - [chainFirstIOK](#chainfirstiok)
   - [chainFirstTaskK](#chainfirsttaskk)
+  - [chainFirstW](#chainfirstw)
   - [chainIOK](#chainiok)
   - [chainReaderK](#chainreaderk)
   - [chainTaskK](#chaintaskk)
@@ -258,6 +259,22 @@ export declare const chainFirstTaskK: <A, B>(f: (a: A) => T.Task<B>) => <E>(firs
 
 Added in v2.10.0
 
+## chainFirstW
+
+Less strict version of [`chainFirst`](#chainFirst).
+
+Derivable from `Chain`.
+
+**Signature**
+
+```ts
+export declare const chainFirstW: <R2, A, B>(
+  f: (a: A) => ReaderTask<R2, B>
+) => <R1>(ma: ReaderTask<R1, A>) => ReaderTask<R1 & R2, A>
+```
+
+Added in v2.11.0
+
 ## chainIOK
 
 **Signature**
diff --git a/src/Reader.ts b/src/Reader.ts
index e268e7eb..4d3fca2d 100644
--- a/src/Reader.ts
+++ b/src/Reader.ts
@@ -332,6 +332,18 @@ export const chainFirst =
   /*#__PURE__*/
   chainFirst_(Chain)
 
+/**
+ * Less strict version of [`chainFirst`](#chainFirst).
+ *
+ * Derivable from `Chain`.
+ *
+ * @category combinators
+ * @since 2.11.0
+ */
+export const chainFirstW: <R2, A, B>(
+  f: (a: A) => Reader<R2, B>
+) => <R1>(ma: Reader<R1, A>) => Reader<R1 & R2, A> = chainFirst as any
+
 /**
  * @category instances
  * @since 2.7.0
diff --git a/src/ReaderTask.ts b/src/ReaderTask.ts
index 1ed0f42a..23948546 100644
--- a/src/ReaderTask.ts
+++ b/src/ReaderTask.ts
@@ -370,6 +370,18 @@ export const chainFirst =
   /*#__PURE__*/
   chainFirst_(Chain)
 
+/**
+ * Less strict version of [`chainFirst`](#chainFirst).
+ *
+ * Derivable from `Chain`.
+ *
+ * @category combinators
+ * @since 2.11.0
+ */
+export const chainFirstW: <R2, A, B>(
+  f: (a: A) => ReaderTask<R2, B>
+) => <R1>(ma: ReaderTask<R1, A>) => ReaderTask<R1 & R2, A> = chainFirst as any
+
 /**
  * @category instances
  * @since 2.10.0


Test patch:
diff --git a/test/Reader.ts b/test/Reader.ts
index 155e8ed7..948ef8b2 100644
--- a/test/Reader.ts
+++ b/test/Reader.ts
@@ -36,6 +36,11 @@ describe('Reader', () => {
       U.deepStrictEqual(pipe(_.of('foo'), _.chainFirst(f))({}), 'foo')
     })
 
+    it('chainFirstW', () => {
+      const f = (s: string) => _.of(s.length)
+      U.deepStrictEqual(pipe(_.of<object, string>('foo'), _.chainFirstW(f))({}), 'foo')
+    })
+
     it('chain', () => {
       U.deepStrictEqual(pipe(_.of(_.of('a')), _.flatten)({}), 'a')
     })
diff --git a/test/ReaderTask.ts b/test/ReaderTask.ts
index 102cca30..0248ebb1 100644
--- a/test/ReaderTask.ts
+++ b/test/ReaderTask.ts
@@ -41,6 +41,11 @@ describe('ReaderTask', () => {
     U.deepStrictEqual(await pipe(_.of('foo'), _.chainFirst(f))({})(), 'foo')
   })
 
+  it('chainFirstW', async () => {
+    const f = (a: string) => _.of(a.length)
+    U.deepStrictEqual(await pipe(_.of<object, string>('foo'), _.chainFirstW(f))({})(), 'foo')
+  })
+
   it('flatten', async () => {
     U.deepStrictEqual(await pipe(_.of(_.of('a')), _.flatten)({})(), 'a')
   })