From 09814e2963c42391589e3294f4c594a88214d29c Mon Sep 17 00:00:00 2001 From: Milan Suk Date: Thu, 18 May 2023 20:51:13 +0200 Subject: [PATCH] feat: as and asUnit --- docs/modules/Either.ts.md | 29 ++++++++++++++++++++ docs/modules/IOEither.ts.md | 29 ++++++++++++++++++++ docs/modules/IOOption.ts.md | 29 ++++++++++++++++++++ docs/modules/Option.ts.md | 26 ++++++++++++++++++ docs/modules/ReaderEither.ts.md | 29 ++++++++++++++++++++ docs/modules/ReaderTaskEither.ts.md | 29 ++++++++++++++++++++ docs/modules/StateReaderTaskEither.ts.md | 31 +++++++++++++++++++++ docs/modules/TaskEither.ts.md | 29 ++++++++++++++++++++ docs/modules/TaskOption.ts.md | 29 ++++++++++++++++++++ src/Either.ts | 21 +++++++++++++- src/Functor.ts | 35 ++++++++++++++++++++++++ src/IOEither.ts | 21 +++++++++++++- src/IOOption.ts | 21 +++++++++++++- src/Option.ts | 21 +++++++++++++- src/ReaderEither.ts | 21 +++++++++++++- src/ReaderTaskEither.ts | 21 +++++++++++++- src/StateReaderTaskEither.ts | 22 ++++++++++++++- src/TaskEither.ts | 21 +++++++++++++- src/TaskOption.ts | 21 +++++++++++++- test/Either.ts | 10 +++++++ test/IOEither.ts | 10 +++++++ test/IOOption.ts | 10 +++++++ test/Option.ts | 10 +++++++ test/ReaderEither.ts | 10 +++++++ test/ReaderTaskEither.ts | 10 +++++++ test/StateReaderTaskEither.ts | 9 ++++++ test/TaskEither.ts | 10 +++++++ test/TaskOption.ts | 10 +++++++ 28 files changed, 565 insertions(+), 9 deletions(-) diff --git a/docs/modules/Either.ts.md b/docs/modules/Either.ts.md index d99f93b1fc..a7616cdba9 100644 --- a/docs/modules/Either.ts.md +++ b/docs/modules/Either.ts.md @@ -147,6 +147,8 @@ Added in v2.0.0 - [liftNullable](#liftnullable) - [liftOption](#liftoption) - [mapping](#mapping) + - [as](#as) + - [asUnit](#asunit) - [bimap](#bimap) - [flap](#flap) - [map](#map) @@ -1306,6 +1308,33 @@ Added in v2.15.0 # mapping +## as + +Maps the `Right` value of this `Either` to the specified constant value. + +**Signature** + +```ts +export declare const as: { + (self: Either, a: A): Either + (a: A): (self: Either) => Either +} +``` + +Added in v2.16.0 + +## asUnit + +Maps the `Right` value of this `Either` to the void constant value. + +**Signature** + +```ts +export declare const asUnit: (self: Either) => Either +``` + +Added in v2.16.0 + ## bimap Map a pair of functions over the two type arguments of the bifunctor. diff --git a/docs/modules/IOEither.ts.md b/docs/modules/IOEither.ts.md index b53c64edc4..698ae0bf1e 100644 --- a/docs/modules/IOEither.ts.md +++ b/docs/modules/IOEither.ts.md @@ -98,6 +98,8 @@ Added in v2.0.0 - [liftNullable](#liftnullable) - [liftOption](#liftoption) - [mapping](#mapping) + - [as](#as) + - [asUnit](#asunit) - [bimap](#bimap) - [flap](#flap) - [map](#map) @@ -1069,6 +1071,33 @@ Added in v2.15.0 # mapping +## as + +Maps the `Right` value of this `IOEither` to the specified constant value. + +**Signature** + +```ts +export declare const as: { + (self: IOEither, a: A): IOEither + (a: A): (self: IOEither) => IOEither +} +``` + +Added in v2.16.0 + +## asUnit + +Maps the `Right` value of this `IOEither` to the void constant value. + +**Signature** + +```ts +export declare const asUnit: (self: IOEither) => IOEither +``` + +Added in v2.16.0 + ## bimap Map a pair of functions over the two type arguments of the bifunctor. diff --git a/docs/modules/IOOption.ts.md b/docs/modules/IOOption.ts.md index 0f126c56de..3cb3e03485 100644 --- a/docs/modules/IOOption.ts.md +++ b/docs/modules/IOOption.ts.md @@ -79,6 +79,8 @@ Added in v2.12.0 - [fromOptionK](#fromoptionk) - [fromPredicate](#frompredicate) - [mapping](#mapping) + - [as](#as) + - [asUnit](#asunit) - [flap](#flap) - [map](#map) - [model](#model) @@ -760,6 +762,33 @@ Added in v2.12.0 # mapping +## as + +Maps the `Some` value of this `IOOption` to the specified constant value. + +**Signature** + +```ts +export declare const as: { + (self: IOOption<_>, a: A): IOOption + (a: A): <_>(self: IOOption<_>) => IOOption +} +``` + +Added in v2.16.0 + +## asUnit + +Maps the `Some` value of this `IOOption` to the void constant value. + +**Signature** + +```ts +export declare const asUnit: <_>(self: IOOption<_>) => IOOption +``` + +Added in v2.16.0 + ## flap **Signature** diff --git a/docs/modules/Option.ts.md b/docs/modules/Option.ts.md index c1923181fd..16a1803537 100644 --- a/docs/modules/Option.ts.md +++ b/docs/modules/Option.ts.md @@ -144,6 +144,8 @@ Added in v2.0.0 - [fromNullableK](#fromnullablek) - [fromPredicate](#frompredicate) - [mapping](#mapping) + - [as](#as) + - [asUnit](#asunit) - [flap](#flap) - [map](#map) - [model](#model) @@ -1151,6 +1153,30 @@ Added in v2.0.0 # mapping +## as + +Maps the `Some` value of this `Option` to the specified constant value. + +**Signature** + +```ts +export declare const as: { (self: Option<_>, a: A): Option; (a: A): <_>(self: Option<_>) => Option } +``` + +Added in v2.16.0 + +## asUnit + +Maps the `Some` value of this `Option` to the void constant value. + +**Signature** + +```ts +export declare const asUnit: <_>(self: Option<_>) => Option +``` + +Added in v2.16.0 + ## flap **Signature** diff --git a/docs/modules/ReaderEither.ts.md b/docs/modules/ReaderEither.ts.md index 6ba5de902d..1696338c23 100644 --- a/docs/modules/ReaderEither.ts.md +++ b/docs/modules/ReaderEither.ts.md @@ -88,6 +88,8 @@ Added in v2.0.0 - [liftNullable](#liftnullable) - [liftOption](#liftoption) - [mapping](#mapping) + - [as](#as) + - [asUnit](#asunit) - [bimap](#bimap) - [flap](#flap) - [map](#map) @@ -1040,6 +1042,33 @@ Added in v2.15.0 # mapping +## as + +Maps the `Right` value of this `ReaderEither` to the specified constant value. + +**Signature** + +```ts +export declare const as: { + (self: ReaderEither, a: A): ReaderEither + (a: A): (self: ReaderEither) => ReaderEither +} +``` + +Added in v2.16.0 + +## asUnit + +Maps the `Right` value of this `ReaderEither` to the void constant value. + +**Signature** + +```ts +export declare const asUnit: (self: ReaderEither) => ReaderEither +``` + +Added in v2.16.0 + ## bimap Map a pair of functions over the two last type arguments of the bifunctor. diff --git a/docs/modules/ReaderTaskEither.ts.md b/docs/modules/ReaderTaskEither.ts.md index ef3f230560..405d5356c1 100644 --- a/docs/modules/ReaderTaskEither.ts.md +++ b/docs/modules/ReaderTaskEither.ts.md @@ -120,6 +120,8 @@ Added in v2.0.0 - [liftNullable](#liftnullable) - [liftOption](#liftoption) - [mapping](#mapping) + - [as](#as) + - [asUnit](#asunit) - [bimap](#bimap) - [flap](#flap) - [map](#map) @@ -1503,6 +1505,33 @@ Added in v2.15.0 # mapping +## as + +Maps the `Right` value of this `ReaderTaskEither` to the specified constant value. + +**Signature** + +```ts +export declare const as: { + (self: ReaderTaskEither, a: A): ReaderTaskEither + (a: A): (self: ReaderTaskEither) => ReaderTaskEither +} +``` + +Added in v2.16.0 + +## asUnit + +Maps the `Right` value of this `ReaderTaskEither` to the void constant value. + +**Signature** + +```ts +export declare const asUnit: (self: ReaderTaskEither) => ReaderTaskEither +``` + +Added in v2.16.0 + ## bimap Map a pair of functions over the two last type arguments of the bifunctor. diff --git a/docs/modules/StateReaderTaskEither.ts.md b/docs/modules/StateReaderTaskEither.ts.md index 5d70df27cd..e71907fa0b 100644 --- a/docs/modules/StateReaderTaskEither.ts.md +++ b/docs/modules/StateReaderTaskEither.ts.md @@ -93,6 +93,8 @@ Added in v2.0.0 - [fromTaskEitherK](#fromtaskeitherk) - [fromTaskK](#fromtaskk) - [mapping](#mapping) + - [as](#as) + - [asUnit](#asunit) - [bimap](#bimap) - [flap](#flap) - [map](#map) @@ -1046,6 +1048,35 @@ Added in v2.10.0 # mapping +## as + +Maps the `Right` value of this `StateReaderTaskEither` to the specified constant value. + +**Signature** + +```ts +export declare const as: { + (self: StateReaderTaskEither, a: A): StateReaderTaskEither + (a: A): (self: StateReaderTaskEither) => StateReaderTaskEither +} +``` + +Added in v2.16.0 + +## asUnit + +Maps the `Right` value of this `StateReaderTaskEither` to the void constant value. + +**Signature** + +```ts +export declare const asUnit: ( + self: StateReaderTaskEither +) => StateReaderTaskEither +``` + +Added in v2.16.0 + ## bimap Map a pair of functions over the two last type arguments of the bifunctor. diff --git a/docs/modules/TaskEither.ts.md b/docs/modules/TaskEither.ts.md index f0a8914046..567771356f 100644 --- a/docs/modules/TaskEither.ts.md +++ b/docs/modules/TaskEither.ts.md @@ -115,6 +115,8 @@ Added in v2.0.0 - [liftNullable](#liftnullable) - [liftOption](#liftoption) - [mapping](#mapping) + - [as](#as) + - [asUnit](#asunit) - [bimap](#bimap) - [flap](#flap) - [map](#map) @@ -1447,6 +1449,33 @@ Added in v2.15.0 # mapping +## as + +Maps the `Right` value of this `TaskEither` to the specified constant value. + +**Signature** + +```ts +export declare const as: { + (self: TaskEither, a: A): TaskEither + (a: A): (self: TaskEither) => TaskEither +} +``` + +Added in v2.16.0 + +## asUnit + +Maps the `Right` value of this `TaskEither` to the void constant value. + +**Signature** + +```ts +export declare const asUnit: (self: TaskEither) => TaskEither +``` + +Added in v2.16.0 + ## bimap Map a pair of functions over the two type arguments of the bifunctor. diff --git a/docs/modules/TaskOption.ts.md b/docs/modules/TaskOption.ts.md index 398ad189b4..ace1f2757a 100644 --- a/docs/modules/TaskOption.ts.md +++ b/docs/modules/TaskOption.ts.md @@ -81,6 +81,8 @@ Added in v2.10.0 - [fromPredicate](#frompredicate) - [fromTaskK](#fromtaskk) - [mapping](#mapping) + - [as](#as) + - [asUnit](#asunit) - [flap](#flap) - [map](#map) - [model](#model) @@ -854,6 +856,33 @@ Added in v2.10.0 # mapping +## as + +Maps the `Some` value of this `TaskOption` to the specified constant value. + +**Signature** + +```ts +export declare const as: { + (self: TaskOption<_>, a: A): TaskOption + (a: A): <_>(self: TaskOption<_>) => TaskOption +} +``` + +Added in v2.16.0 + +## asUnit + +Maps the `Some` value of this `TaskOption` to the void constant value. + +**Signature** + +```ts +export declare const asUnit: <_>(self: TaskOption<_>) => TaskOption +``` + +Added in v2.16.0 + ## flap **Signature** diff --git a/src/Either.ts b/src/Either.ts index a45eacf188..7df38a7846 100644 --- a/src/Either.ts +++ b/src/Either.ts @@ -87,7 +87,7 @@ import { fromPredicate as fromPredicate_ } from './FromEither' import { dual, flow, identity, LazyArg, pipe } from './function' -import { bindTo as bindTo_, flap as flap_, Functor2, let as let__ } from './Functor' +import { as as as_, asUnit as asUnit_, bindTo as bindTo_, flap as flap_, Functor2, let as let__ } from './Functor' import { HKT } from './HKT' import * as _ from './internal' import { Monad2, Monad2C } from './Monad' @@ -479,6 +479,25 @@ export const Functor: Functor2 = { map: _map } +/** + * Maps the `Right` value of this `Either` to the specified constant value. + * + * @category mapping + * @since 2.16.0 + */ +export const as: { + (self: Either, a: A): Either + (a: A): (self: Either) => Either +} = dual(2, as_(Functor)) + +/** + * Maps the `Right` value of this `Either` to the void constant value. + * + * @category mapping + * @since 2.16.0 + */ +export const asUnit: (self: Either) => Either = asUnit_(Functor) + /** * @category constructors * @since 2.7.0 diff --git a/src/Functor.ts b/src/Functor.ts index ae875f1ffa..341fbcd41d 100644 --- a/src/Functor.ts +++ b/src/Functor.ts @@ -427,3 +427,38 @@ export function getFunctorComposition(F: Functor, G: Functor): Funct map: (fga, f) => pipe(fga, _map(f)) } } + +/** @internal */ +export function as( + F: Functor4 +): (self: Kind4, a: A) => Kind4 +/** @internal */ +export function as(F: Functor3): (self: Kind3, a: _) => Kind3 +/** @internal */ +export function as(F: Functor2): (self: Kind2, a: A) => Kind2 +/** @internal */ +export function as(F: Functor1): (self: Kind, a: A) => Kind +/** @internal */ +export function as(F: Functor): (self: HKT, a: A) => HKT +/** @internal */ +export function as(F: Functor): (self: HKT, b: A) => HKT { + return (self, b) => F.map(self, () => b) +} + +/** @internal */ +export function asUnit( + F: Functor4 +): (self: Kind4) => Kind4 +/** @internal */ +export function asUnit(F: Functor3): (self: Kind3) => Kind3 +/** @internal */ +export function asUnit(F: Functor2): (self: Kind2) => Kind2 +/** @internal */ +export function asUnit(F: Functor1): <_>(self: Kind) => Kind +/** @internal */ +export function asUnit(F: Functor): <_>(self: HKT) => HKT +/** @internal */ +export function asUnit(F: Functor): <_>(self: HKT) => HKT { + const asM = as(F) + return (self) => asM(self, undefined) +} diff --git a/src/IOEither.ts b/src/IOEither.ts index 9b0c24b579..6b4b65f358 100644 --- a/src/IOEither.ts +++ b/src/IOEither.ts @@ -41,7 +41,7 @@ import { } from './FromEither' import { chainIOK as chainIOK_, FromIO2, fromIOK as fromIOK_, tapIO as tapIO_ } from './FromIO' import { dual, flow, identity, LazyArg, pipe, SK } from './function' -import { bindTo as bindTo_, flap as flap_, Functor2, let as let__ } from './Functor' +import { as as as_, asUnit as asUnit_, bindTo as bindTo_, flap as flap_, Functor2, let as let__ } from './Functor' import * as _ from './internal' import * as I from './IO' import { Monad2, Monad2C } from './Monad' @@ -493,6 +493,25 @@ export const Functor: Functor2 = { map: _map } +/** + * Maps the `Right` value of this `IOEither` to the specified constant value. + * + * @category mapping + * @since 2.16.0 + */ +export const as: { + (self: IOEither, a: A): IOEither + (a: A): (self: IOEither) => IOEither +} = dual(2, as_(Functor)) + +/** + * Maps the `Right` value of this `IOEither` to the void constant value. + * + * @category mapping + * @since 2.16.0 + */ +export const asUnit: (self: IOEither) => IOEither = asUnit_(Functor) + /** * @category mapping * @since 2.10.0 diff --git a/src/IOOption.ts b/src/IOOption.ts index 5cad031dfd..2f6aa65af0 100644 --- a/src/IOOption.ts +++ b/src/IOOption.ts @@ -28,7 +28,7 @@ import { } from './FromEither' import { chainIOK as chainIOK_, FromIO1, fromIOK as fromIOK_, tapIO as tapIO_ } from './FromIO' import { dual, flow, identity, LazyArg, pipe, SK } from './function' -import { bindTo as bindTo_, flap as flap_, Functor1, let as let__ } from './Functor' +import { as as as_, asUnit as asUnit_, bindTo as bindTo_, flap as flap_, Functor1, let as let__ } from './Functor' import * as _ from './internal' import * as I from './IO' import { IOEither } from './IOEither' @@ -377,6 +377,25 @@ export const Functor: Functor1 = { map: _map } +/** + * Maps the `Some` value of this `IOOption` to the specified constant value. + * + * @category mapping + * @since 2.16.0 + */ +export const as: { + (self: IOOption<_>, a: A): IOOption + (a: A): <_>(self: IOOption<_>) => IOOption +} = dual(2, as_(Functor)) + +/** + * Maps the `Some` value of this `IOOption` to the void constant value. + * + * @category mapping + * @since 2.16.0 + */ +export const asUnit: <_>(self: IOOption<_>) => IOOption = asUnit_(Functor) + /** * @category mapping * @since 2.12.0 diff --git a/src/Option.ts b/src/Option.ts index 2d074d8411..88115b46e2 100644 --- a/src/Option.ts +++ b/src/Option.ts @@ -84,7 +84,7 @@ import { tapEither as tapEither_ } from './FromEither' import { constNull, constUndefined, dual, flow, identity, LazyArg, pipe } from './function' -import { bindTo as bindTo_, flap as flap_, Functor1, let as let__ } from './Functor' +import { as as as_, asUnit as asUnit_, bindTo as bindTo_, flap as flap_, Functor1, let as let__ } from './Functor' import { HKT } from './HKT' import * as _ from './internal' import { Monad1 } from './Monad' @@ -345,6 +345,25 @@ export const Functor: Functor1 = { map: _map } +/** + * Maps the `Some` value of this `Option` to the specified constant value. + * + * @category mapping + * @since 2.16.0 + */ +export const as: { + (self: Option<_>, a: A): Option + (a: A): <_>(self: Option<_>) => Option +} = dual(2, as_(Functor)) + +/** + * Maps the `Some` value of this `Option` to the void constant value. + * + * @category mapping + * @since 2.16.0 + */ +export const asUnit: <_>(self: Option<_>) => Option = asUnit_(Functor) + /** * @category constructors * @since 2.7.0 diff --git a/src/ReaderEither.ts b/src/ReaderEither.ts index 427c6fb45b..7b9ac5e934 100644 --- a/src/ReaderEither.ts +++ b/src/ReaderEither.ts @@ -42,7 +42,7 @@ import { fromReaderK as fromReaderK_ } from './FromReader' import { dual, flow, identity, LazyArg, pipe, SK } from './function' -import { bindTo as bindTo_, flap as flap_, Functor3, let as let__ } from './Functor' +import { as as as_, asUnit as asUnit_, bindTo as bindTo_, flap as flap_, Functor3, let as let__ } from './Functor' import * as _ from './internal' import { Monad3, Monad3C } from './Monad' import { MonadThrow3, MonadThrow3C } from './MonadThrow' @@ -515,6 +515,25 @@ export const Functor: Functor3 = { map: _map } +/** + * Maps the `Right` value of this `ReaderEither` to the specified constant value. + * + * @category mapping + * @since 2.16.0 + */ +export const as: { + (self: ReaderEither, a: A): ReaderEither + (a: A): (self: ReaderEither) => ReaderEither +} = dual(2, as_(Functor)) + +/** + * Maps the `Right` value of this `ReaderEither` to the void constant value. + * + * @category mapping + * @since 2.16.0 + */ +export const asUnit: (self: ReaderEither) => ReaderEither = asUnit_(Functor) + /** * @category mapping * @since 2.10.0 diff --git a/src/ReaderTaskEither.ts b/src/ReaderTaskEither.ts index 5a6d2806ea..9f89bf7dba 100644 --- a/src/ReaderTaskEither.ts +++ b/src/ReaderTaskEither.ts @@ -50,7 +50,7 @@ import { fromTaskK as fromTaskK_ } from './FromTask' import { dual, flow, identity, LazyArg, pipe, SK } from './function' -import { bindTo as bindTo_, flap as flap_, Functor3, let as let__ } from './Functor' +import { as as as_, asUnit as asUnit_, bindTo as bindTo_, flap as flap_, Functor3, let as let__ } from './Functor' import * as _ from './internal' import { IO } from './IO' import { IOEither } from './IOEither' @@ -803,6 +803,25 @@ export const Functor: Functor3 = { map: _map } +/** + * Maps the `Right` value of this `ReaderTaskEither` to the specified constant value. + * + * @category mapping + * @since 2.16.0 + */ +export const as: { + (self: ReaderTaskEither, a: A): ReaderTaskEither + (a: A): (self: ReaderTaskEither) => ReaderTaskEither +} = dual(2, as_(Functor)) + +/** + * Maps the `Right` value of this `ReaderTaskEither` to the void constant value. + * + * @category mapping + * @since 2.16.0 + */ +export const asUnit: (self: ReaderTaskEither) => ReaderTaskEither = asUnit_(Functor) + /** * @category mapping * @since 2.10.0 diff --git a/src/StateReaderTaskEither.ts b/src/StateReaderTaskEither.ts index d794488bb3..ad0951cd1c 100644 --- a/src/StateReaderTaskEither.ts +++ b/src/StateReaderTaskEither.ts @@ -44,7 +44,7 @@ import { fromTaskK as fromTaskK_ } from './FromTask' import { dual, flow, identity, LazyArg, pipe } from './function' -import { bindTo as bindTo_, flap as flap_, Functor4, let as let__ } from './Functor' +import { as as as_, asUnit as asUnit_, bindTo as bindTo_, flap as flap_, Functor4, let as let__ } from './Functor' import * as _ from './internal' import { IO } from './IO' import { IOEither } from './IOEither' @@ -537,6 +537,26 @@ export const Functor: Functor4 = { map: _map } +/** + * Maps the `Right` value of this `StateReaderTaskEither` to the specified constant value. + * + * @category mapping + * @since 2.16.0 + */ +export const as: { + (self: StateReaderTaskEither, a: A): StateReaderTaskEither + (a: A): (self: StateReaderTaskEither) => StateReaderTaskEither +} = dual(2, as_(Functor)) + +/** + * Maps the `Right` value of this `StateReaderTaskEither` to the void constant value. + * + * @category mapping + * @since 2.16.0 + */ +export const asUnit: (self: StateReaderTaskEither) => StateReaderTaskEither = + asUnit_(Functor) + /** * @category mapping * @since 2.10.0 diff --git a/src/TaskEither.ts b/src/TaskEither.ts index ae4cace472..0632687034 100644 --- a/src/TaskEither.ts +++ b/src/TaskEither.ts @@ -49,7 +49,7 @@ import { fromTaskK as fromTaskK_ } from './FromTask' import { dual, flow, identity, LazyArg, pipe, SK } from './function' -import { bindTo as bindTo_, flap as flap_, Functor2, let as let__ } from './Functor' +import { as as as_, asUnit as asUnit_, bindTo as bindTo_, flap as flap_, Functor2, let as let__ } from './Functor' import * as _ from './internal' import { IO } from './IO' import { IOEither } from './IOEither' @@ -748,6 +748,25 @@ export const Functor: Functor2 = { map: _map } +/** + * Maps the `Right` value of this `TaskEither` to the specified constant value. + * + * @category mapping + * @since 2.16.0 + */ +export const as: { + (self: TaskEither, a: A): TaskEither + (a: A): (self: TaskEither) => TaskEither +} = dual(2, as_(Functor)) + +/** + * Maps the `Right` value of this `TaskEither` to the void constant value. + * + * @category mapping + * @since 2.16.0 + */ +export const asUnit: (self: TaskEither) => TaskEither = asUnit_(Functor) + /** * @category mapping * @since 2.10.0 diff --git a/src/TaskOption.ts b/src/TaskOption.ts index 10b9b85a5b..33c4054c1c 100644 --- a/src/TaskOption.ts +++ b/src/TaskOption.ts @@ -29,7 +29,7 @@ import { fromTaskK as fromTaskK_ } from './FromTask' import { dual, flow, identity, LazyArg, pipe, SK } from './function' -import { bindTo as bindTo_, flap as flap_, Functor1, let as let__ } from './Functor' +import { as as as_, asUnit as asUnit_, bindTo as bindTo_, flap as flap_, Functor1, let as let__ } from './Functor' import * as _ from './internal' import { IO } from './IO' import { Monad1 } from './Monad' @@ -420,6 +420,25 @@ export const Functor: Functor1 = { map: _map } +/** + * Maps the `Some` value of this `TaskOption` to the specified constant value. + * + * @category mapping + * @since 2.16.0 + */ +export const as: { + (self: TaskOption<_>, a: A): TaskOption + (a: A): <_>(self: TaskOption<_>) => TaskOption +} = dual(2, as_(Functor)) + +/** + * Maps the `Some` value of this `TaskOption` to the void constant value. + * + * @category mapping + * @since 2.16.0 + */ +export const asUnit: <_>(self: TaskOption<_>) => TaskOption = asUnit_(Functor) + /** * @category mapping * @since 2.10.0 diff --git a/test/Either.ts b/test/Either.ts index 89a4a3bc70..93b6268384 100644 --- a/test/Either.ts +++ b/test/Either.ts @@ -695,4 +695,14 @@ describe.concurrent('Either', () => { U.deepStrictEqual(f('a'), _.right(1)) U.deepStrictEqual(f(''), _.left(new Error('empty string'))) }) + + it('as', () => { + U.deepStrictEqual(pipe(_.right('a'), _.as('b')), _.right('b')) + U.deepStrictEqual(_.as(_.of('a'), 'b'), _.right('b')) + U.deepStrictEqual(_.as(_.left('error'), 'b'), _.left('error')) + }) + + it('asUnit', () => { + U.deepStrictEqual(pipe(_.of('a'), _.asUnit), _.of(undefined)) + }) }) diff --git a/test/IOEither.ts b/test/IOEither.ts index b84c1ad10d..e51f20514d 100644 --- a/test/IOEither.ts +++ b/test/IOEither.ts @@ -699,4 +699,14 @@ describe.concurrent('IOEither', () => { U.deepStrictEqual(pipe(_.left('error'), _.tapIO(add))(), E.left('error')) U.deepStrictEqual(ref, [1]) }) + + it('as', () => { + U.deepStrictEqual(pipe(_.right('a'), _.as('b'))(), E.right('b')) + U.deepStrictEqual(_.as(_.of('a'), 'b')(), E.right('b')) + U.deepStrictEqual(_.as(_.left('error'), 'b')(), E.left('error')) + }) + + it('asUnit', () => { + U.deepStrictEqual(pipe(_.of('a'), _.asUnit)(), E.of(undefined)) + }) }) diff --git a/test/IOOption.ts b/test/IOOption.ts index e4962b7b89..f3507fd183 100644 --- a/test/IOOption.ts +++ b/test/IOOption.ts @@ -265,4 +265,14 @@ describe.concurrent('IOOption', () => { U.deepStrictEqual(pipe(_.none, _.tapIO(add))(), O.none) U.deepStrictEqual(ref, [1]) }) + + it('as', () => { + U.deepStrictEqual(pipe(_.some('a'), _.as('b'))(), O.some('b')) + U.deepStrictEqual(_.as(_.of('a'), 'b')(), O.some('b')) + U.deepStrictEqual(_.as(_.none, 'b')(), O.none) + }) + + it('asUnit', () => { + U.deepStrictEqual(pipe(_.some('a'), _.asUnit)(), O.some(undefined)) + }) }) diff --git a/test/Option.ts b/test/Option.ts index 8bdcf3a716..73dc98c0bd 100644 --- a/test/Option.ts +++ b/test/Option.ts @@ -558,4 +558,14 @@ describe.concurrent('Option', () => { const g = (s: string) => E.left(s.length) U.deepStrictEqual(pipe(_.some('a'), _.chainFirstEitherK(g)), _.none) }) + + it('as', () => { + U.deepStrictEqual(pipe(_.some('a'), _.as('b')), _.some('b')) + U.deepStrictEqual(_.as(_.of('a'), 'b'), _.some('b')) + U.deepStrictEqual(_.as(_.none, 'b'), _.none) + }) + + it('asUnit', () => { + U.deepStrictEqual(pipe(_.some('a'), _.asUnit), _.some(undefined)) + }) }) diff --git a/test/ReaderEither.ts b/test/ReaderEither.ts index 59a66dcf8a..7622031f59 100644 --- a/test/ReaderEither.ts +++ b/test/ReaderEither.ts @@ -413,4 +413,14 @@ describe.concurrent('ReaderEither', () => { const g = (s: string) => E.left(s.length) U.deepStrictEqual(pipe(_.right('a'), _.chainFirstEitherK(g))({}), E.left(1)) }) + + it('as', () => { + U.deepStrictEqual(pipe(_.right('a'), _.as('b'))(undefined), E.right('b')) + U.deepStrictEqual(_.as(_.of('a'), 'b')(undefined), E.right('b')) + U.deepStrictEqual(_.as(_.left('error'), 'b')(undefined), E.left('error')) + }) + + it('asUnit', () => { + U.deepStrictEqual(pipe(_.of('a'), _.asUnit)(undefined), E.of(undefined)) + }) }) diff --git a/test/ReaderTaskEither.ts b/test/ReaderTaskEither.ts index f56bccb975..8fb92b97ff 100644 --- a/test/ReaderTaskEither.ts +++ b/test/ReaderTaskEither.ts @@ -751,4 +751,14 @@ describe.concurrent('ReaderTaskEither', () => { U.deepStrictEqual(await pipe(_.left('error'), _.tapIO(add))(undefined)(), E.left('error')) U.deepStrictEqual(ref, [1]) }) + + it('as', async () => { + U.deepStrictEqual(await pipe(_.right('a'), _.as('b'))(undefined)(), E.right('b')) + U.deepStrictEqual(await _.as(_.of('a'), 'b')(undefined)(), E.right('b')) + U.deepStrictEqual(await _.as(_.left('error'), 'b')(undefined)(), E.left('error')) + }) + + it('asUnit', async () => { + U.deepStrictEqual(await pipe(_.of('a'), _.asUnit)(undefined)(), E.of(undefined)) + }) }) diff --git a/test/StateReaderTaskEither.ts b/test/StateReaderTaskEither.ts index c2a3387b3b..4b9d50a0ba 100644 --- a/test/StateReaderTaskEither.ts +++ b/test/StateReaderTaskEither.ts @@ -523,4 +523,13 @@ describe.concurrent('StateReaderTaskEither', () => { U.deepStrictEqual(await pipe(_.left('error'), _.tapIO(add), _.evaluate(state))(undefined)(), E.left('error')) U.deepStrictEqual(ref, [1]) }) + + it('as', async () => { + U.deepStrictEqual(await pipe(_.right('a'), _.as('b'), _.evaluate(state))(undefined)(), E.right('b')) + U.deepStrictEqual(await pipe(_.as(_.left('error'), 'b'), _.evaluate(state))(undefined)(), E.left('error')) + }) + + it('asUnit', async () => { + U.deepStrictEqual(await pipe(_.of('a'), _.asUnit, _.evaluate(state))(undefined)(), E.of(undefined)) + }) }) diff --git a/test/TaskEither.ts b/test/TaskEither.ts index ed063078fa..d220862639 100644 --- a/test/TaskEither.ts +++ b/test/TaskEither.ts @@ -862,4 +862,14 @@ describe.concurrent('TaskEither', () => { U.deepStrictEqual(await pipe(_.left('error'), _.tapIO(add))(), E.left('error')) U.deepStrictEqual(ref, [1]) }) + + it('as', async () => { + U.deepStrictEqual(await pipe(_.right('a'), _.as('b'))(), E.right('b')) + U.deepStrictEqual(await _.as(_.of('a'), 'b')(), E.right('b')) + U.deepStrictEqual(await _.as(_.left('error'), 'b')(), E.left('error')) + }) + + it('asUnit', async () => { + U.deepStrictEqual(await pipe(_.of('a'), _.asUnit)(), E.of(undefined)) + }) }) diff --git a/test/TaskOption.ts b/test/TaskOption.ts index f297e858ac..17c90ba13f 100644 --- a/test/TaskOption.ts +++ b/test/TaskOption.ts @@ -390,4 +390,14 @@ describe.concurrent('TaskOption', () => { U.deepStrictEqual(await pipe(_.none, _.tapIO(add))(), O.none) U.deepStrictEqual(ref, [1]) }) + + it('as', async () => { + U.deepStrictEqual(await pipe(_.some('a'), _.as('b'))(), O.some('b')) + U.deepStrictEqual(await _.as(_.of('a'), 'b')(), O.some('b')) + U.deepStrictEqual(await _.as(_.none, 'b')(), O.none) + }) + + it('asUnit', async () => { + U.deepStrictEqual(await pipe(_.some('a'), _.asUnit)(), O.some(undefined)) + }) })