얌마들아 그런건 그렇게 하면 안돼지


대략 아래처럼 로직이랑 데이터를 완벽히 분리해야 한다. 이를테면 아래있는 get-tax-rates는 데이터베이스 쿼리로 대치할 수 잇겠지. 그래야 나중에 국세청에서 세율을 바꿔도 코드는 안바꾸고 데이터만 바꿔서 쓸수 있는거지.


뭐 이런거 알려줘도 갤에 이해하는 생키들 몇 있겠냐마는, 밥먹고 할일없어서 심심풀이 땅콩으로 한번 싸줌 ㅎㅎ


(def tax-rates [{:min 0         :max 10000000   :rate 0.08}

                {:min 10000000  :max 40000000   :rate 0.17}

                {:min 40000000  :max 80000000   :rate 0.26}

                {:min 80000000  :max nil        :rate 0.35}])


(defn get-tax-rates []

  (sort #(> (:min %1 ) (:min %2)) tax-rates))


(defn compute-income-tax [amount]

  (loop [[{:keys [min max rate]} & more-rates] (get-tax-rates)

         amount amount

         result 0]

    (cond (nil? min) result


          (< amount min)

          (recur more-rates amount result)

          

          :else (let [amount-diff (- amount min)]

                  (recur more-rates

                         (- amount amount-diff)

                         (+ result (* amount-diff rate)))))))