There was also some discussion of this back in November, where Fluid Dynamics suggested higher-order functions, and tbc++, multimethods(!).
The main source of complexity in get-percentage has to do with selecting what it should do. Also, both percentage computation and rounding could potentially be useful on their own without the other. Those considerations recommend higher order functions. Using higher-order functions can make the code shorter, clearer, and more flexible. First, there's the essential computation: (defn pct [p t] (/ (* p 100.0) t)) Second, various general-purpose ways to round floating-point numbers: (defn round-up [x] (int (Math/ceil x))) (defn round-down [x] (int (Math/floor x))) (defn round [x] (int (Math/round x))) To put them together, get-percentage took a hint about the rounding function, dispatched on the hint, and took upon itself responsibility for detecting a bogus hint. Instead of a hint from a closed set, why not let the caller pass in the rounding function itself? Now get-percentage is down to one line: (defn get-percentage* [f p t] (f (pct p t))) For example (get-percentage* round-up 4 30) 14 (get-percentage* round-down 4 30) 13 Alternatively, the standard function "comp" can produce your canned combinations, so you might not need get-percentage* itself anymore: (def get-percentage-high (comp round-up pct)) For example: (get-percentage-high 4 30) 14 -- You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this group, send email to clojure@googlegroups.com Note that posts from new members are moderated - please be patient with your first post. To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en --- You received this message because you are subscribed to the Google Groups "Clojure" group. To unsubscribe from this group and stop receiving emails from it, send an email to clojure+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.