Skip to content

Commit

Permalink
Covariant: flip flap
Browse files Browse the repository at this point in the history
  • Loading branch information
gcanti committed Feb 7, 2023
1 parent 2bb91d1 commit 9569811
Show file tree
Hide file tree
Showing 12 changed files with 24 additions and 23 deletions.
5 changes: 5 additions & 0 deletions .changeset/poor-crabs-drum.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@fp-ts/core": minor
---

Covariant: flip flap
2 changes: 1 addition & 1 deletion docs/modules/Either.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -1117,7 +1117,7 @@ Added in v1.0.0
**Signature**
```ts
export declare const flap: <A>(a: A) => <E, B>(self: Either<E, (a: A) => B>) => Either<E, B>
export declare const flap: <E, A, B>(self: Either<E, (a: A) => B>) => (a: A) => Either<E, B>
```
Added in v1.0.0
Expand Down
2 changes: 1 addition & 1 deletion docs/modules/Option.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -1173,7 +1173,7 @@ Added in v1.0.0
**Signature**
```ts
export declare const flap: <A>(a: A) => <B>(fab: Option<(a: A) => B>) => Option<B>
export declare const flap: <A, B>(self: Option<(a: A) => B>) => (a: A) => Option<B>
```
Added in v1.0.0
Expand Down
2 changes: 1 addition & 1 deletion docs/modules/ReadonlyArray.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -1441,7 +1441,7 @@ Added in v1.0.0
**Signature**

```ts
export declare const flap: <A>(a: A) => <B>(self: readonly ((a: A) => B)[]) => B[]
export declare const flap: <A, B>(self: readonly ((a: A) => B)[]) => (a: A) => B[]
```

Added in v1.0.0
Expand Down
2 changes: 1 addition & 1 deletion docs/modules/These.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -1213,7 +1213,7 @@ Added in v1.0.0
**Signature**
```ts
export declare const flap: <A>(a: A) => <E, B>(self: any) => any
export declare const flap: <E, A, B>(self: any) => (a: A) => any
```
Added in v1.0.0
Expand Down
2 changes: 1 addition & 1 deletion docs/modules/typeclass/Covariant.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ Added in v1.0.0
**Signature**
```ts
export declare const flap: <F extends any>(F: Covariant<F>) => <A>(a: A) => <R, O, E, B>(self: any) => any
export declare const flap: <F extends any>(F: Covariant<F>) => <R, O, E, A, B>(self: any) => (a: A) => any
```
Added in v1.0.0
Expand Down
6 changes: 2 additions & 4 deletions src/Either.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,10 +160,8 @@ export const bindTo: <N extends string>(
* @category mapping
* @since 1.0.0
*/
export const flap: <A>(a: A) => <E, B>(self: Either<E, (a: A) => B>) => Either<E, B> = covariant
.flap(
Covariant
)
export const flap: <E, A, B>(self: Either<E, (a: A) => B>) => (a: A) => Either<E, B> = covariant
.flap(Covariant)

/**
* Maps the Right value of this effect to the specified constant value.
Expand Down
5 changes: 2 additions & 3 deletions src/Option.ts
Original file line number Diff line number Diff line change
Expand Up @@ -515,9 +515,8 @@ export {
* @category mapping
* @since 1.0.0
*/
export const flap: <A>(a: A) => <B>(fab: Option<(a: A) => B>) => Option<B> = covariant.flap(
Covariant
)
export const flap: <A, B>(self: Option<(a: A) => B>) => (a: A) => Option<B> = covariant
.flap(Covariant)

/**
* Maps the `Some` value of this option to the specified constant value.
Expand Down
6 changes: 3 additions & 3 deletions src/ReadonlyArray.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1317,9 +1317,9 @@ export {
* @category mapping
* @since 1.0.0
*/
export const flap: <A>(a: A) => <B>(
self: ReadonlyArray<(a: A) => B>
) => Array<B> = covariant.flap(Covariant) as any
export const flap: <A, B>(self: ReadonlyArray<(a: A) => B>) => (a: A) => Array<B> = covariant.flap(
Covariant
) as any

/**
* Maps the success value of this effect to the specified constant value.
Expand Down
6 changes: 2 additions & 4 deletions src/These.ts
Original file line number Diff line number Diff line change
Expand Up @@ -634,10 +634,8 @@ export const bindTo: <N extends string>(
* @category mapping
* @since 1.0.0
*/
export const flap: <A>(a: A) => <E, B>(self: These<E, (a: A) => B>) => These<E, B> = covariant
.flap(
Covariant
)
export const flap: <E, A, B>(self: These<E, (a: A) => B>) => (a: A) => These<E, B> = covariant
.flap(Covariant)

/**
* Maps the right value of this effect to the specified constant value.
Expand Down
5 changes: 3 additions & 2 deletions src/typeclass/Covariant.ts
Original file line number Diff line number Diff line change
@@ -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"

Expand Down Expand Up @@ -50,8 +51,8 @@ export const make = <F extends TypeLambda>(map: Covariant<F>["map"]): Covariant<
* @since 1.0.0
*/
export const flap = <F extends TypeLambda>(F: Covariant<F>) =>
<A>(a: A): (<R, O, E, B>(self: Kind<F, R, O, E, (a: A) => B>) => Kind<F, R, O, E, B>) =>
F.map(f => f(a))
<R, O, E, A, B>(self: Kind<F, R, O, E, (a: A) => B>) =>
(a: A): Kind<F, R, O, E, B> => pipe(self, F.map(f => f(a)))

/**
* @category mapping
Expand Down
4 changes: 2 additions & 2 deletions test/typeclass/Covariant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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", () => {
Expand Down

0 comments on commit 9569811

Please sign in to comment.