Difference between "Function Operator" and "Function Factory"

Asked

Viewed 300 times

9

To the chapter 11 of the book Advanced R, the author defines Function Operator as:

A Function Operator is a Function that takes one (or more) functions as input and Returns a Function as output.

Translation Google Translate

A function operator is a function that receives one (or more) functions as input and returns a function as output.

My question with this definition is in relation to the concept of Function Factory:

A Factory for making new functions

Translation Google Translate

A factory to do new functions.

But, a Function Factory also receives functions and returns a function as output:

fun <- function(x){
  c(sum(x), mean(x), sd(x))
}

fun is the function returned.

So,

  • what is the difference between Function Operator and Function Factory?

2 answers

5

I’m not giving a formal definition, but you’re right in realizing that the two concepts are related. In this case, operator is a particular case of factory.

In Mathematics, the expression operator is used for something that associates elements in the same set. For example, in R, + is a function that can be called so:

`+`(2, 3)

Being a function that accepts numerical arguments and returns a numerical value, it can also be called a numerical operator.

Similarly, a functional operator does not cease to be a factory, but has as arguments only functions and is something that returns a function.

4


Ailton is right when he says

operator is a particular factory case.

But I’ll illustrate this idea better. There are factories of functions that can be of different types, I will show factories that are of the type operator and factories that are not.

Factories - the general case

The motivation of a function factory is to make a metafunction, ie a function that creates other functions. With it we are able to abstract the process of creating similar functions.

A great example of factories within the are the functions color* package . These functions return functions that map value in color.

verdes <- leaflet::colorNumeric("Greens", c(0, 4))
args(verdes)
# function (x) 
# NULL

The factory allows us to create a color palette very similar to red, for example.

vermelhos <- leaflet::colorNumeric("Reds", c(0,4))
verdes(2)
# [1] "#74C476"
vermelhos(2)
# [1] "#FB6A4A"

Note, however, that the general definition of factory so far allows us to include in it any functions that return a function, without including restrictions or special cases. to colorNumeric, for example, use text and numbers to create its function.

Functional operator - the specific case

It is possible to think of a specific factory case. A type that receives functions to build the returned function. This idea allows us to create abstractions to think about how to modify the received functions as arguments.

A functional operator does not necessarily need to operate only functions, it can receive arguments of other types as well. It is necessary, however, to receive at least one function within your arguments.

In the chapter of Hadley Wickham quoted he lists four types of operators:

  1. Behavioral
  2. Output ()
  3. Input ()
  4. Combination

An example would be to create one’s own pipe.

pipe <- function(...) {
  dots <- list(...)

  function(x) {
    for (i in seq_along(dots)) {
      x <- dots[[i]](x)
    }
    x
  }
}

soma_raizes <- pipe(sqrt, sum)
soma_raizes(c(1, 4, 9))
# [1] 6

Good examples of functional operators in the sane purrr::safely() and purrr::quietly().

Browser other questions tagged

You are not signed in. Login or sign up in order to post.