title: do.call based on output of previous sapply(seq_along()) iteration
body: <p>I have a named list of some algorithm, which may look like this</p>
  
  <pre><code>&gt; algorithm
  $rBinarize
  $rBinarize$x
  [1] 40
  </code></pre>
  
  <p>and may contain an arbitrary number of additional algorithms. Each algorithm carries out an operation on a spatial object (<code>spObj</code>, of class raster) and returns the modified raster. I would then like to use <code>do.call</code> to (s)apply this (and all the other) algorithm(s) within a parent function to the original input. However, what I additionally want to achieve is a sequential application of the defined algorithms, i.e. on the output of the previous algorithm. I came up with the following code, but I am curious about additional suggestions to improve the performance, given it is possible.</p>
  
  <pre><code>if(sequential){
    for(k in seq_along(algorithm)){
      if(length(algorithm[[k]])==0){
        args &lt;- c(spObj = spObj)
      } else{
        args &lt;- c(algorithm[[k]], spObj = spObj)
      }
      spObj &lt;- do.call(what = names(algorithm)[k], args = args)
    }
  } else{
    algorithm2 &lt;- lapply(algorithm, function(x) x &lt;- c(x, spObj = spObj))
    modified &lt;- sapply(seq_along(algorithm2), function(j) do.call(what = names(algorithm2)[[j]], args = algorithm2[[j]]))
  }
  </code></pre>
  
  <p>Wouldn't it possible to use some sort of <code>apply()</code> construction instead of the <code>for</code>-loop? I am not sure if I simply don't understand the logic of <code>apply</code>/<code>do.call</code> sufficiently or if this is actually not possible in R.</p>
  
  <hr>
  
  <p>I modified to for-loop to make it comparable with Davids suggestion and ran a microbenchmark on it:</p>
  
  <pre><code>microbenchmark(a = for(k in seq_along(alg)){
    if(length(alg[[k]][-1])==0){
      args &lt;- c(spObj = spObj)
    } else{
      args &lt;- c(alg[[k]][-1], spObj = spObj)
    }
    spObj &lt;- do.call(what = alg[[k]]$algorithm, args = args)
  },
  b = Reduce(f = function(x, y) do.call(what = y$algorithm, args = c(list(x), y[-1])),
         x = alg,
         init = spObj))
  </code></pre>
  
  <p>Which resulted in</p>
  
  <pre><code>Unit: milliseconds
   expr      min       lq     mean   median       uq      max neval
      a 33.36777 35.22067 39.60699 36.79661 40.75072 152.0171   100
      b 33.35236 35.39173 40.32860 37.51993 40.25102 154.0441   100
  </code></pre>
  
  <p>Is this one of these examples where a for-loop is in fact not slower than any other solution?</p>