Put them in a namespace. (ns prime-seive)
... definitions ... On Saturday, May 23, 2009, michaelg wrote: > > Thanks Chouser, very nice! I am quite aware of the performance boost > of using ints instead of strings -- this was an intentional choice on > my part. When I first solved the problem, I wrote it in Scala and used > ints. > > I have one question for you, and this will show my OOP roots :-) How > would you encapsulate these various functions in Clojure? I really > want one "thing" that solves the problem, not exposing all of the > various functions needed along the way. There is an obvious way to do > this in OOP languages, as you can see from the variations posted on my > blog. But what about Clojure? > > Thanks, > Michael > > On May 21, 4:37 pm, Chouser wrote: >> On Thu, May 21, 2009 at 4:59 AM, michaelg wrote: >> >> > So if anyone would like to help, I would be very appreciative. All I >> > can offer is recognition in my JavaOne talk. All I ask from the >> > implementations is that they try to stay true to how the Java version >> > worked, while also trying to be fairly idiomatic Clojure. I don't have >> > a strong opinion on type hinting. If it is used, then I would not >> > compare the results to Groovy, Ruby, Python, but to Scala, Fan instead >> > and vice versa >> >> I did reversible first -- here's what I came up with: >> >> (defn all-odd? [n] >> (every? odd? (map #(Integer. (str %)) (str n)))) >> >> (defn reverse-num [n] >> (+ n (Integer. (apply str (reverse (str n)))))) >> >> (defn reversible-num? [n] >> (all-odd? (reverse-num n))) >> >> (defn count-reversible [rev-max] >> {:max rev-max >> :num-reversible (count (filter reversible-num? (range 11 rev-max)))}) >> >> Those stay very true to the ruby version you've already got. >> If you're willing to wander a bit, I'm not sure whether >> you'd rather go more succinct or faster. For example: >> >> (defn all-odd? [n] >> (every? #{\1 \3 \5 \9} (str n))) >> >> That's a bit simpler and more succinct, but it's also a bit >> slower than the original above. But you could go faster >> instead: >> >> (defn all-odd? [n] >> (loop [n (int n)] >> (cond >> (zero? n) true >> (even? n) false >> :else (recur (int (quot n 10)))))) >> >> Use of primitive int and not messing with Strings makes that >> one about 10x faster than the original. >> >> Note we haven't used any traditional type hints yet, though >> they can be useful. We can add a single type hint to >> reverse-num and double its speed: >> >> (defn reverse-num [n] >> (+ n (Integer. #^String (apply str (reverse (str n)))))) >> >> But again primitive numbers win here if you care only about >> speed. This one is more than 5 times faster than the >> original reverse-num: >> >> (defn reverse-num [n] >> (loop [nrem (int n), rev (int 0)] >> (if (zero? nrem) >> (+ n rev) >> (recur (int (quot nrem 10)) >> (int (+ (* 10 rev) (rem nrem 10))))))) >> >> --Chouser > > > --~--~---------~--~----~------------~-------~--~----~ 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 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 -~----------~----~----~----~------~----~------~--~---