Skip to content

Commit

Permalink
Update data types (#163)
Browse files Browse the repository at this point in the history
  • Loading branch information
gcanti authored Jul 7, 2023
1 parent 1881257 commit 8a2e588
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 37 deletions.
7 changes: 6 additions & 1 deletion pages/docs/data-types/chunk.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,12 @@ You can concatenate two Chunks using the `concat` function:
import * as Chunk from "@effect/data/Chunk"
// Chunk<string | number>
const concatenatedChunk = Chunk.concat(Chunk.make(1, 2), Chunk.make("a", "b"))
const concatenatedChunk = Chunk.appendAll(
Chunk.make(1, 2),
Chunk.make("a", "b")
)
console.log(concatenatedChunk) // Output: Chunk(1, 2, "a", "b")
```

### Dropping
Expand Down
4 changes: 2 additions & 2 deletions pages/docs/data-types/duration.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import * as Duration from "@effect/data/Duration"

const duration = Duration.seconds(30)

console.log(duration.millis) // Output: 30000
console.log(Duration.toMillis(duration)) // Output: 30000
```

## Comparing Durations
Expand All @@ -38,7 +38,7 @@ import * as Duration from "@effect/data/Duration"
const duration1 = Duration.seconds(30)
const duration2 = Duration.minutes(1)

console.log(Duration.Order.compare(duration1, duration2)) // Output: -1 (duration1 is less than duration2)
console.log(Duration.Order(duration1, duration2)) // Output: -1 (duration1 is less than duration2)
```

## Performing Arithmetic Operations
Expand Down
27 changes: 12 additions & 15 deletions pages/docs/data-types/either.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,10 @@ import * as Either from "@effect/data/Either"

const foo = Either.right(42)

const result = Either.match(
foo,
// onLeft callback
(left) => `The left value is: ${left}`,
// onRight callback
(right) => `The Right value is: ${right}`
)
const result = Either.match(foo, {
onLeft: (left) => `The left value is: ${left}`,
onRight: (right) => `The Right value is: ${right}`,
})

console.log(result) // Output: "The Right value is: 42"
```
Expand Down Expand Up @@ -147,21 +144,21 @@ import * as Either from "@effect/data/Either"
console.log(
pipe(
Either.right(42),
Either.bimap(
(s) => s + "!",
(n) => n + 1
)
Either.mapBoth({
onLeft: (s) => s + "!",
onRight: (n) => n + 1,
})
)
)
// Output: { _tag: 'Right', right: 43 }

console.log(
pipe(
Either.left("not a number"),
Either.bimap(
(s) => s + "!",
(n) => n + 1
)
Either.mapBoth({
onLeft: (s) => s + "!",
onRight: (n) => n + 1,
})
)
)
// Output: { _tag: 'Left', left: 'not a number!' }
Expand Down
55 changes: 36 additions & 19 deletions pages/docs/data-types/option.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,17 @@ The `some` constructor takes a value of type `A` and returns an `Option<A>` that
```ts
import * as Option from "@effect/data/Option"

// An Option holding the number 1
const value = Option.some(1)
// Option<number>
const value = Option.some(1) // An Option holding the number 1
```

On the other hand, the `none` constructor returns an `Option<never>`, representing the absence of a value:

```ts
import * as Option from "@effect/data/Option"

// An Option holding no value
const noValue = Option.none()
// Option<never>
const noValue = Option.none() // An Option holding no value
```

## Modeling Optional Properties
Expand All @@ -54,8 +54,6 @@ interface User {
Now, let's see how we can create instances of `User` with and without an email:

```ts
import * as Option from "@effect/data/Option"

const withEmail: User = {
id: 1,
username: "john_doe",
Expand Down Expand Up @@ -97,13 +95,10 @@ import * as Option from "@effect/data/Option"

const foo = Option.some(1)

const result = Option.match(
foo,
// onNone callback
() => "Option is empty",
// onSome callback
(value) => `Option has a value: ${value}`
)
const result = Option.match(foo, {
onNone: () => "Option is empty",
onSome: (value) => `Option has a value: ${value}`,
})

console.log(result) // Output: "Option has a value: 1"
```
Expand Down Expand Up @@ -238,11 +233,10 @@ const result: Option.Option<number> = pipe(
Option.orElse(() => performAlternativeComputation())
)
Option.match(
result,
() => console.log("Both computations resulted in None"),
(value) => console.log("Computed value:", value) // At least one computation succeeded
)
Option.match(result, {
onNone: () => console.log("Both computations resulted in None"),
onSome: (value) => console.log("Computed value:", value), // At least one computation succeeded
})
```

Additionally, the `Option.firstSomeOf` function can be used to retrieve the first value that is `Some` within an iterable of `Option` values:
Expand Down Expand Up @@ -367,7 +361,30 @@ These functions allow you to gain insights into what's happening within your cod
Here's an example to demonstrate how you can use `inspectSome` and `inspectNone` for debugging:

```ts
const street: Option.Option<string> = pipe(
import { pipe } from "@effect/data/Function"
import * as Option from "@effect/data/Option"
interface User {
readonly id: number
readonly username: string
readonly email: Option.Option<string>
readonly address: Option.Option<Address>
}
interface Address {
readonly city: string
readonly street: Option.Option<string>
}
const user: User = {
id: 1,
username: "john_doe",
email: Option.some("john.doe@example.com"),
address: Option.none(),
}
// Option<string>
const street = pipe(
user.address,
Option.inspectNone(() => console.log("No address provided")),
Option.flatMap((addr) => addr.street)
Expand Down

0 comments on commit 8a2e588

Please sign in to comment.