Question: For example [1,2,3] and i want to add 5 to the 2nd element so that I get [1,7,3]. Would this actually be possible?

Answer: 
An alternative to rolling your own function composition is to use the [gestalt](https://github.com/egnha/gestalt) package, which provides composition both as a higher-order function, `compose()`, and as an infix operator, `%>>>%`. (For these to read the same, functions are composed from *left to right*.)

Basic usage is straightforward:

```
library(gestalt)

f <- compose(tan, cos, sin)  # apply tan, then cos, then sin
f(pi/4)
#> [1] 0.514395258524

g <- tan %>>>% cos %>>>% sin
g(pi/4)
#> [1] 0.514395258524

```

But you get a lot of additional flexibility:

```
## You can annotate composite functions and apply list methods
f <- first: tan %>>>% cos %>>>% sin
f[[1]](pi/4)
#> [1] 1
f$first(pi/4)
#> [1] 1

## magrittr %>% semantics, such as implicity currying, is supported
scramble <- sample %>>>% paste(collapse = "")
set.seed(1); scramble(letters, 5)
#> [1] "gjnue"

## Compositions are list-like; you can inspect them using higher-order functions
stepwise <- lapply(`%>>>%`, print) %>>>% compose
stepwise(f)(pi/4)
#> [1] 1
#> [1] 0.540302305868
#> [1] 0.514395258524

## formals are preserved
identical(formals(scramble), formals(sample))
#> [1] TRUE

```

One thing you should keep in mind about function calls in R is that their cost is not negligible. Unlike doing literal function composition, `compose()` (and `%>>>%`) flatten compositions when called. In particular, the following invocations produce the same function, *operationally*:

```
fs <- list(tan, cos, sin)

## compose(tan, cos, sin)
Reduce(compose, fs)
Reduce(`%>>>%`, fs)
compose(fs)
compose(!!!fs)  # tidyverse unquote-splicing

```
Here is a solution that builds the function from calls, it gives a readable output similar to Benjamin's :

```
compose_explicit <- function(...){
  funs <- as.character(match.call()[-1])
  body <- Reduce(function(x,y) call(y,x), rev(funs), init = quote(x))
  eval.parent(call("function",as.pairlist(alist(x=)),body))
}
compose_explicit(sin, cos, tan)
# function (x) 
# sin(cos(tan(x)))

compose_explicit(sin, cos, tan)(pi/4)
# [1] 0.5143953

```

It seems quite robust:

```
compose_explicit()
# function (x) 
# x

compose_explicit(sin)
# function (x) 
# sin(x)

```

---

And unrelated but useful, here is the code of `purrr:compose` :

```
#' Compose multiple functions
#'
#' @param ... n functions to apply in order from right to left.
#' @return A function
#' @export
#' @examples
#' not_null <- compose(`!`, is.null)
#' not_null(4)
#' not_null(NULL)
#'
#' add1 <- function(x) x + 1
#' compose(add1, add1)(8)
compose <- function(...) {
  fs <- lapply(list(...), match.fun)
  n <- length(fs)

  last <- fs[[n]]
  rest <- fs[-n]

  function(...) {
    out <- last(...)
    for (f in rev(rest)) {
      out <- f(out)
    }
    out
  }
}

```