Skip to content

Commit

Permalink
Reader: add apFirstW and apSecondW
Browse files Browse the repository at this point in the history
  • Loading branch information
DenisFrezzato committed Aug 22, 2021
1 parent 32820bd commit 21e03f1
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 0 deletions.
26 changes: 26 additions & 0 deletions docs/modules/Reader.ts.md
Expand Up @@ -36,7 +36,9 @@ Added in v2.0.0
- [second](#second)
- [combinators](#combinators)
- [apFirst](#apfirst)
- [apFirstW](#apfirstw)
- [apSecond](#apsecond)
- [apSecondW](#apsecondw)
- [asksReader](#asksreader)
- [asksReaderW](#asksreaderw)
- [chainFirst](#chainfirst)
Expand Down Expand Up @@ -257,6 +259,18 @@ export declare const apFirst: <E, B>(second: Reader<E, B>) => <A>(first: Reader<

Added in v2.0.0

## apFirstW

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

**Signature**

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

Added in v2.12.0

## apSecond

Combine two effectful actions, keeping only the result of the second.
Expand All @@ -271,6 +285,18 @@ export declare const apSecond: <E, B>(second: Reader<E, B>) => <A>(first: Reader

Added in v2.0.0

## apSecondW

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

**Signature**

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

Added in v2.12.0

## asksReader

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

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

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

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

/**
* @category instances
* @since 2.7.0
Expand Down
10 changes: 10 additions & 0 deletions test/Reader.ts
Expand Up @@ -24,10 +24,20 @@ describe('Reader', () => {
U.deepStrictEqual(pipe(_.of('a'), _.apFirst(_.of('b')))({}), 'a')
})

it('apFirstW', () => {
const fb = _.of<{ readonly k: string }, boolean>(true)
U.deepStrictEqual(pipe(_.of<{ readonly x: number }, string>('foo'), _.apFirstW(fb))({ x: 1, k: 'v' }), 'foo')
})

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

it('apSecondW', () => {
const fb = _.of<{ readonly k: string }, boolean>(true)
U.deepStrictEqual(pipe(_.of<{ readonly x: number }, string>('foo'), _.apSecondW(fb))({ x: 1, k: 'v' }), true)
})

it('chain', () => {
const f = (s: string): _.Reader<object, number> => _.of(s.length)
U.deepStrictEqual(pipe(_.of('foo'), _.chain(f))({}), 3)
Expand Down

0 comments on commit 21e03f1

Please sign in to comment.