Operation: A Simple Command Implementation

Share:

Description

An Operation object implements a Command with an R closure. This may be useful as a base for customized Command derivatives. Otherwise, consider it a skeletal proof-of-concept.

Constructors

Operation(do, undo = NULL): Constructs an Operation that invokes the closure do. If undo is specified, it is the closure invoked for the reverse operation.

OperationQ(do, undo): Constructs an Operation that invokes a function with the body taken from the quoted argument do. The function is enclosed in the calling environment. If undo is specified, it is treated equivalently to do, except it should implement the reverse operation.

Methods

eval(expr): Executes the operation by evaluating the closure.

rev(x): Returns a new Operation that performs the reverse operation, as long as undo was provided.

Author(s)

Michael Lawrence

Examples

1
2
3
4
5
6
7
8
  op <- OperationQ(message("hello world"))
  eval(op)

  x <- 2
  op <- OperationQ(x^2, sqrt(x))
  x <- eval(op)
  rop <- rev(op)
  identical(2, eval(rop))