Skip to content

Commit

Permalink
Fix Effect.all occurrences (#173)
Browse files Browse the repository at this point in the history
  • Loading branch information
gcanti authored Jul 11, 2023
1 parent e41a2c8 commit cd28bae
Show file tree
Hide file tree
Showing 16 changed files with 68 additions and 38 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
"react-dom": "18.2.0"
},
"devDependencies": {
"@effect/data": "^0.13.6",
"@effect/io": "^0.31.4",
"@effect/data": "^0.14.1",
"@effect/io": "^0.32.1",
"@mdx-js/mdx": "^2.3.0",
"@types/node": "^18.16.1",
"@types/react": "^18.2.0",
Expand Down
2 changes: 2 additions & 0 deletions pages/docs/_meta.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
"type": "separator"
},
"data-types": "Data types",
"trait": "Traits",
"behaviour": "Behaviours",
"packages": "Packages",
"-More": {
"title": "More",
Expand Down
4 changes: 4 additions & 0 deletions pages/docs/behaviour/_meta.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"equivalence": "Equivalence",
"order": "Order"
}
5 changes: 5 additions & 0 deletions pages/docs/behaviour/equivalence.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Equivalence

import { Stub } from "@/components/Stub"

<Stub />
5 changes: 5 additions & 0 deletions pages/docs/behaviour/order.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Order

import { Stub } from "@/components/Stub"

<Stub />
20 changes: 10 additions & 10 deletions pages/docs/essentials/pipeline.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -208,10 +208,10 @@ The syntax for `all` is as follows:
```ts
import * as Effect from "@effect/io/Effect"

const combinedEffect = Effect.all(...effects)
const combinedEffect = Effect.all(effects)
```

Here, `...effects` represents multiple effects that you want to combine. The `all` function will execute all these effects in sequence and return a new effect that produces a tuple containing the results of each individual effect. This can be useful when you need to perform multiple independent operations and collect their results in a structured way.
Here, `effects` represents multiple effects that you want to combine. The `all` function will execute all these effects in sequence and return a new effect that produces a tuple containing the results of each individual effect. This can be useful when you need to perform multiple independent operations and collect their results in a structured way.

Keep in mind that the order of the results corresponds to the order of the original effects passed to `all`.

Expand All @@ -224,7 +224,7 @@ const foo = Effect.succeed(42)
const bar = Effect.succeed("Hello")

// Effect<never, never, [number, string]>
const combinedEffect = Effect.all(foo, bar)
const combinedEffect = Effect.all([foo, bar])

