Skip to content

Commit

Permalink
Add tap for Identity (#1943)
Browse files Browse the repository at this point in the history
  • Loading branch information
forno committed Jun 29, 2024
1 parent cabd677 commit 1b640d2
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
17 changes: 14 additions & 3 deletions src/Identity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -275,10 +275,21 @@ export const Monad: Monad1<URI> = {
* 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: {
<A, _>(self: Identity<A>, f: (a: A) => Identity<_>): Identity<A>
<A, _>(f: (a: A) => Identity<_>): (self: Identity<A>) => Identity<A>
} = /*#__PURE__*/ dual(2, tap_(Chain))

/**
* Alias of `tap`
*
* @category legacy
* @since 2.0.0
*/
export const chainFirst: <A, B>(f: (a: A) => B) => (first: A) => A = /*#__PURE__*/ chainFirst_(Chain)
export const chainFirst: <A, B>(f: (a: A) => B) => (first: A) => A = tap

/**
* @category instances
Expand Down
5 changes: 5 additions & 0 deletions test/Identity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 1b640d2

Please sign in to comment.