Skip to content

Commit

Permalink
docs(option): add section about joining options
Browse files Browse the repository at this point in the history
  • Loading branch information
lukadev-0 committed Apr 29, 2024
1 parent 530ab1a commit 1668267
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions docs/docs/optional-values.md
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,43 @@ calling them using `:` syntax. [Learn more](/reference/option#typechecking).

:::

## Joining options

Sometimes you have multiple options and you want to combine them into a single
option.

For example, lets say you had two options, and you wanted to get the sum of
their values. You may achieve this using the `andThen` and `map` methods:

```lua
local optA = Option.Some(5)
local optB = Option.Some(10)

local sum = optA:andThen(function(a)
return optB:map(function(b)
return a + b
end)
end)

print(sum) -- Option::Some(15)
```

However, this introduces multiple levels of nesting, which may be undesirable.

Instead, we can use the `join` method to turn both options into a single option,
which we can map over:

```lua
local optA = Option.Some(5)
local optB = Option.Some(10)

local sum = optA:join(optB):map(function(a, b)
return a + b
end)
```

If either option is `None`, the result will be `None`.

## Learn more

There are more methods available on the `Option` type, this was just a small
Expand Down

0 comments on commit 1668267

Please sign in to comment.