https://github.com/stla/either
The 'Either' type in R.
https://github.com/stla/either
either r r6
Last synced: about 1 year ago
JSON representation
The 'Either' type in R.
- Host: GitHub
- URL: https://github.com/stla/either
- Owner: stla
- License: gpl-3.0
- Created: 2022-04-05T11:05:53.000Z (about 4 years ago)
- Default Branch: main
- Last Pushed: 2022-04-05T14:53:45.000Z (about 4 years ago)
- Last Synced: 2025-02-06T16:05:49.389Z (over 1 year ago)
- Topics: either, r, r6
- Language: R
- Homepage:
- Size: 22.5 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.Rmd
- License: LICENSE
Awesome Lists containing this project
README
---
title: 'Either: the ''Either'' type in R'
output: github_document
---
[](https://github.com/stla/Either/actions)
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE, collapse = TRUE)
```
```{r package}
library(Either)
```
```{r}
## ------------------------------------------------
## Method `Either$new`
## ------------------------------------------------
Either$new("left", 123)
## ------------------------------------------------
## Method `Either$isLeft`
## ------------------------------------------------
Either$new("right", 999)$isLeft()
## ------------------------------------------------
## Method `Either$isRight`
## ------------------------------------------------
Either$new("right", 999)$isRight()
## ------------------------------------------------
## Method `Either$getLeft`
## ------------------------------------------------
Either$new("right", 999)$getLeft("abc")
## ------------------------------------------------
## Method `Either$getRight`
## ------------------------------------------------
Either$new("right", 999)$getRight("abc")
## ------------------------------------------------
## Method `Either$toMaybe`
## ------------------------------------------------
Either$new("right", 999)$toMaybe()
Either$new("left", 999)$toMaybe()
## ------------------------------------------------
## Method `Either$mapLeft`
## ------------------------------------------------
Either$new("left", 999)$mapLeft(is.numeric)
Either$new("right", 999)$mapLeft(is.numeric)
## ------------------------------------------------
## Method `Either$mapRight`
## ------------------------------------------------
Either$new("left", 999)$mapRight(is.numeric)
Either$new("right", 999)$mapRight(is.numeric)
## ------------------------------------------------
## Method `Either$either`
## ------------------------------------------------
Either$new("left", 999)$either(is.numeric, is.character)
Either$new("right", 999)$either(is.numeric, is.character)
```
```{r}
## ------------------------------------------------
## Functions `Left` and `Right` (shortcuts)
## ------------------------------------------------
Left("abc")
Right("abc")
```
```{r, warning=FALSE}
## ------------------------------------------------
## Function `eitherFromMaybe`
## ------------------------------------------------
library(maybe)
eitherFromMaybe("abc", nothing())
eitherFromMaybe("abc", just(123))
```