Im busy learning clojure by doing some exercism.io challenges.
I have solved this one :
(defn annual-balance-update
"Returns the annual balance update, taking into account the interest rate."
[balance]
(+ balance (* (abs balance) (bigdec (/ (interest-rate balance) 100)))))
but I wonder if there is a way I can make it more clojure like.
Right now it one big ugly calculation.
p-himik
2
It’s just arithmetic, so there’s not much you can do apart from extracting some bits under their own name inside a let
block.
In general, some similar cases could also be improved by using ->
, but I don’t see this one as a good example of such a case.
oke,
I was thinking of making a function that calculates which amount the balance is updated so I can maybe re-use it
So something like this :
(defn balance-update-amount
"Returns the amount which updates the balance
[balance]
(let interest-rate (bigdec (/ interest-rate balance) / 100)
amount (* (abs balance) interest rate)))
(defn annual-balance-update
"Returns the annual balance update, taking into account the interest rate."
[balance]
(+ balance amount)
but on some way I cannot close the balance-update-amount function
Guess you are looking something like this?
no, I see that I can delete the last /
But still I do not see why I cannot make the function closed with a (
@p-himik
Found it I think
is this good clojure code :
(defn balance-update-amount
"Returns the amount which updates the balance."
[balance]
(let [interest-rate (bigdec (/ (interest-rate balance) 100))]
(* (abs balance) interest-rate)))
(defn annual-balance-update
"Returns the annual balance update, taking into account the interest rate."
[balance]
(+ balance (balance-update-amount balance)))
p-himik
8
Yeah. I would only change the indentation of the last line in the first function.
Thanks a lot then for all the help
system
Closed
10
This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.