Skip to content

Commit

Permalink
IOEither: add apFirstW and apSecondW
Browse files Browse the repository at this point in the history
  • Loading branch information
DenisFrezzato committed Oct 26, 2021
1 parent 7d244d3 commit 12f4e38
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 0 deletions.
30 changes: 30 additions & 0 deletions docs/modules/IOEither.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ Added in v2.0.0
- [of](#of)
- [combinators](#combinators)
- [apFirst](#apfirst)
- [apFirstW](#apfirstw)
- [apSecond](#apsecond)
- [apSecondW](#apsecondw)
- [chainEitherK](#chaineitherk)
- [chainEitherKW](#chaineitherkw)
- [chainFirst](#chainfirst)
Expand Down Expand Up @@ -294,6 +296,20 @@ export declare const apFirst: <E, B>(second: IOEither<E, B>) => <A>(first: IOEit

Added in v2.0.0

## apFirstW

Less strict version of [`apFirst`](#apfirst).

**Signature**

```ts
export declare const apFirstW: <E2, A, B>(
second: IOEither<E2, B>
) => <E1>(first: IOEither<E1, A>) => IOEither<E2 | E1, A>
```

Added in v2.12.0

## apSecond

Combine two effectful actions, keeping only the result of the second.
Expand All @@ -308,6 +324,20 @@ export declare const apSecond: <E, B>(second: IOEither<E, B>) => <A>(first: IOEi

Added in v2.0.0

## apSecondW

Less strict version of [`apSecond`](#apsecond).

**Signature**

```ts
export declare const apSecondW: <E2, A, B>(
second: IOEither<E2, B>
) => <E1>(first: IOEither<E1, A>) => IOEither<E2 | E1, B>
```

Added in v2.12.0

## chainEitherK

**Signature**
Expand Down
20 changes: 20 additions & 0 deletions src/IOEither.ts
Original file line number Diff line number Diff line change
Expand Up @@ -577,6 +577,16 @@ export const apFirst =
/*#__PURE__*/
apFirst_(ApplyPar)

/**
* Less strict version of [`apFirst`](#apfirst).
*
* @category combinators
* @since 2.12.0
*/
export const apFirstW: <E2, A, B>(
second: IOEither<E2, B>
) => <E1>(first: IOEither<E1, A>) => IOEither<E1 | E2, A> = apFirst as any

/**
* Combine two effectful actions, keeping only the result of the second.
*
Expand All @@ -589,6 +599,16 @@ export const apSecond =
/*#__PURE__*/
apSecond_(ApplyPar)

/**
* Less strict version of [`apSecond`](#apsecond).
*
* @category combinators
* @since 2.12.0
*/
export const apSecondW: <E2, A, B>(
second: IOEither<E2, B>
) => <E1>(first: IOEither<E1, A>) => IOEither<E1 | E2, B> = apSecond as any

/**
* @category instances
* @since 2.8.4
Expand Down
12 changes: 12 additions & 0 deletions test/IOEither.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,22 @@ describe('IOEither', () => {
U.deepStrictEqual(pipe(_.right('a'), _.apFirst(_.right('b')))(), E.right('a'))
})

it('apFirstW', () => {
const fa = _.right<'Foo', string>('a')
const fb = _.right<'Bar', number>(1)
U.deepStrictEqual(pipe(fa, _.apFirstW(fb))(), E.right('a'))
})

it('apSecond', () => {
U.deepStrictEqual(pipe(_.right('a'), _.apSecond(_.right('b')))(), E.right('b'))
})

it('apSecondW', () => {
const fa = _.right<'Foo', string>('a')
const fb = _.right<'Bar', number>(1)
U.deepStrictEqual(pipe(fa, _.apSecondW(fb))(), E.right(1))
})

it('chain', () => {
const f = (a: string) => (a.length > 2 ? _.right(a.length) : _.left('foo'))
U.deepStrictEqual(pipe(_.right('foo'), _.chain(f))(), E.right(3))
Expand Down

0 comments on commit 12f4e38

Please sign in to comment.