Hi :

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 ?

* Note: among the param list, `obj.List` is list of peak interval as GRanges object, `intList` is list of integer list as overlap  position index, `val.List` is list of pvalue, `threshold` could be numeric scalar. 

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

    myFunc <- function(obj.List, intList, threshold, ...) {
      # 
      func.1 <- function() {
        keepIdx <- lapply(intList, function(ele_) {
          keepMe <- sapply(val.List, function(x) x<=threshold)
          res <- ele_[keepMe]
        })
        expand.Keep <- Map(unlist, 
                           mapply(extractList, obj.List, keepIdx))
        return(expand.Keep)
      }
      func.2 <- function() {
        dropIdx <- lapply(intList, function(ele_) {
          drop_ <- sapply(val.List, function(x) x > threshold)
          res <- ele_[drop_]
        })
        expand.drop <- 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 ?
    }

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 ? Can anyone give me possible idea to make above code more efficient ? Thanks a lot

I am not completely sure I understand what you are trying to do. Maybe an example with your desired use case and expected output would help. For more advance R programming I would recommend reading [Advanced R][1], by [Hadley Wickham][2] (half-way to finish it myself). My guess is that you want something similar to [functionals][3], or perhaps, a [closure][4], but not completely sure.


### Edit:

You mention that one of your objectives is to obtain *code that it is easy to test and debug*. I also see from your repository that you intend to create an R package. One thing I highly recommend you to do for testing is use [unit testing][5]. There are many options in R, like those provided by the packages [RUnit][6], [testit][7] and [testthat][8]. I now use testthat because I found it easy to implement. You can know more about how to use it in the book (written by Hadley Wickham) [R packages][9], which has a section dedicated to [unit testing][10].


  [1]: http://adv-r.had.co.nz
  [2]: http://hadley.nz
  [3]: http://adv-r.had.co.nz/Functionals.html
  [4]: http://adv-r.had.co.nz/Functional-programming.html#closures
  [5]: https://en.wikipedia.org/wiki/Unit_testing
  [6]: https://cran.r-project.org/package=RUnit
  [7]: https://cran.r-project.org/package=testit
  [8]: https://cran.r-project.org/package=testthat
  [9]: http://r-pkgs.had.co.nz
  [10]: http://r-pkgs.had.co.nz/tests.html