What is and what is an anonymous function in R?

Asked

Viewed 704 times

15

  • What is an anonymous function? And why can it be called função lambda?

  • What is the usefulness of an anonymous function in language R?

  • Where it can be applied (for example, it can be applied in Function Operators)?

  • 3

    I think a lot of the answers are in this link.

  • 1

    Here is the tip for those who want to prepare a content about this in Portuguese : )

1 answer

9


What is anonymous function in R?

  • One anonymous function Anonymous Function (also known as expression lambda) is a definition of a function that is not linked to an identifier. That is, it is a function created and used, but never assigned to a variable. That is, R does not have a special syntax to create a named function: when you create a function, it uses the regular assignment operator to assign a name. If you choose not to name the function, you will receive a anonymous function.

And why it can be called lambda function?

  • Lambda expressions are unnamed functions given as constant values. They may appear anywhere that any other constant, but are typically written as a parameter to some other function.

  • The word "lambda" or "lambda expressions" usually if refers to anonymous functions. So, in this sense, a lambda is a type function (anonymous function), but not every function is a lambda (ie, appointed functions are not usually called Mixed).

  • Therefore, anonymous functions in fine languages are treated as lambda.

What is the usefulness of an anonymous function in the R language?

Simple example of anonymous function in r:

# Sintaxe de função anônima
(function(x) x * 10)(10)

# equivalente (normal)
fun<-function(x) x * 10

fun(10)
  • As the above syntax indicates, you are doing everything at once: a statement and call in a line statement. Therefore, despite of not being transparent when reading it, it is self-contained and you use it because it does not want to define any other function in some other current script (or in an external script): you are dealing with a simple calculation when necessary, and you probably do not use it anywhere else in your code, so it’s not worth it remind him.

Browser other questions tagged

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