# Either
[](https://github.com/jechol/either/actions/workflows/test.yml)
[](https://coveralls.io/github/jechol/either?branch=main)
[](https://hex.pm/packages/either)
[](https://github.com/jechol/either/blob/main/LICENSE)
`Either` is helpers to handle `:ok`, `{:ok, value}`, `:error` and `{:error, reason}` in consistent way.
This library is copied from `Reather.Either` from [SeokminHong/reather-lite](https://github.com/SeokminHong/reather-lite).
## Installation
```elixir
def deps do
[
{:either, "~> 0.1.1"}
]
end
```
## Usage
### `Either.new`
Convert a value into `ok` or `error` tuple. The result is a tuple having
an `:ok` or `:error` atom for the first element, and a value for the second
element.
### `Either.error`
Make an error tuple from a value.
### `Either.map`
`map` a function to an either tuple.
The given function will be applied lazily
when the either is an `ok` tuple.
### `Either.traverse`
Transform a list of eithers to an either of a list.
If any of the eithers is `error`, the result is `error`.
```elixir
iex> [{:ok, 1}, {:ok, 2}] |> Either.traverse()
{:ok, [1, 2]}
iex> [{:ok, 1}, {:error, "error!"}, {:ok, 2}]
...> |> Either.traverse()
{:error, "error!"}
```
## LICENSE
[MIT](./LICENSE)