Skip to contents

convoke unifies functions along a single specification using statements on function argument transformations that would be needed. The result is a single function whose interface can be changed simply by changing the respective argument in the generated function.

Usage

convoke(argslist, ...)

Arguments

argslist

[list] The desired unifying arguments. A list call.

...

[function] All the various specifications for function argument transformations.

Value

Function with additional class convoke with arguments being

convoke_func(specified_args, interface, ..., evaluate = TRUE)

The evaluate argument is useful for debugging purposes. Extra arguments can be passed in the format interface.arg.

Unifying Functions

Specifying the unifying interface

The unifying interface needs to be specified as arglist for the new function

list(arg1 = default1, arg2 = default2, etc.)

Specifying function argument transformations

Transformations follows the function specification format

func1(func1arg1 = transformed_arg1, func1arg2 = transformed_arg2)

Essentially, pass the function as if it is to take arguments from the unified function.

Composing functions

It is also possible to progressively add functions to a convoke function simply by adding to the convoke function the new specifications:

convoke_func + ~func_spec

See also

Other function assemblers: conflate()

Examples

# unifying functions with swapped arguments
foo <- function(a1, b1) {
  a1 / b1
}
bar <- function(b2, a2) {
  a2 / b2
}
convoked <- convoke(
  list(a, b),
  foo(a1 = a, b1 = b),
  bar(b2 = b, a2 = a)
)
c(convoked(9, 5), bar(5, 9))
#> [1] 1.8 1.8
c(convoked(9, 5, interface = "bar"), foo(9, 5))
#> [1] 1.8 1.8

# supplying optional arguments
foo <- function(a1, b1, round = FALSE) {
  if (round) round(a1 / b1) else a1 / b1
}
bar <- function(b2, a2) {
  a2 / b2
}
convoked <- convoke(
  list(a, b),
  foo(a1 = a, b1 = b),
  bar(b2 = b, a2 = a)
)
c(convoked(9, 5, foo.round = FALSE), bar(5, 9))
#> [1] 1.8 1.8
c(convoked(9, 5, foo.round = TRUE), round(bar(5, 9)))
#> [1] 2 2

# TODO fix and show this for three sequences
# adding further functions later
foo <- function(a1, b1) {
  a1 / b1
}
bar <- function(b2, a2) {
  a2 / b2
}
(convoked <- convoke(list(a, b), foo(a1 = a, b1 = b)))
#> convoke function
#>   interfaces: foo()
#>   args: a, b, interface = foo, interface.args
(convoked <- convoked + ~ bar(b2 = b, a2 = a))
#> convoke function
#>   interfaces: foo(), bar()
#>   args: a, b, interface = foo, interface.args
c(convoked(9, 5, interface = "bar"), foo(5, 9))
#> [1] 1.8000000 0.5555556