---
title: "grapes"
author: "William Michael Landau"
date: "`r Sys.Date()`"
output: rmarkdown::html_vignette
vignette: >
%\VignetteIndexEntry{grapes}
%\VignetteEngine{knitr::rmarkdown}
%\VignetteEncoding{UTF-8}
---
In some computing subcultures, the term "grapes" refers to the percent sign, which R uses to name binary operators such as `%*%`. Here, `grapes` is an R package that turns arbitrary functions into binary operators. As with the [magrittr pipe](https://CRAN.R-project.org/package=magrittr/vignettes/magrittr.html), this allows you to avoid cumbersome parentheses in your code.
```{r}
library(grapes)
grow(rbind, c, from = "base") # Use `from` to specify a package or environment to search.
bunch()
nrow(sleep)
longer = sleep %rbind% sleep %rbind% sleep
nrow(longer)
1 %rbind% 2 %c% 3
```
In your workspace, you can define your own functions for your operators. That way, there is no need to set the `from` argument to tell `grow()` where to look.
```{r}
myop <- function(x, y){
1/x + 1/y
}
grow(myop)
2 %myop% 3
```
Use `bunch()` and `functions()` to list the available operators and non-operator functions.
```{r}
bunch()
functions()
```
**Operators are left-associative!**
```{r}
2 %myop% 3 %myop% 4
(2 %myop% 3) %myop% 4
2 %myop% (3 %myop% 4)
```
You can even turn all available functions from a package or environment into operators. But be warned: there is no guarantee that these operators will actually work.
```{r}
functions("knitr")
bunch("knitr")
grow(from = "knitr")
bunch()
`%purl%`
```
Advanced users can supply environments to the `from` and `to` arguments. This affords more control over where the functions come from and where the operators get assigned.
```{r}
to_env = new.env()
from_env = new.env()
from_env$nextop = function(a, b, extra = 3){
sqrt(a^2 + b^2) + extra
}
assign_operator = function(){
grow(nextop, from = from_env, to = to_env)
}
assign_operator()
# 1 %nextop% 2 # throws an error since %nextop% is not defined in your workspace
eval(parse(text = "1 %nextop% 2"), envir = to_env)
bunch(from_env)
functions(from_env)
bunch(to_env)
functions(to_env)
```