Skip to content

Commit

Permalink
ReaderEither: 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 12f4e38 commit ffc0b4d
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 0 deletions.
30 changes: 30 additions & 0 deletions docs/modules/ReaderEither.ts.md
Expand Up @@ -32,7 +32,9 @@ Added in v2.0.0
- [of](#of)
- [combinators](#combinators)
- [apFirst](#apfirst)
- [apFirstW](#apfirstw)
- [apSecond](#apsecond)
- [apSecondW](#apsecondw)
- [asksReaderEither](#asksreadereither)
- [asksReaderEitherW](#asksreadereitherw)
- [chainEitherK](#chaineitherk)
Expand Down Expand Up @@ -300,6 +302,20 @@ export declare const apFirst: <R, E, B>(

Added in v2.0.0

## apFirstW

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

**Signature**

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

Added in v2.12.0

## apSecond

Combine two effectful actions, keeping only the result of the second.
Expand All @@ -316,6 +332,20 @@ export declare const apSecond: <R, E, B>(

Added in v2.0.0

## apSecondW

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

**Signature**

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

Added in v2.12.0

## asksReaderEither

Effectfully accesses the environment.
Expand Down
20 changes: 20 additions & 0 deletions src/ReaderEither.ts
Expand Up @@ -596,6 +596,16 @@ export const apFirst =
/*#__PURE__*/
apFirst_(Apply)

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

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

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

/**
* @category instances
* @since 2.7.0
Expand Down
12 changes: 12 additions & 0 deletions test/ReaderEither.ts
Expand Up @@ -42,10 +42,22 @@ describe('ReaderEither', () => {
U.deepStrictEqual(pipe(_.right('a'), _.apFirst(_.right('b')))({}), E.right('a'))
})

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

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

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

it('chainFirst', () => {
const f = (n: number) => _.right(n * 2)
U.deepStrictEqual(pipe(_.right(1), _.chainFirst(f))({}), E.right(1))
Expand Down

0 comments on commit ffc0b4d

Please sign in to comment.