Home > Software design >  chain functions in second argument
chain functions in second argument

Time:09-28

Is there a simple way to chain functions in a way that a function is partially applied so that one argument remains and is then applied to one argument (not the first one) of another function before calling. (In my example: is there a simpler way of defining the function three given the functions one and two)

I tried to find solutions using magrittr and purrr but didn't come up with something simpler than just defining it as shown below.

one <- function(x,y){
  paste(x,y)
}

two <- function(x, y){
  x * y
}

three <- function(x,y){
  one(x, two(y, 3))
}

three(1,2)

# [1] "1 6"

CodePudding user response:

The new pipe operator introduced in R 4.1 and the new placeholder introduced in R 4.2 are meant for this sort of problem.

one <- function(x,y){
  paste(x,y)
}

two <- function(x, y){
  x * y
}

three <- function(x,y){
  y |>
    two(3) |>
    one(x, y = _)
}

three(1,2)
#> [1] "1 6"

Created on 2022-09-28 with reprex v2.0.2

CodePudding user response:

The only way I see this to be possibly working would be to change the two function and add a third parameter to the three function:

one <- function(x){
  paste(x[1], x[2])
}

two <- function(x){
  c(x[-c(2:3)], x[2] * x[3])
}

three <- . %>% 
  two %>% 
  one

three(c(1, 2, 3))
#[1] "1 6"

CodePudding user response:

I'm not sure if I understand your goal right, according to your comment you want to avoid three(). So, what about this:

one <- function(x, y) paste(x, y)
two <- function(x, y) x * y

#R4.2 
two(2, 3) |> one(x=1)
# [1] "1 6"

#R4.1 
two(2, 3) |> {\(.) one(., x=1)}()
# [1] "1 6"
  • Related