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