Skip to content

Commit

Permalink
StateReaderTaskEither: add missing Bifunctor, Alt instances
Browse files Browse the repository at this point in the history
  • Loading branch information
gcanti committed Feb 13, 2020
1 parent 9e65968 commit 69f9067
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 4 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -31,6 +31,8 @@ high state of flux, you're at risk of it changing without notice.
- add `toggle` (@ryota-ka)
- `TaskEither`
- add `tryCatchK` (@DenisFrezzato)
- `StateReaderTaskEither`
- add missing `Bifunctor`, `Alt` instances (@gcanti)

# 2.4.4

Expand Down
2 changes: 1 addition & 1 deletion docs/modules/StateReaderTaskEither.ts.md
Expand Up @@ -525,7 +525,7 @@ Added in v2.0.0
**Signature**

```ts
export const stateReaderTaskEither: Monad4<URI> & MonadThrow4<URI> & MonadTask4<URI> = ...
export const stateReaderTaskEither: Monad4<URI> & Bifunctor4<URI> & Alt4<URI> & MonadTask4<URI> & MonadThrow4<URI> = ...
```

Added in v2.0.0
Expand Down
11 changes: 8 additions & 3 deletions src/StateReaderTaskEither.ts
@@ -1,10 +1,14 @@
/**
* @since 2.0.0
*/
import { Alt4 } from './Alt'
import { Bifunctor4 } from './Bifunctor'
import { Either } from './Either'
import { IO } from './IO'
import { IOEither } from './IOEither'
import { Monad4 } from './Monad'
import { MonadTask4 } from './MonadTask'
import { MonadThrow4 } from './MonadThrow'
import { pipeable } from './pipeable'
import { Reader } from './Reader'
import { ReaderEither } from './ReaderEither'
Expand All @@ -15,8 +19,6 @@ import { Task } from './Task'
import { TaskEither } from './TaskEither'

import ReaderTaskEither = RTE.ReaderTaskEither
import { MonadThrow4 } from './MonadThrow'
import { MonadTask4 } from './MonadTask'

const T = getStateM(RTE.readerTaskEither)

Expand Down Expand Up @@ -267,12 +269,15 @@ export function chainReaderTaskEitherK<R, E, A, B>(
/**
* @since 2.0.0
*/
export const stateReaderTaskEither: Monad4<URI> & MonadThrow4<URI> & MonadTask4<URI> = {
export const stateReaderTaskEither: Monad4<URI> & Bifunctor4<URI> & Alt4<URI> & MonadTask4<URI> & MonadThrow4<URI> = {
URI,
map: T.map,
of: right,
ap: T.ap,
chain: T.chain,
bimap: (fea, f, g) => s => RTE.readerTaskEither.bimap(fea(s), f, ([a, s]) => [g(a), s]),
mapLeft: (fea, f) => s => RTE.readerTaskEither.mapLeft(fea(s), f),
alt: (fx, fy) => s => RTE.readerTaskEither.alt(fx(s), () => fy()(s)),
fromIO: rightIO,
fromTask: rightTask,
throwError: left
Expand Down
46 changes: 46 additions & 0 deletions test/StateReaderTaskEither.ts
Expand Up @@ -51,6 +51,52 @@ describe('StateReaderTaskEither', () => {
})
})

describe('Bifunctor', () => {
it('bimap', async () => {
const gt2 = (n: number): boolean => n > 2
const len = (s: string): number => s.length
const e1 = await RTE.run(_.evalState(_.stateReaderTaskEither.bimap(_.right('aaa'), gt2, len), {}), {})
assert.deepStrictEqual(e1, E.right(3))
const e2 = await RTE.run(_.evalState(_.stateReaderTaskEither.bimap(_.left(3), gt2, len), {}), {})
assert.deepStrictEqual(e2, E.left(true))
})

it('mapLeft', async () => {
const gt2 = (n: number): boolean => n > 2
const e = await RTE.run(_.evalState(_.stateReaderTaskEither.mapLeft(_.left(3), gt2), {}), {})
assert.deepStrictEqual(e, E.left(true))
})
})

describe('Alt', () => {
it('alt', async () => {
const e1 = await RTE.run(
_.evalState(
_.stateReaderTaskEither.alt(_.right('a'), () => _.left(1)),
{}
),
{}
)
assert.deepStrictEqual(e1, E.right('a'))
const e2 = await RTE.run(
_.evalState(
_.stateReaderTaskEither.alt(_.left(1), () => _.right('b')),
{}
),
{}
)
assert.deepStrictEqual(e2, E.right('b'))
const e3 = await RTE.run(
_.evalState(
_.stateReaderTaskEither.alt(_.left(1), () => _.left(2)),
{}
),
{}
)
assert.deepStrictEqual(e3, E.left(2))
})
})

it('execState', async () => {
const ma = _.right('aaa')
const s = {}
Expand Down

0 comments on commit 69f9067

Please sign in to comment.