From 9569811ea5506fb4206c1b9dbde7dbdbe7e9a8bc Mon Sep 17 00:00:00 2001 From: gcanti Date: Sun, 29 Jan 2023 09:11:33 +0100 Subject: [PATCH] Covariant: flip flap --- .changeset/poor-crabs-drum.md | 5 +++++ docs/modules/Either.ts.md | 2 +- docs/modules/Option.ts.md | 2 +- docs/modules/ReadonlyArray.ts.md | 2 +- docs/modules/These.ts.md | 2 +- docs/modules/typeclass/Covariant.ts.md | 2 +- src/Either.ts | 6 ++---- src/Option.ts | 5 ++--- src/ReadonlyArray.ts | 6 +++--- src/These.ts | 6 ++---- src/typeclass/Covariant.ts | 5 +++-- test/typeclass/Covariant.ts | 4 ++-- 12 files changed, 24 insertions(+), 23 deletions(-) create mode 100644 .changeset/poor-crabs-drum.md diff --git a/.changeset/poor-crabs-drum.md b/.changeset/poor-crabs-drum.md new file mode 100644 index 000000000..a9e23201c --- /dev/null +++ b/.changeset/poor-crabs-drum.md @@ -0,0 +1,5 @@ +--- +"@fp-ts/core": minor +--- + +Covariant: flip flap diff --git a/docs/modules/Either.ts.md b/docs/modules/Either.ts.md index a787d4819..de81e01f5 100644 --- a/docs/modules/Either.ts.md +++ b/docs/modules/Either.ts.md @@ -1117,7 +1117,7 @@ Added in v1.0.0 **Signature** ```ts -export declare const flap: (a: A) => (self: Either B>) => Either +export declare const flap: (self: Either B>) => (a: A) => Either ``` Added in v1.0.0 diff --git a/docs/modules/Option.ts.md b/docs/modules/Option.ts.md index 928989ffa..e8c73f456 100644 --- a/docs/modules/Option.ts.md +++ b/docs/modules/Option.ts.md @@ -1173,7 +1173,7 @@ Added in v1.0.0 **Signature** ```ts -export declare const flap: (a: A) => (fab: Option<(a: A) => B>) => Option +export declare const flap: (self: Option<(a: A) => B>) => (a: A) => Option ``` Added in v1.0.0 diff --git a/docs/modules/ReadonlyArray.ts.md b/docs/modules/ReadonlyArray.ts.md index 2f90570f3..ddf8d7e70 100644 --- a/docs/modules/ReadonlyArray.ts.md +++ b/docs/modules/ReadonlyArray.ts.md @@ -1441,7 +1441,7 @@ Added in v1.0.0 **Signature** ```ts -export declare const flap: (a: A) => (self: readonly ((a: A) => B)[]) => B[] +export declare const flap: (self: readonly ((a: A) => B)[]) => (a: A) => B[] ``` Added in v1.0.0 diff --git a/docs/modules/These.ts.md b/docs/modules/These.ts.md index 2c7cc515e..f36d347cb 100644 --- a/docs/modules/These.ts.md +++ b/docs/modules/These.ts.md @@ -1213,7 +1213,7 @@ Added in v1.0.0 **Signature** ```ts -export declare const flap: (a: A) => (self: any) => any +export declare const flap: (self: any) => (a: A) => any ``` Added in v1.0.0 diff --git a/docs/modules/typeclass/Covariant.ts.md b/docs/modules/typeclass/Covariant.ts.md index 77af9c2d2..26665b76a 100644 --- a/docs/modules/typeclass/Covariant.ts.md +++ b/docs/modules/typeclass/Covariant.ts.md @@ -66,7 +66,7 @@ Added in v1.0.0 **Signature** ```ts -export declare const flap: (F: Covariant) => (a: A) => (self: any) => any +export declare const flap: (F: Covariant) => (self: any) => (a: A) => any ``` Added in v1.0.0 diff --git a/src/Either.ts b/src/Either.ts index 9b78b7f45..0a21211eb 100644 --- a/src/Either.ts +++ b/src/Either.ts @@ -160,10 +160,8 @@ export const bindTo: ( * @category mapping * @since 1.0.0 */ -export const flap: (a: A) => (self: Either B>) => Either = covariant - .flap( - Covariant - ) +export const flap: (self: Either B>) => (a: A) => Either = covariant + .flap(Covariant) /** * Maps the Right value of this effect to the specified constant value. diff --git a/src/Option.ts b/src/Option.ts index d8f198c47..ac89f6556 100644 --- a/src/Option.ts +++ b/src/Option.ts @@ -515,9 +515,8 @@ export { * @category mapping * @since 1.0.0 */ -export const flap: (a: A) => (fab: Option<(a: A) => B>) => Option = covariant.flap( - Covariant -) +export const flap: (self: Option<(a: A) => B>) => (a: A) => Option = covariant + .flap(Covariant) /** * Maps the `Some` value of this option to the specified constant value. diff --git a/src/ReadonlyArray.ts b/src/ReadonlyArray.ts index 3c85f113b..2cca14ffa 100644 --- a/src/ReadonlyArray.ts +++ b/src/ReadonlyArray.ts @@ -1317,9 +1317,9 @@ export { * @category mapping * @since 1.0.0 */ -export const flap: (a: A) => ( - self: ReadonlyArray<(a: A) => B> -) => Array = covariant.flap(Covariant) as any +export const flap: (self: ReadonlyArray<(a: A) => B>) => (a: A) => Array = covariant.flap( + Covariant +) as any /** * Maps the success value of this effect to the specified constant value. diff --git a/src/These.ts b/src/These.ts index ea198ea05..808ceb78f 100644 --- a/src/These.ts +++ b/src/These.ts @@ -634,10 +634,8 @@ export const bindTo: ( * @category mapping * @since 1.0.0 */ -export const flap: (a: A) => (self: These B>) => These = covariant - .flap( - Covariant - ) +export const flap: (self: These B>) => (a: A) => These = covariant + .flap(Covariant) /** * Maps the right value of this effect to the specified constant value. diff --git a/src/typeclass/Covariant.ts b/src/typeclass/Covariant.ts index d1200e9e4..972540128 100644 --- a/src/typeclass/Covariant.ts +++ b/src/typeclass/Covariant.ts @@ -1,6 +1,7 @@ /** * @since 1.0.0 */ +import { pipe } from "@fp-ts/core/Function" import type { Kind, TypeLambda } from "@fp-ts/core/HKT" import type { Invariant } from "@fp-ts/core/typeclass/Invariant" @@ -50,8 +51,8 @@ export const make = (map: Covariant["map"]): Covariant< * @since 1.0.0 */ export const flap = (F: Covariant) => - (a: A): ((self: Kind B>) => Kind) => - F.map(f => f(a)) + (self: Kind B>) => + (a: A): Kind => pipe(self, F.map(f => f(a))) /** * @category mapping diff --git a/test/typeclass/Covariant.ts b/test/typeclass/Covariant.ts index 5fc4acac0..c803cfbb0 100644 --- a/test/typeclass/Covariant.ts +++ b/test/typeclass/Covariant.ts @@ -21,8 +21,8 @@ describe("Covariant", () => { it("flap", () => { const flap = _.flap(O.Covariant) - U.deepStrictEqual(pipe(O.none(), flap(1)), O.none()) - U.deepStrictEqual(pipe(O.some(U.double), flap(1)), O.some(2)) + U.deepStrictEqual(pipe(1, flap(O.none())), O.none()) + U.deepStrictEqual(pipe(1, flap(O.some(U.double))), O.some(2)) }) it("as", () => {