--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clo...@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+u...@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+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
(defn pct [p t] (/ (* p 100.0) t))
(defn round-up [x] (int (Math/ceil x)))
(defn round-down [x] (int (Math/floor x)))
(defn round [x] (int (Math/round x)))
(defn get-percentage* [f p t] (f (pct p t)))
(get-percentage* round-up 4 30)
14
(get-percentage* round-down 4 30)
13
(def get-percentage-high (comp round-up pct))
(get-percentage-high 4 30)
14
(defn to-mode-fn [mode](if (keyword? mode)(case mode :high Math/ceil:low Math/floor:normal Math/round(throw (Exception. "ERROR: get-percentage [:high|:low|:normal] <PLACE> <TOTAL_COUNT>")))mode))
--
"Unable to resolve symbol: in this "I get this error when I have a non-breaking space in my code :
2015-02-14 12:33 GMT+01:00 Cecil Westerhof <cldwes...@gmail.com>:2015-02-11 8:32 GMT+01:00 Cecil Westerhof <cldwes...@gmail.com>:I needed a function to get the percentage as an int. Input is place and total-count.
I want the normal definition (which is the default) and a high and low variant.
I came up with the following code:
(defn get-percentage
([place total-count] (get-percentage :normal place total-count))
([mode place total-count]
(let [percentage(/ (* place 100.0) total-count)]
(condp = mode
:high (int (Math/ceil percentage))
:low (int (Math/floor percentage))
:normal (int (Math/round percentage))
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clo...@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+u...@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+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
On Sat, Feb 14, 2015 at 6:08 PM, Cecil Westerhof <cldwes...@gmail.com> wrote:2015-02-14 12:33 GMT+01:00 Cecil Westerhof <cldwes...@gmail.com>:2015-02-11 8:32 GMT+01:00 Cecil Westerhof <cldwes...@gmail.com>:I needed a function to get the percentage as an int. Input is place and total-count.
I want the normal definition (which is the default) and a high and low variant.
I came up with the following code:
(defn get-percentage
([place total-count] (get-percentage :normal place total-count))
([mode place total-count]
(let [percentage(/ (* place 100.0) total-count)]
(condp = mode
:high (int (Math/ceil percentage))
:low (int (Math/floor percentage))
:normal (int (Math/round percentage))I don't understand why you prefer this over Phill's suggestion (using comp and having 3 versions of get-percentage). Then you wouldn't need the extra arity (2 param one) too.Phill's version is 7 lines of code, 4 simple defn's & 3 def's. I'm just saying this in case it was overlooked.