Apply decorators to a function

Description

%@% reads left to right: in a %@% b %@% f, a is the outermost decorator and f the innermost function. A plain function(f) that returns a function also works as a decorator on the left-hand side.

Usage

`%@%`(lhs, rhs, name = NULL)

Arguments

lhs A decorator.
rhs A function, or another decorator to compose with.
name The name to give the decorated function, for decorator_context()$name. Normally the name is read from rhs when it is a symbol, so you only need this for an anonymous function. It is also what S7’s :=, re-exported by adorn, supplies: add_one := dec %@% function(x) x + 1 names the function add_one. It has no effect when rhs is a decorator.

Value

The decorated function, or a composed decorator if rhs is itself a decorator.

Examples

library("adorn")

loud <- decorator(function(f, ...) toupper(f(...))) %@% function() "hi"
loud()
#> [1] "HI"

# `:=` is re-exported from S7 and supplies `name`
add_one := decorator(function(f, ...) f(...)) %@% function(x) x + 1
add_one(1)
#> [1] 2