Skip to content

Commit 1b640d2

Browse files
authored
Add tap for Identity (#1943)
1 parent cabd677 commit 1b640d2

File tree

2 files changed

+19
-3
lines changed

2 files changed

+19
-3
lines changed

src/Identity.ts

+14-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import { Alt1 } from './Alt'
55
import { Applicative as ApplicativeHKT, Applicative1 } from './Applicative'
66
import { apFirst as apFirst_, Apply1, apS as apS_, apSecond as apSecond_ } from './Apply'
7-
import { bind as bind_, Chain1, chainFirst as chainFirst_ } from './Chain'
7+
import { bind as bind_, Chain1, tap as tap_ } from './Chain'
88
import { ChainRec1, tailRec } from './ChainRec'
99
import { Comonad1 } from './Comonad'
1010
import { Eq } from './Eq'
@@ -275,10 +275,21 @@ export const Monad: Monad1<URI> = {
275275
* Composes computations in sequence, using the return value of one computation to determine the next computation and
276276
* keeping only the result of the first.
277277
*
278-
* @category sequencing
278+
* @category combinators
279+
* @since 2.16.7
280+
*/
281+
export const tap: {
282+
<A, _>(self: Identity<A>, f: (a: A) => Identity<_>): Identity<A>
283+
<A, _>(f: (a: A) => Identity<_>): (self: Identity<A>) => Identity<A>
284+
} = /*#__PURE__*/ dual(2, tap_(Chain))
285+
286+
/**
287+
* Alias of `tap`
288+
*
289+
* @category legacy
279290
* @since 2.0.0
280291
*/
281-
export const chainFirst: <A, B>(f: (a: A) => B) => (first: A) => A = /*#__PURE__*/ chainFirst_(Chain)
292+
export const chainFirst: <A, B>(f: (a: A) => B) => (first: A) => A = tap
282293

283294
/**
284295
* @category instances

test/Identity.ts

+5
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@ describe.concurrent('Identity', () => {
3636
U.deepStrictEqual(pipe(1, _.chain(f)), 2)
3737
})
3838

39+
it('tap', () => {
40+
const f = (n: number) => n * 2
41+
U.deepStrictEqual(pipe(1, _.tap(f)), 1)
42+
})
43+
3944
it('chainFirst', () => {
4045
const f = (n: number) => n * 2
4146
U.deepStrictEqual(pipe(1, _.chainFirst(f)), 1)

0 commit comments

Comments
 (0)