Also, you can use multi-methods:

(defmulti get-percentage (fn [x mode] mode))

(defmethod get-percentage :default
  [x _]
  (get-percentage x :high))

(defmethod get-percentage :high
  [x _]
  ...)

(defmethod get-percentage :low
  [x _]
  ...)

This has the advantage to not only having a clear default, but also being
open to extension. You or other developers can easily add more overrides
later via defmethod. Other methods using cond have the disadvantage of
being closed to modification.

Timothy

On Tue, Nov 25, 2014 at 8:09 AM, Fluid Dynamics <a2093...@trbvm.com> wrote:

> On Tuesday, November 25, 2014 8:32:14 AM UTC-5, Gary Verhaegen wrote:
>>
>> Add an arity 3 version which takes a symbol and dispatches on it. You can
>> choose the position that you like best; to keep in line with your current
>> scheme:
>>
>> (defn get-percentage
>>   ([place total-count] (get-percentage :normal place total-count))
>>   ([mode place total-count]
>>     (condp = mode
>>       :low (... floor ...)
>>       :normal (... round ...)
>>       :high (... ceil ...))))
>>
>
> Or, supply three functions that wrap the interop functions:
>
> (defn round
>   "Rounds x to nearest."
>   ^long [x]
>   (Math/round (double x)))
>
> (defn floor
>   "Rounds x down."
>   ^long [x]
>   (long (Math/floor x)))
>
> (defn ceil
>   "Rounds x up."
>   ^long [x]
>   (long (Math/ceil x)))
>
> And then make your main function take a function:
>
> (defn get-percentage
>   "Expresses place as a percentage of total-count, producing an integer
> from 0 to 100
>    using either rounding to nearest or a passed-in rounding function."
>   (^long [place total-count] (get-percentage round place total-count))
>   (^long [f place total-count]
>     (long (f (/ (* place 100) total-count)))))
>
> This has the disadvantage of larger code size but the advantage of
> flexibility. Callers can define their own rounding function to pass in. For
> example, for progress reporting they might not want to report any nonzero
> amount of progress as 0 *or* any amount less than completion as 100, in
> which case:
>
> (defn progress-round
>   "Rounds x to nearest, but avoids 0 and 100 unless exact."
>   ^long [x]
>   (let [y (round x)]
>     (cond
>       (== y x) y
>       (== y 0) (ceil x)
>       (== y 100) (floor x)
>       :else y)))
>
>  --
> 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.
>



-- 
“One of the main causes of the fall of the Roman Empire was that–lacking
zero–they had no way to indicate successful termination of their C
programs.”
(Robert Firth)

-- 
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.

Reply via email to