An open API service indexing awesome lists of open source software.

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.

Awesome Lists containing this project

README

          

---
title: 'Either: the ''Either'' type in R'
output: github_document
---

[![R-CMD-check](https://github.com/stla/Either/workflows/R-CMD-check/badge.svg)](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))
```