Any way to efficiently reuse and optimize repeated code block in wrapper function?
<p>I have implemented several functions where some function behaved very similar way and share some identical structure. However, I intend to reuse the code more efficiently in my wrapper functions to make function body smaller for the sake of easy to test and debug. I am trying to find better way to construct my wrapper function as small as possible. How can I efficiently reuse the code multiple times in wrapper function easily ? What's the strategy to efficiently use same code structure in many times? Can anyone give me possible idea to overcome this issue ? Any idea ?</p>

<ul>
<li>Note: among the param list, <code>obj.List</code> could be list of data.frame, <code>intList</code> could be list of integer list as position index, <code>val.List</code> could be list of numeric vector, <code>threshold</code> could be numeric scalar. </li>
</ul>

<p>This is the wrapper function where two small sub functions' code is shared same pattern : </p>

<pre><code>myFunc &lt;- function(obj.List, intList, threshold, ...) {
  # 
  func.1 &lt;- function() {
    keepIdx &lt;- lapply(intList, function(ele_) {
      keepMe &lt;- sapply(val.List, function(x) x&lt;=threshold)
      res &lt;- ele_[keepMe]
    })
    expand.Keep &lt;- Map(unlist, 
                       mapply(extractList, obj.List, keepIdx))
    return(expand.Keep)
  }
  func.2 &lt;- function() {
    dropIdx &lt;- lapply(intList, function(ele_) {
      drop_ &lt;- sapply(val.List, function(x) x &gt; threshold)
      res &lt;- ele_[drop_]
    })
    expand.drop &lt;- Map(unlist,
                       mapply(extractList, obj.List, keepIdx))
    return(expand.drop)
  }
  # then use the output of func.1 and func.2 as an argument for another function 
  # in this wrapper
  # how can I make this wrapper more efficient ? How to resue the code ?
}
</code></pre>

<p>I am just trying to make function body of this wrapper smaller, while I can call func.1, func.2 as an argument in the wrapper to trigger another sub function. How can I make this happen ? Any way efficiently optimize the code structure of above wrapper function ?</p>