console.log(Effect.runSync(combinedEffect)) // Output: [42, "Hello"]
```
Expand All @@ -251,7 +251,7 @@ const bar = Effect.succeed(2)

// Effect<never, Error, string>
const program = pipe(
Effect.all(foo, bar),
Effect.all([foo, bar]),
Effect.flatMap(([a, b]) => divide(a, b)),
Effect.map((n1) => increment(n1)),
Effect.map((n2) => `Result is: ${n2}`)
Expand Down Expand Up @@ -294,7 +294,7 @@ const foo = Effect.succeed(10)
const bar = Effect.succeed(2)

// Effect<never, Error, string>
const program = Effect.all(foo, bar).pipe(
const program = Effect.all([foo, bar]).pipe(
Effect.flatMap(([a, b]) => divide(a, b)),
Effect.map((n1) => increment(n1)),
Effect.map((n2) => `Result is: ${n2}`)
Expand All @@ -307,11 +307,11 @@ console.log(Effect.runSync(program)) // Output: "Result is: 6"

Let's summarize the transformation functions we have seen so far:

| **Function** | **Input** | **Output** |
| ------------ | ----------------------------------------- | ---------------------- |
| `map` | `Effect<R, E, A>`, `A => B` | `Effect<R, E, B>` |
| `flatMap` | `Effect<R, E, A>`, `A => Effect<R, E, B>` | `Effect<R, E, B>` |
| `all` | `Effect<R, E, A>`, `Effect<R, E, B>` | `Effect<R, E, [A, B]>` |
| **Function** | **Input** | **Output** |
| ------------ | ----------------------------------------- | --------------------------- |
| `map` | `Effect<R, E, A>`, `A => B` | `Effect<R, E, B>` |
| `flatMap` | `Effect<R, E, A>`, `A => Effect<R, E, B>` | `Effect<R, E, B>` |
| `all` | `Effect<R, E, A>`, `Effect<R, E, B>`, ... | `Effect<R, E, [A, B, ...]>` |

These functions are powerful tools for transforming and chaining `Effect` computations. They allow you to apply functions to values inside `Effect` and build complex pipelines of computations.

Expand Down
10 changes: 5 additions & 5 deletions pages/docs/essentials/using-generators.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ const divide = (a: number, b: number): Effect.Effect<never, Error, number> =>

// Effect<never, Error, string>
const program = Effect.gen(function* (_) {
const [a, b] = yield* _(Effect.all(Effect.succeed(10), Effect.succeed(2)))
const [a, b] = yield* _(Effect.all([Effect.succeed(10), Effect.succeed(2)]))
const n1 = yield* _(divide(a, b))
const n2 = increment(n1)
return `Result is: ${n2}`
Expand All @@ -56,7 +56,7 @@ const divide = (a: number, b: number): Effect.Effect<never, Error, number> =>

// Effect<never, Error, string>
const program = pipe(
Effect.all(Effect.succeed(10), Effect.succeed(2)),
Effect.all([Effect.succeed(10), Effect.succeed(2)]),
Effect.flatMap(([a, b]) => divide(a, b)),
Effect.map((n1) => increment(n1)),
Effect.map((n2) => `Result is: ${n2}`)
Expand All @@ -75,9 +75,9 @@ console.log(Effect.runSync(program)) // Output: "Result is: 6"

When working with generators in Effect, the `_` helper plays a crucial role when yielding an effect. By passing the effect you want to yield to the `_` function

```ts /Effect.all(Effect.succeed(10), Effect.succeed(2))/ /divide(a, b)/
```ts /Effect.all([Effect.succeed(10), Effect.succeed(2)])/ /divide(a, b)/
const result = Effect.gen(function* (_) {
const [a, b] = yield* _(Effect.all(Effect.succeed(10), Effect.succeed(2)))
const [a, b] = yield* _(Effect.all([Effect.succeed(10), Effect.succeed(2)]))
const n1 = yield* _(divide(a, b))
...
})
Expand Down Expand Up @@ -111,7 +111,7 @@ const divide = (a: number, b: number): Effect.Effect<never, Error, number> =>
: Effect.succeed(a / b)

const program = Effect.gen(function* (_) {
const [a, b] = yield* _(Effect.all(Effect.succeed(10), Effect.succeed(2)))
const [a, b] = yield* _(Effect.all([Effect.succeed(10), Effect.succeed(2)]))
const n1 = yield* _(divide(a, b))
const n2 = increment(n1)
return `Result is: ${n2}`
Expand Down
2 changes: 1 addition & 1 deletion pages/docs/guide/state-management/global-shared-state.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ const getNames = Effect.flatMap(Ref.make(Chunk.empty<string>()), (ref) => {
{ concurrency: "unbounded", discard: true }
)
)
return Effect.all(fiber1, fiber2).pipe(
return Effect.all([fiber1, fiber2]).pipe(
Effect.flatMap(([f1, f2]) =>
Fiber.join(f1).pipe(Effect.flatMap(() => Fiber.join(f2)))
),
Expand Down
4 changes: 4 additions & 0 deletions pages/docs/trait/_meta.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"hash": "Hash",
"equal": "Equal"
}
5 changes: 5 additions & 0 deletions pages/docs/trait/equal.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Equal

import { Stub } from "@/components/Stub"

<Stub />
5 changes: 5 additions & 0 deletions pages/docs/trait/hash.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Hash

import { Stub } from "@/components/Stub"

<Stub />
6 changes: 3 additions & 3 deletions pages/docs/tutorial/configuration.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ import * as Effect from "@effect/io/Effect"
import * as Config from "@effect/io/Config"

// Effect<never, ConfigError, void>
const program = Effect.all(
const program = Effect.all([
Effect.config(Config.string("HOST")),
Effect.config(Config.float("PORT"))
).pipe(
Effect.config(Config.float("PORT")),
]).pipe(
Effect.flatMap(([host, port]) =>
Effect.sync(() => console.log(`Application started: ${host}:${port}`))
)
Expand Down
6 changes: 3 additions & 3 deletions pages/docs/tutorial/context-management/layers.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -187,9 +187,9 @@ export const Recipe = Context.Tag<Recipe>()
// Layer<Flour | Sugar, never, Recipe>
export const RecipeLive = Layer.effect(
Recipe,
Effect.map(Effect.all(Sugar, Flour), ([sugar, flour]) =>
Effect.map(Effect.all([Sugar, Flour]), ([sugar, flour]) =>
Recipe.of({
steps: Effect.all(sugar.grams(200), flour.cups(1)),
steps: Effect.all([sugar.grams(200), flour.cups(1)]),
})
)
)
Expand Down Expand Up @@ -217,7 +217,7 @@ export const RecipeLive = Layer.effect(
const sugar = yield* _(Sugar)
const flour = yield* _(Flour)
return Recipe.of({
steps: Effect.all(sugar.grams(200), flour.cups(1)),
steps: Effect.all([sugar.grams(200), flour.cups(1)]),
})
})
)
Expand Down
6 changes: 3 additions & 3 deletions pages/docs/tutorial/context-management/services.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ By using the `Effect.serviceOption` function, we can gracefully handle scenarios

## Using Multiple Services

When we need to manage multiple services in our program, we can utilize the `Effect.all(...tags){:ts}` function.
When we need to manage multiple services in our program, we can utilize the `Effect.all(tags){:ts}` function.
By passing a tuple of tags, we can access the corresponding tuple of services:

<Tabs>
Expand All @@ -261,7 +261,7 @@ interface Logger {
const Logger = Context.Tag<Logger>()

// Effect<Random | Logger, never, void>
const program = Effect.all(Random, Logger).pipe(
const program = Effect.all([Random, Logger]).pipe(
Effect.flatMap(([random, logger]) =>
Effect.flatMap(random.next(), (randomNumber) =>
logger.log(String(randomNumber))
Expand Down Expand Up @@ -291,7 +291,7 @@ const Logger = Context.Tag<Logger>()

// Effect<Random | Logger, never, void>
const program = Effect.gen(function* (_) {
const [random, logger] = yield* _(Effect.all(Random, Logger))
const [random, logger] = yield* _(Effect.all([Random, Logger]))
const randomNumber = yield* _(random.next())
return yield* _(logger.log(String(randomNumber)))
})
Expand Down
4 changes: 2 additions & 2 deletions pages/docs/tutorial/error-management/expected-errors.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -82,13 +82,13 @@ const flakyBar = Random.next.pipe(
)

// Effect<never, FooError | BarError, string>
const program = Effect.all(flakyFoo, flakyBar).pipe(
const program = Effect.all([flakyFoo, flakyBar]).pipe(
Effect.map(([foo, bar]) => foo + bar)
)
```

In the above program, we have two operations: `flakyFoo` and `flakyBar`, each representing a potential source of error.
These operations are combined using the `Effect.all(...effects){:ts}` function from the Effect library, which allows us to sequence them together.
These operations are combined using the `Effect.all(effects){:ts}` function from the Effect library, which allows us to sequence them together.

</Tab>
<Tab>
Expand Down
18 changes: 9 additions & 9 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit cd28bae

Please sign in to comment.