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

feat: add ap method to Effect, ap and zipWith to Either ⚡️ #1504

Merged
merged 7 commits into from
Oct 9, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
5 changes: 5 additions & 0 deletions .changeset/breeze-maps-love.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"effect": patch
---

feat: add `ap` method to `Effect`, `ap` and `zipWith` to `Either` ⚡️
17 changes: 16 additions & 1 deletion src/Effect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import type * as FiberRef from "./FiberRef"
import type * as FiberRefs from "./FiberRefs"
import type * as FiberRefsPatch from "./FiberRefsPatch"
import type { LazyArg } from "./Function"
import { identity } from "./Function"
import { dual, identity } from "./Function"
import type * as HashMap from "./HashMap"
import type * as HashSet from "./HashSet"
import type { TypeLambda } from "./HKT"
Expand Down Expand Up @@ -4744,6 +4744,21 @@ export const zipWith: {
): Effect<R | R2, E | E2, B>
} = fiberRuntime.zipWithOptions

// -------------------------------------------------------------------------------------
// applicatives
// -------------------------------------------------------------------------------------
/**
* @category combining
* @since 2.0.0
*/
export const ap: {
gcanti marked this conversation as resolved.
Show resolved Hide resolved
<R2, E2, A>(that: Effect<R2, E2, A>): <R, E, B>(self: Effect<R, E, (a: A) => B>) => Effect<R | R2, E | E2, B>
<R, E, A, B, R2, E2>(self: Effect<R, E, (a: A) => B>, that: Effect<R2, E2, A>): Effect<R | R2, E | E2, B>
} = dual(
2,
<R, E, A, B, R2, E2>(self: Effect<R, E, (a: A) => B>, that: Effect<R2, E2, A>): Effect<R | R2, E | E2, B> =>
zipWith(self, that, (f, a) => f(a))
)
// -------------------------------------------------------------------------------------
// requests & batching
// -------------------------------------------------------------------------------------
Expand Down
33 changes: 33 additions & 0 deletions src/Either.ts
Original file line number Diff line number Diff line change
Expand Up @@ -503,6 +503,39 @@ export const flatMap: {
isLeft(self) ? left(self.left) : f(self.right)
)

/**
* @since 2.0.0
* @category combining
*/
export const zipWith: {
<E2, A2, A, B>(
that: Either<E2, A2>,
f: (a: A, b: A2) => B
): <E>(self: Either<E, A>) => Either<E2 | E, B>
<E, A, E2, A2, B>(
self: Either<E, A>,
that: Either<E2, A2>,
f: (a: A, b: A2) => B
): Either<E | E2, B>
} = dual(
3,
<E, A, E2, A2, B>(self: Either<E, A>, that: Either<E2, A2>, f: (a: A, b: A2) => B): Either<E | E2, B> =>
flatMap(self, (a) => map(that, (b) => f(a, b)))
)

/**
* @category combining
* @since 2.0.0
*/
export const ap: {
<E2, A>(that: Either<E2, A>): <E, B>(self: Either<E, (a: A) => B>) => Either<E | E2, B>
<E, A, B, E2>(self: Either<E, (a: A) => B>, that: Either<E2, A>): Either<E | E2, B>
} = dual(
2,
<E, A, B, E2>(self: Either<E, (a: A) => B>, that: Either<E2, A>): Either<E | E2, B> =>
zipWith(self, that, (f, a) => f(a))
)

/**
* Takes a structure of `Option`s and returns an `Option` of values with the same structure.
*
Expand Down
6 changes: 6 additions & 0 deletions test/Option.ts
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,12 @@ describe.concurrent("Option", () => {
expect(pipe(_.some(1), _.zipWith(_.some(2), (a, b) => a + b))).toEqual(_.some(3))
})

it("ap", () => {
expect(pipe(_.some((a: number) => (b: number) => a + b), _.ap(_.none()), _.ap(_.some(2)))).toEqual(_.none())
expect(pipe(_.some((a: number) => (b: number) => a + b), _.ap(_.some(1)), _.ap(_.none()))).toEqual(_.none())
expect(pipe(_.some((a: number) => (b: number) => a + b), _.ap(_.some(1)), _.ap(_.some(2)))).toEqual(_.some(3))
})

it("reduceCompact", () => {
const sumCompact = _.reduceCompact(0, N.sum)
expect(sumCompact([])).toEqual(0)
Expand Down