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'll keep this post open because there is a healthy discussion going on. I enjoy discussing programming issues also, however, this is really more suited to stackexchange as it is tangentially related to bioinformatics.
Agreed 100% (stackexchange)!