2016-11-16 2 views
0

일부 기능이 매우 유사한 방식으로 작동하고 동일한 구조를 공유하는 여러 기능을 구현했습니다. 그러나 래퍼 함수에서 코드를보다 효율적으로 재사용하여 테스트하고 디버그하기 쉽도록 함수 본문을 더 작게 만듭니다. 가능한 한 작게 래퍼 함수를 ​​구성하는 더 좋은 방법을 찾으려고 노력 중입니다. 래퍼 함수에서 코드를 여러 번 효율적으로 재사용하는 방법은 무엇입니까? 여러 번 동일한 코드 구조를 효율적으로 사용하기위한 전략은 무엇입니까? 아무도이 문제를 극복하기 위해 가능한 아이디어를 줄 수 있습니까? 어떤 생각?래퍼 함수에서 반복적으로 코드 블록을 효율적으로 재사용하고 최적화하는 방법은 무엇입니까?

  • 참고 : PARAM 목록 중 obj.List이 data.frame의 목록이 될 수 intList 위치의 인덱스로 정수리스트의 목록이 될 수 val.List 숫자 벡터의 목록이 될 수 threshold 숫자 스칼라 수 있습니다. 내가 func.1 호출 할 수 있지만, 난 그냥이 래퍼의 작은 함수 본문을 만들기 위해 노력하고

    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 ? 
    } 
    

    :

이은 두 개의 작은 하위 기능 '코드가 같은 패턴을 공유하는 래퍼 함수입니다 , func.2를 래퍼의 인수로 사용하여 다른 하위 함수를 트리거합니다. 어떻게해야합니까? 위의 래퍼 함수의 코드 구조를 효율적으로 최적화하는 방법은 무엇입니까?

+1

더 많은 함수 형식을 사용하십시오. 그래서'function.1()'은'function.1 (intList)'와 같을 수 있습니다. 이렇게하면 함수 밖에서 선언 할 수 있고 코드 구성 요소를 훨씬 더 읽기 쉽고 안전하게 만들 수 있습니다. –

답변

0

앞서 말했듯이 더 많은 공식을 사용하십시오. 개별 구성 요소의 작동 방식을 명확하게하고 코드를 더 쉽게 분리 할 수 ​​있습니다.

작동하는 예제가 제공되지 않았기 때문에 테스트 할 수 없었지만 여기서는 어떻게 구별 할 수 있는지 보여줍니다.

# This function mirrors most of what func.1 and func.2 do 
unlistAndSelect <- function(intList, val.list, threshold, selection.method) { 

    # Making use of declaring other variables in lapply 
    keepIdx <- lapply(intList, selectionFunction, 
         val.list = val.list, threshold = threshold, 
         selection.fun = selection.method) 

    # unlist 
    expand.Keep <- Map(unlist, mapply(extractList, obj.List, keepIdx)) 

    # Return result 
    return(expand.Keep) 
} 

# Mirrors most of dropIdx and keepIdx passing a selection method 
selctionFunction <- function(ele_, val.list, selection.fun) { 
    keepMe <- sapply(val.List, selection.fun) 
    res <- ele_[keepMe] 
} 

# Your two selection methods 
selectGEQ <- function(x) x <= threshold 
selectLT <- function(x) x > threshold 

# unlistAndSelect(... , selection.method = selectGEQ) will have similar results to function 1 
# unlistAndSelect(... , selection.method = selectLT) will have similar results to function 1 

myFunc <- function(obj.List, intList, threshold, ...) { 


    # 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 ? 
} 

나는 또한 당신이 선택 방법을 다시 작성할 수 있습니다 생각하는 점에 유의해야한다,하지만 당신은 당신이 그들을 전달하는 데이터 구조에주의 할 필요가 나는이 인스턴스에서하지 않았습니다.

+0

당신은 단지'<'또는'<='를 넘기고 래퍼 함수 selectGEQ와 selectLT를 쓰는 것을 피할 수있을 것 같아요. –

+0

예를 들어 함수 형식'unlistAndSelect (..., selection.method = \'< \')' –

+0

정확히 무엇을하고 있는지 재현 할 수있는 예제가 없으면 어떻게 보이지 않습니까? –