Skip to content

Commit

Permalink
Remove Effect.unified and Effect.unifiedFn in favour of Unify.unify (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
mikearnaldi authored and gcanti committed Feb 16, 2024
1 parent fa5fb63 commit 908293f
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 52 deletions.
31 changes: 31 additions & 0 deletions .changeset/fair-scissors-compare.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
---
"effect": minor
---

Remove Effect.unified and Effect.unifiedFn in favour of Unify.unify.

The `Unify` module fully replaces the need for specific unify functions, when before you did:

```ts
import { Effect } from "effect";

const effect = Effect.unified(
Math.random() > 0.5 ? Effect.succeed("OK") : Effect.fail("NO")
);
const effectFn = Effect.unifiedFn((n: number) =>
Math.random() > 0.5 ? Effect.succeed("OK") : Effect.fail("NO")
);
```

You can now do:

```ts
import { Effect, Unify } from "effect";

const effect = Unify.unify(
Math.random() > 0.5 ? Effect.succeed("OK") : Effect.fail("NO")
);
const effectFn = Unify.unify((n: number) =>
Math.random() > 0.5 ? Effect.succeed("OK") : Effect.fail("NO")
);
```
3 changes: 2 additions & 1 deletion packages/cli/src/internal/cliApp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import * as HashMap from "effect/HashMap"
import * as Option from "effect/Option"
import { pipeArguments } from "effect/Pipeable"
import * as ReadonlyArray from "effect/ReadonlyArray"
import * as Unify from "effect/Unify"
import type * as BuiltInOptions from "../BuiltInOptions.js"
import type * as CliApp from "../CliApp.js"
import type * as CliConfig from "../CliConfig.js"
Expand Down Expand Up @@ -80,7 +81,7 @@ export const run = dual<
// Handle the command
return Effect.matchEffect(InternalCommand.parse(self.command, prefixedArgs, config), {
onFailure: (e) => Effect.zipRight(printDocs(e.error), Effect.fail(e)),
onSuccess: Effect.unifiedFn((directive) => {
onSuccess: Unify.unify((directive) => {
switch (directive._tag) {
case "UserDefined": {
return ReadonlyArray.matchLeft(directive.leftover, {
Expand Down
33 changes: 1 addition & 32 deletions packages/effect/src/Effect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import type * as FiberRef from "./FiberRef.js"
import type * as FiberRefs from "./FiberRefs.js"
import type * as FiberRefsPatch from "./FiberRefsPatch.js"
import type { LazyArg } from "./Function.js"
import { dual, identity } from "./Function.js"
import { dual } from "./Function.js"
import type * as HashMap from "./HashMap.js"
import type * as HashSet from "./HashSet.js"
import type { TypeLambda } from "./HKT.js"
Expand Down Expand Up @@ -212,15 +212,6 @@ export declare namespace Effect {
readonly _E: Covariant<E>
readonly _R: Covariant<R>
}
/**
* @since 2.0.0
* @category models
*/
export type Unify<Ret extends Effect<any, any, any>> = Effect<
Success<Ret>,
Error<Ret>,
Context<Ret>
>
/**
* @since 2.0.0
* @category type-level
Expand Down Expand Up @@ -4617,28 +4608,6 @@ export const withMetric: {
<R, E, A extends In, Type, In, Out>(self: Effect<A, E, R>, metric: Metric.Metric<Type, In, Out>): Effect<A, E, R>
} = effect.withMetric

// -------------------------------------------------------------------------------------
// unify
// -------------------------------------------------------------------------------------

/**
* Used to unify functions that would otherwise return `Effect<A, B, C> | Effect<D, E, F>`
*
* @category unify
* @since 2.0.0
*/
export const unifiedFn: <Args extends ReadonlyArray<any>, Ret extends Effect<any, any, any>>(
f: (...args: Args) => Ret
) => (...args: Args) => Effect.Unify<Ret> = core.unified

/**
* Used to unify effects that would otherwise be `Effect<A, B, C> | Effect<D, E, F>`
*
* @category unify
* @since 2.0.0
*/
export const unified: <Ret extends Effect<any, any, any>>(f: Ret) => Effect.Unify<Ret> = identity

// -------------------------------------------------------------------------------------
// semaphore
// -------------------------------------------------------------------------------------
Expand Down
18 changes: 12 additions & 6 deletions packages/effect/src/internal/core-effect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,10 +147,13 @@ export const catchAllDefect = dual<
self: Effect.Effect<A, E, R>,
f: (defect: unknown) => Effect.Effect<A2, E2, R2>
) => Effect.Effect<A | A2, E | E2, R | R2>
>(2, (self, f) =>
>(2, <A, E, R, A2, E2, R2>(
self: Effect.Effect<A, E, R>,
f: (defect: unknown) => Effect.Effect<A2, E2, R2>
): Effect.Effect<A | A2, E | E2, R | R2> =>
core.catchAllCause(
self,
core.unified((cause) => {
(cause): Effect.Effect<A | A2, E | E2, R | R2> => {
const option = internalCause.find(cause, (_) => internalCause.isDieType(_) ? Option.some(_) : Option.none())
switch (option._tag) {
case "None": {
Expand All @@ -160,7 +163,7 @@ export const catchAllDefect = dual<
return f(option.value.defect)
}
}
})
}
))

/* @internal */
Expand Down Expand Up @@ -205,10 +208,13 @@ export const catchSomeDefect = dual<
) => Effect.Effect<A | A2, E | E2, R | R2>
>(
2,
(self, pf) =>
<A, E, R, A2, E2, R2>(
self: Effect.Effect<A, E, R>,
pf: (defect: unknown) => Option.Option<Effect.Effect<A2, E2, R2>>
): Effect.Effect<A | A2, E | E2, R | R2> =>
core.catchAllCause(
self,
core.unified((cause) => {
(cause): Effect.Effect<A | A2, E | E2, R | R2> => {
const option = internalCause.find(cause, (_) => internalCause.isDieType(_) ? Option.some(_) : Option.none())
switch (option._tag) {
case "None": {
Expand All @@ -219,7 +225,7 @@ export const catchSomeDefect = dual<
return optionEffect._tag === "Some" ? optionEffect.value : core.failCause(cause)
}
}
})
}
)
)

Expand Down
13 changes: 2 additions & 11 deletions packages/effect/src/internal/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -514,15 +514,6 @@ export const catchAll: {
): Effect.Effect<A2 | A, E2, R2 | R> => matchEffect(self, { onFailure: f, onSuccess: succeed })
)

/**
* @macro identity
* @internal
*/
export const unified = <Args extends ReadonlyArray<any>, Ret extends Effect.Effect<any, any, any>>(
f: (...args: Args) => Ret
) =>
(...args: Args): Effect.Effect.Unify<Ret> => f(...args)

/* @internal */
export const catchIf: {
<E, EB extends E, A2, E2, R2>(
Expand Down Expand Up @@ -925,7 +916,7 @@ export const if_ = dual<
(self: boolean | Effect.Effect<unknown, unknown, unknown>, { onFalse, onTrue }: {
readonly onTrue: Effect.Effect<unknown, unknown, unknown>
readonly onFalse: Effect.Effect<unknown, unknown, unknown>
}) => typeof self === "boolean" ? (self ? onTrue : onFalse) : flatMap(self, unified((b) => (b ? onTrue : onFalse)))
}) => typeof self === "boolean" ? (self ? onTrue : onFalse) : flatMap(self, (b) => (b ? onTrue : onFalse))
)

/* @internal */
Expand Down Expand Up @@ -1035,7 +1026,7 @@ export const onError: {
} = dual(2, <A, E, R, X, R2>(
self: Effect.Effect<A, E, R>,
cleanup: (cause: Cause.Cause<E>) => Effect.Effect<X, never, R2>
): Effect.Effect<A, E, R2 | R> => onExit(self, unified((exit) => exitIsSuccess(exit) ? unit : cleanup(exit.i0))))
): Effect.Effect<A, E, R2 | R> => onExit(self, (exit) => exitIsSuccess(exit) ? unit : cleanup(exit.i0)))

/* @internal */
export const onExit: {
Expand Down
4 changes: 2 additions & 2 deletions packages/platform/test/Http/Multipart.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as Multipart from "@effect/platform/Http/Multipart"
import { Chunk, Effect, identity, Stream } from "effect"
import { Chunk, Effect, identity, Stream, Unify } from "effect"
import { assert, describe, test } from "vitest"

describe("Multipart", () => {
Expand All @@ -15,7 +15,7 @@ describe("Multipart", () => {
Stream.fromReadableStream(() => response.body!, identity),
Stream.pipeThroughChannel(Multipart.makeChannel(Object.fromEntries(response.headers))),
Stream.mapEffect((part) => {
return Effect.unified(
return Unify.unify(
part._tag === "File" ?
Effect.zip(
Effect.succeed(part.name),
Expand Down

0 comments on commit 908293f

Please sign in to comment.