Skip to content

Commit

Permalink
Added bracketW functions
Browse files Browse the repository at this point in the history
  • Loading branch information
thomasvargiu authored and gcanti committed Apr 21, 2022
1 parent b395762 commit 4a7c32e
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 12 deletions.
17 changes: 14 additions & 3 deletions src/IOEither.ts
Expand Up @@ -889,16 +889,27 @@ export const bracket = <E, A, B>(
acquire: IOEither<E, A>,
use: (a: A) => IOEither<E, B>,
release: (a: A, e: Either<E, B>) => IOEither<E, void>
): IOEither<E, B> =>
): IOEither<E, B> => bracketW(acquire, use, release)

/**
* Less strict version of [`bracket`](#bracket).
*
* @since 2.12.0
*/
export const bracketW: <E1, E2, E3, A, B>(
acquire: IOEither<E1, A>,
use: (a: A) => IOEither<E2, B>,
release: (a: A, e: E.Either<E2, B>) => IOEither<E3, void>
) => IOEither<E1 | E2 | E3, B> = (acquire, use, release) =>
pipe(
acquire,
chain((a) =>
chainW((a) =>
pipe(
use(a),
I.chain((e) =>
pipe(
release(a, e),
chain(() => I.of(e))
chainW(() => I.of(e))
)
)
)
Expand Down
19 changes: 16 additions & 3 deletions src/ReaderTaskEither.ts
Expand Up @@ -1331,13 +1331,26 @@ export const chainFirstTaskK =
* @since 2.0.4
*/
export function bracket<R, E, A, B>(
aquire: ReaderTaskEither<R, E, A>,
acquire: ReaderTaskEither<R, E, A>,
use: (a: A) => ReaderTaskEither<R, E, B>,
release: (a: A, e: Either<E, B>) => ReaderTaskEither<R, E, void>
): ReaderTaskEither<R, E, B> {
return bracketW(acquire, use, release)
}

/**
* Less strict version of [`bracket`](#bracket).
*
* @since 2.12.0
*/
export function bracketW<R1, R2, R3, E1, E2, E3, A, B>(
acquire: ReaderTaskEither<R1, E1, A>,
use: (a: A) => ReaderTaskEither<R2, E2, B>,
release: (a: A, e: Either<E2, B>) => ReaderTaskEither<R3, E3, void>
): ReaderTaskEither<R1 & R2 & R3, E1 | E2 | E3, B> {
return (r) =>
TE.bracket(
aquire(r),
TE.bracketW(
acquire(r),
(a) => use(a)(r),
(a, e) => release(a, e)(r)
)
Expand Down
17 changes: 14 additions & 3 deletions src/TaskEither.ts
Expand Up @@ -1183,16 +1183,27 @@ export const bracket = <E, A, B>(
acquire: TaskEither<E, A>,
use: (a: A) => TaskEither<E, B>,
release: (a: A, e: Either<E, B>) => TaskEither<E, void>
): TaskEither<E, B> =>
): TaskEither<E, B> => bracketW(acquire, use, release)

/**
* Less strict version of [`bracket`](#bracket).
*
* @since 2.12.0
*/
export const bracketW: <E1, E2, E3, A, B>(
acquire: TaskEither<E1, A>,
use: (a: A) => TaskEither<E2, B>,
release: (a: A, e: E.Either<E2, B>) => TaskEither<E3, void>
) => TaskEither<E1 | E2 | E3, B> = (acquire, use, release) =>
pipe(
acquire,
chain((a) =>
chainW((a) =>
pipe(
use(a),
T.chain((e) =>
pipe(
release(a, e),
chain(() => T.of(e))
chainW(() => T.of(e))
)
)
)
Expand Down
11 changes: 10 additions & 1 deletion test/IOEither.ts
@@ -1,7 +1,7 @@
import * as U from './util'
import { sequenceT } from '../src/Apply'
import * as E from '../src/Either'
import { identity, pipe, SK } from '../src/function'
import { constVoid, identity, pipe, SK } from '../src/function'
import * as I from '../src/IO'
import * as _ from '../src/IOEither'
import * as O from '../src/Option'
Expand Down Expand Up @@ -384,6 +384,15 @@ describe('IOEither', () => {
})
})

it('bracketW', async () => {
const res = _.bracketW(
_.right<string, string>('string'),
(_a: string) => _.right<number, string>('test'),
(_a: string, _e: E.Either<number, string>) => _.right<Error, void>(constVoid())
)()
U.deepStrictEqual(res, E.right('test'))
})

it('getApplicativeIOValidation', () => {
const A = _.getApplicativeIOValidation(S.Monoid)
U.deepStrictEqual(sequenceT(A)(_.left('a'), _.left('b'))(), E.left('ab'))
Expand Down
19 changes: 18 additions & 1 deletion test/ReaderTaskEither.ts
@@ -1,6 +1,6 @@
import { sequenceT } from '../src/Apply'
import * as E from '../src/Either'
import { flow, pipe, SK } from '../src/function'
import { constVoid, flow, pipe, SK } from '../src/function'
import * as I from '../src/IO'
import * as IE from '../src/IOEither'
import * as N from '../src/number'
Expand Down Expand Up @@ -427,6 +427,23 @@ describe('ReaderTaskEither', () => {
})
})

it('bracketW', async () => {
const acquire = _.right<{ readonly a: string }, string, string>('string')
const use = (_a: string) => _.right<{ readonly b: number }, number, string>('test')
const release = (_a: string, _e: E.Either<number, string>) =>
_.right<{ readonly c: boolean }, Error, void>(constVoid())
const res = await _.bracketW(
acquire,
use,
release
)({
a: 'string',
b: 5,
c: true
})()
U.deepStrictEqual(res, E.right('test'))
})

it('chainEitherK', async () => {
const f = (s: string) => E.right(s.length)
U.deepStrictEqual(await pipe(_.right('a'), _.chainEitherK(f))(undefined)(), E.right(1))
Expand Down
11 changes: 10 additions & 1 deletion test/TaskEither.ts
Expand Up @@ -2,7 +2,7 @@ import * as U from './util'
import { sequenceT } from '../src/Apply'
import * as RA from '../src/ReadonlyArray'
import * as E from '../src/Either'
import { identity, pipe, SK } from '../src/function'
import { constVoid, identity, pipe, SK } from '../src/function'
import * as I from '../src/IO'
import * as IE from '../src/IOEither'
import { monoidString } from '../src/Monoid'
Expand Down Expand Up @@ -370,6 +370,15 @@ describe('TaskEither', () => {
})
})

it('bracketW', async () => {
const res = await _.bracketW(
_.right<string, string>('string'),
(_a: string) => _.right<number, string>('test'),
(_a: string, _e: E.Either<number, string>) => _.right<Error, void>(constVoid())
)()
U.deepStrictEqual(res, E.right('test'))
})

// -------------------------------------------------------------------------------------
// combinators
// -------------------------------------------------------------------------------------
Expand Down

0 comments on commit 4a7c32e

Please sign in to comment.