A Local, Temporary Place for Your Stuff

We’ll begin our exploration of local naming in Clojure by imagining that our book store runs periodic specials. Every now and then we offer our customers a percentage discount on their book purchases. Unfortunately, our deal does come with some fine print: there’s a minimum charge for each order that overrides the discount.

Armed with our hard-won knowledge of functions and if, it’s not difficult to turn this discount policy into Clojure code:

 (​defn​ compute-discount-amount [amount discount-percent min-charge]
  (​if​ (> (* amount (- 1.0 discount-percent)) min-charge)
  (* amount (- 1.0 discount-percent))
  min-charge))

As a bit of everyday software engineering, compute-discount-amount ...

Get Getting Clojure now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.