#.
comments.
source_decoratees
on the file, and then call tmbg()
or
cats(5)
.# emphasize text emph <- function(f, style = '**') { function(...) { if (length(style) == 1) { paste(style, f(...), style) } else { paste(style[1], f(...), style[2]) } } }
#. emph tmbg <- function() { 'tmbg are okay' }
#. emph(c('', '')) cats <- function(n) { paste(rep('cats', n), collapse = ' ') }
The call you make to tmbg
is equivalent to emph(tmbg)
. The
second example, cats(5)
, illustrates passing arguments to the
decorator function.
While the above examples are small hopefully you begin to see how decorators may be used to transform or ensure function output without modifying the function's code by hand.