From 1b640d276c8af0a0870aced12196c5ffd7b5f188 Mon Sep 17 00:00:00 2001 From: FORNO Date: Sun, 30 Jun 2024 00:47:22 +0900 Subject: [PATCH] Add tap for Identity (#1943) --- src/Identity.ts | 17 ++++++++++++++--- test/Identity.ts | 5 +++++ 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/src/Identity.ts b/src/Identity.ts index da78aa162..ecd76e77b 100644 --- a/src/Identity.ts +++ b/src/Identity.ts @@ -4,7 +4,7 @@ import { Alt1 } from './Alt' import { Applicative as ApplicativeHKT, Applicative1 } from './Applicative' import { apFirst as apFirst_, Apply1, apS as apS_, apSecond as apSecond_ } from './Apply' -import { bind as bind_, Chain1, chainFirst as chainFirst_ } from './Chain' +import { bind as bind_, Chain1, tap as tap_ } from './Chain' import { ChainRec1, tailRec } from './ChainRec' import { Comonad1 } from './Comonad' import { Eq } from './Eq' @@ -275,10 +275,21 @@ export const Monad: Monad1 = { * Composes computations in sequence, using the return value of one computation to determine the next computation and * keeping only the result of the first. * - * @category sequencing + * @category combinators + * @since 2.16.7 + */ +export const tap: { + (self: Identity, f: (a: A) => Identity<_>): Identity + (f: (a: A) => Identity<_>): (self: Identity) => Identity +} = /*#__PURE__*/ dual(2, tap_(Chain)) + +/** + * Alias of `tap` + * + * @category legacy * @since 2.0.0 */ -export const chainFirst: (f: (a: A) => B) => (first: A) => A = /*#__PURE__*/ chainFirst_(Chain) +export const chainFirst: (f: (a: A) => B) => (first: A) => A = tap /** * @category instances diff --git a/test/Identity.ts b/test/Identity.ts index a813967ec..3a235c424 100644 --- a/test/Identity.ts +++ b/test/Identity.ts @@ -36,6 +36,11 @@ describe.concurrent('Identity', () => { U.deepStrictEqual(pipe(1, _.chain(f)), 2) }) + it('tap', () => { + const f = (n: number) => n * 2 + U.deepStrictEqual(pipe(1, _.tap(f)), 1) + }) + it('chainFirst', () => { const f = (n: number) => n * 2 U.deepStrictEqual(pipe(1, _.chainFirst(f)), 1)