Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added bracketW functions #1627

Merged
merged 1 commit into from Apr 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 14 additions & 3 deletions src/IOEither.ts
Expand Up @@ -845,16 +845,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 @@ -1292,13 +1292,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 @@ -1122,16 +1122,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 @@ -366,6 +366,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 @@ -415,6 +415,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 @@ -356,6 +356,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