https://github.com/jechol/either
https://github.com/jechol/either
Last synced: about 1 year ago
JSON representation
- Host: GitHub
- URL: https://github.com/jechol/either
- Owner: jechol
- License: mit
- Created: 2022-10-19T01:40:34.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2023-12-15T05:42:57.000Z (over 2 years ago)
- Last Synced: 2025-03-23T23:39:15.978Z (about 1 year ago)
- Language: Elixir
- Size: 81.1 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# 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)