2016-06-21 11 views
3

쪼개짐은 코드 중복을 최소화하는 데 정말로 유용한 조합입니다. 내가 Abundant, Perfect, Deficient numbers을 분류한다고 가정 : "런타임 계산 된 값으로, 쪼개짐을 적용 할 수 없습니다"때문에 런타임 계산 값을 제거 하시겠습니까?

USING: arrays assocs combinators formatting io kernel math 
math.order math.primes.factors math.ranges sequences ; 
IN: adp 

CONSTANT: ADP { "deficient" "perfect" "abundant" } 

: proper-divisors (n -- seq) 
    dup zero? [ drop { } ] [ divisors dup length 1 - head ] if ; 

: adp-classify (n -- a/d/p) 
    dup proper-divisors sum <=> 
    { +lt+ +eq+ +gt+ } ADP zip 
    H{ } assoc-clone-like at ; 

: range>adp-classes (n -- seq) 
    1 swap 1 <range> [ adp-classify ] map 
    ADP dup 
    [ 
    [ 
     [ = ]  curry 
     [ count ] curry 
    ] map 
    cleave 3array 
    ] dip 
    swap zip H{ } assoc-clone-like ; 

: print-adp-stats (seq --) 
    ADP [ 
    [ dup [ swap at ] dip swap "%s: %s" sprintf ] curry 
    ] map cleave 
    [ print ] [email protected] ; 

range>adp-classes

가 컴파일되지 않습니다. 나는 클리브을 사용할 수없는 경우

, 그때가 기본적으로 수행합니다

불구하고 이상, 정말 추한 키 - 문자열의 배열이 이상이라면 긴 얻을 것
[ [ [ "deficient" = ] count ] 
    [ [ "abundant" = ] count ] 
    [ [ "perfect" = ] count ] 
    tri 
] dip 

. 또한 키 배열이 런타임에 생성되면 쪼개짐없이이 작업을 수행하는 것은 중요합니다. print-adp-stats에 대한 유사

는 :

{ 
    [ "deficient" dup [ swap at ] dip swap "%s: %s" sprintf ] 
    [ "perfect" dup [ swap at ] dip swap "%s: %s" sprintf ] 
    [ "abundant" dup [ swap at ] dip swap "%s: %s" sprintf ] 
} 

총 : cleave없이 나는이 문자 그대로의 내 소스에 주위에 거짓말을해야합니다.

cleave을 런타임 계산 값으로 바꾸려면 연결자가 있습니까? 런타임에 계산을 허용하면서 추악한 복제를 다른 방법으로 최소화 할 수 있습니까?

답변

2

cleave 여기에 맞지 않을 가능성이 있습니다. Factor가 이라고 말하면 실행 시간 계산 값에 SometHING을 적용 할 수없는 경우가 가장 많습니다. 여기서 cleave을 히스토그램으로 바꾸고 싶다고 생각합니다.

IN: scratchpad 100 [ { "abundant" "deficient" "perfect" } random ] replicate 
    histogram 

--- Data stack: 
H{ { "deficient" 33 } { "perfect" 30 } { "abundant" 37 } }