官术网_书友最值得收藏!

Understanding closure

Functions are the first-class citizens of R. In other words, you can pass a function as the input to an other function. In previous recipes, we illustrated how to create a named function. However, we can also create a function without a name, known as closure (that is, an anonymous function). In this recipe, we will show you how to use closure in a standard function.

Getting ready

Ensure that you completed the previous recipes by installing R on your operating system.

How to do it...

Perform the following steps to create a closure in function:

  1. First, let's review how a named function works:
    >addnum<- function(a,b){
    + a + b
    + }
    >addnum(2,3)
    [1] 5
    
  2. Now, let's perform the same task to sum up two variables with closure:
    > (function(a,b){
    + a + b
    + })(2,3)
    [1] 5
    
  3. We can also invoke a closure function within another function:
    >maxval<- function(a,b){
    + (function(a,b){
    + return(max(a,b))
    + }
    + )(a, b)
    + }
    >maxval(c(1,10,5),c(2,11))
    [1] 11
    
  4. In a similar manner to the apply family function, you can use the vectorization calculation:
    > x <- c(1,10,100)
    > y <- c(2,4,6)
    > z <- c(30,60,90)
    > a <- list(x,y,z)
    >lapply(a, function(e){e[1] * 10})
    [[1]]
    [1] 10
    [[2]]
    [1] 20
    [[3]]
    [1] 300
    
  5. Finally, we can add functions into a list and apply the function to a given vector:
    > x <- c(1,10,100)
    >func<- list(min1 = function(e){min(e)}, 
     max1 = function(e){max(e)} )
    >func$min1(x)
    [1] 1
    >lapply(func, function(f){f(x)})
    $min1
    [1] 1
    $max1
    [1] 100
    

How it works...

In R, you do not have to create a function with the actual name. Instead, you can use closure to integrate methods within objects. Thus, you can create a smaller and simpler function within another object to accomplish complicated tasks.

In our first example, we illustrated how a normally-named function is created. We can simply invoke the function by passing values into the function. On the other hand, we demonstrate how closure works in our second example. In this case, we do not need to assign a name to the function, but we can still pass the value to the anonymous function and obtain the return value.

Next, we demonstrate how to add a closure within a maxval named function. This function simply returns the maximum value of two passed parameters. However, it is possible to use closure within any other function. Moreover, we can use closure as an argument in higher order functions, such as lapply and sapply. Here, we can input an anonymous function as a function argument to return the multiplication of 10 and the first value of any vector within a given list.

Furthermore, we can specify a single function, or we can store functions in a list. Therefore, when we want to apply multiple functions to a given vector, we can pass the function calls as an argument list to the lapply function.

There's more...

Besides using closure within a lapply function, we can also pass a closure to other functions of the apply function family. Here, we demonstrate how we can pass the same closure to the sapply function:

> x <- c(1,10,100)
> y <- c(2,4,6)
> z <- c(30,60,90)
> a <- list(x,y,z)
>sapply(a, function(e){e[1] * 10})
[1] 10 20 300
主站蜘蛛池模板: 阿尔山市| 祁东县| 武冈市| 玉门市| 石景山区| 鄯善县| 峨眉山市| 巫溪县| 辽阳市| 定西市| 元谋县| 武隆县| 商水县| 子长县| 武城县| 城步| 仙游县| 青海省| 宜都市| 丰原市| 浙江省| 明光市| 临武县| 浪卡子县| 农安县| 邻水| 伊春市| 安义县| 牟定县| 都江堰市| 吴旗县| 千阳县| 卓资县| 清丰县| 武胜县| 上饶市| 汉寿县| 绍兴市| 荆门市| 莆田市| 股票|