From 47ac29f9e573f090761803e9441d3bdfe005e0bc Mon Sep 17 00:00:00 2001 From: Thomas Vargiu Date: Thu, 2 Dec 2021 15:58:46 +0100 Subject: [PATCH] Added bracketW functions --- src/IOEither.ts | 17 ++++++++++++++--- src/ReaderTaskEither.ts | 19 ++++++++++++++++--- src/TaskEither.ts | 17 ++++++++++++++--- test/IOEither.ts | 11 ++++++++++- test/ReaderTaskEither.ts | 19 ++++++++++++++++++- test/TaskEither.ts | 11 ++++++++++- 6 files changed, 82 insertions(+), 12 deletions(-) diff --git a/src/IOEither.ts b/src/IOEither.ts index 830a70d22..9f7aed02a 100644 --- a/src/IOEither.ts +++ b/src/IOEither.ts @@ -845,16 +845,27 @@ export const bracket = ( acquire: IOEither, use: (a: A) => IOEither, release: (a: A, e: Either) => IOEither -): IOEither => +): IOEither => bracketW(acquire, use, release) + +/** + * Less strict version of [`bracket`](#bracket). + * + * @since 2.12.0 + */ +export const bracketW: ( + acquire: IOEither, + use: (a: A) => IOEither, + release: (a: A, e: E.Either) => IOEither +) => IOEither = (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)) ) ) ) diff --git a/src/ReaderTaskEither.ts b/src/ReaderTaskEither.ts index dfa68bca6..c76d6d833 100644 --- a/src/ReaderTaskEither.ts +++ b/src/ReaderTaskEither.ts @@ -1292,13 +1292,26 @@ export const chainFirstTaskK = * @since 2.0.4 */ export function bracket( - aquire: ReaderTaskEither, + acquire: ReaderTaskEither, use: (a: A) => ReaderTaskEither, release: (a: A, e: Either) => ReaderTaskEither ): ReaderTaskEither { + return bracketW(acquire, use, release) +} + +/** + * Less strict version of [`bracket`](#bracket). + * + * @since 2.12.0 + */ +export function bracketW( + acquire: ReaderTaskEither, + use: (a: A) => ReaderTaskEither, + release: (a: A, e: Either) => ReaderTaskEither +): ReaderTaskEither { return (r) => - TE.bracket( - aquire(r), + TE.bracketW( + acquire(r), (a) => use(a)(r), (a, e) => release(a, e)(r) ) diff --git a/src/TaskEither.ts b/src/TaskEither.ts index 28c4608fa..703db01bb 100644 --- a/src/TaskEither.ts +++ b/src/TaskEither.ts @@ -1122,16 +1122,27 @@ export const bracket = ( acquire: TaskEither, use: (a: A) => TaskEither, release: (a: A, e: Either) => TaskEither -): TaskEither => +): TaskEither => bracketW(acquire, use, release) + +/** + * Less strict version of [`bracket`](#bracket). + * + * @since 2.12.0 + */ +export const bracketW: ( + acquire: TaskEither, + use: (a: A) => TaskEither, + release: (a: A, e: E.Either) => TaskEither +) => TaskEither = (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)) ) ) ) diff --git a/test/IOEither.ts b/test/IOEither.ts index e7d313b47..3c12ae37f 100644 --- a/test/IOEither.ts +++ b/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' @@ -366,6 +366,15 @@ describe('IOEither', () => { }) }) + it('bracketW', async () => { + const res = _.bracketW( + _.right('string'), + (_a: string) => _.right('test'), + (_a: string, _e: E.Either) => _.right(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')) diff --git a/test/ReaderTaskEither.ts b/test/ReaderTaskEither.ts index e9d94ee09..ff80f8220 100644 --- a/test/ReaderTaskEither.ts +++ b/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' @@ -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) => + _.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)) diff --git a/test/TaskEither.ts b/test/TaskEither.ts index afb972281..6b63f7ade 100644 --- a/test/TaskEither.ts +++ b/test/TaskEither.ts @@ -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' @@ -356,6 +356,15 @@ describe('TaskEither', () => { }) }) + it('bracketW', async () => { + const res = await _.bracketW( + _.right('string'), + (_a: string) => _.right('test'), + (_a: string, _e: E.Either) => _.right(constVoid()) + )() + U.deepStrictEqual(res, E.right('test')) + }) + // ------------------------------------------------------------------------------------- // combinators // -------------------------------------------------------------------------------------