Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tap for Identity #1943

Merged
merged 5 commits into from
Jun 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading