From 21e03f1ec383802a6be6983ee9b64d65156b1c73 Mon Sep 17 00:00:00 2001 From: DenisFrezzato Date: Sun, 22 Aug 2021 12:40:27 +0200 Subject: [PATCH] Reader: add apFirstW and apSecondW --- docs/modules/Reader.ts.md | 26 ++++++++++++++++++++++++++ src/Reader.ts | 20 ++++++++++++++++++++ test/Reader.ts | 10 ++++++++++ 3 files changed, 56 insertions(+) diff --git a/docs/modules/Reader.ts.md b/docs/modules/Reader.ts.md index 9347969068..d5ff9a71c8 100644 --- a/docs/modules/Reader.ts.md +++ b/docs/modules/Reader.ts.md @@ -36,7 +36,9 @@ Added in v2.0.0 - [second](#second) - [combinators](#combinators) - [apFirst](#apfirst) + - [apFirstW](#apfirstw) - [apSecond](#apsecond) + - [apSecondW](#apsecondw) - [asksReader](#asksreader) - [asksReaderW](#asksreaderw) - [chainFirst](#chainfirst) @@ -257,6 +259,18 @@ export declare const apFirst: (second: Reader) => (first: Reader< Added in v2.0.0 +## apFirstW + +Less strict version of [`apFirst`](#apfirst). + +**Signature** + +```ts +export declare const apFirstW: (second: Reader) => (first: Reader) => Reader +``` + +Added in v2.12.0 + ## apSecond Combine two effectful actions, keeping only the result of the second. @@ -271,6 +285,18 @@ export declare const apSecond: (second: Reader) => (first: Reader Added in v2.0.0 +## apSecondW + +Less strict version of [`apSecond`](#apsecond). + +**Signature** + +```ts +export declare const apSecondW: (second: Reader) => (first: Reader) => Reader +``` + +Added in v2.12.0 + ## asksReader Effectfully accesses the environment. diff --git a/src/Reader.ts b/src/Reader.ts index ef01433096..08431556e7 100644 --- a/src/Reader.ts +++ b/src/Reader.ts @@ -283,6 +283,16 @@ export const apFirst = /*#__PURE__*/ apFirst_(Apply) +/** + * Less strict version of [`apFirst`](#apfirst). + * + * @category combinators + * @since 2.12.0 + */ +export const apFirstW: (second: Reader) => (first: Reader) => Reader = + /*#__PURE__*/ + apFirst as any + /** * Combine two effectful actions, keeping only the result of the second. * @@ -295,6 +305,16 @@ export const apSecond = /*#__PURE__*/ apSecond_(Apply) +/** + * Less strict version of [`apSecond`](#apsecond). + * + * @category combinators + * @since 2.12.0 + */ +export const apSecondW: (second: Reader) => (first: Reader) => Reader = + /*#__PURE__*/ + apSecond as any + /** * @category instances * @since 2.7.0 diff --git a/test/Reader.ts b/test/Reader.ts index 188cc70528..b6e011c681 100644 --- a/test/Reader.ts +++ b/test/Reader.ts @@ -24,10 +24,20 @@ describe('Reader', () => { U.deepStrictEqual(pipe(_.of('a'), _.apFirst(_.of('b')))({}), 'a') }) + it('apFirstW', () => { + const fb = _.of<{ readonly k: string }, boolean>(true) + U.deepStrictEqual(pipe(_.of<{ readonly x: number }, string>('foo'), _.apFirstW(fb))({ x: 1, k: 'v' }), 'foo') + }) + it('apSecond', () => { U.deepStrictEqual(pipe(_.of('a'), _.apSecond(_.of('b')))({}), 'b') }) + it('apSecondW', () => { + const fb = _.of<{ readonly k: string }, boolean>(true) + U.deepStrictEqual(pipe(_.of<{ readonly x: number }, string>('foo'), _.apSecondW(fb))({ x: 1, k: 'v' }), true) + }) + it('chain', () => { const f = (s: string): _.Reader => _.of(s.length) U.deepStrictEqual(pipe(_.of('foo'), _.chain(f))({}), 3)