Hi Michael, Thanks for the implementation suggestion. I left school 2 years only so missed out on some of the more advanced mathematics, hence I was unaware of the function and purpose of log. Since reading your post, I've done a little bit of research on log as to educate myself. I've now re-implemented my function using your logn implementation. It now has a name also...
(defn recursively-divide "Divides n by base until n is smaller than base. Returns a map consisting of the structure {:result :times}, where :result is the result of the recursive division, and :times is the number of times n was divided by base." [n base] (let [times (int (logn base n))] {:result (float (/ n (expt base times))) :times times})) I've chosen "recursively-divide" as it seemed more clojuresque, as it reads like it would in english "recursively divide n by base". I guess if I've learnt anything from just this function alone, it's that often names are difficult to think of when it's hard to define exactly what it is a function is doing. If you can't easily define what a functions purpose is, then I think that usually means it's been poorly designed/implemented (suggesting there's a better way) or the coder isn't exactly sure what they're trying to achieve. On Feb 4, 5:28 pm, Michael Wood <esiot...@gmail.com> wrote: > On 4 February 2010 09:04, Wardrop <t...@tomwardrop.com> wrote: > > > > > I often myself creating functions which perform a rather clear and > > simple task, but which is hard to describe, either because I don't > > know the term for what I've just implemented, or because the function > > is difficult to summarise in a couple of words. As an example, I've > > just created a function which determines how many times the value n, > > can be divided by the base, until n becomes smaller than the base. > > Here's the implementation... > > > (defn <insert-name-here> [n base] > > (loop [n n count 0] > > (if (< n base) > > {:val n :count count} > > (recur (float (/ n base)) (inc count))))) > > > This can be used among other things, to determine how to format bytes. > > Say I have a file that's 6,789,354 bytes, and I want to display that > > in a more readable manner, I could use my unnamed function like so > > (<unnamed> 6789354 1024), and get a new appropriately rounded number, > > including the number of times it has been rounded, which I can use the > > pick the appropriate suffix from a list ("Bytes", "KB", "MB", "GB", > > "TB). > > I would probably split that up: > > The division can just use /, so no need for a separate function. > The rest can be done like this: > > (defn logn [n x] > (/ (java.lang.Math/log x) (java.lang.Math/log n))) > > Then you could have a function that formats file sizes: > > (defn format-file-size [num-bytes] > (let [base 1024 > val (float (/ num-bytes base)) > times (int (logn base num-bytes))] > ...) > > Not sure if that helps with your general question, though :) > > > I mean, what the hell would you name this function, or would you not > > create such an obscure and generalised function to start with? > > -- > Michael Wood <esiot...@gmail.com> -- 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