title: What is the best way to pass a list of functions as argument?
body: <p>I am looking for advice on the best way to code passing a list of functions as argument.</p>
  
  <p><strong>What I want to do:</strong></p>
  
  <p>I would like to pass as an argument a list a functions to apply them to a specific input. And give output name based on those.</p>
  
  <p><strong>A "reproducible" example</strong></p>
  
  <pre><code>input = 1:5
  </code></pre>
  
  <p>Functions to pass are <code>mean</code>, <code>min</code></p>
  
  <p>Expected call: </p>
  
  <pre><code>foo(input, something_i_ask_help_for)
  </code></pre>
  
  <p>Expected output:</p>
  
  <pre><code>list(mean = 3, min = 1)
  </code></pre>
  
  <p>If it's not perfectly clear, please see my two solutions to have an illustration.</p>
  
  <p><strong>Solution 1:</strong> Passing functions as arguments</p>
  
  <pre><code>foo &lt;- function(input, funs){
    # Initialize output
    output = list()
  
    # Compute output
    for (fun_name in names(funs)){
      # For each function I calculate it and store it in output
      output[fun_name] = funs[[fun_name]](input)
    }
  
    return(output)
  }
  
  foo(1:5, list(mean=mean, min=min))
  </code></pre>
  
  <p>What I don't like with this method is that we can't call it by doing: <code>foo(1:5, list(mean, min))</code>.</p>
  
  <p><strong>Solution 2:</strong> passing functions names as argument and using get</p>
  
  <pre><code>foo2 &lt;- function(input, funs){
    # Initialize output
    output = list()
  
    # Compute output
    for (fun in funs){
      # For each function I calculate it and store it in output
      output[fun] = get(fun)(input)
    }
  
    return(output)
  }
  foo2(1:5, c("mean", "min"))
  </code></pre>
  
  <p>What i don't like with this method is that we are not really passing the function-object as argument.</p>
  
  <p><strong>My question:</strong></p>
  
  <p>Both ways works, but I not quite sure which one to choose.</p>
  
  <p>Could you help me by:</p>
  
  <ul>
  <li>Telling me which one is the best?</li>
  <li>What are the advantages and defaults of each methods?</li>
  <li>Is there a another (better) method</li>
  </ul>
  
  <p>If you need any more information, don't hesitate to ask.</p>
  
  <p>Thanks!</p>